From a6d89b0730effacb367ff956cd283eec8f2e2d24 Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Mon, 17 Apr 2023 09:02:35 +0200 Subject: [PATCH] DSEdge and DSTime commands for Door Sensor --- src/driver/drv_doorSensorWithDeepSleep.c | 42 ++++++++++++++++++++++++ src/driver/drv_main.c | 2 +- src/httpserver/http_fns.c | 2 +- src/new_pins.c | 15 ++++++--- src/new_pins.h | 1 + 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/driver/drv_doorSensorWithDeepSleep.c b/src/driver/drv_doorSensorWithDeepSleep.c index 4356084a1..da196a651 100644 --- a/src/driver/drv_doorSensorWithDeepSleep.c +++ b/src/driver/drv_doorSensorWithDeepSleep.c @@ -23,9 +23,51 @@ static int g_emergencyTimeWithNoConnection = 0; #define EMERGENCY_TIME_TO_SLEEP_WITHOUT_MQTT 60 * 5 + +commandResult_t DoorDeepSleep_SetEdge(const void* context, const char* cmd, const char* args, int cmdFlags) { + + Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_DONT_EXPAND); + // following check must be done after 'Tokenizer_TokenizeString', + // so we know arguments count in Tokenizer. 'cmd' argument is + // only for warning display + if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) + { + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + + setting_timeRequiredUntilDeepSleep = Tokenizer_GetArgInteger(0); + + return CMD_RES_OK; +} +commandResult_t DoorDeepSleep_SetTime(const void* context, const char* cmd, const char* args, int cmdFlags) { + + Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_DONT_EXPAND); + // following check must be done after 'Tokenizer_TokenizeString', + // so we know arguments count in Tokenizer. 'cmd' argument is + // only for warning display + if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) + { + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + + g_defaultDoorWakeEdge = Tokenizer_GetArgInteger(0); + + return CMD_RES_OK; +} void DoorDeepSleep_Init() { // 0 seconds since last change g_noChangeTimePassed = 0; + + //cmddetail:{"name":"DSEdge","args":"[edgeCode]", + //cmddetail:"descr":"0 means always wake up on rising edge, 1 means on falling, 2 means if state is high, use falling edge, if low, use rising. Default is 2", + //cmddetail:"fn":"DoorDeepSleep_SetEdge","file":"drv/drv_doorSensorWithDeepSleep.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("DSEdge", DoorDeepSleep_SetEdge, NULL); + //cmddetail:{"name":"DSTime","args":"[timeSeconds]", + //cmddetail:"descr":"Time to keep device running before next sleep after last door sensor change. In future we may add also an option to automatically sleep after MQTT confirms door state receival", + //cmddetail:"fn":"DoorDeepSleep_SetTime","file":"drv/drv_doorSensorWithDeepSleep.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("DSTime", DoorDeepSleep_SetTime, NULL); } void DoorDeepSleep_OnEverySecond() { diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 63bbb093e..6a09abab1 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -179,7 +179,7 @@ static driver_t g_drivers[] = { { "PWMToggler", DRV_InitPWMToggler, NULL, DRV_Toggler_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, //drvdetail:{"name":"DoorSensor", //drvdetail:"title":"TODO", - //drvdetail:"descr":"DoorSensor is using deep sleep to preserve battery. This is used for devices without TuyaMCU, where BK deep sleep and wakeup on GPIO is used. This drives requires you to set a DoorSensor pin. Change on door sensor pin wakes up the device. If there are no changes for some time, device goes to sleep. See example [here](https://www.elektroda.com/rtvforum/topic3960149.html).", + //drvdetail:"descr":"DoorSensor is using deep sleep to preserve battery. This is used for devices without TuyaMCU, where BK deep sleep and wakeup on GPIO is used. This drives requires you to set a DoorSensor pin. Change on door sensor pin wakes up the device. If there are no changes for some time, device goes to sleep. See example [here](https://www.elektroda.com/rtvforum/topic3960149.html). If your door sensor does not wake up in certain pos, please use DSEdge command (try all 3 options, default is 2). ", //drvdetail:"requires":""} { "DoorSensor", DoorDeepSleep_Init, DoorDeepSleep_OnEverySecond, DoorDeepSleep_AppendInformationToHTTPIndexPage, NULL, NULL, DoorDeepSleep_OnChannelChanged, false }, //drvdetail:{"name":"MAX72XX_Clock", diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 7566cbfab..6de585017 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2188,7 +2188,7 @@ const char* g_obk_flagNames[] = { "[DRV] Deactivate Autostart of all drivers", "[WiFi] Quick connect to WiFi on reboot (TODO: check if it works for you and report on github)", "[Power] Set power and current to zero if all relays are open", - "[DoorSensor] Alternate wake up mode (always on falling edge)", + "TODO - UNUSED", "error", "error", "error", diff --git a/src/new_pins.c b/src/new_pins.c index cd8177b80..c608d023c 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -33,7 +33,7 @@ int BTN_SHORT_MS; int BTN_LONG_MS; int BTN_HOLD_REPEAT_MS; - +byte g_defaultDoorWakeEdge = 2; typedef enum { BTN_PRESS_DOWN = 0, @@ -136,10 +136,12 @@ void PINS_BeginDeepSleepWithPinWakeUp() { || g_cfg.pins.roles[i] == IOR_DigitalInput_NoPup || g_cfg.pins.roles[i] == IOR_DigitalInput_NoPup_n) { //value = CHANNEL_Get(g_cfg.pins.channels[i]); - if (CFG_HasFlag(OBK_FLAG_DOORSENSOR_ALWAYSWAKEONFALLING)) { - falling = 1; - } - else { + + // added per request + // https://www.elektroda.pl/rtvforum/viewtopic.php?p=20543190#20543190 + // forcing a certain edge for both states helps on some door sensors, somehow + // 0 means always wake up on rising edge, 1 means on falling, 2 means if state is high, use falling edge, if low, use rising + if (g_defaultDoorWakeEdge == 2) { value = HAL_PIN_ReadDigitalInput(i); if (value) { // on falling edge wake up @@ -150,6 +152,9 @@ void PINS_BeginDeepSleepWithPinWakeUp() { falling = 0; } } + else { + falling = g_defaultDoorWakeEdge; + } setGPIActive(i, 1, falling); } } diff --git a/src/new_pins.h b/src/new_pins.h index 1cd31e3ad..98cbde0e2 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -1021,6 +1021,7 @@ typedef struct mainConfig_s { extern mainConfig_t g_cfg; extern char g_enable_pins; +extern byte g_defaultDoorWakeEdge; #define CHANNEL_SET_FLAG_FORCE 1 #define CHANNEL_SET_FLAG_SKIP_MQTT 2