Skip to content

Commit

Permalink
Flashing works, key press does not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul D.Smith committed Dec 29, 2023
1 parent 9cea655 commit 9c980a3
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 120 deletions.
100 changes: 61 additions & 39 deletions esp32/cloudsmets/main/cs_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,60 +105,93 @@ static void esp32_info()
"Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
}

static void IRAM_ATTR intr_handler(void *arg)
{
BaseType_t task_unblocked;

esp_event_isr_post_to(
app_event_loop_handle,
CS_APP_EVENT,
CS_APP_EVENT_KEY_ACTION,
NULL,
0,
&task_unblocked);
}

/**
* Note that the line is pull DOWN by a key press.
* We send ourselves an event to avoid any processing that might not be
* possible from inside an interrupt handler.
*/
static void intr_handler(void *arg)
static void key_action(void)
{
time_t pressed = 0;
static time_t pressed = 0;
time_t released = 0;
int level;

level = gpio_get_level(CLEAR_KEY);
ESP_LOGI(TAG, "Key action: %d", level);
if (level)
{
/* Rising line; key has been released. */
released = time(NULL);
released = released - pressed;
released = time(NULL) - pressed;
if (released > FIRST_JAN_2023)
{
/**
* Time has been set whilst we were pressing the key so we cannot
* tell how long it was held for; have to just ignore it.
*/
ESP_LOGI(TAG, "Invalid interval - ignore");
}
else if (released > FACTORY_RESET_PRESS)
{
/**
* Factory resetting the ESP32c3 will happen on a timer to allow
* the tlsr8258 to be reset first.
*/
ESP_ERROR_CHECK(esp_event_isr_post_to(
app_event_loop_handle,
CS_APP_EVENT,
CS_APP_EVENT_FACTORY_RESET,
ESP_LOGI(TAG, "Full factory reset");
ESP_ERROR_CHECK(esp_event_post_to(
zigbee_event_loop_handle,
CS_ZIGBEE_EVENT,
CS_ZIGBEE_EVENT_FACTORY_RESET,
NULL,
0,
NULL));
10));

/* Delay the ESP32c3 reset to allow ZigBee time to reset first. */

// TODO: Enable this later...
// ESP_ERROR_CHECK(esp_timer_start_once(factory_reset_timer_handle, FACTORY_RESET_TIMER_WAIT));
}
else if (released > ZIGBEE_RESET_PRESS)
{
ESP_ERROR_CHECK(esp_event_isr_post_to(
app_event_loop_handle,
CS_APP_EVENT,
CS_APP_EVENT_FACTORY_RESET,
ESP_LOGI(TAG, "Full ZigBee omly reset");
ESP_ERROR_CHECK(esp_event_post_to(
zigbee_event_loop_handle,
CS_ZIGBEE_EVENT,
CS_ZIGBEE_EVENT_FACTORY_RESET,
NULL,
0,
NULL));
10));
}
else
{
ESP_LOGV(TAG, "Short press: %ld", time(NULL));
struct timeval ptime;
gettimeofday(&ptime, NULL);
ESP_LOGV(TAG, "Short press: %ld", ptime.tv_sec);
ESP_LOGV(TAG, "Short press: %ld", pressed);
ESP_LOGV(TAG, "Short press: %ld", released);
}
}
else
{
/* Falling line; key has been pressed. */
pressed = time(NULL);
released = pressed;
ESP_LOGV(TAG, "Short press: %ld", pressed);
struct timeval ptime;
gettimeofday(&ptime, NULL);
ESP_LOGV(TAG, "Short press: %ld", ptime.tv_sec);
}
}

Expand All @@ -173,16 +206,22 @@ static void init_gpio(void)
.intr_type = GPIO_INTR_DISABLE
};
ESP_ERROR_CHECK(gpio_config(&gpio_config_data));

#if 1
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1));
ESP_ERROR_CHECK(gpio_isr_handler_add(CLEAR_KEY, intr_handler, NULL));
#endif
/* Configure the input key GPIO line used by T-ZigBee. */
gpio_config_data.pin_bit_mask = (1 << CLEAR_KEY);
gpio_config_data.mode = GPIO_MODE_INPUT;
gpio_config_data.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config_data.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config_data.intr_type = GPIO_INTR_ANYEDGE;
ESP_ERROR_CHECK(gpio_config(&gpio_config_data));
#if 0
// TODo: Comment on using alternatives above.
ESP_ERROR_CHECK(gpio_isr_register(intr_handler, NULL, ESP_INTR_FLAG_LEVEL1, NULL));
ESP_ERROR_CHECK(gpio_intr_enable(CLEAR_KEY));
#endif
}

