mirror of https://github.com/OpenIPC/firmware.git
[no ci] Package: update autonight source (#1372)
parent
790b0c40a8
commit
a3190bef7a
|
@ -1,4 +1,4 @@
|
||||||
config BR2_PACKAGE_AUTONIGHT
|
config BR2_PACKAGE_AUTONIGHT
|
||||||
bool "autonight"
|
bool "autonight"
|
||||||
help
|
help
|
||||||
autonight - automatic switching of day/night modes depending on the readings of the IR Sensor
|
Automatic day/night mode switcher for Ingenic devices.
|
||||||
|
|
|
@ -4,22 +4,18 @@
|
||||||
#
|
#
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
AUTONIGHT_SITE_METHOD = local
|
||||||
|
AUTONIGHT_SITE = $(AUTONIGHT_PKGDIR)/src
|
||||||
|
|
||||||
AUTONIGHT_LICENSE = MIT
|
AUTONIGHT_LICENSE = MIT
|
||||||
AUTONIGHT_LICENSE_FILES = LICENSE
|
AUTONIGHT_LICENSE_FILES = LICENSE
|
||||||
|
|
||||||
define AUTONIGHT_EXTRACT_CMDS
|
|
||||||
cp -avr $(AUTONIGHT_PKGDIR)/src/* $(@D)/
|
|
||||||
endef
|
|
||||||
|
|
||||||
AUTONIGHT_MAKE_OPTS = \
|
|
||||||
CC="$(TARGET_CC)"
|
|
||||||
|
|
||||||
define AUTONIGHT_BUILD_CMDS
|
define AUTONIGHT_BUILD_CMDS
|
||||||
$(MAKE) $(AUTONIGHT_MAKE_OPTS) -C $(@D)
|
$(TARGET_CC) $(@D)/autonight.c -o $(@D)/autonight -s
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define AUTONIGHT_INSTALL_TARGET_CMDS
|
define AUTONIGHT_INSTALL_TARGET_CMDS
|
||||||
install -m 0755 -D $(@D)/autonight $(TARGET_DIR)/usr/bin/autonight
|
$(INSTALL) -m 0755 -t $(TARGET_DIR)/usr/bin $(@D)/autonight
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(eval $(generic-package))
|
$(eval $(generic-package))
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
.PHONY: all clean
|
|
||||||
|
|
||||||
all: autonight
|
|
||||||
|
|
||||||
autonight: autonight.o
|
|
||||||
$(CC) -o $@ $^
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-rm -rf *.o
|
|
|
@ -9,107 +9,104 @@
|
||||||
#define BUF_SIZE 256
|
#define BUF_SIZE 256
|
||||||
|
|
||||||
const char *device = "/dev/jz_adc_aux_0";
|
const char *device = "/dev/jz_adc_aux_0";
|
||||||
const char *nightModeCmd = "curl localhost/night/";
|
const char *nightModeCmd = "curl localhost/night";
|
||||||
int delayBetweenReads = 3;
|
int delayBetweenReads = 3;
|
||||||
int thresholdOn = 900;
|
int thresholdOn = 800;
|
||||||
int thresholdOff = 1000;
|
int thresholdOff = 1200;
|
||||||
bool nightModeEnabled = false;
|
bool nightModeEnabled = false;
|
||||||
|
|
||||||
void
|
void jzAuxRead(const char *device, int *value) {
|
||||||
jzAuxRead(const char *device, int *value)
|
|
||||||
{
|
|
||||||
int fd, size, count;
|
int fd, size, count;
|
||||||
|
|
||||||
fd = open(device, O_RDONLY);
|
fd = open(device, O_RDONLY);
|
||||||
if(fd < 0){
|
if (fd < 0) {
|
||||||
perror(device);
|
perror(device);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
size = sizeof(*value);
|
size = sizeof(*value);
|
||||||
count = read(fd, (void *)value, size);
|
count = read(fd, (void *)value, size);
|
||||||
close(fd);
|
close(fd);
|
||||||
if(count != size){
|
|
||||||
|
if (count != size) {
|
||||||
perror("read()");
|
perror("read()");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void updateNightMode() {
|
||||||
updateNightMode()
|
|
||||||
{
|
|
||||||
int ret;
|
int ret;
|
||||||
char buf[BUF_SIZE];
|
char buf[BUF_SIZE];
|
||||||
|
|
||||||
printf("Night Mode %s\n", nightModeEnabled ? "Enabled" : "Disabled");
|
printf("> Mode: %s\n", nightModeEnabled ? "Night" : "Day");
|
||||||
snprintf(buf, BUF_SIZE, "%s%s\n", nightModeCmd, nightModeEnabled ? "on" : "off");
|
snprintf(buf, BUF_SIZE, "%s/%s\n", nightModeCmd, nightModeEnabled ? "on" : "off");
|
||||||
|
|
||||||
ret = system(buf);
|
ret = system(buf);
|
||||||
if(ret != 0){
|
if (ret != 0) {
|
||||||
perror("system()");
|
perror("system()");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void usage(const char *argv) {
|
||||||
usage(const char *argv0)
|
fprintf(stderr, "Usage: %s [option]\n\n"
|
||||||
{
|
"Options:\n"
|
||||||
fprintf(stderr, "\nusage: %s [options]\n\n"
|
|
||||||
"options:\n"
|
|
||||||
" -D sets jz_adc_aux device (default: %s)\n"
|
" -D sets jz_adc_aux device (default: %s)\n"
|
||||||
" -c sets the command to call to set night mode (default: %s)\n"
|
" -c sets the command to call to set night mode (default: %s)\n"
|
||||||
" -d delay (in seconds) between ADC reads (default: %d)\n"
|
" -d delay (in seconds) between ADC reads (default: %d)\n"
|
||||||
" -O turn on night mode when ADC value drops below this threshold (default: %d)\n"
|
" -O turn on night mode when ADC drops below threshold (default: %d)\n"
|
||||||
" -F turn off night mode when ADC value goes above this threshold (default: %d)\n"
|
" -F turn off night mode when ADC goes above threshold (default: %d)\n"
|
||||||
" -h print this usage statement and exit\n\n", argv0, device, nightModeCmd, delayBetweenReads, thresholdOn, thresholdOff);
|
" -h print this usage statement and exit\n\n",
|
||||||
|
argv, device, nightModeCmd, delayBetweenReads, thresholdOn, thresholdOff);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main(int argc, char **argv) {
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int opt, value;
|
int opt, value;
|
||||||
|
|
||||||
while((opt = getopt(argc, argv, "D:c:d:O:F:h:")) != -1){
|
while ((opt = getopt(argc, argv, "D:c:d:O:F:h:")) != -1) {
|
||||||
switch(opt){
|
switch (opt) {
|
||||||
case 'D':
|
case 'D':
|
||||||
device = optarg;
|
device = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
nightModeCmd = optarg;
|
nightModeCmd = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'd':
|
case 'd':
|
||||||
delayBetweenReads = atoi(optarg);
|
delayBetweenReads = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'O':
|
case 'O':
|
||||||
thresholdOn = atoi(optarg);
|
thresholdOn = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'F':
|
case 'F':
|
||||||
thresholdOff = atoi(optarg);
|
thresholdOff = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(daemon(0, 0) < 0){
|
while (true) {
|
||||||
perror("daemon()");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
sleep(5);
|
|
||||||
|
|
||||||
while(true){
|
|
||||||
jzAuxRead(device, &value);
|
jzAuxRead(device, &value);
|
||||||
printf("Current value: %d\n", value);
|
printf("> Value: %d\n", value);
|
||||||
if(!nightModeEnabled && value >= thresholdOn){
|
|
||||||
|
if (!nightModeEnabled && value >= thresholdOn) {
|
||||||
nightModeEnabled = true;
|
nightModeEnabled = true;
|
||||||
updateNightMode();
|
updateNightMode();
|
||||||
} else if(nightModeEnabled && value < thresholdOff){
|
} else if (nightModeEnabled && value < thresholdOff) {
|
||||||
nightModeEnabled = false;
|
nightModeEnabled = false;
|
||||||
updateNightMode();
|
updateNightMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(delayBetweenReads);
|
sleep(delayBetweenReads);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue