Enable fake gpio for native app

This commit is contained in:
fvanroie 2021-04-26 00:20:11 +02:00
parent b64d889fad
commit 828b518de2
10 changed files with 90 additions and 41 deletions

View File

@ -882,19 +882,26 @@ void dispatch_send_discovery(const char*, const char*)
{ {
#if HASP_USE_MQTT > 0 #if HASP_USE_MQTT > 0
StaticJsonDocument<512> doc;
char data[512]; char data[512];
{ haspGetVersion(data, sizeof(data));
char buffer[128];
haspGetVersion(buffer, sizeof(buffer)); doc[F("node")] = haspDevice.get_hostname();
snprintf_P(data, sizeof(data), doc[F("model")] = haspDevice.get_model();
PSTR("{\"node\":\"%s\",\"manufacturer\":\"" D_MANUFACTURER doc[F("manufacturer")] = F(D_MANUFACTURER);
"\",\"model\":\"%s\",\"hwid\":\"%s\",\"version\":\"%s\",\"numPages\":%u}"), doc[F("hwid")] = haspDevice.get_hardware_id();
haspDevice.get_hostname(), haspDevice.get_model(), haspDevice.get_hardware_id(), buffer, doc[F("version")] = data;
haspPages.count()); doc[F("numPages")] = haspPages.count();
}
switch(mqtt_send_discovery(data)) { JsonArray relay = doc.createNestedArray(F("relay"));
JsonArray led = doc.createNestedArray(F("led"));
#if HASP_USE_GPIO > 0
gpio_discovery(relay, led);
#endif
size_t len = serializeJson(doc, data);
switch(mqtt_send_discovery(data, len)) {
case MQTT_ERR_OK: case MQTT_ERR_OK:
LOG_TRACE(TAG_MQTT_PUB, F("discovery => %s"), data); LOG_TRACE(TAG_MQTT_PUB, F("discovery => %s"), data);
break; break;
@ -907,7 +914,7 @@ void dispatch_send_discovery(const char*, const char*)
default: default:
LOG_ERROR(TAG_MQTT, F(D_ERROR_UNKNOWN)); LOG_ERROR(TAG_MQTT, F(D_ERROR_UNKNOWN));
} }
dispatchLastMillis = millis(); // dispatchLastMillis = millis();
#endif #endif
} }

View File

@ -132,6 +132,11 @@ void setup()
mqttStart(); mqttStart();
#endif #endif
#if HASP_USE_GPIO > 0
printf("%s %d\n", __FILE__, __LINE__);
gpioSetup();
#endif
mainLastLoopTime = millis() - 1000; // reset loop counter mainLastLoopTime = millis() - 1000; // reset loop counter
delay(250); delay(250);
printf("%s %d\n", __FILE__, __LINE__); printf("%s %d\n", __FILE__, __LINE__);
@ -146,6 +151,10 @@ void loop()
haspDevice.loop(); haspDevice.loop();
guiLoop(); guiLoop();
#if HASP_USE_GPIO > 0
gpioLoop();
#endif
/* Timer Loop */ /* Timer Loop */
if(millis() - mainLastLoopTime >= 1000) { if(millis() - mainLastLoopTime >= 1000) {
/* Runs Every Second */ /* Runs Every Second */

View File

@ -30,7 +30,7 @@ void mqttStop();
int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload); int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload);
int mqtt_send_state(const char* subtopic, const char* payload); int mqtt_send_state(const char* subtopic, const char* payload);
int mqtt_send_discovery(const char* payload); int mqtt_send_discovery(const char* payload, size_t len);
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain); int mqttPublish(const char* topic, const char* payload, size_t len, bool retain);
bool mqttIsConnected(); bool mqttIsConnected();

View File

@ -238,11 +238,11 @@ int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
return mqttPublish(tmp_topic, payload, strlen(payload), false); return mqttPublish(tmp_topic, payload, strlen(payload), false);
} }
int mqtt_send_discovery(const char* payload) int mqtt_send_discovery(const char* payload, size_t len)
{ {
char tmp_topic[20]; char tmp_topic[20];
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR(MQTT_PREFIX "/discovery")); snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR(MQTT_PREFIX "/discovery"));
return mqttPublish(tmp_topic, payload, strlen(payload), false); return mqttPublish(tmp_topic, payload, len, false);
} }
int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload) int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload)

View File

