DSEdge and DSTime commands for Door Sensor

This commit is contained in:
openshwprojects
2023-04-17 09:02:35 +02:00
parent 132fd16e1d
commit a6d89b0730
5 changed files with 55 additions and 7 deletions

View File

@@ -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() {

View File

@@ -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",

View File

@@ -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",

View File

@@ -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);
}
}

View File

@@ -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