Skip to content

Commit

Permalink
Merge pull request #3 from descipher/secplus.timing
Browse files Browse the repository at this point in the history
Add TTC Control
  • Loading branch information
descipher authored Oct 10, 2024
2 parents 98a9a02 + cafca55 commit 69f5c9e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
57 changes: 52 additions & 5 deletions gdo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -101,7 +102,8 @@ static esp_timer_handle_t motion_detect_timer;
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 = 250; // Less than 250 is too frequent and brownouts many wall controllers
static uint32_t g_tx_delay_ms = 50;
static uint32_t g_ttc_delay_s = 0;
static portMUX_TYPE gdo_spinlock = portMUX_INITIALIZER_UNLOCKED;


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions gdo_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions gdo_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 14 additions & 3 deletions include/gdo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 69f5c9e

Please sign in to comment.