2018-11-10 15:30:29 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdrv_18_armtronix_dimmers.ino - Armtronix dimmers support for Tasmota
|
2018-11-10 15:30:29 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 wvdv2002 and Theo Arends
|
2018-11-10 15:30:29 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:43:23 +01:00
|
|
|
#ifdef USE_LIGHT
|
2018-11-10 15:30:29 +00:00
|
|
|
#ifdef USE_ARMTRONIX_DIMMERS
|
2018-11-16 11:22:15 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* This code can be used for Armtronix dimmers.
|
|
|
|
* The dimmers contain a Atmega328 to do the actual dimming.
|
|
|
|
* Checkout the Tasmota Wiki for information on how to flash this Atmega328 with the firmware
|
|
|
|
* to work together with this driver.
|
|
|
|
\*********************************************************************************************/
|
2018-11-10 15:30:29 +00:00
|
|
|
|
|
|
|
#define XDRV_18 18
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
TasmotaSerial *ArmtronixSerial = nullptr;
|
|
|
|
|
2019-08-17 14:07:46 +01:00
|
|
|
struct ARMTRONIX {
|
|
|
|
bool ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction
|
|
|
|
int8_t wifi_state = -2; // Keep MCU wifi-status in sync with WifiState()
|
|
|
|
int8_t dim_state[2]; // Dimmer state values.
|
|
|
|
int8_t knob_state[2]; // Dimmer state values.
|
|
|
|
} Armtronix;
|
2018-11-10 15:30:29 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Internal Functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool ArmtronixSetChannels(void)
|
2018-12-17 16:34:55 +00:00
|
|
|
{
|
|
|
|
LightSerial2Duty(((uint8_t*)XdrvMailbox.data)[0], ((uint8_t*)XdrvMailbox.data)[1]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-10 15:30:29 +00:00
|
|
|
void LightSerial2Duty(uint8_t duty1, uint8_t duty2)
|
|
|
|
{
|
2019-08-17 14:07:46 +01:00
|
|
|
if (ArmtronixSerial && !Armtronix.ignore_dim) {
|
2018-11-16 11:22:15 +00:00
|
|
|
duty1 = ((float)duty1)/2.575757; //max 99
|
|
|
|
duty2 = ((float)duty2)/2.575757; //max 99
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.dim_state[0] = duty1;
|
|
|
|
Armtronix.dim_state[1] = duty2;
|
2018-11-16 11:22:15 +00:00
|
|
|
ArmtronixSerial->print("Dimmer1:");
|
|
|
|
ArmtronixSerial->print(duty1);
|
|
|
|
ArmtronixSerial->print("\nDimmer2:");
|
|
|
|
ArmtronixSerial->println(duty2);
|
2018-11-10 15:30:29 +00:00
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("ARM: Send Serial Packet Dim Values=%d,%d"), Armtronix.dim_state[0],Armtronix.dim_state[1]);
|
2018-11-10 15:30:29 +00:00
|
|
|
|
|
|
|
} else {
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.ignore_dim = false;
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("ARM: Send Dim Level skipped due to already set. Value=%d,%d"), Armtronix.dim_state[0],Armtronix.dim_state[1]);
|
2018-11-10 15:30:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:22:15 +00:00
|
|
|
void ArmtronixRequestState(void)
|
|
|
|
{
|
|
|
|
if (ArmtronixSerial) {
|
2018-11-10 15:30:29 +00:00
|
|
|
// Get current status of MCU
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("ARM: Request MCU state"));
|
2018-11-10 15:30:29 +00:00
|
|
|
ArmtronixSerial->println("Status");
|
2018-11-16 11:22:15 +00:00
|
|
|
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* API Functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool ArmtronixModuleSelected(void)
|
2018-11-10 15:30:29 +00:00
|
|
|
{
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.devices_present++;
|
|
|
|
TasmotaGlobal.light_type = LT_SERIAL2;
|
2018-11-10 15:30:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:22:15 +00:00
|
|
|
void ArmtronixInit(void)
|
2018-11-10 15:30:29 +00:00
|
|
|
{
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.dim_state[0] = -1;
|
|
|
|
Armtronix.dim_state[1] = -1;
|
|
|
|
Armtronix.knob_state[0] = -1;
|
|
|
|
Armtronix.knob_state[1] = -1;
|
2020-04-26 16:33:27 +01:00
|
|
|
ArmtronixSerial = new TasmotaSerial(Pin(GPIO_RXD), Pin(GPIO_TXD), 2);
|
2018-11-10 15:30:29 +00:00
|
|
|
if (ArmtronixSerial->begin(115200)) {
|
|
|
|
if (ArmtronixSerial->hardwareSerial()) { ClaimSerial(); }
|
|
|
|
ArmtronixSerial->println("Status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:22:15 +00:00
|
|
|
void ArmtronixSerialInput(void)
|
2018-11-10 15:30:29 +00:00
|
|
|
{
|
|
|
|
String answer;
|
|
|
|
int8_t newDimState[2];
|
|
|
|
uint8_t temp;
|
|
|
|
int commaIndex;
|
2018-11-16 11:22:15 +00:00
|
|
|
char scmnd[20];
|
2018-11-10 15:30:29 +00:00
|
|
|
if (ArmtronixSerial->available()) {
|
|
|
|
yield();
|
|
|
|
answer = ArmtronixSerial->readStringUntil('\n');
|
2018-11-16 11:22:15 +00:00
|
|
|
if (answer.substring(0,7) == "Status:") {
|
2018-11-10 15:30:29 +00:00
|
|
|
commaIndex = 6;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i =0; i<2; i++) {
|
2018-11-10 15:30:29 +00:00
|
|
|
newDimState[i] = answer.substring(commaIndex+1,answer.indexOf(',',commaIndex+1)).toInt();
|
2019-08-17 14:07:46 +01:00
|
|
|
if (newDimState[i] != Armtronix.dim_state[i]) {
|
2018-11-10 15:30:29 +00:00
|
|
|
temp = ((float)newDimState[i])*1.01010101010101; //max 255
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.dim_state[i] = newDimState[i];
|
|
|
|
Armtronix.ignore_dim = true;
|
2018-11-10 15:30:29 +00:00
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_CHANNEL "%d %d"),i+1, temp);
|
|
|
|
ExecuteCommand(scmnd,SRC_SWITCH);
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("ARM: Send CMND_CHANNEL=%s"), scmnd );
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
2018-11-16 11:22:15 +00:00
|
|
|
commaIndex = answer.indexOf(',',commaIndex+1);
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.knob_state[0] = answer.substring(commaIndex+1,answer.indexOf(',',commaIndex+1)).toInt();
|
2018-11-10 15:30:29 +00:00
|
|
|
commaIndex = answer.indexOf(',',commaIndex+1);
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.knob_state[1] = answer.substring(commaIndex+1,answer.indexOf(',',commaIndex+1)).toInt();
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 11:22:15 +00:00
|
|
|
void ArmtronixSetWifiLed(void)
|
|
|
|
{
|
|
|
|
uint8_t wifi_state = 0x02;
|
|
|
|
|
|
|
|
switch (WifiState()) {
|
|
|
|
case WIFI_MANAGER:
|
|
|
|
wifi_state = 0x01;
|
|
|
|
break;
|
|
|
|
case WIFI_RESTART:
|
|
|
|
wifi_state = 0x03;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-10 15:30:29 +00:00
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("ARM: Set WiFi LED to state %d (%d)"), wifi_state, WifiState());
|
2018-11-16 11:22:15 +00:00
|
|
|
|
2018-12-24 17:14:25 +00:00
|
|
|
char state = '0' + ((wifi_state & 1) > 0);
|
2018-11-16 11:22:15 +00:00
|
|
|
ArmtronixSerial->print("Setled:");
|
|
|
|
ArmtronixSerial->write(state);
|
|
|
|
ArmtronixSerial->write(',');
|
2018-12-24 17:14:25 +00:00
|
|
|
state = '0' + ((wifi_state & 2) > 0);
|
2018-11-16 11:22:15 +00:00
|
|
|
ArmtronixSerial->write(state);
|
|
|
|
ArmtronixSerial->write(10);
|
2019-08-17 14:07:46 +01:00
|
|
|
Armtronix.wifi_state = WifiState();
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
2018-11-16 11:22:15 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xdrv18(uint8_t function)
|
2018-11-10 15:30:29 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-11-10 15:30:29 +00:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if (ARMTRONIX_DIMMERS == TasmotaGlobal.module_type) {
|
2018-11-10 15:30:29 +00:00
|
|
|
switch (function) {
|
2019-03-30 12:03:45 +00:00
|
|
|
case FUNC_LOOP:
|
|
|
|
if (ArmtronixSerial) { ArmtronixSerialInput(); }
|
|
|
|
break;
|
2018-11-10 15:30:29 +00:00
|
|
|
case FUNC_MODULE_INIT:
|
|
|
|
result = ArmtronixModuleSelected();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
ArmtronixInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
2018-11-16 11:22:15 +00:00
|
|
|
if (ArmtronixSerial) {
|
2019-08-17 14:07:46 +01:00
|
|
|
if (Armtronix.wifi_state!=WifiState()) { ArmtronixSetWifiLed(); }
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime &1) {
|
2018-11-10 15:30:29 +00:00
|
|
|
ArmtronixSerial->println("Status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-12-17 16:34:55 +00:00
|
|
|
case FUNC_SET_CHANNELS:
|
|
|
|
result = ArmtronixSetChannels();
|
|
|
|
break;
|
2018-11-10 15:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_ARMTRONIX_DIMMERS
|
2019-06-16 15:43:23 +01:00
|
|
|
#endif // USE_LIGHT
|