From eb4c25e8178967f0af95c08553a19262e6e1cdef Mon Sep 17 00:00:00 2001 From: descipher <120155735+GelidusResearch@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:36:04 -0500 Subject: [PATCH] Add TTC Control --- gdo.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++---- gdo_priv.h | 5 +++-- gdo_utils.c | 4 ++-- include/gdo.h | 17 +++++++++++++--- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/gdo.c b/gdo.c index 5b3522a..0fa3c0e 100755 --- a/gdo.c +++ b/gdo.c @@ -80,6 +80,7 @@ static gdo_status_t g_status = { GDO_PAIRED_DEVICE_COUNT_UNKNOWN, GDO_PAIRED_DEVICE_COUNT_UNKNOWN}, .synced = false, + .ttc_enabled = false, .openings = 0, .ttc_seconds = 0, .open_ms = 0, @@ -102,6 +103,7 @@ static esp_timer_handle_t door_position_sync_timer; static esp_timer_handle_t obst_timer; static void *g_user_cb_arg; static uint32_t g_tx_delay_ms = 50; +static uint32_t g_ttc_delay_s = 0; static portMUX_TYPE gdo_spinlock = portMUX_INITIALIZER_UNLOCKED; @@ -526,6 +528,26 @@ esp_err_t gdo_light_off(void) { return err; } +/** + * @brief Sets the time to close. + * @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails. +*/ +esp_err_t gdo_set_ttc(uint16_t seconds) { + esp_err_t err = ESP_OK; + + if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) { + } else { + //queue_command(gdo_command_t command, uint8_t nibble, uint8_t byte1, uint8_t byte2) + err = queue_command(GDO_CMD_SET_TTC, seconds, 0, 0); + if (err == ESP_OK) { + get_status(); + } + } + + return err; +} + + /** * @brief Toggles the light. * @return ESP_OK on success, ESP_ERR_NOT_FOUND if current state is unknown, @@ -1324,8 +1346,12 @@ static void decode_packet(uint8_t *packet) { update_motion_state(GDO_MOTION_STATE_DETECTED); } else if (cmd == GDO_CMD_OPENINGS) { update_openings(nibble, ((byte1 << 8) | byte2)); + } else if (cmd == GDO_CMD_UPDATE_TTC) { + update_ttc((byte1 << 8) | byte2); + queue_event((gdo_event_t){GDO_EVENT_UPDATE_TTC}); } else if (cmd == GDO_CMD_SET_TTC) { update_ttc((byte1 << 8) | byte2); + queue_event((gdo_event_t){GDO_EVENT_SET_TTC}); } else if (cmd == GDO_CMD_PAIRED_DEVICES) { update_paired_devices(nibble, byte2); } else if (cmd == GDO_CMD_BATTERY_STATUS) { @@ -1578,8 +1604,13 @@ static void gdo_main_task(void* arg) { case GDO_EVENT_MOTION_UPDATE: cb_event = GDO_CB_EVENT_MOTION; break; - case GDO_EVENT_TTC_UPDATE: - cb_event = GDO_CB_EVENT_TTC; + case GDO_EVENT_UPDATE_TTC: + cb_event = GDO_CB_EVENT_UPDATE_TTC; + ESP_LOGI(TAG, "GDO_CB_EVENT_UPDATE_TTC"); + break; + case GDO_EVENT_SET_TTC: + cb_event = GDO_CB_EVENT_SET_TTC; + ESP_LOGI(TAG, "GDO_CB_EVENT_SET_TTC"); break; case GDO_EVENT_PAIRED_DEVICES_UPDATE: cb_event = GDO_CB_EVENT_PAIRED_DEVICES; @@ -1896,13 +1927,29 @@ inline static void update_openings(uint8_t flag, uint16_t count) { * @param ttc The new TTC to update to. */ inline static void update_ttc(uint16_t ttc) { - ESP_LOGD(TAG, "TTC: %u", ttc); + ESP_LOGI(TAG, "TTC Seconds remaining: %u", ttc); if (g_status.ttc_seconds != ttc) { g_status.ttc_seconds = ttc; - queue_event((gdo_event_t){GDO_EVENT_TTC_UPDATE}); } } +/** + * @brief Set the time to close + * @param time_to_close The new time to close set by the UI +*/ +esp_err_t gdo_set_time_to_close(uint16_t time_to_close) { + g_ttc_delay_s = time_to_close; + g_status.ttc_enabled = (time_to_close > 0) ? 1 : 0; + esp_err_t err = ESP_OK; + uint8_t byte1 = (time_to_close >> 8); + uint8_t byte2 = (uint8_t)time_to_close; + uint8_t nibble = 1; + update_ttc(time_to_close); + queue_command(GDO_CMD_SET_TTC, nibble, byte1, byte2); + queue_event((gdo_event_t){GDO_EVENT_SET_TTC}); + return err; +} + /** * @brief Updates the local paired devices count and queues an event if it has changed. * @param type The type of paired devices to update. diff --git a/gdo_priv.h b/gdo_priv.h index eff7a93..e139f19 100644 --- a/gdo_priv.h +++ b/gdo_priv.h @@ -56,7 +56,7 @@ typedef enum { GDO_CMD_PAIR_2_RESP = 0x401, GDO_CMD_SET_TTC = 0x402, GDO_CMD_CANCEL_TTC = 0x408, - GDO_CMD_TTC = 0x40a, + GDO_CMD_UPDATE_TTC = 0x40a, GDO_CMD_GET_OPENINGS = 0x48b, GDO_CMD_OPENINGS = 0x48c, GDO_CMD_MAX, @@ -90,7 +90,8 @@ typedef enum { GDO_EVENT_LEARN_UPDATE, GDO_EVENT_OPENINGS_UPDATE, GDO_EVENT_MOTION_UPDATE, - GDO_EVENT_TTC_UPDATE, + GDO_EVENT_UPDATE_TTC, + GDO_EVENT_SET_TTC, GDO_EVENT_PAIRED_DEVICES_UPDATE, GDO_EVENT_DOOR_OPEN_DURATION_MEASUREMENT, GDO_EVENT_DOOR_CLOSE_DURATION_MEASUREMENT, diff --git a/gdo_utils.c b/gdo_utils.c index 9935c6e..ffce5ec 100644 --- a/gdo_utils.c +++ b/gdo_utils.c @@ -161,8 +161,8 @@ const char* cmd_to_string(gdo_command_t cmd) { return "SET_TTC"; case GDO_CMD_CANCEL_TTC: return "CANCEL_TTC"; - case GDO_CMD_TTC: - return "TTC"; + case GDO_CMD_UPDATE_TTC: + return "UPDATE_TTC"; case GDO_CMD_GET_OPENINGS: return "GET_OPENINGS"; case GDO_CMD_OPENINGS: diff --git a/include/gdo.h b/include/gdo.h index 34339ee..c9e5014 100755 --- a/include/gdo.h +++ b/include/gdo.h @@ -112,7 +112,9 @@ typedef enum { GDO_CB_EVENT_LEARN, GDO_CB_EVENT_OPENINGS, GDO_CB_EVENT_MOTION, - GDO_CB_EVENT_TTC, + GDO_CB_EVENT_SET_TTC, + GDO_CB_EVENT_CANCLE_TTC, + GDO_CB_EVENT_UPDATE_TTC, GDO_CB_EVENT_PAIRED_DEVICES, GDO_CB_EVENT_OPEN_DURATION_MEASURMENT, GDO_CB_EVENT_CLOSE_DURATION_MEASURMENT, @@ -140,6 +142,7 @@ typedef struct { gdo_learn_state_t learn; // Learn state gdo_paired_device_t paired_devices; // Paired devices bool synced; // Synced state + bool ttc_enabled; //ttc active uint16_t openings; // Number of openings uint16_t ttc_seconds; // Time to close in seconds uint16_t open_ms; // Time door takes to open from fully closed in milliseconds @@ -157,8 +160,8 @@ typedef struct { gpio_num_t uart_tx_pin; // UART TX pin gpio_num_t uart_rx_pin; // UART RX pin gpio_num_t obst_in_pin; // Obstruction input pin - gpio_num_t rf_tx_pin; // UART TX pin - gpio_num_t rf_rx_pin; // UART RX pin + gpio_num_t rf_tx_pin; // RF TX pin + gpio_num_t rf_rx_pin; // RF RX pin } gdo_config_t; #define GDO_PAIRED_DEVICE_COUNT_UNKNOWN 0xff @@ -392,6 +395,14 @@ esp_err_t gdo_set_client_id(uint32_t client_id); */ esp_err_t gdo_set_protocol(gdo_protocol_type_t protocol); +/** + * @brief Sets the time to close in minutes + * @param time_to_close The time to close value. + * @return ESP_OK on success, ESP_ERR_INVALID_ARG if the time is invalid, + * ESP_ERR_INVALID_STATE if the time is out of range. +*/ +esp_err_t gdo_set_time_to_close(uint16_t time_to_close); + /** * @brief Sets the time the door takes to open from fully closed in milliseconds. * @param ms The time the door takes to open from fully closed in milliseconds.