Skip to content

Commit

Permalink
Making it possible to measure a light level from an ADC device for th…
Browse files Browse the repository at this point in the history
…e night mode
  • Loading branch information
wberube committed Jun 19, 2024
1 parent 09e765e commit 959dadd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
2 changes: 2 additions & 0 deletions divinus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ night_mode:
ir_cut_pin1: 1
ir_cut_pin2: 2
pin_switch_delay_us: 150
adc_device: /dev/sar_adc_drv
adc_threshold: 128

record:
enable: false
Expand Down
31 changes: 13 additions & 18 deletions src/app_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum ConfigError parse_app_config(void) {
app_config.ir_cut_pin2 = 999;
app_config.pin_switch_delay_us = 250;
app_config.check_interval_s = 10;
app_config.adc_device[0] = 0;
app_config.adc_threshold = 128;

struct IniConfig ini;
memset(&ini, 0, sizeof(struct IniConfig));
Expand Down Expand Up @@ -96,35 +98,28 @@ enum ConfigError parse_app_config(void) {

err =
parse_bool(&ini, "night_mode", "enable", &app_config.night_mode_enable);
if (err != CONFIG_OK)
goto RET_ERR;
#define PIN_MAX 95
if (app_config.night_mode_enable) {
#define PIN_MAX 95
err = parse_int(
parse_int(
&ini, "night_mode", "ir_sensor_pin", 0, PIN_MAX,
&app_config.ir_sensor_pin);
if (err != CONFIG_OK)
goto RET_ERR;
err = parse_int(
parse_int(
&ini, "night_mode", "check_interval_s", 0, 600,
&app_config.check_interval_s);
if (err != CONFIG_OK)
goto RET_ERR;
err = parse_int(
parse_int(
&ini, "night_mode", "ir_cut_pin1", 0, PIN_MAX,
&app_config.ir_cut_pin1);
if (err != CONFIG_OK)
goto RET_ERR;
err = parse_int(
parse_int(
&ini, "night_mode", "ir_cut_pin2", 0, PIN_MAX,
&app_config.ir_cut_pin2);
if (err != CONFIG_OK)
goto RET_ERR;
err = parse_int(
parse_int(
&ini, "night_mode", "pin_switch_delay_us", 0, 1000,
&app_config.pin_switch_delay_us);
if (err != CONFIG_OK)
goto RET_ERR;
parse_param_value(
&ini, "night_mode", "adc_device", app_config.adc_device);
parse_int(
&ini, "night_mode", "adc_threshold", INT_MIN, INT_MAX,
&app_config.adc_threshold);
}

err = parse_bool(&ini, "isp", "mirror", &app_config.mirror);
Expand Down
2 changes: 2 additions & 0 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ struct AppConfig {
unsigned int ir_sensor_pin;
unsigned int check_interval_s;
unsigned int pin_switch_delay_us;
char adc_device[128];
int adc_threshold;
};

extern struct AppConfig app_config;
Expand Down
53 changes: 44 additions & 9 deletions src/night.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,53 @@ void *night_thread(void) {
usleep(1000);
set_night_mode(night_mode);

while (keepRunning) {
bool state = false;
if (!gpio_read(app_config.ir_sensor_pin, &state)) {
sleep(app_config.check_interval_s);
continue;
if (app_config.adc_device[0]) {
int adc_fd = -1;
fd_set adc_fds;
int cnt = 0, tmp = 0, val;

if ((adc_fd = open(app_config.adc_device, O_RDONLY | O_NONBLOCK)) <= 0) {
printf(tag "Could not open the ADC virtual device!\n");
return NULL;
}
while (keepRunning) {
struct timeval tv = {
.tv_sec = app_config.check_interval_s, .tv_usec = 0 };
FD_ZERO(&adc_fds);
FD_SET(adc_fd, &adc_fds);
select(adc_fd + 1, &adc_fds, NULL, NULL, &tv);
if (read(adc_fd, &val, sizeof(val)) > 0) {
usleep(10000);
tmp += val;
}
cnt++;
if (cnt == 12) {
tmp /= cnt;
if (tmp >= app_config.adc_threshold)
night_mode = true;
else
night_mode = false;
set_night_mode(night_mode);
cnt = tmp = 0;
}
usleep(250000);
}
if (night_mode != state) {
night_mode = state;
set_night_mode(night_mode);
if (adc_fd) close(adc_fd);
} else {
while (keepRunning) {
bool state = false;
if (!gpio_read(app_config.ir_sensor_pin, &state)) {
sleep(app_config.check_interval_s);
continue;
}
if (night_mode != state) {
night_mode = state;
set_night_mode(night_mode);
}
sleep(app_config.check_interval_s);
}
sleep(app_config.check_interval_s);
}
printf(tag "Night mode thread is closing...\n");
}

int start_monitor_light_sensor() {
Expand Down

0 comments on commit 959dadd

Please sign in to comment.