sm16716 works via mqtt

This commit is contained in:
Gabor Simon 2019-01-19 22:28:58 +00:00
parent c6ddba8fd7
commit 5537383352
2 changed files with 179 additions and 115 deletions

View File

@ -146,6 +146,8 @@ enum UserSelectablePins {
GPIO_MAX31855CS, // MAX31855 Serial interface
GPIO_MAX31855CLK, // MAX31855 Serial interface
GPIO_MAX31855DO, // MAX31855 Serial interface
GPIO_SM16716_CLK, // SM16716 CLK
GPIO_SM16716_DAT, // SM16716 DAT
GPIO_SENSOR_END };
// Programmer selectable GPIO functionality offset by user selectable GPIOs
@ -209,7 +211,8 @@ const char kSensorNames[] PROGMEM =
D_SENSOR_SSPI_MISO "|" D_SENSOR_SSPI_MOSI "|" D_SENSOR_SSPI_SCLK "|" D_SENSOR_SSPI_CS "|" D_SENSOR_SSPI_DC "|"
D_SENSOR_RF_SENSOR "|"
D_SENSOR_AZ_TX "|" D_SENSOR_AZ_RX "|"
D_SENSOR_MAX31855_CS "|" D_SENSOR_MAX31855_CLK "|" D_SENSOR_MAX31855_DO;
D_SENSOR_MAX31855_CS "|" D_SENSOR_MAX31855_CLK "|" D_SENSOR_MAX31855_DO "|"
D_SENSOR_SM16716_CLK "|" D_SENSOR_SM16716_DAT;
/********************************************************************************************/
@ -281,6 +284,7 @@ enum SupportedModules {
KA10,
ZX2820,
MI_DESK_LAMP,
SYF05,
MAXMODULE };
/********************************************************************************************/
@ -506,6 +510,10 @@ const uint8_t kGpioNiceList[] PROGMEM = {
GPIO_MAX31855CLK, // MAX31855 Serial interface
GPIO_MAX31855DO, // MAX31855 Serial interface
#endif
#ifdef USE_SM16716
GPIO_SM16716_CLK, // SM16716 CLK
GPIO_SM16716_DAT, // SM16716 DAT
#endif // USE_SM16716
};
const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = {
@ -574,7 +582,8 @@ const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = {
PHILIPS,
YTF_IR_BRIDGE,
WITTY, // Development Devices
WEMOS
WEMOS,
SYF05
};
// Default module settings
@ -1774,6 +1783,30 @@ const mytmplt kModules[MAXMODULE] PROGMEM = {
GPIO_ROT_A, // GPIO12 Rotary switch A pin
GPIO_ROT_B, // GPIO13 Rotary switch B pin
0, 0, 0, 0
},
{ "SYF05", // Sunyesmart SYF05 (a.k.a. Fcmila) = TYWE3S + SM16726
// https://www.flipkart.com/fc-mila-bxav-xs-ad-smart-bulb/p/itmf85zgs45fzr7n
// https://docs.tuya.com/en/hardware/WiFi-module/wifi-e3s-module.html
// http://www.datasheet-pdf.com/PDF/SM16716-Datasheet-Sunmoon-932771
GPIO_USER, // GPIO00 N.C.
0,
GPIO_USER, // GPIO02 N.C.
0,
GPIO_SM16716_CLK, // GPIO04
GPIO_PWM1, // GPIO05 Cold White
// GPIO06
// GPIO07
// GPIO08
0, // GPIO09
0, // GPIO10
// GPIO11
GPIO_USER, // GPIO12 Warm White, not used on the LED panel
GPIO_USER, // GPIO13 N.C. (used as LED if you add it)
GPIO_SM16716_DAT, // GPIO14
0, // GPIO15
GPIO_USER, // GPIO16 N.C.
GPIO_FLAG_ADC0 // ADC0 A0 Analog input
// + GPIO_FLAG_PULLUP // Allow input pull-up control
}
};

View File

