From 68da6a07bd3df7cfde84f4e2e658f9ff91c3f08d Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Thu, 22 Apr 2021 01:45:37 +0200 Subject: [PATCH] Add Serial Dimmer --- src/hasp/hasp_dispatch.cpp | 4 +++- src/sys/gpio/hasp_gpio.cpp | 34 +++++++++++++++++++++++++++++++++- src/sys/gpio/hasp_gpio.h | 1 + src/sys/svc/hasp_http.cpp | 6 ++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 4baddc32..71d33185 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -213,12 +213,15 @@ static void dispatch_gpio(const char* topic, const char* payload) if(topic == strstr_P(topic, PSTR("relay"))) { topic += 5; + val = Parser::is_true(payload); } else if(topic == strstr_P(topic, PSTR("led"))) { topic += 3; + val = atoi(payload); } else if(topic == strstr_P(topic, PSTR("pwm"))) { topic += 3; + val = atoi(payload); } else { LOG_WARNING(TAG_MSGR, F("Invalid gpio %s"), topic); @@ -228,7 +231,6 @@ static void dispatch_gpio(const char* topic, const char* payload) if(Parser::is_only_digits(topic)) { pin = atoi(topic); if(strlen(payload) > 0) { - val = Parser::is_true(payload); gpio_set_value(pin, val); } else { gpio_get_value(pin); diff --git a/src/sys/gpio/hasp_gpio.cpp b/src/sys/gpio/hasp_gpio.cpp index 8e3ed265..178475d2 100644 --- a/src/sys/gpio/hasp_gpio.cpp +++ b/src/sys/gpio/hasp_gpio.cpp @@ -24,6 +24,8 @@ hasp_gpio_config_t gpioConfig[HASP_NUM_GPIO_CONFIG] = { }; #if defined(ARDUINO_ARCH_ESP32) +#include "driver/uart.h" + class TouchConfig : public ButtonConfig { public: TouchConfig(); @@ -271,6 +273,20 @@ void gpioSetup() ledcSetup(gpioConfig[i].group, 20000, 12); // attach the channel to the GPIO to be controlled ledcAttachPin(gpioConfig[i].pin, gpioConfig[i].group); +#endif + break; + + case HASP_GPIO_SERIAL_DIMMER: +#if defined(ARDUINO_ARCH_ESP32) + Serial2.begin(115200, SERIAL_8N1, UART_PIN_NO_CHANGE, gpioConfig[i].pin); + delay(20); + const char command[5] = "\xEF\x01\x4D\xA3"; // Start Lanbon Dimmer + Serial2.print(command); + + char buffer[32]; + snprintf_P(buffer, sizeof(buffer), PSTR("Dimmer: %02x %02x %02x %02x"), command[0], command[1], + command[2], command[3]); + LOG_VERBOSE(TAG_GPIO, buffer); #endif break; } @@ -306,7 +322,7 @@ void gpio_set_value(hasp_gpio_config_t gpio, int16_t val) inverted = true; case HASP_GPIO_RELAY: gpio.val = val > 0 ? HIGH : LOW; - digitalWrite(gpio.pin, inverted ? !gpio.val : gpio.val); + digitalWrite(gpio.pin, inverted ? gpio.val : !gpio.val); break; case HASP_GPIO_LED_INVERTED: @@ -337,6 +353,22 @@ void gpio_set_value(hasp_gpio_config_t gpio, int16_t val) #endif break; + case HASP_GPIO_SERIAL_DIMMER: { +#if defined(ARDUINO_ARCH_ESP32) + char command[5] = "\xEF\x02\x00\xED"; + if(gpio.val == 0) { + command[2] = 0x20; + } else { + command[2] = (uint8_t)gpio.val; + command[3] ^= command[2]; + } + Serial2.print(command); + LOG_VERBOSE(TAG_GPIO, F("%02x %02x %02x %02x"), command[0], command[1], command[2], command[3]); + +#endif + break; + } + default: return; } diff --git a/src/sys/gpio/hasp_gpio.h b/src/sys/gpio/hasp_gpio.h index 619345a2..4e114da4 100644 --- a/src/sys/gpio/hasp_gpio.h +++ b/src/sys/gpio/hasp_gpio.h @@ -82,6 +82,7 @@ bool gpioSetConfig(const JsonObject& settings); #define HASP_GPIO_DAC_INVERTED 0x51 #define HASP_GPIO_ADC 0x52 #define HASP_GPIO_ADC_INVERTED 0x53 +#define HASP_GPIO_SERIAL_DIMMER 0x60 #define HASP_GPIO_USER 0xFF #ifdef __cplusplus diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 7a74d919..3a861317 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -1453,6 +1453,9 @@ void webHandleGpioConfig() // case HASP_GPIO_PWM_INVERTED: httpMessage += F("PWM"); break; + case HASP_GPIO_SERIAL_DIMMER: + httpMessage += F("Serial Dimmer"); + break; default: httpMessage += F("Unknown"); } @@ -1574,6 +1577,9 @@ void webHandleGpioOptions() selected = (conf.type == HASP_GPIO_RELAY) || (conf.type == HASP_GPIO_RELAY_INVERTED); httpMessage += getOption(HASP_GPIO_RELAY, F("Relay"), selected); + selected = (conf.type == HASP_GPIO_SERIAL_DIMMER); + httpMessage += getOption(HASP_GPIO_SERIAL_DIMMER, F("Serial Dimmer"), selected); + if(digitalPinHasPWM(webServer.arg(0).toInt())) { selected = (conf.type == HASP_GPIO_PWM) || (conf.type == HASP_GPIO_PWM_INVERTED); httpMessage += getOption(HASP_GPIO_PWM, F("PWM"), selected);