From 6dbfc0108a331f9451e7977175fc34d27567ea25 Mon Sep 17 00:00:00 2001 From: Bruno Chabrier Date: Wed, 10 Apr 2019 21:56:32 +0200 Subject: [PATCH] Added command "gpio set high|low for " --- README.md | 1 + user/user_main.c | 65 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 81f40bf..16afe84 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ Most of the set-commands are effective only after save and reset. - set vmin_sleep _secs_: sets the time interval in seconds the ESP sleeps on low voltage - gpio [0-16] mode [_in_|_in_pullup_|_out_]: configures a GPIO port of the ESP (saved to flash) - gpio [0-16] set [_high_|_low_]: writes to an output port +- gpio [0-16] set [_high_|_low_] for _seconds_: writes to an output port and reverts after a certain duration - gpio [0-16] get: reads from an input port # Status LED diff --git a/user/user_main.c b/user/user_main.c index c4f9099..90b0f49 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -895,6 +895,33 @@ LOCAL void gpio_change_handler(void *arg) #endif /* GPIO_CMDS */ #endif +#if GPIO_CMDS +static os_timer_t duration_timer[17]; +void do_outputSet(uint8_t pin, uint8_t value, uint16_t duration); + +void ICACHE_FLASH_ATTR set_high(void *arg){ + uint16_t pin = (intptr_t)arg; + do_outputSet(pin, 1, 0); +} + +void ICACHE_FLASH_ATTR set_low(void *arg){ + uint16_t pin = (intptr_t)arg; + do_outputSet(pin, 0, 0); +} + +void do_outputSet(uint8_t pin, uint8_t value, uint16_t duration) { + os_timer_disarm(&duration_timer[pin]); + easygpio_outputSet(pin, value); +#if MQTT_CLIENT + notifyValueToMQTT(pin); +#endif + if (duration > 0) { + os_timer_setfn(&duration_timer[pin], value>0?set_low:set_high, (void *)(uint32_t)pin); + os_timer_arm(&duration_timer[pin], duration * 1000, 0); + } +} +#endif + // Use this from ROM instead int ets_str2macaddr(uint8 *mac, char *str_mac); #define parse_mac ets_str2macaddr @@ -2597,6 +2624,8 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) * gpio 4 set high * Set GPIO pin 16 to low: * gpio 16 set low + * Set GPIO pin 04 to high for 5 seconds: + * gpio 4 set high for 5 * Get GPIO pin 2 value: * gpio 2 get */ @@ -2609,7 +2638,7 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) { uint16_t pin = atoi(tokens[1]); // 0-16 uint8_t *action = tokens[2]; // mode|set|get - uint8_t *value = nTokens == 4 ? tokens[3] : ""; + uint8_t *value = nTokens >= 4 ? tokens[3] : ""; if ((pin < 0) && (pin > 16)) { @@ -2657,15 +2686,33 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) if (strcmp(action, "set")==0) { - if (strcmp(value, "high")==0) - { - easygpio_outputSet(pin, 1); - goto command_handled; + bool correct = false; + int16_t duration = -1; + if (nTokens == 4) { + duration = 0; + correct = true; + } else if (nTokens == 6 && strcmp(tokens[4], "for")==0) { + duration = atoi(tokens[5]); + if (duration <= 0) { + os_sprintf_flash(response, "Invalid duration (seconds)\r\n"); + goto command_handled; + } + correct = true; + } else { + os_sprintf_flash(response, "Syntax: gpio set low|high [for ]\r\n"); + goto command_handled; + } + if (correct) { + if (strcmp(value, "high")==0) + { + do_outputSet(pin, 1, duration); + goto command_handled; + } + if (strcmp(value, "low")==0) + { + do_outputSet(pin, 0, duration); + goto command_handled; } - if (strcmp(value, "low")==0) - { - easygpio_outputSet(pin, 0); - goto command_handled; } os_sprintf_flash(response, "Invalid value (high or low)\r\n"); }