Commit f8c4f74e authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add code for the latch module

parent d5341eea
......@@ -8,6 +8,7 @@ extern "C" {
#define WIFI_CONNECTED_EVENT DOOR_BIT0
#define TIME_SYNCED_EVENT DOOR_BIT1
#define DOOR_OPEN_MODE_EVENT DOOR_BIT2
#define DOOR_LATCH_EVENT DOOR_BIT3
#ifdef __cplusplus
}
......
......@@ -4,16 +4,6 @@
extern "C" {
#endif
// Define keypad pins
//#define ROW0_PIN 14
//#define ROW1_PIN 12
//#define ROW2_PIN 13
//#define ROW3_PIN 15
//
//#define COL0_PIN 4
//#define COL1_PIN 5
//#define COL2_PIN 18
/**
* @brief callback for a keypress event. Passes in the key pressed or released.
*/
......
#ifndef DOOR_CONTROLLER_MAIN_INCLUDE_APP_LATCH_H_
#define DOOR_CONTROLLER_MAIN_INCLUDE_APP_LATCH_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define LATCH_CHECK_PERIOD_MS 100
typedef enum {
DOOR_LATCH_CLOSED = 0,
DOOR_LATCH_OPENED = 1,
} door_latch_state_t;
/**
* @brief Function is triggered when the door opens or closes
* and takes the state the door currently is in as well as how many
* latch check periods have gone by (currently 1/10'th of a second)
* since it was last opened/closed/when the thread booted
*/
typedef void (* door_latch_event_cb)(door_latch_state_t state, uint64_t);
typedef void* door_latch_event_handle_t;
door_latch_event_handle_t door_latch_register_watch(door_latch_event_cb);
void door_latch_unregister_watch(door_latch_event_handle_t);
void door_latch_watch_task(void*);
void door_latch_stop(void);
void door_latch_initialize(void);
#ifdef __cplusplus
}
#endif
#endif //DOOR_CONTROLLER_MAIN_INCLUDE_APP_LATCH_H_
......@@ -4,9 +4,6 @@
extern "C" {
#endif
// Hardware pin that the lock is on
//#define DOOR_LOCK_PIN 21
/**
* @brief initializes the door lock module
*/
......
......@@ -9,6 +9,7 @@ extern "C" {
#include <stdint.h>
#include <stddef.h>
#define DOOR_BIT3 ((unsigned)0x00000008)
#define DOOR_BIT2 ((unsigned)0x00000004)
#define DOOR_BIT1 ((unsigned)0x00000002)
#define DOOR_BIT0 ((unsigned)0x00000001)
......
......@@ -32,7 +32,7 @@
#define BEGIN_KEYPAD_CHECK for (unsigned r = 0; r < ROWS; ++r) door_gpio_set_output_pin(key_row_pins[r], DOOR_GPIO_LOW)
#define END_KEYPAD_CHECK for (unsigned r = 0; r < ROWS; ++r) door_gpio_set_output_pin(key_row_pins[r], DOOR_GPIO_HIGH)
door_list_node_t key_handle_list_head;
static door_list_node_t key_handle_list_head;
static const int key_row_pins[ROWS] = {
KEYPAD_ROW0_PIN,
......@@ -214,7 +214,8 @@ void door_keypad_unregister_callback(door_keypad_event_handle_t handle)
void door_keypad_initialize(void)
{
mutex = door_rtos_mutex_create();
if (!mutex)
mutex = door_rtos_mutex_create();
door_keypad_scan_stop = false;
door_gpio_configure_input_pins_pull_down(KEYPAD_COL_PIN_MASK);
......
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
#include "app/latch.h"
#include "app/list.h"
#include "app/event.h"
#include "connectors/rtos.h"
#include "connectors/gpio.h"
#define LATCH_HANDLE_LIST_WAIT_MS 50
static mutex_handle_t latch_handle_list_guard;
static door_list_node_t latch_handle_list_head;
static bool done;
door_latch_event_handle_t door_latch_register_watch(door_latch_event_cb cb)
{
door_list_node_t handle = NULL;
BEGIN_PROTECT_RESOURCE(latch_handle_list_guard, LATCH_HANDLE_LIST_WAIT_MS);
handle = door_list_add(&latch_handle_list_head, &cb, sizeof(door_latch_event_cb));
END_PROTECT_RESOURCE(latch_handle_list_guard);
return handle;
}
void door_latch_unregister_watch(door_latch_event_handle_t handle)
{
BEGIN_PROTECT_RESOURCE(latch_handle_list_guard, LATCH_HANDLE_LIST_WAIT_MS);
door_list_remove(&latch_handle_list_head, handle);
END_PROTECT_RESOURCE(latch_handle_list_guard);
}
void door_latch_watch_task(void* pvParameters)
{
(void)pvParameters;
door_latch_initialize();
uint64_t time_counter = 0;
door_latch_state_t prev_state = DOOR_LATCH_CLOSED;
do
{
door_latch_state_t next_state;
door_rtos_task_delay(LATCH_CHECK_PERIOD_MS);
++time_counter;
if ((next_state = (door_latch_state_t)door_gpio_get_input_pin(DOOR_LATCH_PIN)))
door_rtos_event_set(DOOR_LATCH_EVENT);
else
door_rtos_event_clear(DOOR_LATCH_EVENT);
// We have changed states!
if (next_state != prev_state)
{
BEGIN_PROTECT_RESOURCE(latch_handle_list_guard, LATCH_HANDLE_LIST_WAIT_MS);
door_list_node_t tmp = latch_handle_list_head;
while (tmp)
{
door_latch_event_cb cb = *(door_latch_event_cb*)tmp->data;
cb(next_state, time_counter);
tmp = tmp->next;
}
END_PROTECT_RESOURCE(latch_handle_list_guard);
time_counter = 0;
}
prev_state = next_state;
} while (!done);
}
void door_latch_stop(void)
{
done = true;
}
void door_latch_initialize(void)
{
if (!latch_handle_list_guard)
latch_handle_list_guard = door_rtos_mutex_create();
door_gpio_configure_input_pins_pull_down(PIN_MASK(DOOR_LATCH_PIN));
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment