--- embedtools/src/pceng_run.c 2011/07/23 23:41:00 1.1.2.3 +++ embedtools/src/pceng_run.c 2011/07/29 12:12:40 1.1.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: pceng_run.c,v 1.1.2.3 2011/07/23 23:41:00 misho Exp $ + * $Id: pceng_run.c,v 1.1.2.5 2011/07/29 12:12:40 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -47,9 +47,9 @@ SUCH DAMAGE. #include "pceng.h" -const u_int led_base[2][4] = {{0xf410, 0xf400, 0xf400, 0xf400}, +const u_int led_base[2][MAX_GPIO_PINS] = {{0xf410, 0xf400, 0xf400, 0xf400}, {0x61b0, 0x6100, 0x6180, 0x6180}}; -const int led_bit[2][4] = {{8, 2, 3, 18}, {8, 6, 9, 11}}; +const int led_bit[2][MAX_GPIO_PINS] = {{8, 2, 3, 18}, {8, 6, 9, 11}}; const int gpio_off[2] = { 0x4, 0x10 }; @@ -75,7 +75,7 @@ getBoard() } static u_int -gpioRead(u_char ledno, u_char state) +gpioRead(u_char ledno) { u_int n; board_t type = getBoard(); @@ -116,12 +116,20 @@ LED(u_char ledno, u_char state) switch (type) { case wrap: n = inl(led_base[type - 1][ledno]); + if (state == (u_char) -1) + return (n &= (1 << led_bit[type - 1][ledno])); + if (state) n |= (1 << led_bit[type - 1][ledno]); else n &= ~(1 << led_bit[type - 1][ledno]); break; case alix: + if (state == (u_char) -1) { + n = inl(led_base[type - 1][ledno]); + return !(n &= (1 << led_bit[type - 1][ledno])); + } + if (state) n = (1 << (led_bit[type - 1][ledno] + gpio_off[type - 1])); else @@ -135,10 +143,91 @@ LED(u_char ledno, u_char state) return 0; } +static int +RunCmd(u_char pin, u_int state) +{ + char szStr[STRSIZ], szArg[STRSIZ] = { 0 }; + + FTRACE(3); + + switch (pin) { + case 0: /* button */ + cfg_LoadAttribute(&cfg, CFG("event"), CFG("button_exec"), + CFG(szStr), sizeof szStr, NULL); + break; + case 1: /* LEDs */ + case 2: + case 3: + cfg_LoadAttribute(&cfg, CFG("event"), CFG("led_exec"), + CFG(szStr), sizeof szStr, NULL); + break; + } + if (!*szStr) + return 0; + else + snprintf(szArg, sizeof szArg, "%d", state); + + switch (vfork()) { + case -1: + syslog(LOG_ERR, "Error:: RunCmd #%d - %s\n", errno, strerror(errno)); + return -1; + case 0: /* execute command */ + return execl(szStr, szStr, szArg, NULL); + } + + return 0; +} + int Run() { + register u_char i; + u_int ret, t, slice, states = 0; + char szStr[STRSIZ]; + FTRACE(3); + + /* init array */ + for (i = 0; i < MAX_GPIO_PINS; i++) + if ((ret = gpioRead(i)) == -1) + return 126; + else + states |= (1 << i); + + /* state machine */ + while (!Kill) { + cfg_LoadAttribute(&cfg, CFG("event"), CFG("button_slice"), + CFG(szStr), sizeof szStr, DEFAULT_SLICE); + slice = strtol(szStr, NULL, 0); + if (!slice) + slice = strtol(DEFAULT_SLICE, NULL, 0); + slice *= 1000; + + for (i = t = 0; i < MAX_GPIO_PINS; t = 0, i++) { + if ((ret = gpioRead(i)) == -1) + return 127; + + if (ret != ((states >> i) & 0x1)) { + if (!i) { + t = 0; + do { + t++; + usleep(slice); + if (gpioRead(i) != ret) + break; + } while (!Kill); + } else { + t = ret; + if (ret) + states |= (1 << i); + else + states &= ~(1 << i); + } + + RunCmd(i, t); + } + } + } return 0; }