From: Nathaniel Wesley Filardo Date: Tue, 12 Dec 2017 19:20:37 +0000 (-0500) Subject: Add door monitor support X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=8461ee4b3dd1a81487e01c47b272aa792db6eec0;p=acmetensortoys-snakecontrol Add door monitor support While here, tweak regexes in lcdproc-monitor --- diff --git a/bin/.gitignore b/bin/.gitignore index 0d20b64..6130b4a 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1 +1,2 @@ *.pyc +pollfd diff --git a/bin/door-monitor.sh b/bin/door-monitor.sh new file mode 100755 index 0000000..e7c8b20 --- /dev/null +++ b/bin/door-monitor.sh @@ -0,0 +1,46 @@ +#!/usr/bin/zsh + +set -e -u + +I2C_ADDRESS="21" # hex +GPIO_LEFT_OFFSET=6 +GPIO_RIGHT_OFFSET=7 + +GPIO_IRQ=22 + +[ -d /sys/bus/i2c/devices/i2c-1/1-00${I2C_ADDRESS} ] || { + echo pcf8574 0x${I2C_ADDRESS} > /sys/bus/i2c/devices/i2c-1/new_device +} + +sleep 1 +[ -d /sys/bus/i2c/devices/i2c-1/1-00${I2C_ADDRESS}/gpio ] || { + echo "No GPIO chip registered. Bailing out." + exit 1 +} + +GPIOBASE=`cat /sys/bus/i2c/devices/i2c-1/1-00${I2C_ADDRESS}/gpio/gpiochip*/base` + +GPIO_LEFT=$((GPIOBASE+GPIO_LEFT_OFFSET)) + +[ -d /sys/class/gpio/gpio${GPIO_LEFT} ] || { + echo ${GPIO_LEFT} > /sys/class/gpio/export +} + +GPIO_RIGHT=$((GPIOBASE+GPIO_RIGHT_OFFSET)) + +[ -d /sys/class/gpio/gpio${GPIO_RIGHT} ] || { + echo ${GPIO_RIGHT} > /sys/class/gpio/export +} + +# IRQ gpio +[ -d /sys/class/gpio/gpio${GPIO_IRQ} ] || { + echo ${GPIO_IRQ} > /sys/class/gpio/export +} +echo 1 > /sys/class/gpio/gpio${GPIO_IRQ}/active_low +echo rising > /sys/class/gpio/gpio${GPIO_IRQ}/edge + +exec 3 +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) { + + int fd; + int efd; + int tmo; + struct epoll_event ev; + + if (argc <= 2) { + fprintf(stderr, "Need fd and timeout\n"); + exit(1); + } + + fd = atoi(argv[1]); + if (fd < 0) { + fprintf(stderr, "Invalid fd\n"); + exit(1); + } + + tmo = atoi(argv[2]); + + if ((efd = epoll_create1(0)) < 0) { + fprintf(stderr, "epoll_create1 failed: %d\n", errno); + exit(1); + } + + ev.events = EPOLLPRI | EPOLLET; + ev.data.fd = fd; + if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev) < 0) { + fprintf(stderr, "epoll_ctl ADD failed: %d\n", errno); + exit(1); + } + + while(1) { + int er, rr; + char buf[8] = {0, }; + + memset(&ev, 0, sizeof(ev)); + + er = epoll_wait(efd, &ev, 1, tmo); + + if (er < 0) { + fprintf(stderr, "epoll_wait() failure: %d\n", errno); + exit(2); + } + + if (lseek(fd, 0, SEEK_SET) != 0) { + fprintf(stderr, "lseek failed: %d\n", errno); + exit(2); + } + + if ((rr = read(fd, buf, sizeof(buf))) < 2) { + fprintf(stderr, "read failed: ret=%d err=%d\n", rr, errno); + exit(2); + } + + if (er == 0) { + printf("tick: %s", buf); + } else { + if (ev.events & EPOLLPRI) { + printf("irq: %s", buf); + } + } + + fflush(stdout); + } +} diff --git a/runit/pi-sc-doormon/log/run b/runit/pi-sc-doormon/log/run new file mode 100755 index 0000000..f7c46c2 --- /dev/null +++ b/runit/pi-sc-doormon/log/run @@ -0,0 +1,4 @@ +#!/bin/sh +mkdir -p /run/snakecontrol/door-monitor.log +chown -R pi:pi /run/snakecontrol/door-monitor.log +exec chpst -u pi svlogd -tt /run/snakecontrol/door-monitor.log diff --git a/runit/pi-sc-doormon/run b/runit/pi-sc-doormon/run new file mode 100755 index 0000000..fc78754 --- /dev/null +++ b/runit/pi-sc-doormon/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec /home/pi/sc/bin/door-monitor.sh