Skip to content

Commit 69f5c9e

Browse files
authored
Merge pull request #3 from descipher/secplus.timing
Add TTC Control
2 parents 98a9a02 + cafca55 commit 69f5c9e

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

gdo.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ static gdo_status_t g_status = {
8080
GDO_PAIRED_DEVICE_COUNT_UNKNOWN,
8181
GDO_PAIRED_DEVICE_COUNT_UNKNOWN},
8282
.synced = false,
83+
.ttc_enabled = false,
8384
.openings = 0,
8485
.ttc_seconds = 0,
8586
.open_ms = 0,
@@ -101,7 +102,8 @@ static esp_timer_handle_t motion_detect_timer;
101102
static esp_timer_handle_t door_position_sync_timer;
102103
static esp_timer_handle_t obst_timer;
103104
static void *g_user_cb_arg;
104-
static uint32_t g_tx_delay_ms = 250; // Less than 250 is too frequent and brownouts many wall controllers
105+
static uint32_t g_tx_delay_ms = 50;
106+
static uint32_t g_ttc_delay_s = 0;
105107
static portMUX_TYPE gdo_spinlock = portMUX_INITIALIZER_UNLOCKED;
106108

107109

@@ -526,6 +528,26 @@ esp_err_t gdo_light_off(void) {
526528
return err;
527529
}
528530

531+
/**
532+
* @brief Sets the time to close.
533+
* @return ESP_OK on success, ESP_ERR_NO_MEM if the queue is full, ESP_FAIL if the encoding fails.
534+
*/
535+
esp_err_t gdo_set_ttc(uint16_t seconds) {
536+
esp_err_t err = ESP_OK;
537+
538+
if (g_status.protocol & GDO_PROTOCOL_SEC_PLUS_V1) {
539+
} else {
540+
//queue_command(gdo_command_t command, uint8_t nibble, uint8_t byte1, uint8_t byte2)
541+
err = queue_command(GDO_CMD_SET_TTC, seconds, 0, 0);
542+
if (err == ESP_OK) {
543+
get_status();
544+
}
545+
}
546+
547+
return err;
548+
}
549+
550+
529551
/**
530552
* @brief Toggles the light.
531553
* @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) {
13241346
update_motion_state(GDO_MOTION_STATE_DETECTED);
13251347
} else if (cmd == GDO_CMD_OPENINGS) {
13261348
update_openings(nibble, ((byte1 << 8) | byte2));
1349+
} else if (cmd == GDO_CMD_UPDATE_TTC) {
1350+
update_ttc((byte1 << 8) | byte2);
1351+
queue_event((gdo_event_t){GDO_EVENT_UPDATE_TTC});
13271352
} else if (cmd == GDO_CMD_SET_TTC) {
13281353
update_ttc((byte1 << 8) | byte2);
1354+
queue_event((gdo_event_t){GDO_EVENT_SET_TTC});
13291355
} else if (cmd == GDO_CMD_PAIRED_DEVICES) {
13301356
update_paired_devices(nibble, byte2);
13311357
} else if (cmd == GDO_CMD_BATTERY_STATUS) {
@@ -1578,8 +1604,13 @@ static void gdo_main_task(void* arg) {
15781604
case GDO_EVENT_MOTION_UPDATE:
15791605
cb_event = GDO_CB_EVENT_MOTION;
15801606
break;
1581-
case GDO_EVENT_TTC_UPDATE:
1582-
cb_event = GDO_CB_EVENT_TTC;
1607+
case GDO_EVENT_UPDATE_TTC:
1608+
cb_event = GDO_CB_EVENT_UPDATE_TTC;
1609+
ESP_LOGI(TAG, "GDO_CB_EVENT_UPDATE_TTC");
1610+
break;
1611+
case GDO_EVENT_SET_TTC:
1612+
cb_event = GDO_CB_EVENT_SET_TTC;
1613+
ESP_LOGI(TAG, "GDO_CB_EVENT_SET_TTC");
15831614
break;
15841615
case GDO_EVENT_PAIRED_DEVICES_UPDATE:
15851616
cb_event = GDO_CB_EVENT_PAIRED_DEVICES;
@@ -1896,13 +1927,29 @@ inline static void update_openings(uint8_t flag, uint16_t count) {
18961927
* @param ttc The new TTC to update to.
18971928
*/
18981929
inline static void update_ttc(uint16_t ttc) {
1899-
ESP_LOGD(TAG, "TTC: %u", ttc);
1930+
ESP_LOGI(TAG, "TTC Seconds remaining: %u", ttc);
19001931
if (g_status.ttc_seconds != ttc) {
19011932
g_status.ttc_seconds = ttc;
1902-
queue_event((gdo_event_t){GDO_EVENT_TTC_UPDATE});
19031933
}
19041934
}
19051935