@ -144,11 +144,11 @@ int mqtt_send_state(const char* subtopic, const char* payload)
return mqttPublish(tmp_topic, payload, false); return mqttPublish(tmp_topic, payload, false);
} }
int mqtt_send_discovery(const char* payload) int mqtt_send_discovery(const char* payload, size_t len)
{ {
char tmp_topic[20]; char tmp_topic[20];
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR(MQTT_PREFIX "/discovery")); snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR(MQTT_PREFIX "/discovery"));
return mqttPublish(tmp_topic, payload, false); return mqttPublish(tmp_topic, payload, len, false);
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,7 +1,6 @@
/* MIT License - Copyright (c) 2019-2021 Francis Van Roie /* MIT License - Copyright (c) 2019-2021 Francis Van Roie
For full license information read the LICENSE file in the project folder */ For full license information read the LICENSE file in the project folder */
#include "AceButton.h"
#include "lv_conf.h" // For timing defines #include "lv_conf.h" // For timing defines
#include "hasplib.h" #include "hasplib.h"
@ -12,10 +11,19 @@
#define INPUT_PULLDOWN INPUT #define INPUT_PULLDOWN INPUT
#endif #endif
uint8_t gpioUsedInputCount = 0; #ifdef ARDUINO
#include "AceButton.h"
using namespace ace_button; using namespace ace_button;
static AceButton* button[HASP_NUM_INPUTS]; static AceButton* button[HASP_NUM_INPUTS];
#else
#define HIGH 1
#define LOW 0
#define NUM_DIGITAL_PINS 40
#define digitalWrite(x, y)
#define analogWrite(x, y)
#endif
uint8_t gpioUsedInputCount = 0;
// An array of button pins, led pins, and the led states. Cannot be const // An array of button pins, led pins, and the led states. Cannot be const
// because ledState is mutable. // because ledState is mutable.
@ -48,6 +56,7 @@ class TouchConfig : public ButtonConfig {
TouchConfig touchConfig(); TouchConfig touchConfig();
#endif #endif
#ifdef ARDUINO
static void gpio_event_handler(AceButton* button, uint8_t eventType, uint8_t buttonState) static void gpio_event_handler(AceButton* button, uint8_t eventType, uint8_t buttonState)
{ {
uint8_t btnid = button->getId(); uint8_t btnid = button->getId();
@ -94,14 +103,6 @@ static void gpio_event_handler(AceButton* button, uint8_t eventType, uint8_t but
/* ********************************* GPIO Setup *************************************** */ /* ********************************* GPIO Setup *************************************** */
void gpio_log_serial_dimmer(const char* 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);
}
void aceButtonSetup(void) void aceButtonSetup(void)
{ {
ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig(); ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig();
@ -122,14 +123,6 @@ void aceButtonSetup(void)
buttonConfig->setRepeatPressInterval(LV_INDEV_DEF_LONG_PRESS_REP_TIME); buttonConfig->setRepeatPressInterval(LV_INDEV_DEF_LONG_PRESS_REP_TIME);
} }
void gpioLoop(void)
{
// Should be called every 4-5ms or faster, for the default debouncing time of ~20ms.
for(uint8_t i = 0; i < gpioUsedInputCount; i++) {
if(button[i]) button[i]->check();
}
}
void gpioAddButton(uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t index) void gpioAddButton(uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t index)
{ {
uint8_t i; uint8_t i;
@ -297,6 +290,34 @@ void gpioSetup()
} }
} }
void gpioLoop(void)
{
// Should be called every 4-5ms or faster, for the default debouncing time of ~20ms.
for(uint8_t i = 0; i < gpioUsedInputCount; i++) {
if(button[i]) button[i]->check();
}
}
#else
void gpioSetup(void)
{
gpioSavePinConfig(0, 3, HASP_GPIO_RELAY, 0, -1);
gpioSavePinConfig(1, 4, HASP_GPIO_RELAY_INVERTED, 0, -1);
gpioSavePinConfig(2, 13, HASP_GPIO_LED, 0, -1);
gpioSavePinConfig(3, 14, HASP_GPIO_LED_INVERTED, 0, -1);
}
void gpioLoop(void)
{}
#endif // ARDUINO
void gpio_log_serial_dimmer(const char* 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);
}
/* ********************************* State Setters *************************************** */ /* ********************************* State Setters *************************************** */
void gpio_get_value(hasp_gpio_config_t gpio) void gpio_get_value(hasp_gpio_config_t gpio)
@ -358,8 +379,7 @@ void gpio_set_value(hasp_gpio_config_t gpio, int16_t val)
break; break;
case HASP_GPIO_SERIAL_DIMMER: { case HASP_GPIO_SERIAL_DIMMER: {
gpio.val = val >= 100 ? 100 : val > 0 ? val : 0; gpio.val = val >= 100 ? 100 : val > 0 ? val : 0;
#if defined(ARDUINO_ARCH_ESP32)
char command[5] = "\xEF\x02\x00\xED"; char command[5] = "\xEF\x02\x00\xED";
if(gpio.val == 0) { if(gpio.val == 0) {
command[2] = 0x20; command[2] = 0x20;
@ -367,10 +387,11 @@ void gpio_set_value(hasp_gpio_config_t gpio, int16_t val)
command[2] = (uint8_t)gpio.val; command[2] = (uint8_t)gpio.val;
command[3] ^= command[2]; command[3] ^= command[2];
} }
#if defined(ARDUINO_ARCH_ESP32)
Serial2.print(command); Serial2.print(command);
#endif
gpio_log_serial_dimmer(command); gpio_log_serial_dimmer(command);
#endif
break; break;
} }
@ -628,6 +649,13 @@ hasp_gpio_config_t gpioGetPinConfig(uint8_t num)
return gpioConfig[num]; return gpioConfig[num];
} }
void gpio_discovery(JsonArray& relay, JsonArray& led)
{
relay.add(5);
relay.add(12);
led.add(3);
};
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 0 #if HASP_USE_CONFIG > 0
bool gpioGetConfig(const JsonObject& settings) bool gpioGetConfig(const JsonObject& settings)

View File

@ -30,6 +30,8 @@ void gpio_get_value(uint8_t pin);
void gpio_set_value(uint8_t pin, int16_t val); void gpio_set_value(uint8_t pin, int16_t val);
void gpio_set_moodlight(uint8_t r, uint8_t g, uint8_t b); void gpio_set_moodlight(uint8_t r, uint8_t g, uint8_t b);
void gpio_discovery(JsonArray& relay, JsonArray& led);
bool gpioSavePinConfig(uint8_t config_num, uint8_t pin, uint8_t type, uint8_t group, uint8_t pinfunc); bool gpioSavePinConfig(uint8_t config_num, uint8_t pin, uint8_t type, uint8_t group, uint8_t pinfunc);
bool gpioIsSystemPin(uint8_t gpio); bool gpioIsSystemPin(uint8_t gpio);
bool gpioInUse(uint8_t gpio); bool gpioInUse(uint8_t gpio);

View File

@ -26,7 +26,7 @@ build_flags =
-D HASP_USE_SPIFFS=0 -D HASP_USE_SPIFFS=0
-D HASP_USE_LITTLEFS=0 -D HASP_USE_LITTLEFS=0
-D HASP_USE_EEPROM=0 -D HASP_USE_EEPROM=0
-D HASP_USE_GPIO=0 -D HASP_USE_GPIO=1
-D HASP_USE_CONFIG=0 ; Standalone application, as library -D HASP_USE_CONFIG=0 ; Standalone application, as library
-D HASP_USE_DEBUG=1 -D HASP_USE_DEBUG=1
-D HASP_USE_MQTT=1 -D HASP_USE_MQTT=1
@ -86,6 +86,7 @@ src_filter =
-<MQTTVersion.c> -<MQTTVersion.c>
-<SSLSocket.c> -<SSLSocket.c>
-<sys/> -<sys/>
+<sys/gpio/>
-<hal/> -<hal/>
+<drv/> +<drv/>
-<drv/touch> -<drv/touch>

View File

@ -25,7 +25,7 @@ build_flags =
-D HASP_USE_SPIFFS=0 -D HASP_USE_SPIFFS=0
-D HASP_USE_LITTLEFS=0 -D HASP_USE_LITTLEFS=0
-D HASP_USE_EEPROM=0 -D HASP_USE_EEPROM=0
-D HASP_USE_GPIO=0 -D HASP_USE_GPIO=1
-D HASP_USE_CONFIG=0 ; Standalone application, as library -D HASP_USE_CONFIG=0 ; Standalone application, as library
-D HASP_USE_DEBUG=1 -D HASP_USE_DEBUG=1
-D HASP_USE_MQTT=1 -D HASP_USE_MQTT=1
@ -81,6 +81,7 @@ src_filter =
-<MQTTVersion.c> -<MQTTVersion.c>
-<SSLSocket.c> -<SSLSocket.c>
-<sys/> -<sys/>
+<sys/gpio/>
-<hal/> -<hal/>
+<drv/> +<drv/>
-<drv/touch> -<drv/touch>

View File

@ -25,7 +25,7 @@ build_flags =
-D HASP_USE_SPIFFS=0 -D HASP_USE_SPIFFS=0
-D HASP_USE_LITTLEFS=0 -D HASP_USE_LITTLEFS=0
-D HASP_USE_EEPROM=0 -D HASP_USE_EEPROM=0
-D HASP_USE_GPIO=0 -D HASP_USE_GPIO=1
-D HASP_USE_CONFIG=0 ; Standalone application, as library -D HASP_USE_CONFIG=0 ; Standalone application, as library
-D HASP_USE_DEBUG=1 -D HASP_USE_DEBUG=1
-D HASP_USE_MQTT=1 -D HASP_USE_MQTT=1
@ -102,6 +102,7 @@ src_filter =
-<MQTTVersion.c> -<MQTTVersion.c>
-<SSLSocket.c> -<SSLSocket.c>
-<sys/> -<sys/>
+<sys/gpio/>
-<hal/> -<hal/>
+<drv/> +<drv/>
-<drv/touch> -<drv/touch>