@ -18,6 +18,7 @@
*/
#ifdef USE_SM16716
/*********************************************************************************************\
* SM16716 - Controlling RGB over a synchronous serial line
*
@ -25,134 +26,156 @@
*
\*********************************************************************************************/
#define D_LOG_SM16716 "SM16716: "
#define XDRV_20 20
#define CLK_USEC 10 // Clock interval in microseconds
// Enable this for debug logging
//#define D_LOG_SM16716 "SM16716: "
enum SM16716_Commands {
CMND_SM16716_POWER, CMND_SM16716_DIMMER, CMND_SM16716_COLOR };
CMND_SM16716_COLOR };
const char k_SM16716_Commands[] PROGMEM =
D_CMND_POWER "|" D_CMND_DIMMER "|" D_CMND_COLOR ;
D_CMND_COLOR ;
uint8_t sm61716_pin_clk = 100;
uint8_t sm61716_pin_dat = 100;
uint8_t sm16716_pin_clk = 100;
uint8_t sm16716_pin_dat = 100;
uint8_t sm61716_colour[3] = {0, 0, 0};
boolean sm16716_is_selected = false;
boolean sm16716_is_on = false;
uint8_t sm16716_color[3] = {0, 0, 0};
int8_t sm16716_color_preset = 0;
uint8_t sm61716_eff_red = 0;
uint8_t sm61716_eff_green = 0;
uint8_t sm61716_eff_blue = 0;
int8_t sm61716_colour_preset = 0;
/*********************************************************************************************/
void SM16716_SendBit(uint8_t v)
{
digitalWrite(sm61716_pin_dat, v ? HIGH : LOW);
digitalWrite(sm61716_pin_clk, HIGH);
delayMicroseconds(CLK_USEC);
digitalWrite(sm61716_pin_clk, LOW);
delayMicroseconds(CLK_USEC);
/* NOTE:
* According to the spec sheet, max freq is 30 MHz, that is 16.6 ns per high/low half of the
* clk square wave. That is less than the overhead of 'digitalWrite' at this clock rate,
* so no additional delays are needed yet. */
digitalWrite(sm16716_pin_dat, (v != 0) ? HIGH : LOW);
//delayMicroseconds(1);
digitalWrite(sm16716_pin_clk, HIGH);
//delayMicroseconds(1);
digitalWrite(sm16716_pin_clk, LOW);
}
/*********************************************************************************************/
void SM16716_SendByte(uint8_t v)
{
uint8_t mask;
for (mask = 0x80; mask; mask >>= 1)
for (mask = 0x80; mask; mask >>= 1) {
SM16716_SendBit(v & mask);
}
}
/*********************************************************************************************/
void SM16716_Append_JSON(void)
{
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Append_JSON;"));
AddLog(LOG_LEVEL_DEBUG);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SM16716\":{\"Red\":%d,\"Green\":%d,\"Blue\":%d}"), mqtt_data, sm61716_colour[0], sm61716_colour[1], sm61716_colour[2]);
snprintf_P(mqtt_data, sizeof(mqtt_data),
PSTR("%s,\"SM16716\":{\"Red\":%d,\"Green\":%d,\"Blue\":%d}"),
mqtt_data, sm16716_color[0], sm16716_color[1], sm16716_color[2]);
}
/*********************************************************************************************/
boolean SM16716_Show_State(void)
{
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"" D_CMND_COLOR "\":\"%02x%02x%02x\"}"),
sm61716_colour[0], sm61716_colour[1], sm61716_colour[2]);
snprintf_P(mqtt_data, sizeof(mqtt_data),
PSTR("{\"" D_CMND_COLOR "\":\"%02x%02x%02x\"}"),
sm16716_color[0], sm16716_color[1], sm16716_color[2]);
return true;
}
/*********************************************************************************************/
void SM16716_Update()
{
uint8_t eff_red, eff_green, eff_blue;
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Update; pwr=%02x, rgb=%02x%02x%02x"),
sm16716_is_on, sm16716_color[0], sm16716_color[1], sm16716_color[2]);
AddLog(LOG_LEVEL_DEBUG);
#endif // D_LOG_SM16716
if (Settings.power) { // any bit is ok for us
eff_red = (uint16_t)sm61716_colour[0] * Settings.light_dimmer / 100;
eff_green = (uint16_t)sm61716_colour[1] * Settings.light_dimmer / 100;
eff_blue = (uint16_t)sm61716_colour[2] * Settings.light_dimmer / 100;
// send start bit
SM16716_SendBit(1);
// send 24-bit rgb data
if (sm16716_is_on) {
SM16716_SendByte(sm16716_color[0]);
SM16716_SendByte(sm16716_color[1]);
SM16716_SendByte(sm16716_color[2]);
}
else {
eff_red = eff_green = eff_blue = 0;
}
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Update; pwr=%02x, rgb=%02x%02x%02x, dimmer=%d, eff=%02x%02x%02x"),
Settings.power, sm61716_colour[0], sm61716_colour[1], sm61716_colour[2], Settings.light_dimmer, eff_red, eff_green, eff_blue);
AddLog(LOG_LEVEL_DEBUG);
if ((eff_red != sm61716_eff_red) || (eff_green != sm61716_eff_green) || (eff_blue != sm61716_eff_blue)) {
sm61716_eff_red = eff_red;
sm61716_eff_green = eff_green;
sm61716_eff_blue = eff_blue;
SM16716_SendBit(1);
SM16716_SendByte(eff_red);
SM16716_SendByte(eff_green);
SM16716_SendByte(eff_blue);
SM16716_SendByte(0);
SM16716_SendByte(0);
SM16716_SendByte(0);
}
// send a 'do it' pulse
// (if multiple chips are chained, each one processes the 1st '1rgb' 25-bit block and
// passes on the rest, right until the one starting with 0)
SM16716_SendBit(0);
SM16716_SendByte(0);
SM16716_SendByte(0);
SM16716_SendByte(0);
SM16716_Show_State();
}
/*********************************************************************************************/
boolean SM16716_ModuleSelected(void)
{
sm61716_pin_clk = pin[GPIO_SM16716_CLK];
sm61716_pin_dat = pin[GPIO_SM16716_DAT];
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "ModuleSelected; clk_pin=%d, dat_pin=%d)"), sm61716_pin_clk, sm61716_pin_dat);
sm16716_pin_clk = pin[GPIO_SM16716_CLK];
sm16716_pin_dat = pin[GPIO_SM16716_DAT];
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "ModuleSelected; clk_pin=%d, dat_pin=%d)"),
sm16716_pin_clk, sm16716_pin_dat);
AddLog(LOG_LEVEL_DEBUG);
return (sm61716_pin_clk < 99) && (sm61716_pin_dat < 99);
#endif // D_LOG_SM16716
sm16716_is_selected = (sm16716_pin_clk < 99) && (sm16716_pin_dat < 99);
return sm16716_is_selected;
}
/*********************************************************************************************/
boolean SM16716_Init(void)
{
uint8_t t_init;
if (!SM16716_ModuleSelected())
return false;
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Init;"));
pinMode(sm16716_pin_clk, OUTPUT);
digitalWrite(sm16716_pin_clk, LOW);
pinMode(sm61716_pin_clk, OUTPUT);
digitalWrite(sm61716_pin_clk, LOW);
pinMode(sm16716_pin_dat, OUTPUT);
digitalWrite(sm16716_pin_dat, LOW);
pinMode(sm61716_pin_dat, OUTPUT);
digitalWrite(sm61716_pin_dat, LOW);
AddLog(LOG_LEVEL_DEBUG);
for (t_init = 0; t_init < 50; ++t_init)
SM16716_SendBit(0);
return true;
}
/*********************************************************************************************/
boolean SM16716_Parse_RRGGBB(const char *data, int data_len) {
char component[3];
char *endptr = NULL;
uint8_t candidate[3];
int i;
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Parse_RRGGBB; data='%s', data_len=%d"), data, data_len);
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Parse_RRGGBB; data='%s', data_len=%d"),
data, data_len);
AddLog(LOG_LEVEL_DEBUG);
#endif // D_LOG_SM16716
if (data_len != 6)
return false;
@ -168,39 +191,37 @@ boolean SM16716_Parse_RRGGBB(const char *data, int data_len) {
data += 2;
}
sm61716_colour[0] = candidate[0];
sm61716_colour[1] = candidate[1];
sm61716_colour[2] = candidate[2];
sm16716_color[0] = candidate[0];
sm16716_color[1] = candidate[1];
sm16716_color[2] = candidate[2];
return true;
}
boolean SM16716_Parse_Colour(char *data, int data_len) {
/* NOTE: Very similar to 'LightColorEntry', but it's not reusable, because
* 'light_type' must be PWM, but then it won't parse the colour components.
* It's indeed more effective to not do the parsing when the current light
* type couldn't handle it, but enum LightTypes is not to be changed, so
* I can't implement new light types.
*
* If the parsing/handling of light attributes were separated from the
* technical details of actually sending it to the device, then only the
* device-specific parts should've been adapted, but unfortunately this is
* not the case here, so, here we go */
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Parse_Colour; data='%s', data_len=%d"), data, data_len);
/*********************************************************************************************/
boolean SM16716_Parse_Color(char *data, int data_len) {
/* NOTE: Very similar to 'LightColorEntry', but can't reuse it here, because
* 'light_type' must be PWM, and then it won't parse the color components. */
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Parse_Color; data='%s', data_len=%d"),
data, data_len);
AddLog(LOG_LEVEL_DEBUG);
#endif // D_LOG_SM16716
if (data_len < 3) { // Colour preset
if (data_len < 3) { // a color preset
switch (data[0]) {
case '+':
++sm61716_colour_preset;
if (sm61716_colour_preset >= MAX_FIXED_COLOR)
sm61716_colour_preset = 0;
++sm16716_color_preset;
if (sm16716_color_preset >= MAX_FIXED_COLOR)
sm16716_color_preset = 0;
break;
case '-':
--sm61716_colour_preset;
if (sm61716_colour_preset < 0)
sm61716_colour_preset = MAX_FIXED_COLOR - 1;
--sm16716_color_preset;
if (sm16716_color_preset < 0)
sm16716_color_preset = MAX_FIXED_COLOR - 1;
break;
default:
@ -209,13 +230,17 @@ boolean SM16716_Parse_Colour(char *data, int data_len) {
uint8_t candidate = (uint8_t)strtoul(data, &endptr, 10);
if (!endptr || *endptr || (candidate < 0) || (MAX_FIXED_COLOR <= candidate))
return false;
sm61716_colour_preset = candidate;
sm16716_color_preset = candidate;
}
break;
}
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Parse_Colour; preset=%d"), sm61716_colour_preset);
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Parse_Color; preset=%d"),
sm16716_color_preset);
AddLog(LOG_LEVEL_DEBUG);
memcpy_P(sm61716_colour, &kFixedColor[sm61716_colour_preset], 3);
#endif // D_LOG_SM16716
memcpy_P(sm16716_color, &kFixedColor[sm16716_color_preset], 3);
}
else if (data[0] == '#') { // #RRGGBB
if (!SM16716_Parse_RRGGBB(data + 1, data_len - 1))
@ -240,36 +265,32 @@ boolean SM16716_Parse_Colour(char *data, int data_len) {
tok = strtok_r(NULL, ",", &last);
if (tok)
return false; // junk at the end
sm61716_colour[0] = candidate[0];
sm61716_colour[1] = candidate[1];
sm61716_colour[2] = candidate[2];
sm16716_color[0] = candidate[0];
sm16716_color[1] = candidate[1];
sm16716_color[2] = candidate[2];
}
return true;
}
/*********************************************************************************************/
bool SM16716_Command(void)
{
char command [CMDSZ];
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, k_SM16716_Commands);
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Command; topic='%s', data_len=%d, data='%s', code=%d"),
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Command; topic='%s', data_len=%d, data='%s', code=%d"),
XdrvMailbox.topic, XdrvMailbox.data_len, XdrvMailbox.data, command_code);
AddLog(LOG_LEVEL_DEBUG);
#endif // D_LOG_SM16716
switch (command_code) {
case CMND_SM16716_POWER:
SM16716_Update();
return false; // Don't catch the event, only handle the change
case CMND_SM16716_DIMMER:
SM16716_Update();
return false; // Don't catch the event, only handle the change
case CMND_SM16716_COLOR:
if (XdrvMailbox.data_len == 0)
return SM16716_Show_State();
if (!SM16716_Parse_Colour(XdrvMailbox.data, XdrvMailbox.data_len))
if (!SM16716_Parse_Color(XdrvMailbox.data, XdrvMailbox.data_len))
return false;
SM16716_Update();
return true;
@ -277,43 +298,54 @@ bool SM16716_Command(void)
return false; // Unknown command
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
boolean Xdrv20(byte function)
{
//snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Xdrv20; function=%d, index=%d"), function, XdrvMailbox.index);
//AddLog(LOG_LEVEL_DEBUG);
if (function == FUNC_MODULE_INIT) {
return SM16716_ModuleSelected();
}
if (!sm16716_is_selected)
return false;
switch (function) {
case FUNC_MODULE_INIT:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_MODULE_INIT"));
AddLog(LOG_LEVEL_DEBUG);
return SM16716_ModuleSelected();
case FUNC_INIT:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_INIT"));
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Entry; function=FUNC_INIT"));
AddLog(LOG_LEVEL_DEBUG);
#endif
return SM16716_Init();
case FUNC_COMMAND:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_COMMAND"));
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Entry; function=FUNC_COMMAND"));
AddLog(LOG_LEVEL_DEBUG);
return SM16716_Command();
#endif
case FUNC_SET_POWER:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_SET_POWER, index=%02x, payload=%02x"), XdrvMailbox.index, XdrvMailbox.payload);
AddLog(LOG_LEVEL_DEBUG);
return false;
//case FUNC_SET_POWER:
case FUNC_SET_DEVICE_POWER:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_SET_DEVICE_POWER, index=%02x, payload=%02x"), XdrvMailbox.index, XdrvMailbox.payload);
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Entry; function=FUNC_SET_DEVICE_POWER, index=%02x, payload=%02x"),
XdrvMailbox.index, XdrvMailbox.payload);
AddLog(LOG_LEVEL_DEBUG);
return false;
#endif
sm16716_is_on = (XdrvMailbox.index != 0);
SM16716_Update();
return false; // don't catch the event
case FUNC_JSON_APPEND:
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_SM16716 "Entry; function=FUNC_JSON_APPEND"));
#ifdef D_LOG_SM16716
snprintf_P(log_data, sizeof(log_data),
PSTR(D_LOG_SM16716 "Entry; function=FUNC_JSON_APPEND"));
AddLog(LOG_LEVEL_DEBUG);
#endif
SM16716_Append_JSON();
break;
}
@ -321,5 +353,4 @@ boolean Xdrv20(byte function)
}
#endif // USE_SM16716
// vim: set ft=c sw=2 ts=2 et: