src.nth.io/

summaryrefslogtreecommitdiff
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/bell.c108
-rw-r--r--main/src/led.c24
-rw-r--r--main/src/lock.c92
3 files changed, 100 insertions, 124 deletions
diff --git a/main/src/bell.c b/main/src/bell.c
index 94dfeac..10c92a6 100644
--- a/main/src/bell.c
+++ b/main/src/bell.c
@@ -15,23 +15,23 @@
static hap_val_t HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS = {.u = 0};
-#define INTERCOM_BELL_TASK_PRIORITY 1
-#define INTERCOM_BELL_TASK_STACKSIZE 4 * 1024
-#define INTERCOM_BELL_TASK_NAME "hap_intercom_bell"
+#define BELL_TASK_PRIORITY 1
+#define BELL_TASK_STACKSIZE 4 * 1024
+#define BELL_TASK_NAME "hap_intercom_bell"
-static TaskHandle_t intercom_bell_read_task;
-static TimerHandle_t intercom_bell_timer; // ignore new bells until timer triggered
-volatile bool is_intercom_bell_blocked;
-static hap_char_t *intercom_bell_current_state;
+static TaskHandle_t bell_read_task_handle;
+static TimerHandle_t bell_block_timer_handle; // ignore new bells until timer triggered
+volatile bool is_bell_blocked;
+static hap_char_t *bell_current_state;
void bell_rang()
{
ESP_LOGI(TAG, "bell HAP RING");
- hap_char_update_val(intercom_bell_current_state, &HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS);
+ hap_char_update_val(bell_current_state, &HAP_PROGRAMMABLE_SWITCH_EVENT_SINGLE_PRESS);
- is_intercom_bell_blocked = true;
+ is_bell_blocked = true;
ESP_LOGI(TAG, "bell timer set; bell updated [blocked: true]");
- xTimerReset(intercom_bell_timer, pdFALSE);
+ xTimerReset(bell_block_timer_handle, pdFALSE);
}
bool is_bell_ringing()
@@ -42,7 +42,7 @@ bool is_bell_ringing()
return is_ringing;
}
-void intercom_bell_read(void *p)
+void bell_read_task(void *p)
{
for (;;)
{
@@ -52,76 +52,76 @@ void intercom_bell_read(void *p)
}
}
-void IRAM_ATTR intercom_bell_isr(void *arg)
+void IRAM_ATTR bell_isr(void *arg)
{
- if (is_intercom_bell_blocked)
- return;
-
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
- configASSERT(intercom_bell_read_task != NULL);
- vTaskNotifyGiveFromISR(intercom_bell_read_task, &xHigherPriorityTaskWoken);
- portYIELD_FROM_ISR();
+ if (!is_bell_blocked)
+ {
+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+ configASSERT(bell_read_task_handle != NULL);
+ vTaskNotifyGiveFromISR(bell_read_task_handle, &xHigherPriorityTaskWoken);
+ portYIELD_FROM_ISR();
+ }
}
-void intercom_bell_timer_cb(TimerHandle_t timer)
+void bell_block_timer_cb(TimerHandle_t timer)
{
- is_intercom_bell_blocked = false;
+ is_bell_blocked = false;
ESP_LOGI(TAG, "bell timer triggered; bell updated [blocked: false]");
}
-void intercom_bell_blocker_init()
+void bell_blocker_init()
{
- is_intercom_bell_blocked = false;
- intercom_bell_timer = xTimerCreate("intercom_bell_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT),
- pdFALSE, 0, intercom_bell_timer_cb);
+ is_bell_blocked = false;
+ bell_block_timer_handle = xTimerCreate("intercom_bell_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT),
+ pdFALSE, 0, bell_block_timer_cb);
}
-hap_serv_t *intercom_bell_service_init()
+hap_serv_t *bell_init()
{
- hap_serv_t *intercom_bell_service = hap_serv_doorbell_create(0);
- hap_serv_add_char(intercom_bell_service, hap_char_name_create("Intercom Bell"));
- intercom_bell_current_state = hap_serv_get_char_by_uuid(intercom_bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
- return intercom_bell_service;
+ hap_serv_t *bell_service = hap_serv_doorbell_create(0);
+ hap_serv_add_char(bell_service, hap_char_name_create("Intercom Bell"));
+ bell_current_state = hap_serv_get_char_by_uuid(bell_service, HAP_CHAR_UUID_PROGRAMMABLE_SWITCH_EVENT);
+ return bell_service;
}
-void intercom_bell_isr_gpio_init()
+void bell_isr_gpio_init()
{
// Configure ISR GPIO Pin 27
gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_NEGEDGE; /* Interrupt for falling edge */
- io_conf.pin_bit_mask = GPIO_SEL_27; /* Bit mask of the pins */
- io_conf.mode = GPIO_MODE_INPUT; /* Set as input mode */
- io_conf.pull_up_en = GPIO_PULLUP_DISABLE; /* Disable internal pull-up */
- io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; /* Enable internal pull-down */
- gpio_config(&io_conf); /* Set the GPIO configuration */
-
- gpio_install_isr_service(0); /* Install gpio isr service */
- gpio_isr_handler_add(GPIO_NUM_27, intercom_bell_isr, (void *)0); /* Hook isr handler for specified gpio pin */
+ io_conf.intr_type = GPIO_INTR_NEGEDGE;
+ io_conf.pin_bit_mask = GPIO_SEL_27;
+ io_conf.mode = GPIO_MODE_INPUT;
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
+ gpio_config(&io_conf);
+
+ gpio_install_isr_service(0);
+ gpio_isr_handler_add(GPIO_NUM_27, bell_isr, (void *)0);
}
-void intercom_bell_adc_gpio_init()
+void bell_adc_gpio_init()
{
// Configure ADC1 Channel 5, GPIO Pin 33
gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_DISABLE; /* Interrupt for falling edge */
- io_conf.pin_bit_mask = GPIO_SEL_33; /* Bit mask of the pins */
- io_conf.mode = GPIO_MODE_INPUT; /* Set as input mode */
- io_conf.pull_up_en = GPIO_PULLUP_DISABLE; /* Disable internal pull-up */
- io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; /* Enable internal pull-down */
- gpio_config(&io_conf); /* Set the GPIO configuration */
+ io_conf.intr_type = GPIO_INTR_DISABLE;
+ io_conf.pin_bit_mask = GPIO_SEL_33;
+ io_conf.mode = GPIO_MODE_INPUT;
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
+ gpio_config(&io_conf);
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#_CPPv425adc1_config_channel_atten14adc1_channel_t11adc_atten_t
adc1_config_width(ADC_WIDTH_BIT_12); /* The value read is 12 bits wide (range 0-4095). */
adc1_config_channel_atten(ADC1_GPIO33_CHANNEL, ADC_ATTEN_DB_11);
}
-hap_serv_t *intercom_bell_init()
+hap_serv_t *bell_service_init()
{
- xTaskCreate(intercom_bell_read, INTERCOM_BELL_TASK_NAME, INTERCOM_BELL_TASK_STACKSIZE, NULL,
- INTERCOM_BELL_TASK_PRIORITY, &intercom_bell_read_task);
+ xTaskCreate(bell_read_task, BELL_TASK_NAME, BELL_TASK_STACKSIZE, NULL,
+ BELL_TASK_PRIORITY, &bell_read_task_handle);
- intercom_bell_blocker_init();
- intercom_bell_isr_gpio_init();
- intercom_bell_adc_gpio_init();
- return intercom_bell_service_init();
+ bell_blocker_init();
+ bell_isr_gpio_init();
+ bell_adc_gpio_init();
+ return bell_init();
}
diff --git a/main/src/led.c b/main/src/led.c
index b000e18..55c6ae4 100644
--- a/main/src/led.c
+++ b/main/src/led.c
@@ -7,31 +7,31 @@
#include <led.h>
#include <intercom.h>
-#define LED_ON 1
#define LED_OFF 0
-#define LED_DELAY 500
+#define LED_ON 1
+#define LED_DELAY_MILLIS 500
#define LED_NUM_BLINK 3
-int intercom_led_identify(hap_acc_t *ha)
+int led_identify(hap_acc_t *ha)
{
ESP_LOGI(TAG, "accessory identified");
for (int i = 0; i < LED_NUM_BLINK; i++)
{
gpio_set_level(GPIO_NUM_13, LED_ON);
- vTaskDelay(pdMS_TO_TICKS(LED_DELAY));
+ vTaskDelay(pdMS_TO_TICKS(LED_DELAY_MILLIS));
gpio_set_level(GPIO_NUM_13, LED_OFF);
- vTaskDelay(pdMS_TO_TICKS(LED_DELAY));
+ vTaskDelay(pdMS_TO_TICKS(LED_DELAY_MILLIS));
}
return HAP_SUCCESS;
}
-void intercom_led_init()
+void led_identify_service_init()
{
gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_DISABLE; /* Interrupt for falling edge */
- io_conf.pin_bit_mask = GPIO_SEL_13; /* 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_ENABLE; /* Enable internal pull-down */
- gpio_config(&io_conf); /* Set the GPIO configuration */
+ io_conf.intr_type = GPIO_INTR_DISABLE;
+ io_conf.pin_bit_mask = GPIO_SEL_13;
+ io_conf.mode = GPIO_MODE_OUTPUT;
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
+ gpio_config(&io_conf);
} \ No newline at end of file
diff --git a/main/src/lock.c b/main/src/lock.c
index cc1605b..94b346d 100644
--- a/main/src/lock.c
+++ b/main/src/lock.c
@@ -10,39 +10,26 @@
#include <lock.h>
#include <intercom.h>
-#define INTERCOM_LOCK_GPIO_LOCKED 0
-#define INTERCOM_LOCK_GPIO_UNLOCKED 1
+#define LOCK_UNSECURED 0
+#define LOCK_SECURED 1
-#define HAP_LOCK_CURRENT_STATE_UNSECURED 0
-#define HAP_LOCK_CURRENT_STATE_SECURED 1
+static TimerHandle_t lock_auto_secure_timer; // lock the door when timer triggered
+static hap_char_t *lock_current_state;
+static hap_char_t *lock_target_state;
-#define HAP_LOCK_TARGET_STATE_UNSECURED 0
-#define HAP_LOCK_TARGET_STATE_SECURED 1
-
-static hap_val_t HAP_VAL_LOCK_CURRENT_STATE_UNSECURED = {.u = HAP_LOCK_CURRENT_STATE_UNSECURED};
-static hap_val_t HAP_VAL_LOCK_CURRENT_STATE_SECURED = {.u = HAP_LOCK_CURRENT_STATE_SECURED};
-static hap_val_t HAP_VAL_LOCK_TARGET_STATE_SECURED = {.u = HAP_LOCK_TARGET_STATE_SECURED};
-
-static TimerHandle_t intercom_lock_timer; // lock the door when timer triggered
-static hap_char_t *intercom_lock_current_state;
-static hap_char_t *intercom_lock_target_state;
-
-void intercom_lock_unsecure()
+void lock_update_current_state(uint8_t is_secured)
{
- ESP_LOGI(TAG, "lock updated [secured: false]");
- gpio_set_level(GPIO_NUM_21, INTERCOM_LOCK_GPIO_UNLOCKED);
- hap_char_update_val(intercom_lock_current_state, &HAP_VAL_LOCK_CURRENT_STATE_UNSECURED);
- xTimerReset(intercom_lock_timer, 10);
-}
+ ESP_LOGI(TAG, "lock updated [%s]", is_secured ? "secured" : "unsecured");
+ gpio_set_level(GPIO_NUM_21, is_secured);
-void intercom_lock_secure()
-{
- ESP_LOGI(TAG, "lock updated [secured: true]");
- gpio_set_level(GPIO_NUM_21, INTERCOM_LOCK_GPIO_LOCKED);
- hap_char_update_val(intercom_lock_current_state, &HAP_VAL_LOCK_CURRENT_STATE_SECURED);
+ hap_val_t val = {.u = is_secured};
+ hap_char_update_val(lock_current_state, &val);
+
+ if (!is_secured)
+ xTimerReset(lock_auto_secure_timer, 10);
}
-int intercom_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_priv, void *write_priv)
+int lock_write_cb(hap_write_data_t write_data[], int count, void *serv_priv, void *write_priv)
{
int i, ret = HAP_SUCCESS;
hap_write_data_t *write;
@@ -51,19 +38,7 @@ int intercom_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_
write = &write_data[i];
if (!strcmp(hap_char_get_type_uuid(write->hc), HAP_CHAR_UUID_LOCK_TARGET_STATE))
{
- ESP_LOGI(TAG, "lock received write [val: %d]", write->val.u);
-
- switch (write->val.u)
- {
- case HAP_LOCK_TARGET_STATE_UNSECURED:
- intercom_lock_unsecure();
- break;
- case HAP_LOCK_TARGET_STATE_SECURED:
- intercom_lock_secure();
- break;
- }
-
- /* Update target state */
+ lock_update_current_state(write->val.u);
hap_char_update_val(write->hc, &(write->val));
*(write->status) = HAP_STATUS_SUCCESS;
}
@@ -75,37 +50,38 @@ int intercom_lock_write_cb(hap_write_data_t write_data[], int count, void *serv_
return ret;
}
-void intercom_lock_timer_cb(TimerHandle_t timer)
+void lock_auto_secure_timer_cb(TimerHandle_t timer)
{
ESP_LOGI(TAG, "lock timer fired");
- intercom_lock_secure();
- hap_char_update_val(intercom_lock_target_state, &HAP_VAL_LOCK_TARGET_STATE_SECURED);
+ lock_update_current_state(LOCK_SECURED);
+ hap_val_t val = {.u = LOCK_SECURED};
+ hap_char_update_val(lock_target_state, &val);
}
-void intercom_lock_gpio_init()
+void lock_gpio_init()
{
gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_DISABLE; /* Disable interrupt */
- io_conf.pin_bit_mask = GPIO_SEL_21; /* 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_ENABLE; /* Enable internal pull-down */
+ io_conf.intr_type = GPIO_INTR_DISABLE;
+ io_conf.pin_bit_mask = GPIO_SEL_21;
+ io_conf.mode = GPIO_MODE_OUTPUT;
+ io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
gpio_config(&io_conf);
}
-hap_serv_t *intercom_lock_init()
+hap_serv_t *lock_service_init()
{
- hap_serv_t *intercom_lock_service = hap_serv_lock_mechanism_create(HAP_VAL_LOCK_CURRENT_STATE_SECURED.u, HAP_LOCK_TARGET_STATE_SECURED);
- hap_serv_add_char(intercom_lock_service, hap_char_name_create("Intercom Lock"));
+ hap_serv_t *lock_service = hap_serv_lock_mechanism_create(LOCK_SECURED, LOCK_SECURED);
+ hap_serv_add_char(lock_service, hap_char_name_create("Intercom Lock"));
- intercom_lock_current_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_CURRENT_STATE);
- intercom_lock_target_state = hap_serv_get_char_by_uuid(intercom_lock_service, HAP_CHAR_UUID_LOCK_TARGET_STATE);
+ lock_current_state = hap_serv_get_char_by_uuid(lock_service, HAP_CHAR_UUID_LOCK_CURRENT_STATE);
+ lock_target_state = hap_serv_get_char_by_uuid(lock_service, HAP_CHAR_UUID_LOCK_TARGET_STATE);
- hap_serv_set_write_cb(intercom_lock_service, intercom_lock_write_cb); /* Set the write callback for the service */
+ hap_serv_set_write_cb(lock_service, lock_write_cb);
- intercom_lock_timer = xTimerCreate("intercom_lock_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, intercom_lock_timer_cb);
+ lock_auto_secure_timer = xTimerCreate("lock_auto_secure_timer", pdMS_TO_TICKS(CONFIG_HOMEKIT_INTERCOM_LOCK_TIMEOUT), pdFALSE, 0, lock_auto_secure_timer_cb);
- intercom_lock_gpio_init();
+ lock_gpio_init();
- return intercom_lock_service;
+ return lock_service;
}