--- a/main/app_main.c Sun Feb 28 20:09:32 2021 -0600
+++ b/main/app_main.c Mon Mar 01 13:43:49 2021 -0600
@@ -9,6 +9,8 @@
#include <esp_log.h>
#include <driver/gpio.h>
+#include <iot_button.h>
+
#include <hap.h>
#include <hap_apple_servs.h>
@@ -23,17 +25,15 @@
#define DOOR_TASK_STACKSIZE 4 * 1024
#define DOOR_TASK_NAME "hap_door"
-#define DOOR_LOCK_GPIO_PIN GPIO_NUM_21
-#define DOOR_BELL_GPIO_PIN GPIO_NUM_14
+#define RESET_NETWORK_BUTTON_TIMEOUT 3
+
#define DOOR_LOCK_GPIO_LOCKED 0
#define DOOR_LOCK_GPIO_UNLOCKED 1
#define HAP_LOCK_TARGET_STATE_UNSECURED 0
#define HAP_LOCK_TARGET_STATE_SECURED 1
-
static hap_val_t HAP_LOCK_CURRENT_STATE_UNSECURED = {.u = 0};
static hap_val_t HAP_LOCK_CURRENT_STATE_SECURED = {.u = 1};
-
static hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
#define ESP_INTR_FLAG_DEFAULT 0
@@ -43,6 +43,9 @@
static const uint8_t DOOR_EVENT_QUEUE_LOCK = 3;
static const uint8_t DOOR_EVENT_QUEUE_LOCK_TIMEOUT = 4;
+static uint8_t tlv8buff[128];
+static hap_data_val_t null_tlv8 = { .buf = &tlv8buff, .buflen = 127 };
+
static xQueueHandle door_event_queue = NULL;
static TimerHandle_t door_lock_timer = NULL;
@@ -87,16 +90,44 @@
}
/**
+ * Enable a GPIO Pin for LED
+ */
+static void led_init(uint32_t key_gpio_pin) {
+ gpio_config_t io_conf;
+
+ io_conf.intr_type = GPIO_INTR_DISABLE; /* Interrupt for falling edge */
+ io_conf.pin_bit_mask = 1 << key_gpio_pin; /* Bit mask of the pins */
+ io_conf.mode = GPIO_MODE_OUTPUT; /* Set as input mode */
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE; /* Disable internal pull-up */
+ io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; /* Disable internal pull-down */
+
+ gpio_config(&io_conf); /* Set the GPIO configuration */
+}
+
+static void reset_network_handler(void* arg) {
+ ESP_LOGI(TAG, "Resetting network");
+ hap_reset_network();
+}
+
+static void reset_init(uint32_t key_gpio_pin) {
+ button_handle_t handle = iot_button_create(key_gpio_pin, BUTTON_ACTIVE_LOW);
+ iot_button_add_on_release_cb(handle, RESET_NETWORK_BUTTON_TIMEOUT, reset_network_handler, NULL);
+}
+
+/**
* Initialize the Door Hardware. Here, we just enebale the Door Bell detection.
*/
-void door_hardware_init(gpio_num_t door_bell_gpio_num, gpio_num_t door_lock_gpio_num) {
+void door_hardware_init(gpio_num_t reset_gpio_num, gpio_num_t door_bell_gpio_num, gpio_num_t door_lock_gpio_num, gpio_num_t led_gpio_num) {
int queue_len = 4;
int queue_item_size = sizeof(uint8_t);
door_event_queue = xQueueCreate(queue_len, queue_item_size);
if (door_event_queue != NULL) {
+ /* reset_init(reset_gpio_num); */
door_bell_init(door_bell_gpio_num);
door_lock_init(door_lock_gpio_num);
+ led_init(led_gpio_num);
+
}
}
@@ -106,6 +137,14 @@
*/
static int door_identify(hap_acc_t *ha) {
ESP_LOGI(TAG, "Accessory identified");
+
+ for (int i = 0; i < 3; i++) {
+ gpio_set_level(CONFIG_HOMEKIT_DOOR_LED_GPIO_PIN, 1);
+ vTaskDelay(pdMS_TO_TICKS(500));
+ gpio_set_level(CONFIG_HOMEKIT_DOOR_LED_GPIO_PIN, 0);
+ vTaskDelay(pdMS_TO_TICKS(500));
+ }
+
return HAP_SUCCESS;
}
@@ -118,7 +157,7 @@
static void door_unlock(hap_char_t *door_lock_current_state) {
ESP_LOGI(TAG, "Door unlock event processed");
- gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_UNLOCKED);
+ gpio_set_level(CONFIG_HOMEKIT_DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_UNLOCKED);
hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_UNSECURED);
xTimerReset(door_lock_timer, 10);
@@ -127,7 +166,7 @@
static void door_lock(hap_char_t *door_lock_current_state) {
ESP_LOGI(TAG, "Door lock event processed");
- gpio_set_level(DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_LOCKED);
+ gpio_set_level(CONFIG_HOMEKIT_DOOR_LOCK_GPIO_PIN, DOOR_LOCK_GPIO_LOCKED);
hap_char_update_val(door_lock_current_state, &HAP_LOCK_CURRENT_STATE_SECURED);
}
@@ -196,7 +235,7 @@
uint8_t product_data[] = {'E','S','P','3','2','H','A','P'};
hap_acc_add_product_data(door_accessory, product_data, sizeof(product_data));
- hap_serv_t *door_bell_service = hap_serv_stateless_programmable_switch_create(0);
+ hap_serv_t *door_bell_service = hap_serv_doorbell_create(0);
hap_serv_add_char(door_bell_service, hap_char_name_create("Doorbell"));
hap_char_t *door_bell_current_state = hap_serv_get_char_by_uuid(door_bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
@@ -213,7 +252,8 @@
hap_add_accessory(door_accessory); /* Add the Accessory to the HomeKit Database */
- door_hardware_init(DOOR_BELL_GPIO_PIN, DOOR_LOCK_GPIO_PIN); /* Initialize the appliance specific hardware. This enables out-in-use detection */
+ /* Initialize the appliance specific hardware. This enables out-in-use detection */
+ door_hardware_init(CONFIG_HOMEKIT_DOOR_WIFI_RESET_GPIO_PIN, CONFIG_HOMEKIT_DOOR_BELL_GPIO_PIN, CONFIG_HOMEKIT_DOOR_LOCK_GPIO_PIN, CONFIG_HOMEKIT_DOOR_LED_GPIO_PIN);
/* For production accessories, the setup code shouldn't be programmed on to
* the device. Instead, the setup info, derived from the setup code must