Add TM1638 switch support

Add TM1638 switch support (#2226)
This commit is contained in:
Theo Arends 2018-07-12 14:52:35 +02:00
parent 5372cdf511
commit bee95c5b56
3 changed files with 28 additions and 17 deletions

View File

@ -1,4 +1,5 @@
/* 6.1.0a /* 6.1.0a
* Add TM1638 switch support (#2226)
* Fix invalid response using more than 4 switches and domoticz * Fix invalid response using more than 4 switches and domoticz
* Update sensor drivers to provide instant results * Update sensor drivers to provide instant results
* Add read sensor retry to DS18B20, DS18x20, DHT, SHT1X and HTU21 * Add read sensor retry to DS18B20, DS18x20, DHT, SHT1X and HTU21

View File

@ -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 multipress[MAX_KEYS] = { 0 }; // Number of button presses within multiwindow
uint8_t lastwallswitch[MAX_SWITCHES]; // Last wall switch states uint8_t lastwallswitch[MAX_SWITCHES]; // Last wall switch states
uint8_t holdwallswitch[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold 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 mytmplt my_module; // Active copy of Module name and GPIOs
uint8_t pin[GPIO_MAX]; // Possible pin configurations uint8_t pin[GPIO_MAX]; // Possible pin configurations
@ -1734,7 +1734,7 @@ void SwitchHandler(byte mode)
uint8_t switchflag; uint8_t switchflag;
for (byte i = 0; i < MAX_SWITCHES; i++) { for (byte i = 0; i < MAX_SWITCHES; i++) {
if (pin[GPIO_SWT1 +i] < 99) { if ((pin[GPIO_SWT1 +i] < 99) || (mode)) {
if (holdwallswitch[i]) { if (holdwallswitch[i]) {
holdwallswitch[i]--; holdwallswitch[i]--;
@ -2390,10 +2390,13 @@ void GpioInit()
} }
} }
for (byte i = 0; i < MAX_SWITCHES; i++) { for (byte i = 0; i < MAX_SWITCHES; i++) {
lastwallswitch[i] = 1; // Init global to virtual switch state;
if (pin[GPIO_SWT1 +i] < 99) { if (pin[GPIO_SWT1 +i] < 99) {
pinMode(pin[GPIO_SWT1 +i], (16 == pin[GPIO_SWT1 +i]) ? INPUT_PULLDOWN_16 :INPUT_PULLUP); 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 #ifdef USE_WS2812

View File

@ -35,19 +35,20 @@ uint8_t tm1638_clock_pin = 0;
uint8_t tm1638_data_pin = 0; uint8_t tm1638_data_pin = 0;
uint8_t tm1638_strobe_pin = 0; uint8_t tm1638_strobe_pin = 0;
uint8_t tm1638_displays = 8; 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_intensity = 0;
uint8_t tm1638_state = 0;
/*********************************************************************************************\ /*********************************************************************************************\
* Pieces from library https://github.com/rjbatista/tm1638-library * Pieces from library https://github.com/rjbatista/tm1638-library
* and from library https://github.com/MartyMacGyver/TM1638-demos-and-examples
\*********************************************************************************************/ \*********************************************************************************************/
void Tm16XXSend(byte data) 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_clock_pin, LOW);
digitalWrite(tm1638_data_pin, data & 1 ? HIGH : LOW);
data >>= 1;
delayMicroseconds(TM1638_CLOCK_DELAY); delayMicroseconds(TM1638_CLOCK_DELAY);
digitalWrite(tm1638_clock_pin, HIGH); digitalWrite(tm1638_clock_pin, HIGH);
} }
@ -77,11 +78,10 @@ byte Tm16XXReceive()
pinMode(tm1638_data_pin, INPUT); pinMode(tm1638_data_pin, INPUT);
digitalWrite(tm1638_data_pin, HIGH); digitalWrite(tm1638_data_pin, HIGH);
for (int i = 0; i < 8; i++) { for (uint8_t i = 0; i < 8; ++i) {
temp >>= 1;
digitalWrite(tm1638_clock_pin, LOW); digitalWrite(tm1638_clock_pin, LOW);
delayMicroseconds(TM1638_CLOCK_DELAY); delayMicroseconds(TM1638_CLOCK_DELAY);
if (digitalRead(tm1638_data_pin)) { temp |= 0x80; } temp |= digitalRead(tm1638_data_pin) << i;
digitalWrite(tm1638_clock_pin, HIGH); digitalWrite(tm1638_clock_pin, HIGH);
} }
@ -165,25 +165,32 @@ void TmInit()
digitalWrite(tm1638_strobe_pin, HIGH); digitalWrite(tm1638_strobe_pin, HIGH);
tm1638_type = 1; tm1638_type = 1;
tm1638_state = 1;
} }
} }
void TmLoop() void TmLoop()
{ {
byte buttons = Tm1638GetButtons(); if (tm1638_state) {
for (byte i = 0; i < MAX_SWITCHES; i++) { byte buttons = Tm1638GetButtons();
virtualswitch[i] = buttons &1; for (byte i = 0; i < MAX_SWITCHES; i++) {
byte color = (virtualswitch[i]) ? TM1638_COLOR_RED : TM1638_COLOR_NONE; virtualswitch[i] = (buttons &1) ^1;
Tm1638SetLED(color, i); byte color = (virtualswitch[i]) ? TM1638_COLOR_NONE : TM1638_COLOR_RED;
buttons >>= 1; Tm1638SetLED(color, i);
buttons >>= 1;
}
SwitchHandler(1);
} }
SwitchHandler(1);
} }
/*
void TmShow(boolean json) void TmShow(boolean json)
{ {
if (tm1638_type) {
}
} }
*/
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface