Temp commit

This commit is contained in:
fvanroie 2021-04-21 17:52:34 +02:00
parent 19408175ca
commit af8a0e95e5
5 changed files with 103 additions and 110 deletions

View File

@ -61,7 +61,7 @@ build_flags =
-D HASP_VER_MAJ=0 -D HASP_VER_MAJ=0
-D HASP_VER_MIN=5 -D HASP_VER_MIN=5
-D HASP_VER_REV=0 -D HASP_VER_REV=0
-D HASP_LOG_LEVEL=LOG_LEVEL_TRACE -D HASP_LOG_LEVEL=LOG_LEVEL_OUTPUT
${override.build_flags} ${override.build_flags}
; -- Shared library dependencies in all environments ; -- Shared library dependencies in all environments

View File

@ -212,51 +212,30 @@ static void dispatch_gpio(const char* topic, const char* payload)
uint8_t pin; uint8_t pin;
if(topic == strstr_P(topic, PSTR("relay"))) { if(topic == strstr_P(topic, PSTR("relay"))) {
if(strlen(payload) == 0) {
// val = gpio_get_relay_value(pin);
} else {
topic += 5; topic += 5;
if(Parser::is_only_digits(topic)) {
pin = atoi(topic);
val = Parser::is_true(payload);
// gpio_set_relay_value(pin, val);
return;
}
// invalid pin
}
} else if(topic == strstr_P(topic, PSTR("led"))) { } else if(topic == strstr_P(topic, PSTR("led"))) {
if(strlen(payload) == 0) {
} else {
topic += 3; topic += 3;
if(Parser::is_only_digits(topic)) {
pin = atoi(topic);
val = Parser::is_true(payload);
// gpio_set_led_value(pin, val);
return;
}
// invalid pin
}
} else if(topic == strstr_P(topic, PSTR("pwm"))) { } else if(topic == strstr_P(topic, PSTR("pwm"))) {
if(strlen(payload) == 0) { topic += 3;
} else { } else {
topic += 3; LOG_WARNING(TAG_MSGR, F("Invalid gpio %s"), topic);
return;
}
if(Parser::is_only_digits(topic)) { if(Parser::is_only_digits(topic)) {
pin = atoi(topic); pin = atoi(topic);
if(strlen(payload) > 0) {
val = Parser::is_true(payload); val = Parser::is_true(payload);
// gpio_set_pwm_value(pin, val); gpio_set_value(pin, val);
return;
}
// invalid pin
}
} else { } else {
LOG_WARNING(TAG_MSGR, F("Invalid gpio type %s"), topic); gpio_get_value(pin);
return; }
} else {
LOG_WARNING(TAG_MSGR, F("Invalid pin %s"), topic);
} }
LOG_WARNING(TAG_MSGR, F("Invalid pin number %s"), topic);
} }
// objectattribute=value // objectattribute=value
@ -521,19 +500,6 @@ static void dispatch_config(const char* topic, const char* payload)
} }
#endif // HASP_USE_CONFIG #endif // HASP_USE_CONFIG
/********************************************** Input Events *******************************************/
#if HASP_USE_GPIO > 0
void dispatch_gpio_output_value(const char* gpiotype, uint8_t pin, int16_t val)
{
char payload[16];
char topic[16];
snprintf_P(topic, sizeof(topic), PSTR("%s%d"), gpiotype, pin);
snprintf_P(payload, sizeof(payload), PSTR("%d"), val);
dispatch_state_subtopic(topic, payload);
}
#endif
/********************************************** Output States ******************************************/ /********************************************** Output States ******************************************/
/* /*
static inline void dispatch_state_msg(const __FlashStringHelper* subtopic, const char* payload) static inline void dispatch_state_msg(const __FlashStringHelper* subtopic, const char* payload)
@ -595,7 +561,7 @@ void dispatch_screenshot(const char*, const char* filename)
} }
#else #else
LOG_WARNING(TAG_MSGR, "Failed to save %s, no storage", filename); LOG_WARNING(TAG_MSGR, D_FILE_SAVE_FAILED, filename);
#endif #endif
} }

View File

@ -64,7 +64,6 @@ void dispatch_current_state();
void dispatch_output_current_page(); void dispatch_output_current_page();
void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid); void dispatch_gpio_input_event(uint8_t pin, uint8_t group, uint8_t eventid);
void dispatch_gpio_output_value(const char* gpiotype, uint8_t pin, int16_t val);
void dispatch_object_value_changed(lv_obj_t* obj, int16_t state); void dispatch_object_value_changed(lv_obj_t* obj, int16_t state);

View File

@ -90,6 +90,8 @@ static void gpio_event_handler(AceButton* button, uint8_t eventType, uint8_t but
dispatch_normalized_group_value(gpioConfig[btnid].group, NULL, state, HASP_EVENT_OFF, HASP_EVENT_ON); dispatch_normalized_group_value(gpioConfig[btnid].group, NULL, state, HASP_EVENT_OFF, HASP_EVENT_ON);
} }
/* ********************************* GPIO Setup *************************************** */
void aceButtonSetup(void) void aceButtonSetup(void)
{ {
ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig(); ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig();
@ -276,6 +278,80 @@ void gpioSetup()
} }
/* ********************************* State Setters *************************************** */ /* ********************************* State Setters *************************************** */
void gpio_get_value(hasp_gpio_config_t gpio)
{
char payload[32];
char topic[12];
snprintf_P(topic, sizeof(topic), PSTR("gpio%dp%d"), gpio.type, gpio.pin);
snprintf_P(payload, sizeof(payload), PSTR("%d"), gpio.val);
dispatch_state_subtopic(topic, payload);
}
void gpio_get_value(uint8_t pin)
{
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
if(gpioConfig[i].pin == pin) return gpio_get_value(gpioConfig[i]);
}
LOG_WARNING(TAG_GPIO, F(D_BULLET "Pin %d is not configured"), pin);
}
void gpio_set_value(hasp_gpio_config_t gpio, int16_t val)
{
bool inverted = false;
switch(gpio.type) {
case HASP_GPIO_RELAY_INVERTED:
inverted = true;
case HASP_GPIO_RELAY:
gpio.val = val > 0 ? HIGH : LOW;
digitalWrite(gpio.pin, inverted ? !gpio.val : gpio.val);
break;
case HASP_GPIO_LED_INVERTED:
case HASP_GPIO_LED_R_INVERTED:
case HASP_GPIO_LED_G_INVERTED:
case HASP_GPIO_LED_B_INVERTED:
inverted = true;
case HASP_GPIO_LED:
case HASP_GPIO_LED_R:
case HASP_GPIO_LED_G:
case HASP_GPIO_LED_B:
gpio.val = val >= 255 ? 255 : val > 0 ? val : 0;
#if defined(ARDUINO_ARCH_ESP32)
ledcWrite(gpio.group, gpio.val); // ledChannel and value
#else
analogWrite(gpio.pin, gpio.val); // 1023
#endif
break;
case HASP_GPIO_PWM_INVERTED:
inverted = true;
case HASP_GPIO_PWM:
gpio.val = val >= 4095 ? 4095 : val > 0 ? val : 0;
#if defined(ARDUINO_ARCH_ESP32)
ledcWrite(gpio.group, inverted ? 4095 - gpio.val : gpio.val); // ledChannel and value
#else
analogWrite(gpio.pin, (inverted ? 4095 - gpio.val : gpio.val) >> 2); // 1023
#endif
break;
default:
return;
}
gpio_get_value(gpio);
LOG_VERBOSE(TAG_GPIO, F(D_BULLET "Group %d - Pin %d = %d"), gpio.group, gpio.pin, gpio.val);
}
void gpio_set_value(uint8_t pin, int16_t val)
{
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
if(gpioConfig[i].pin == pin) return gpio_set_value(gpioConfig[i], val);
}
LOG_WARNING(TAG_GPIO, F(D_BULLET "Pin %d is not configured"), pin);
}
void gpio_set_normalized_value(hasp_gpio_config_t gpio, int16_t val, int16_t min, int16_t max) void gpio_set_normalized_value(hasp_gpio_config_t gpio, int16_t val, int16_t min, int16_t max)
{ {
if(min == max) { if(min == max) {
@ -283,73 +359,35 @@ void gpio_set_normalized_value(hasp_gpio_config_t gpio, int16_t val, int16_t min
return; return;
} }
int16_t newval;
switch(gpio.type) { switch(gpio.type) {
case HASP_GPIO_RELAY: case HASP_GPIO_RELAY:
gpio.val = val > min ? HIGH : LOW;
digitalWrite(gpio.pin, gpio.val);
dispatch_gpio_output_value("relay", gpio.pin, gpio.val);
break;
case HASP_GPIO_RELAY_INVERTED: case HASP_GPIO_RELAY_INVERTED:
gpio.val = val > min ? LOW : HIGH; newval = val > min ? HIGH : LOW;
digitalWrite(gpio.pin, gpio.val);
dispatch_gpio_output_value("relay", gpio.pin, gpio.val);
break; break;
case HASP_GPIO_LED: case HASP_GPIO_LED:
case HASP_GPIO_LED_R: case HASP_GPIO_LED_R:
case HASP_GPIO_LED_G: case HASP_GPIO_LED_G:
case HASP_GPIO_LED_B: case HASP_GPIO_LED_B:
#if defined(ARDUINO_ARCH_ESP32)
gpio.val = map(val, min, max, 0, 4095);
ledcWrite(gpio.group, gpio.val); // ledChannel and value
#else
gpio.val = map(val, min, max, 0, 1023);
analogWrite(gpio.pin, gpio.val);
#endif
dispatch_gpio_output_value("led", gpio.pin, gpio.val);
break;
case HASP_GPIO_LED_INVERTED: case HASP_GPIO_LED_INVERTED:
case HASP_GPIO_LED_R_INVERTED: case HASP_GPIO_LED_R_INVERTED:
case HASP_GPIO_LED_G_INVERTED: case HASP_GPIO_LED_G_INVERTED:
case HASP_GPIO_LED_B_INVERTED: case HASP_GPIO_LED_B_INVERTED:
case HASP_GPIO_PWM_INVERTED: newval = map(val, min, max, 0, 255);
#if defined(ARDUINO_ARCH_ESP32)
gpio.val = map(val, min, max, 0, 4095);
ledcWrite(gpio.group, gpio.val); // ledChannel and value
#else
gpio.val = map(val, min, max, 0, 1023);
analogWrite(gpio.pin, gpio.val);
#endif
dispatch_gpio_output_value("led", gpio.pin, gpio.val);
break; break;
case HASP_GPIO_PWM: case HASP_GPIO_PWM:
#if defined(ARDUINO_ARCH_ESP32) case HASP_GPIO_PWM_INVERTED:
gpio.val = map(val, min, max, 0, 4095); newval = map(val, min, max, 0, 4095);
ledcWrite(gpio.group, gpio.val); // ledChannel and value
#else
gpio.val = map(val, min, max, 0, 1023);
analogWrite(gpio.pin, gpio.val);
#endif
dispatch_gpio_output_value("pwm", gpio.pin, gpio.val);
break; break;
default: default:
return; return;
} }
LOG_VERBOSE(TAG_GPIO, F(D_BULLET "Group %d - Pin %d = %d"), gpio.group, gpio.pin, gpio.val);
}
// void gpio_set_group_onoff(uint8_t groupid, bool ison) gpio_set_value(gpio, newval);
// { }
// for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
// if(gpioConfig[i].group == groupid) {
// gpio_set_value(gpioConfig[i], ison ? gpioConfig[i].max : 0);
// }
// }
// }
void gpio_set_normalized_group_value(uint8_t groupid, int16_t val, int16_t min, int16_t max) void gpio_set_normalized_group_value(uint8_t groupid, int16_t val, int16_t min, int16_t max)
{ {
@ -389,18 +427,6 @@ void gpio_set_moodlight(uint8_t r, uint8_t g, uint8_t b)
} }
} }
// not used
// void gpio_set_gpio_value(uint8_t pin, uint16_t state)
// {
// // bool state = Parser::get_event_state(eventid);
// for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
// if(gpioConfig[i].pin == pin) {
// gpio_set_value(gpioConfig[i], state);
// return;
// }
// }
// }
bool gpioIsSystemPin(uint8_t gpio) bool gpioIsSystemPin(uint8_t gpio)
{ {
if((gpio >= NUM_DIGITAL_PINS) // invalid pins if((gpio >= NUM_DIGITAL_PINS) // invalid pins

View File

@ -26,6 +26,8 @@ void gpioEvery5Seconds(void);
// void gpio_set_group_onoff(uint8_t groupid, bool ison); // void gpio_set_group_onoff(uint8_t groupid, bool ison);
void gpio_set_normalized_group_value(uint8_t groupid, int16_t val, int16_t min, int16_t max); void gpio_set_normalized_group_value(uint8_t groupid, int16_t val, int16_t min, int16_t max);
// void gpio_set_gpio_state(uint8_t pin, uint16_t state); // void gpio_set_gpio_state(uint8_t pin, uint16_t state);
void gpio_get_value(uint8_t pin);
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);
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);