static void factory_reset_timer_cb(void *arg)
Expand All @@ -198,27 +237,8 @@ static void event_handler(void *arg, esp_event_base_t event_base,
{
switch (event_id)
{
case CS_APP_EVENT_FACTORY_RESET:
ESP_ERROR_CHECK(esp_event_post_to(
zigbee_event_loop_handle,
CS_ZIGBEE_EVENT,
CS_ZIGBEE_EVENT_FACTORY_RESET,
NULL,
0,
10));

/* Delay the ESP32c3 reset to allow ZigBee time to reset first. */
ESP_ERROR_CHECK(esp_timer_start_once(factory_reset_timer_handle, FACTORY_RESET_TIMER_WAIT));
break;

case CS_APP_EVENT_ZIGBEE_RESET:
ESP_ERROR_CHECK(esp_event_post_to(
zigbee_event_loop_handle,
CS_ZIGBEE_EVENT,
CS_ZIGBEE_EVENT_FACTORY_RESET,
NULL,
0,
10));
case CS_APP_EVENT_KEY_ACTION:
key_action();
break;

default:
Expand Down Expand Up @@ -303,9 +323,11 @@ void app_main(void)
.task_name = cs_app_task_name,
.task_priority = CS_APP_TASK_PRIORITY_DEFAULT,
// TODO: Do we really need this big a stack? Do we care?
.task_stack_size = 32768,
.task_stack_size = 4096,
.task_core_id = tskNO_AFFINITY
};
esp_event_loop_args.task_name = cs_app_task_name;
ESP_ERROR_CHECK(esp_event_loop_create(&esp_event_loop_args, &app_event_loop_handle));
esp_event_loop_args.task_name = cs_flash_task_name;
ESP_ERROR_CHECK(esp_event_loop_create(&esp_event_loop_args, &flash_event_loop_handle));
esp_event_loop_args.task_name = cs_web_task_name;
Expand Down Expand Up @@ -364,7 +386,7 @@ void app_main(void)
NULL));

// TODO: remove this which was added for testing.
#define NOW 1703592595
#define NOW 1703870896
struct timeval now = { .tv_sec = NOW };
settimeofday(&now, NULL);
ESP_LOGV(TAG, "Sending time event");
Expand Down
3 changes: 1 addition & 2 deletions esp32/cloudsmets/main/cs_app.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
typedef enum {
CS_APP_EVENT_FACTORY_RESET, /*!< Reset everything! */
CS_APP_EVENT_ZIGBEE_RESET, /*!< Reset ZigBee (new join). */
CS_APP_EVENT_KEY_ACTION, /*!< Reset everything! */
} cs_app_event_t;

ESP_EVENT_DECLARE_BASE(CS_APP_EVENT);
64 changes: 39 additions & 25 deletions esp32/cloudsmets/main/cs_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ESP_EVENT_DECLARE_BASE(MQTT_EVENTS);

esp_event_loop_handle_t flash_event_loop_handle;

#define STATUS_WIFI_SOFTAP_CONNECTED 0x01
#define STATUS_WIFI_SOFTAP_STARTED 0x01
#define STATUS_WIFI_AP_CONNECTED 0x02
#define STATUS_MQTT_CONNECTED 0x04
#define STATUS_ZIGBEE_CONNECTED 0x08
Expand All @@ -54,17 +54,15 @@ static int flashes_per_cycle = 0;

static void led_on()
{
esp_timer_stop(flash_timer_handle);
ESP_ERROR_CHECK(gpio_set_level(BLUE_LED, 1));
}

static void led_off()
{
esp_timer_stop(flash_timer_handle);
ESP_ERROR_CHECK(gpio_set_level(BLUE_LED, 0));
}

#define FLASH_TIME_PERIOD (uint64_t)1000 * 500
#define FLASH_TIME_PERIOD ((uint64_t)1000 * 500)
static void led_flash(int flashes)
{
// TODO: Much nicer if we get rid of globals.
Expand All @@ -82,7 +80,7 @@ static void led_flash(int flashes)
* on, off, on, off, on, off, off, off
* So a sequence of X (on, off) followed by (off, off).
*/
void flash_timer_cb(void *arg)
static void flash_timer_cb(void *arg)
{
static int count = 0;
static bool lit = false;
Expand All @@ -94,25 +92,26 @@ void flash_timer_cb(void *arg)
}
else
{
if (count <= 0)
if (count)
{
/* Time for next cycle but LED stays off for this cycle. */
count = flashes_per_cycle;
}
else
{
/* Turn the LED on. */
led_on();
}
count--;
if (count <= -1)
{
/* Time for next cycle but LED stays off for this cycle. */
count = flashes_per_cycle;
}
}
lit = !lit;
}

void update_flash_status(uint8_t status)
static void update_flash_status(uint8_t status)
{
ESP_LOGV(TAG, "status: %2.2X", status);
if ((status & ALL_ZIGBEE_ACTIVE) == ALL_ZIGBEE_ACTIVE)
{
esp_timer_stop(flash_timer_handle);
led_on();
}
else if ((status & ALL_MQTT_ACTIVE) == ALL_MQTT_ACTIVE)
Expand All @@ -129,6 +128,7 @@ void update_flash_status(uint8_t status)
}
else
{
esp_timer_stop(flash_timer_handle);
led_off();
}
}
Expand All @@ -144,18 +144,20 @@ void update_flash_status(uint8_t status)
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
uint8_t status = 0;
static uint8_t status = 0;

ESP_LOGV(TAG, "Event: %s, %d", event_base, event_id);

if (event_base == WIFI_EVENT)
{
switch (event_id)
{
case WIFI_EVENT_AP_STACONNECTED:
status |= STATUS_WIFI_SOFTAP_CONNECTED;
case WIFI_EVENT_AP_START:
status |= STATUS_WIFI_SOFTAP_STARTED;
break;

case WIFI_EVENT_AP_STADISCONNECTED:
status &= ~STATUS_WIFI_SOFTAP_CONNECTED;
case WIFI_EVENT_AP_STOP:
status &= ~STATUS_WIFI_SOFTAP_STARTED;
break;

case WIFI_EVENT_STA_CONNECTED:
Expand All @@ -167,7 +169,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
break;

default:
ESP_LOGE(TAG, "Unexpected flash event: %d", event_id);
ESP_LOGE(TAG, "Unexpected WiFi event: %d", event_id);
break;
}
}
Expand All @@ -184,7 +186,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
break;

default:
ESP_LOGE(TAG, "Unexpected flash event: %d", event_id);
ESP_LOGE(TAG, "Unexpected MQTT event: %d", event_id);
break;
}
}
Expand All @@ -201,20 +203,22 @@ static void event_handler(void *arg, esp_event_base_t event_base,
break;

default:
ESP_LOGE(TAG, "Unexpected flash event: %d", event_id);
ESP_LOGE(TAG, "Unexpected ZigBee event: %d", event_id);
break;
}
}
else
{
ESP_LOGE(TAG, "Unexpected event: %s, %d", event_base, event_id);
}
ESP_LOGV(TAG, "Update flasher status");
update_flash_status(status);
}

void cs_flash_task(cs_flash_create_parms_t *create_parms)
{
static int flashes = 0;
// TODO: Remove this or perhaps replace with config?
esp_log_level_set(TAG, ESP_LOG_VERBOSE);

flash_event_loop_handle = create_parms->flash_event_loop_handle;

Expand All @@ -228,20 +232,30 @@ void cs_flash_task(cs_flash_create_parms_t *create_parms)
WIFI_EVENT,
ESP_EVENT_ANY_ID,
event_handler,
(void *)&flashes,
NULL,
NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register_with(
flash_event_loop_handle,
MQTT_EVENTS,
ESP_EVENT_ANY_ID,
event_handler,
(void *)&flashes,
NULL,
NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register_with(
flash_event_loop_handle,
CS_ZIGBEE_EVENT,
ESP_EVENT_ANY_ID,
event_handler,
(void *)&flashes,
NULL,
NULL));

/**
* Create the timer that will make the LED flash.
*/
esp_timer_create_args_t timer_args =
{
.callback = flash_timer_cb,
.name = "Flash timer"
};
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &flash_timer_handle));
}
4 changes: 2 additions & 2 deletions esp32/cloudsmets/main/cs_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ static void generate_sas_token()
cs_string hmac_sig = {hmac_sig_value, SHA256_LEN};
unsigned char b64_hmac_sig_value[URLENCODED_BASE64_SHA256_LEN + 1];
cs_string b64_hmac_sig = {b64_hmac_sig_value, URLENCODED_BASE64_SHA256_LEN};
// DEBUG: TODO: time_t ttl = time(NULL) + 3600;
time_t ttl = 1703596659 + 3600;
time_t ttl = time(NULL) + 3600;
unsigned char *sptr;
int offset;
cs_string temp_str = {NULL, 0};
Expand Down Expand Up @@ -630,6 +629,7 @@ void cs_mqtt_task(cs_mqtt_create_parms_t *create_parms)

ESP_LOGI(TAG, "Init. MQTT task");
mqtt_event_loop_handle = create_parms->mqtt_event_loop_handle;
flash_event_loop_handle = create_parms->flash_event_loop_handle;

ESP_LOGI(TAG, "Register event handlers");

Expand Down
Loading

0 comments on commit 9c980a3

Please sign in to comment.