2018-06-28 16:40:37 +01:00
|
|
|
/*
|
|
|
|
xsns_28_tm1638.ino - TM1638 8 switch, led and 7 segment unit support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
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 TM16 DIO, TM16 CLK and TM16 STB
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_28 28
|
|
|
|
|
2018-06-28 16:40:37 +01:00
|
|
|
#define TM1638_COLOR_NONE 0
|
|
|
|
#define TM1638_COLOR_RED 1
|
|
|
|
#define TM1638_COLOR_GREEN 2
|
|
|
|
|
2018-06-30 13:41:40 +01:00
|
|
|
#define TM1638_CLOCK_DELAY 1 // uSec
|
|
|
|
|
2018-06-28 16:40:37 +01:00
|
|
|
uint8_t tm1638_type = 1;
|
|
|
|
uint8_t tm1638_clock_pin = 0;
|
|
|
|
uint8_t tm1638_data_pin = 0;
|
|
|
|
uint8_t tm1638_strobe_pin = 0;
|
|
|
|
uint8_t tm1638_displays = 8;
|
2018-07-12 13:52:35 +01:00
|
|
|
uint8_t tm1638_active_display = 1;
|
2018-06-28 16:40:37 +01:00
|
|
|
uint8_t tm1638_intensity = 0;
|
2018-07-12 13:52:35 +01:00
|
|
|
uint8_t tm1638_state = 0;
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Pieces from library https://github.com/rjbatista/tm1638-library
|
2018-07-12 13:52:35 +01:00
|
|
|
* and from library https://github.com/MartyMacGyver/TM1638-demos-and-examples
|
2018-06-28 16:40:37 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Tm16XXSend(uint8_t data)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2018-07-12 13:52:35 +01:00
|
|
|
for (uint8_t i = 0; i < 8; i++) {
|
|
|
|
digitalWrite(tm1638_data_pin, !!(data & (1 << i)));
|
2018-06-28 16:40:37 +01:00
|
|
|
digitalWrite(tm1638_clock_pin, LOW);
|
2018-06-30 13:53:04 +01:00
|
|
|
delayMicroseconds(TM1638_CLOCK_DELAY);
|
2018-06-28 16:40:37 +01:00
|
|
|
digitalWrite(tm1638_clock_pin, HIGH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Tm16XXSendCommand(uint8_t cmd)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
|
|
|
digitalWrite(tm1638_strobe_pin, LOW);
|
|
|
|
Tm16XXSend(cmd);
|
|
|
|
digitalWrite(tm1638_strobe_pin, HIGH);
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void TM16XXSendData(uint8_t address, uint8_t data)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
|
|
|
Tm16XXSendCommand(0x44);
|
|
|
|
digitalWrite(tm1638_strobe_pin, LOW);
|
|
|
|
Tm16XXSend(0xC0 | address);
|
|
|
|
Tm16XXSend(data);
|
|
|
|
digitalWrite(tm1638_strobe_pin, HIGH);
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t Tm16XXReceive(void)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t temp = 0;
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
// Pull-up on
|
|
|
|
pinMode(tm1638_data_pin, INPUT);
|
|
|
|
digitalWrite(tm1638_data_pin, HIGH);
|
|
|
|
|
2018-07-12 13:52:35 +01:00
|
|
|
for (uint8_t i = 0; i < 8; ++i) {
|
2018-06-28 16:40:37 +01:00
|
|
|
digitalWrite(tm1638_clock_pin, LOW);
|
2018-06-30 13:41:40 +01:00
|
|
|
delayMicroseconds(TM1638_CLOCK_DELAY);
|
2018-07-12 13:52:35 +01:00
|
|
|
temp |= digitalRead(tm1638_data_pin) << i;
|
2018-06-28 16:40:37 +01:00
|
|
|
digitalWrite(tm1638_clock_pin, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull-up off
|
|
|
|
pinMode(tm1638_data_pin, OUTPUT);
|
|
|
|
digitalWrite(tm1638_data_pin, LOW);
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Tm16XXClearDisplay(void)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < tm1638_displays; i++) {
|
|
|
|
TM16XXSendData(i << 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Tm1638SetLED(uint8_t color, uint8_t pos)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
|
|
|
TM16XXSendData((pos << 1) + 1, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tm1638SetLEDs(word leds)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < tm1638_displays; i++) {
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t color = 0;
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
if ((leds & (1 << i)) != 0) {
|
|
|
|
color |= TM1638_COLOR_RED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((leds & (1 << (i + 8))) != 0) {
|
|
|
|
color |= TM1638_COLOR_GREEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tm1638SetLED(color, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t Tm1638GetButtons(void)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t keys = 0;
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
digitalWrite(tm1638_strobe_pin, LOW);
|
|
|
|
Tm16XXSend(0x42);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
keys |= Tm16XXReceive() << i;
|
|
|
|
}
|
|
|
|
digitalWrite(tm1638_strobe_pin, HIGH);
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void TmInit(void)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
|
|
|
tm1638_type = 0;
|
|
|
|
if ((pin[GPIO_TM16CLK] < 99) && (pin[GPIO_TM16DIO] < 99) && (pin[GPIO_TM16STB] < 99)) {
|
|
|
|
tm1638_clock_pin = pin[GPIO_TM16CLK];
|
|
|
|
tm1638_data_pin = pin[GPIO_TM16DIO];
|
|
|
|
tm1638_strobe_pin = pin[GPIO_TM16STB];
|
|
|
|
|
|
|
|
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);
|
2018-07-14 11:10:13 +01:00
|
|
|
Tm16XXSendCommand(0x80 | (tm1638_active_display ? 8 : 0) | tmin(7, tm1638_intensity));
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
digitalWrite(tm1638_strobe_pin, LOW);
|
|
|
|
Tm16XXSend(0xC0);
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
Tm16XXSend(0x00);
|
|
|
|
}
|
|
|
|
digitalWrite(tm1638_strobe_pin, HIGH);
|
|
|
|
|
|
|
|
tm1638_type = 1;
|
2018-07-12 13:52:35 +01:00
|
|
|
tm1638_state = 1;
|
2018-06-28 16:40:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void TmLoop(void)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2018-07-12 13:52:35 +01:00
|
|
|
if (tm1638_state) {
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t buttons = Tm1638GetButtons();
|
|
|
|
for (uint8_t i = 0; i < MAX_SWITCHES; i++) {
|
2018-12-28 15:35:19 +00:00
|
|
|
SwitchSetVirtual(i, (buttons &1) ^1);
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t color = (SwitchGetVirtual(i)) ? TM1638_COLOR_NONE : TM1638_COLOR_RED;
|
2018-07-12 13:52:35 +01:00
|
|
|
Tm1638SetLED(color, i);
|
|
|
|
buttons >>= 1;
|
|
|
|
}
|
|
|
|
SwitchHandler(1);
|
2018-06-28 16:40:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 13:52:35 +01:00
|
|
|
/*
|
2019-01-28 13:08:33 +00:00
|
|
|
void TmShow(bool json)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2018-07-12 13:52:35 +01:00
|
|
|
if (tm1638_type) {
|
2018-06-28 16:40:37 +01:00
|
|
|
|
2018-07-12 13:52:35 +01:00
|
|
|
}
|
2018-06-28 16:40:37 +01:00
|
|
|
}
|
2018-07-12 13:52:35 +01:00
|
|
|
*/
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns28(uint8_t function)
|
2018-06-28 16:40:37 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-06-28 16:40:37 +01:00
|
|
|
|
|
|
|
if (tm1638_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
TmInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_50_MSECOND:
|
|
|
|
TmLoop();
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
TmShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_APPEND:
|
|
|
|
TmShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_TM1638
|