1936+
/**
1937+
* @brief Set the time to close
1938+
* @param time_to_close The new time to close set by the UI
1939+
*/
1940+
esp_err_t gdo_set_time_to_close(uint16_t time_to_close) {
1941+
g_ttc_delay_s = time_to_close;
1942+
g_status.ttc_enabled = (time_to_close > 0) ? 1 : 0;
1943+
esp_err_t err = ESP_OK;
1944+
uint8_t byte1 = (time_to_close >> 8);
1945+
uint8_t byte2 = (uint8_t)time_to_close;
1946+
uint8_t nibble = 1;
1947+
update_ttc(time_to_close);
1948+
queue_command(GDO_CMD_SET_TTC, nibble, byte1, byte2);
1949+
queue_event((gdo_event_t){GDO_EVENT_SET_TTC});
1950+
return err;
1951+
}
1952+
19061953
/**
19071954
* @brief Updates the local paired devices count and queues an event if it has changed.
19081955
* @param type The type of paired devices to update.

gdo_priv.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ typedef enum {
5656
GDO_CMD_PAIR_2_RESP = 0x401,
5757
GDO_CMD_SET_TTC = 0x402,
5858
GDO_CMD_CANCEL_TTC = 0x408,
59-
GDO_CMD_TTC = 0x40a,
59+
GDO_CMD_UPDATE_TTC = 0x40a,
6060
GDO_CMD_GET_OPENINGS = 0x48b,
6161
GDO_CMD_OPENINGS = 0x48c,
6262
GDO_CMD_MAX,
@@ -90,7 +90,8 @@ typedef enum {
9090
GDO_EVENT_LEARN_UPDATE,
9191
GDO_EVENT_OPENINGS_UPDATE,
9292
GDO_EVENT_MOTION_UPDATE,
93-
GDO_EVENT_TTC_UPDATE,
93+
GDO_EVENT_UPDATE_TTC,
94+
GDO_EVENT_SET_TTC,
9495
GDO_EVENT_PAIRED_DEVICES_UPDATE,
9596
GDO_EVENT_DOOR_OPEN_DURATION_MEASUREMENT,
9697
GDO_EVENT_DOOR_CLOSE_DURATION_MEASUREMENT,

gdo_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ const char* cmd_to_string(gdo_command_t cmd) {
161161
return "SET_TTC";
162162
case GDO_CMD_CANCEL_TTC:
163163
return "CANCEL_TTC";
164-
case GDO_CMD_TTC:
165-
return "TTC";
164+
case GDO_CMD_UPDATE_TTC:
165+
return "UPDATE_TTC";
166166
case GDO_CMD_GET_OPENINGS:
167167
return "GET_OPENINGS";
168168
case GDO_CMD_OPENINGS:

include/gdo.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ typedef enum {
112112
GDO_CB_EVENT_LEARN,
113113
GDO_CB_EVENT_OPENINGS,
114114
GDO_CB_EVENT_MOTION,
115-
GDO_CB_EVENT_TTC,
115+
GDO_CB_EVENT_SET_TTC,
116+
GDO_CB_EVENT_CANCLE_TTC,
117+
GDO_CB_EVENT_UPDATE_TTC,
116118
GDO_CB_EVENT_PAIRED_DEVICES,
117119
GDO_CB_EVENT_OPEN_DURATION_MEASURMENT,
118120
GDO_CB_EVENT_CLOSE_DURATION_MEASURMENT,
@@ -140,6 +142,7 @@ typedef struct {
140142
gdo_learn_state_t learn; // Learn state
141143
gdo_paired_device_t paired_devices; // Paired devices
142144
bool synced; // Synced state
145+
bool ttc_enabled; //ttc active
143146
uint16_t openings; // Number of openings
144147
uint16_t ttc_seconds; // Time to close in seconds
145148
uint16_t open_ms; // Time door takes to open from fully closed in milliseconds
@@ -157,8 +160,8 @@ typedef struct {
157160
gpio_num_t uart_tx_pin; // UART TX pin
158161
gpio_num_t uart_rx_pin; // UART RX pin
159162
gpio_num_t obst_in_pin; // Obstruction input pin
160-
gpio_num_t rf_tx_pin; // UART TX pin
161-
gpio_num_t rf_rx_pin; // UART RX pin
163+
gpio_num_t rf_tx_pin; // RF TX pin
164+
gpio_num_t rf_rx_pin; // RF RX pin
162165
} gdo_config_t;
163166

164167
#define GDO_PAIRED_DEVICE_COUNT_UNKNOWN 0xff
@@ -392,6 +395,14 @@ esp_err_t gdo_set_client_id(uint32_t client_id);
392395
*/
393396
esp_err_t gdo_set_protocol(gdo_protocol_type_t protocol);
394397

398+
/**
399+
* @brief Sets the time to close in minutes
400+
* @param time_to_close The time to close value.
401+
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if the time is invalid,
402+
* ESP_ERR_INVALID_STATE if the time is out of range.
403+
*/
404+
esp_err_t gdo_set_time_to_close(uint16_t time_to_close);
405+
395406
/**
396407
* @brief Sets the time the door takes to open from fully closed in milliseconds.
397408
* @param ms The time the door takes to open from fully closed in milliseconds.

0 commit comments

Comments
 (0)