From a41f50eeaad99d74bfc7a3326401a7ea71ac92db Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Wed, 30 May 2018 08:26:59 +0100 Subject: [PATCH] Track the requested state so we can return it when asked The original USB relay code can query the GPIO lines to see which relays are enabled. We only have one way communication to the power sockets so they might be in a different state to that requested, but store the state we think we should have so when asked we can return it, rather than always returning that everything is off. --- main.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index 5805cee..d97777a 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,7 @@ int serno_str[] = { uint32_t serno; unsigned long cmd = 0; int repeat = 0, wait = 0; +uint8_t state = 0; PROGMEM const char usbHidReportDescriptor[22] = { 0x06, 0x00, 0xff, /* USAGE PAGE (Generic Desktop) */ @@ -148,10 +149,8 @@ uchar usbFunctionRead(uchar *data, uchar len) for (i = 0; i < 5; i++) { data[i] = serno_str[i + 1]; } - data[5] = data[6] = data[7] = 0; - if (PORTB & (1 << PB0)) { - data[7] = 1; - } + data[5] = data[6] = 0; + data[7] = state; return len; } @@ -162,10 +161,12 @@ uchar usbFunctionWrite(uchar *data, uchar len) { if (data[0] == CMD_ALL_ON) { cmd = serno | 0xd; + state = 0xf; wait = 200; repeat = 5; } else if (data[0] == CMD_ALL_OFF) { cmd = serno | 0xc; + state = 0; wait = 10; repeat = 5; } else if (data[0] == CMD_ON) { @@ -173,45 +174,41 @@ uchar usbFunctionWrite(uchar *data, uchar len) switch (data[1]) { case 1: cmd = serno | 0xf; - repeat = 5; break; case 2: cmd = serno | 0x7; - repeat = 5; break; case 3: cmd = serno | 0xb; - repeat = 5; break; case 4: cmd = serno | 0x3; - repeat = 5; break; default: - break; + return len; } + repeat = 5; + state |= (1 << (data[1] - 1)); } else if (data[0] == CMD_OFF) { wait = 200; switch (data[1]) { case 1: cmd = serno | 0xe; - repeat = 5; break; case 2: cmd = serno | 0x6; - repeat = 5; break; case 3: cmd = serno | 0xa; - repeat = 5; break; case 4: cmd = serno | 0x2; - repeat = 5; break; default: - break; + return len; } + repeat = 5; + state &= ~(1 << (data[1] - 1)); } else if (data[0] == CMD_SET_SERIAL) { update_serno(&data[1], 6); } -- 2.39.5