2020-03-01 14:31:08 +00:00
|
|
|
/*
|
|
|
|
xdrv_37_sonoff_d1.ino - sonoff D1 dimmer support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2020-03-01 14:31:08 +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/>.
|
|
|
|
*/
|
|
|
|
|
2020-05-02 16:13:50 +01:00
|
|
|
#ifdef USE_LIGHT
|
2020-03-01 14:31:08 +00:00
|
|
|
#ifdef USE_SONOFF_D1
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Sonoff D1 dimmer 433
|
|
|
|
*
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 A B C D E F 10
|
|
|
|
* AA 55 - Header
|
|
|
|
* 01 04 - Version?
|
|
|
|
* 00 0A - Following data length (10 bytes)
|
|
|
|
* 01 - Power state (00 = off, 01 = on, FF = ignore)
|
|
|
|
* 64 - Dimmer percentage (01 to 64 = 1 to 100%)
|
|
|
|
* FF FF FF FF FF FF FF FF - Not used
|
|
|
|
* 6C - CRC over bytes 2 to F (Addition)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XDRV_37 37
|
|
|
|
|
|
|
|
struct SONOFFD1 {
|
2020-03-08 13:49:57 +00:00
|
|
|
uint8_t receive_len = 0;
|
|
|
|
uint8_t power = 255; // Not initialized
|
|
|
|
uint8_t dimmer = 255; // Not initialized
|
2020-03-01 14:31:08 +00:00
|
|
|
} SnfD1;
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void SonoffD1Received(void)
|
|
|
|
{
|
2020-10-29 11:21:24 +00:00
|
|
|
if (TasmotaGlobal.serial_in_byte_counter < 8) { return; } // Received ack from Rf chip (aa 55 01 04 00 00 05)
|
2020-03-01 14:31:08 +00:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
uint8_t action = TasmotaGlobal.serial_in_buffer[6] & 1;
|
2020-03-08 13:49:57 +00:00
|
|
|
if (action != SnfD1.power) {
|
|
|
|
SnfD1.power = action;
|
2020-03-01 14:31:08 +00:00
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SD1: Remote power (%d, %d)"), SnfD1.power, SnfD1.dimmer);
|
2020-03-08 13:49:57 +00:00
|
|
|
|
|
|
|
ExecuteCommandPower(1, action, SRC_SWITCH);
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
2020-03-08 13:49:57 +00:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
uint8_t dimmer = TasmotaGlobal.serial_in_buffer[7];
|
2020-03-08 13:49:57 +00:00
|
|
|
if (dimmer != SnfD1.dimmer) {
|
2020-03-01 14:31:08 +00:00
|
|
|
SnfD1.dimmer = dimmer;
|
2020-03-08 13:49:57 +00:00
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SD1: Remote dimmer (%d, %d)"), SnfD1.power, SnfD1.dimmer);
|
2020-03-08 13:49:57 +00:00
|
|
|
|
|
|
|
char scmnd[20];
|
|
|
|
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), SnfD1.dimmer);
|
|
|
|
ExecuteCommand(scmnd, SRC_SWITCH);
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 13:49:57 +00:00
|
|
|
/*
|
2020-03-01 14:31:08 +00:00
|
|
|
// Send Acknowledge - Copy first 5 bytes, reset byte 6 and store crc in byte 7
|
|
|
|
// AA 55 01 04 00 00 05
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.serial_in_buffer[5] = 0; // Ack
|
|
|
|
TasmotaGlobal.serial_in_buffer[6] = 0; // Crc
|
2020-03-01 14:31:08 +00:00
|
|
|
for (uint32_t i = 0; i < 7; i++) {
|
2020-10-30 11:29:48 +00:00
|
|
|
if ((i > 1) && (i < 6)) { TasmotaGlobal.serial_in_buffer[6] += TasmotaGlobal.serial_in_buffer[i]; }
|
|
|
|
Serial.write(TasmotaGlobal.serial_in_buffer[i]);
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
2020-03-08 13:49:57 +00:00
|
|
|
*/
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SonoffD1SerialInput(void)
|
|
|
|
{
|
2020-10-30 11:29:48 +00:00
|
|
|
if (0xAA == TasmotaGlobal.serial_in_byte) { // 0xAA - Start of text
|
2020-10-29 11:21:24 +00:00
|
|
|
TasmotaGlobal.serial_in_byte_counter = 0;
|
2020-03-08 13:49:57 +00:00
|
|
|
SnfD1.receive_len = 7;
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
2020-03-08 13:49:57 +00:00
|
|
|
if (SnfD1.receive_len) {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.serial_in_buffer[TasmotaGlobal.serial_in_byte_counter++] = TasmotaGlobal.serial_in_byte;
|
2020-10-29 11:21:24 +00:00
|
|
|
if (6 == TasmotaGlobal.serial_in_byte_counter) {
|
2020-10-30 11:29:48 +00:00
|
|
|
SnfD1.receive_len += TasmotaGlobal.serial_in_byte; // 8 or 17
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
2020-10-29 11:21:24 +00:00
|
|
|
if (TasmotaGlobal.serial_in_byte_counter == SnfD1.receive_len) {
|
2020-03-01 14:31:08 +00:00
|
|
|
|
|
|
|
// Sonoff D1 codes
|
2020-03-08 13:49:57 +00:00
|
|
|
// aa 55 01 04 00 0a 01 01 ff ff ff ff ff ff ff ff 09 - Power On, Dimmer 1%
|
|
|
|
// aa 55 01 04 00 0a 01 28 ff ff ff ff ff ff ff ff 30 - Power On, Dimmer 40%
|
|
|
|
// aa 55 01 04 00 0a 01 3c ff ff ff ff ff ff ff ff 44 - Power On, Dimmer 60%
|
|
|
|
// aa 55 01 04 00 0a 01 64 ff ff ff ff ff ff ff ff 6c - Power On, Dimmer 100%
|
|
|
|
// aa 55 01 04 00 0a 00 64 ff ff ff ff ff ff ff ff 6b - Power Off (with last dimmer 100%)
|
|
|
|
// aa 55 01 04 00 0a 01 64 ff ff ff ff ff ff ff ff 6c - Power On (with last dimmer 100%)
|
2020-03-01 14:31:08 +00:00
|
|
|
|
|
|
|
AddLogSerial(LOG_LEVEL_DEBUG);
|
|
|
|
uint8_t crc = 0;
|
2020-03-08 13:49:57 +00:00
|
|
|
for (uint32_t i = 2; i < SnfD1.receive_len -1; i++) {
|
2020-10-30 11:29:48 +00:00
|
|
|
crc += TasmotaGlobal.serial_in_buffer[i];
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
2020-10-30 11:29:48 +00:00
|
|
|
if (crc == TasmotaGlobal.serial_in_buffer[SnfD1.receive_len -1]) {
|
2020-03-01 14:31:08 +00:00
|
|
|
SonoffD1Received();
|
2020-03-08 13:49:57 +00:00
|
|
|
SnfD1.receive_len = 0;
|
2020-03-01 14:31:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.serial_in_byte = 0;
|
2020-03-01 14:31:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2020-03-08 13:49:57 +00:00
|
|
|
void SonoffD1Send()
|
2020-03-01 14:31:08 +00:00
|
|
|
{
|
2020-03-01 15:13:59 +00:00
|
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
|
|
|
uint8_t buffer[17] = { 0xAA,0x55,0x01,0x04,0x00,0x0A,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00 };
|
2020-03-01 14:31:08 +00:00
|
|
|
|
2020-03-08 13:49:57 +00:00
|
|
|
buffer[6] = SnfD1.power;
|
|
|
|
buffer[7] = SnfD1.dimmer;
|
2020-03-01 14:31:08 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < sizeof(buffer); i++) {
|
|
|
|
if ((i > 1) && (i < sizeof(buffer) -1)) { buffer[16] += buffer[i]; }
|
|
|
|
Serial.write(buffer[i]);
|
|
|
|
}
|
2020-03-01 15:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SonoffD1SendPower(void)
|
|
|
|
{
|
2020-03-08 13:49:57 +00:00
|
|
|
uint8_t action = XdrvMailbox.index &1;
|
|
|
|
if (action != SnfD1.power) {
|
|
|
|
SnfD1.power = action;
|
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SD1: Tasmota power (%d, %d)"), SnfD1.power, SnfD1.dimmer);
|
2020-03-08 13:49:57 +00:00
|
|
|
|
|
|
|
SonoffD1Send();
|
|
|
|
}
|
2020-03-01 14:31:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SonoffD1SendDimmer(void)
|
|
|
|
{
|
2020-03-08 13:49:57 +00:00
|
|
|
uint8_t dimmer = LightGetDimmer(1);
|
2021-06-11 17:14:12 +01:00
|
|
|
dimmer = (dimmer < Settings->dimmer_hw_min) ? Settings->dimmer_hw_min : dimmer;
|
|
|
|
dimmer = (dimmer > Settings->dimmer_hw_max) ? Settings->dimmer_hw_max : dimmer;
|
2020-03-08 13:49:57 +00:00
|
|
|
if (dimmer != SnfD1.dimmer) {
|
|
|
|
SnfD1.dimmer = dimmer;
|
|
|
|
|
2021-01-23 15:26:23 +00:00
|
|
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("SD1: Tasmota dimmer (%d, %d)"), SnfD1.power, SnfD1.dimmer);
|
2020-03-01 14:31:08 +00:00
|
|
|
|
2020-03-08 13:49:57 +00:00
|
|
|
SonoffD1Send();
|
|
|
|
}
|
2020-03-01 14:31:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SonoffD1ModuleSelected(void)
|
|
|
|
{
|
|
|
|
SetSerial(9600, TS_SERIAL_8N1);
|
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.devices_present++;
|
|
|
|
TasmotaGlobal.light_type = LT_SERIAL1;
|
2020-03-01 14:31:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xdrv37(uint32_t function)
|
2020-03-01 14:31:08 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if (SONOFF_D1 == TasmotaGlobal.module_type) {
|
2020-03-01 14:31:08 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_SERIAL:
|
|
|
|
result = SonoffD1SerialInput();
|
|
|
|
break;
|
|
|
|
case FUNC_SET_DEVICE_POWER:
|
|
|
|
result = SonoffD1SendPower();
|
|
|
|
break;
|
|
|
|
case FUNC_SET_CHANNELS:
|
|
|
|
result = SonoffD1SendDimmer();
|
|
|
|
break;
|
|
|
|
case FUNC_MODULE_INIT:
|
|
|
|
result = SonoffD1ModuleSelected();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SONOFF_D1
|
2020-05-02 16:13:50 +01:00
|
|
|
#endif // USE_LIGHT
|