diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 9c872e25e..0635beae8 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,5 @@ /* 6.1.0a + * Add TM1638 switch support (#2226) * Fix invalid response using more than 4 switches and domoticz * Update sensor drivers to provide instant results * Add read sensor retry to DS18B20, DS18x20, DHT, SHT1X and HTU21 diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 98fb7b7a3..01051db1f 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -177,7 +177,7 @@ uint8_t multiwindow[MAX_KEYS] = { 0 }; // Max time between button presses t uint8_t multipress[MAX_KEYS] = { 0 }; // Number of button presses within multiwindow uint8_t lastwallswitch[MAX_SWITCHES]; // Last wall switch states uint8_t holdwallswitch[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold -uint8_t virtualswitch[MAX_SWITCHES] = { 0 }; // Virtual switch states +uint8_t virtualswitch[MAX_SWITCHES]; // Virtual switch states mytmplt my_module; // Active copy of Module name and GPIOs uint8_t pin[GPIO_MAX]; // Possible pin configurations @@ -1734,7 +1734,7 @@ void SwitchHandler(byte mode) uint8_t switchflag; for (byte i = 0; i < MAX_SWITCHES; i++) { - if (pin[GPIO_SWT1 +i] < 99) { + if ((pin[GPIO_SWT1 +i] < 99) || (mode)) { if (holdwallswitch[i]) { holdwallswitch[i]--; @@ -2390,10 +2390,13 @@ void GpioInit() } } for (byte i = 0; i < MAX_SWITCHES; i++) { + lastwallswitch[i] = 1; // Init global to virtual switch state; if (pin[GPIO_SWT1 +i] < 99) { pinMode(pin[GPIO_SWT1 +i], (16 == pin[GPIO_SWT1 +i]) ? INPUT_PULLDOWN_16 :INPUT_PULLUP); - lastwallswitch[i] = digitalRead(pin[GPIO_SWT1 +i]); // set global now so doesn't change the saved power state on first switch check + lastwallswitch[i] = digitalRead(pin[GPIO_SWT1 +i]); // Set global now so doesn't change the saved power state on first switch check + } + virtualswitch[i] = lastwallswitch[i]; } #ifdef USE_WS2812 diff --git a/sonoff/xsns_28_tm1638.ino b/sonoff/xsns_28_tm1638.ino index 6945261c8..01f2f21a5 100644 --- a/sonoff/xsns_28_tm1638.ino +++ b/sonoff/xsns_28_tm1638.ino @@ -35,19 +35,20 @@ uint8_t tm1638_clock_pin = 0; uint8_t tm1638_data_pin = 0; uint8_t tm1638_strobe_pin = 0; uint8_t tm1638_displays = 8; -uint8_t tm1638_active_display = 0; +uint8_t tm1638_active_display = 1; uint8_t tm1638_intensity = 0; +uint8_t tm1638_state = 0; /*********************************************************************************************\ * Pieces from library https://github.com/rjbatista/tm1638-library + * and from library https://github.com/MartyMacGyver/TM1638-demos-and-examples \*********************************************************************************************/ void Tm16XXSend(byte data) { - for (int i = 0; i < 8; i++) { + for (uint8_t i = 0; i < 8; i++) { + digitalWrite(tm1638_data_pin, !!(data & (1 << i))); digitalWrite(tm1638_clock_pin, LOW); - digitalWrite(tm1638_data_pin, data & 1 ? HIGH : LOW); - data >>= 1; delayMicroseconds(TM1638_CLOCK_DELAY); digitalWrite(tm1638_clock_pin, HIGH); } @@ -77,11 +78,10 @@ byte Tm16XXReceive() pinMode(tm1638_data_pin, INPUT); digitalWrite(tm1638_data_pin, HIGH); - for (int i = 0; i < 8; i++) { - temp >>= 1; + for (uint8_t i = 0; i < 8; ++i) { digitalWrite(tm1638_clock_pin, LOW); delayMicroseconds(TM1638_CLOCK_DELAY); - if (digitalRead(tm1638_data_pin)) { temp |= 0x80; } + temp |= digitalRead(tm1638_data_pin) << i; digitalWrite(tm1638_clock_pin, HIGH); } @@ -165,25 +165,32 @@ void TmInit() digitalWrite(tm1638_strobe_pin, HIGH); tm1638_type = 1; + tm1638_state = 1; } } void TmLoop() { - byte buttons = Tm1638GetButtons(); - for (byte i = 0; i < MAX_SWITCHES; i++) { - virtualswitch[i] = buttons &1; - byte color = (virtualswitch[i]) ? TM1638_COLOR_RED : TM1638_COLOR_NONE; - Tm1638SetLED(color, i); - buttons >>= 1; + if (tm1638_state) { + byte buttons = Tm1638GetButtons(); + for (byte i = 0; i < MAX_SWITCHES; i++) { + virtualswitch[i] = (buttons &1) ^1; + byte color = (virtualswitch[i]) ? TM1638_COLOR_NONE : TM1638_COLOR_RED; + Tm1638SetLED(color, i); + buttons >>= 1; + } + SwitchHandler(1); } - SwitchHandler(1); } +/* void TmShow(boolean json) { + if (tm1638_type) { + } } +*/ /*********************************************************************************************\ * Interface