Skip to content

Commit

Permalink
Allow manually adjusting the night mode using the API if there isn't …
Browse files Browse the repository at this point in the history
…a defined sensor pin
  • Loading branch information
wberube committed Jun 20, 2024
1 parent 2e12d50 commit dd2df7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/night.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void ircut_on() {
usleep(app_config.pin_switch_delay_us);
gpio_write(app_config.ir_cut_pin1, false);
gpio_write(app_config.ir_cut_pin2, false);
set_grayscale(true);
}

void ircut_off() {
Expand All @@ -29,17 +28,16 @@ void ircut_off() {
usleep(app_config.pin_switch_delay_us);
gpio_write(app_config.ir_cut_pin1, false);
gpio_write(app_config.ir_cut_pin2, false);
set_grayscale(false);
}

void set_night_mode(bool night) {
if (night == night_mode) return;
if (night) {
printf(tag "Change mode to NIGHT\n");
printf(tag "Changing mode to NIGHT\n");
ircut_off();
set_grayscale(true);
} else {
printf(tag "Change mode to DAY\n");
printf(tag "Changing mode to DAY\n");
ircut_on();
set_grayscale(false);
}
Expand Down Expand Up @@ -78,6 +76,8 @@ void *night_thread(void) {
usleep(250000);
}
if (adc_fd) close(adc_fd);
} else if (app_config.ir_sensor_pin == 999) {
while (keepRunning) sleep(1);
} else {
while (keepRunning) {
bool state = false;
Expand Down
1 change: 1 addition & 0 deletions src/night.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "gpio.h"
#include "media.h"

void set_night_mode(bool night);
bool night_mode_is_enabled();
void *night_thread();

Expand Down
40 changes: 40 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,46 @@ void *server_thread(void *vargp) {
continue;
}

if (app_config.night_mode_enable && equals(uri, "/api/night")) {
int respLen;
if (equals(method, "GET")) {
if (app_config.ir_sensor_pin == 999 && !empty(query)) {
char *remain;
while (query) {
char *value = split(&query, "&");
if (!value || !*value) continue;
unescape_uri(value);
char *key = split(&value, "=");
if (!key || !*key || !value || !*value) continue;
if (equals(key, "active")) {
if (equals_case(value, "true") || equals_case(value, "1"))
set_night_mode(1);
else if (equals_case(value, "false") || equals_case(value, "0"))
set_night_mode(0);
}
}
}
respLen = sprintf(response,
"HTTP/1.1 200 OK\r\n" \
"Content-Type: application/json;charset=UTF-8\r\n" \
"Connection: close\r\n" \
"\r\n" \
"{\"active\":%s}",
night_mode_is_enabled() ? "true" : "false");
} else {
respLen = sprintf(response,
"HTTP/1.1 400 Bad Request\r\n" \
"Content-Type: text/plain\r\n" \
"Connection: close\r\n" \
"\r\n" \
"The server has no handler to the request.\r\n" \
);
}
send_to_fd(client_fd, response, respLen);
close_socket_fd(client_fd);
continue;
}

if (app_config.osd_enable && starts_with(uri, "/api/osd/") &&
uri[9] && uri[9] >= '0' && uri[9] <= (MAX_OSD - 1 + '0')) {
char id = uri[9] - '0';
Expand Down

0 comments on commit dd2df7c

Please sign in to comment.