Skip to content

Commit

Permalink
Breaking change TM1638 button and led support
Browse files Browse the repository at this point in the history
Breaking change TM1638 button and led support are handled as virtual switches and relays (#11031)
  • Loading branch information
arendst committed Feb 6, 2023
1 parent ce29bc3 commit 055331d
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 237 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- Basic support for Shelly Pro 4PM

### Breaking Changed
- TM1638 button and led support are handled as virtual switches and relays (#11031)

### Changed

Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm
- ESP32 preliminary support for Matter protocol, milestone 1 (commissioning) by Stephan Hadinger

### Breaking Changed
- TM1638 button and led support are handled as virtual switches and relays [#11031](https://github.com/arendst/Tasmota/issues/11031)

### Changed
- ESP32 Framework (Core) from v2.0.5.3 to v2.0.6 (IPv6 support)
Expand Down
2 changes: 1 addition & 1 deletion tasmota/tasmota_support/support_features.ino
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void ResponseAppendFeatures(void)
feature3 |= 0x40000000; // xsns_27_apds9960.ino
#endif
#ifdef USE_TM1638
feature3 |= 0x80000000; // xsns_28_tm1638.ino
feature3 |= 0x80000000; // xdrv_66_tm1638.ino
#endif
}

Expand Down
4 changes: 0 additions & 4 deletions tasmota/tasmota_support/support_tasmota.ino
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 +963,7 @@ bool MqttShowSensor(bool call_show_sensor) {

int json_data_start = ResponseLength();
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
#ifdef USE_TM1638
if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) {
#else
if (SwitchUsed(i)) {
#endif // USE_TM1638
ResponseAppend_P(PSTR(",\"%s\":\"%s\""), GetSwitchText(i).c_str(), GetStateText(SwitchState(i)));
}
}
Expand Down
4 changes: 0 additions & 4 deletions tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,7 @@ void RulesEvery50ms(void)
}
// Boot time SWITCHES Status
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
#ifdef USE_TM1638
if (PinUsed(GPIO_SWT1, i) || (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB))) {
#else
if (SwitchUsed(i)) {
#endif // USE_TM1638
snprintf_P(json_event, sizeof(json_event), PSTR("{\"%s\":{\"Boot\":%d}}"), GetSwitchText(i).c_str(), (SwitchState(i)));
RulesProcessEvent(json_event);
}
Expand Down
269 changes: 269 additions & 0 deletions tasmota/tasmota_xdrv_driver/xdrv_66_tm1638.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/*
xdrv_66_tm1638.ino - TM1638 8 switch, led and 7 segment unit support for Tasmota
Copyright (C) 2021 Theo Arends
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef USE_TM1638
/*********************************************************************************************\
* TM1638 8 switch, led and 7 segment
*
* Uses GPIO TM1638 DIO, TM1638 CLK and TM1638 STB
\*********************************************************************************************/

#define XDRV_66 66

#ifdef TM1638_USE_AS_BUTTON
#define TM1638_USE_BUTTONS // Use keys as buttons
#endif
#ifdef TM1638_USE_AS_SWITCH
#undef TM1638_USE_BUTTONS // Use keys as switches
#endif
#ifndef TM1638_MAX_DISPLAYS
#define TM1638_MAX_DISPLAYS 8
#endif
#ifndef TM1638_MAX_KEYS
#define TM1638_MAX_KEYS 8
#endif
#ifndef TM1638_MAX_LEDS
#define TM1638_MAX_LEDS 8
#endif

#define TM1638_COLOR_NONE 0
#define TM1638_COLOR_RED 1
#define TM1638_COLOR_GREEN 2

#define TM1638_CLOCK_DELAY 1 // uSec

struct TM1638 {
int8_t clock_pin = 0;
int8_t data_pin = 0;
int8_t strobe_pin = 0;
int8_t key_offset;
int8_t led_offset;
uint8_t displays;
uint8_t power;

uint8_t active_display = 1;
uint8_t intensity = 0;
bool detected = false;
} Tm1638;

/*********************************************************************************************\
* Pieces from library https://github.com/rjbatista/tm1638-library
* and from library https://github.com/MartyMacGyver/TM1638-demos-and-examples
\*********************************************************************************************/

void Tm16XXSend(uint8_t data) {
for (uint32_t i = 0; i < 8; i++) {
digitalWrite(Tm1638.data_pin, !!(data & (1 << i)));
digitalWrite(Tm1638.clock_pin, LOW);
delayMicroseconds(TM1638_CLOCK_DELAY);
digitalWrite(Tm1638.clock_pin, HIGH);
}
}

void Tm16XXSendCommand(uint8_t cmd) {
digitalWrite(Tm1638.strobe_pin, LOW);
Tm16XXSend(cmd);
digitalWrite(Tm1638.strobe_pin, HIGH);
}

void TM16XXSendData(uint8_t address, uint8_t data) {
Tm16XXSendCommand(0x44);
digitalWrite(Tm1638.strobe_pin, LOW);
Tm16XXSend(0xC0 | address);
Tm16XXSend(data);
digitalWrite(Tm1638.strobe_pin, HIGH);
}

uint8_t Tm16XXReceive(void) {
uint8_t temp = 0;

// Pull-up on
pinMode(Tm1638.data_pin, INPUT);
digitalWrite(Tm1638.data_pin, HIGH);

for (uint32_t i = 0; i < 8; ++i) {
digitalWrite(Tm1638.clock_pin, LOW);
delayMicroseconds(TM1638_CLOCK_DELAY);
temp |= digitalRead(Tm1638.data_pin) << i;
digitalWrite(Tm1638.clock_pin, HIGH);
}

// Pull-up off
pinMode(Tm1638.data_pin, OUTPUT);
digitalWrite(Tm1638.data_pin, LOW);

return temp;
}

/*********************************************************************************************/

void Tm16XXClearDisplay(void) {
for (uint32_t i = 0; i < Tm1638.displays; i++) {
TM16XXSendData(i << 1, 0);
}
}

void Tm1638SetLED(uint8_t color, uint8_t pos) {
TM16XXSendData((pos << 1) + 1, color);
}

void Tm1638SetLEDs(word leds) {
for (uint32_t i = 0; i < Tm1638.displays; i++) {
uint8_t color = 0;

if ((leds & (1 << i)) != 0) {
color |= TM1638_COLOR_RED;
}

if ((leds & (1 << (i + 8))) != 0) {
color |= TM1638_COLOR_GREEN;
}

Tm1638SetLED(color, i);
}
}

uint8_t Tm1638GetButtons(void) {
uint8_t keys = 0;

digitalWrite(Tm1638.strobe_pin, LOW);
Tm16XXSend(0x42);
for (uint32_t i = 0; i < 4; i++) {
keys |= Tm16XXReceive() << i;
}
digitalWrite(Tm1638.strobe_pin, HIGH);

return keys;
}

/*********************************************************************************************/

void TmInit(void) {
if (PinUsed(GPIO_TM1638CLK) && PinUsed(GPIO_TM1638DIO) && PinUsed(GPIO_TM1638STB)) {
Tm1638.clock_pin = Pin(GPIO_TM1638CLK);
Tm1638.data_pin = Pin(GPIO_TM1638DIO);
Tm1638.strobe_pin = Pin(GPIO_TM1638STB);

pinMode(Tm1638.data_pin, OUTPUT);
pinMode(Tm1638.clock_pin, OUTPUT);
pinMode(Tm1638.strobe_pin, OUTPUT);

digitalWrite(Tm1638.strobe_pin, HIGH);
digitalWrite(Tm1638.clock_pin, HIGH);

Tm16XXSendCommand(0x40);
Tm16XXSendCommand(0x80 | (Tm1638.active_display ? 8 : 0) | tmin(7, Tm1638.intensity));

digitalWrite(Tm1638.strobe_pin, LOW);
Tm16XXSend(0xC0);
for (uint32_t i = 0; i < 16; i++) {
Tm16XXSend(0x00);
}
digitalWrite(Tm1638.strobe_pin, HIGH);

// Dirty hack to offset TM1638 leds from GPIO relays
// At this time in code sequence the number of GPIO relays has not been established
uint32_t bi_device = 0;
uint32_t devices_present = 0;
for (uint32_t i = 0; i < MAX_RELAYS; i++) {
if (PinUsed(GPIO_REL1, i)) {
devices_present++;
if (bitRead(TasmotaGlobal.rel_bistable, i)) {
if (bi_device &1) { devices_present--; }
bi_device++;
}
}
}

Tm1638.led_offset = devices_present;
TasmotaGlobal.devices_present += TM1638_MAX_LEDS;
Tm1638.key_offset = -1;
Tm1638.displays = TM1638_MAX_DISPLAYS;
Tm1638.detected = true;
}
}

void TmLoop(void) {
uint8_t buttons = Tm1638GetButtons();
for (uint32_t i = 0; i < TM1638_MAX_KEYS; i++) {
uint32_t state = buttons &1;
#ifdef TM1638_USE_BUTTONS
ButtonSetVirtualPinState(Tm1638.key_offset +i, state);
#else
SwitchSetVirtualPinState(Tm1638.key_offset +i, state ^1);
#endif
buttons >>= 1;
}
}

void TmPower(void) {
power_t rpower = XdrvMailbox.index >> Tm1638.led_offset;
for (uint32_t i = 0; i < TM1638_MAX_LEDS; i++) {
uint32_t state = rpower &1;
uint8_t color = (state) ? TM1638_COLOR_RED : TM1638_COLOR_NONE;
Tm1638SetLED(color, i);
rpower >>= 1; // Select next power
}
}

bool TmAddKey(void) {
// XdrvMailbox.index = button/switch index
if (Tm1638.key_offset < 0) { Tm1638.key_offset = XdrvMailbox.index; }
uint32_t index = XdrvMailbox.index - Tm1638.key_offset;
if (index >= TM1638_MAX_KEYS) { return false; }
uint8_t buttons = Tm1638GetButtons();
uint32_t state = bitRead(buttons, index);
#ifdef TM1638_USE_BUTTONS
XdrvMailbox.index = state | BUTTON_INVERT; // Invert - default is 0
#else
XdrvMailbox.index = state ^1; // Invert - default is 0
#endif
return true;
}

/*********************************************************************************************\
* Interface
\*********************************************************************************************/

bool Xdrv66(uint32_t function) {
bool result = false;

if (FUNC_MODULE_INIT == function) {
TmInit();
} else if (Tm1638.detected) {
switch (function) {
case FUNC_EVERY_50_MSECOND:
TmLoop();
break;
case FUNC_SET_POWER:
TmPower();
break;
#ifdef TM1638_USE_BUTTONS
case FUNC_ADD_BUTTON:
#else
case FUNC_ADD_SWITCH:
#endif
result = TmAddKey();
break;
}
}
return result;
}

#endif // USE_TM1638
Loading

0 comments on commit 055331d

Please sign in to comment.