Commit b372baa4 authored by Bc. Petr Elexa's avatar Bc. Petr Elexa

panel: finalize door state notification

parent fc55bb07
......@@ -19,6 +19,8 @@
#include <stdio.h>
#include <string.h>
#define USER_REQUEST_WAIT_MS 1000
// Cache entry type mapping to our type.
typedef cache_item_t term_cache_item_t; // 4 bytes
......@@ -43,6 +45,9 @@ static TimerHandle_t _act_timer = NULL;
// Timer ID for master timeout.
static const uint32_t _act_timer_id = TERMINAL_TIMER_ID;
// Last door open(true) / close(false) status
static bool last_door_state[ACS_READER_COUNT] = {false, false};
// Map reader index to correct cache value.
static inline uint8_t map_reader_idx_to_cache(uint8_t reader_idx)
{
......@@ -195,7 +200,7 @@ void term_can_recv(uint8_t msg_obj_num)
}
// Send door status update to server
static void terminal_send_door_status(uint8_t reader_idx, bool open)
static void terminal_send_door_status(uint8_t reader_idx, bool is_open)
{
//check if master online
if (_act_master == ACS_RESERVED_ADDR)
......@@ -211,7 +216,7 @@ static void terminal_send_door_status(uint8_t reader_idx, bool open)
head.fc = FC_DOOR_STATUS;
head.dst = _act_master;
uint8_t status = (open ? DATA_DOOR_STATUS_OPEN : DATA_DOOR_STATUS_CLOSED);
uint8_t status = (is_open ? DATA_DOOR_STATUS_OPEN : DATA_DOOR_STATUS_CLOSED);
if (reader_idx == ACS_READER_A_IDX)
{
......@@ -292,12 +297,22 @@ static void terminal_task(void *pvParameters)
while (true)
{
uint32_t user_id;
uint8_t reader_idx = reader_get_request_from_buffer(&user_id);
uint8_t reader_idx = reader_get_request_from_buffer(&user_id, USER_REQUEST_WAIT_MS);
if (reader_idx < ACS_READER_COUNT)
{
DEBUGSTR("user identified\n");
terminal_user_identified(user_id, reader_idx);
}
// Check if door open/close state changed
for (size_t idx = 0; idx < ACS_READER_COUNT; ++idx)
{
if (reader_is_door_open(idx) != last_door_state[idx])
{
DEBUGSTR("new door state\n");
last_door_state[idx] = !last_door_state[idx];
terminal_send_door_status(idx, last_door_state[idx]);
}
}
}
}
......
......@@ -73,9 +73,9 @@ void set_reader_addr(const uint16_t first_acs_addr);
#define ACS_COMM_STATUS_LED_PORT 0
#define ACS_COMM_STATUS_LED_PIN 6
// Door sensor types.
#define SENSOR_IS_NO 0 // Normally open.
#define SENSOR_IS_NC 1 // Normally closed.
// Door sensor types (reed switch).
#define SENSOR_IS_NO 0 // Normally open (Form A).
#define SENSOR_IS_NC 1 // Normally closed (Form B).
// If DOOR_SENSOR_TYPE is defined enables door sensor
#define DOOR_SENSOR_TYPE SENSOR_IS_NO
......
#include <reader.h>
// Define sensor binary value when door is open.
#if DOOR_SENSOR_TYPE == SENSOR_IS_NC
#define DOOR_SENSOR_VALUE_OPEN LOG_LOW // Switch is closed when door is open.
#else
#define DOOR_SENSOR_VALUE_OPEN LOG_HIGH // Switch is open when door is open.
#endif
#define DOOR_OPEN 1
#define DOOR_CLOSED 0
// buffer for user_id received from RFID reader
// Buffer for user_id received from RFID reader.
static StreamBufferHandle_t _reader_buffer;
static const reader_wiring_t _reader_wiring[ACS_READER_MAXCOUNT] =
......@@ -47,6 +56,7 @@ reader_conf_t reader_conf[ACS_READER_MAXCOUNT] =
.open_time_sec = ACS_READER_A_OPEN_TIME_MS,
.gled_time_sec = ACS_READER_A_OK_GLED_TIME_MS,
.enabled = ACS_READER_A_ENABLED,
.door_open = DOOR_CLOSED
},
{
.timer_ok = NULL,
......@@ -54,6 +64,7 @@ reader_conf_t reader_conf[ACS_READER_MAXCOUNT] =
.open_time_sec = ACS_READER_B_OPEN_TIME_MS,
.gled_time_sec = ACS_READER_B_OK_GLED_TIME_MS,
.enabled = ACS_READER_B_ENABLED,
.door_open = DOOR_CLOSED
}
};
......@@ -160,13 +171,13 @@ void reader_deinit(uint8_t idx)
weigand_disable(_reader_wiring[idx].data_port, _reader_wiring[idx].d0_pin, _reader_wiring[idx].d1_pin);
}
int8_t reader_get_request_from_buffer(uint32_t * user_id)
int8_t reader_get_request_from_buffer(uint32_t * user_id, uint16_t time_to_wait_ms)
{
weigand26_buff_item_t item;
size_t bytes_got;
// Suspend if empty
bytes_got = xStreamBufferReceive(_reader_buffer, &item, WEIGAND26_BUFF_ITEM_SIZE, pdMS_TO_TICKS(portMAX_DELAY));
bytes_got = xStreamBufferReceive(_reader_buffer, &item, WEIGAND26_BUFF_ITEM_SIZE, pdMS_TO_TICKS(time_to_wait_ms));
if (bytes_got == WEIGAND26_BUFF_ITEM_SIZE && weigand_is_parity_ok(item.frame))
{
......@@ -197,20 +208,33 @@ void reader_unlock(uint8_t idx, bool with_beep, bool with_ok_led)
configASSERT(xTimerStart(reader_conf[idx].timer_ok, 0));
}
// Sensor interrupt handler
bool reader_is_door_open(uint8_t reader_idx)
{
return reader_conf[reader_idx].door_open == DOOR_OPEN;
}
// Sensor interrupt handler.
void reader_sensor_int_handler(uint8_t port, uint32_t int_states)
{
if (_reader_wiring[ACS_READER_A_IDX].sensor_port != port &&
_reader_wiring[ACS_READER_B_IDX].sensor_port != port)
{
return;
}
// door sensor A
if (int_states & (1 << _reader_wiring[ACS_READER_A_IDX].sensor_pin))
{
// TODO inform terminal
// Lock door after open
Chip_GPIO_SetPinState(LPC_GPIO, _reader_wiring[ACS_READER_A_IDX].relay_port, _reader_wiring[ACS_READER_A_IDX].relay_pin, LOG_HIGH);
// update current state
uint8_t sensor_value = Chip_GPIO_ReadPortBit(LPC_GPIO, port, _reader_wiring[ACS_READER_A_IDX].sensor_pin);
reader_conf[ACS_READER_A_IDX].door_open = (sensor_value == DOOR_SENSOR_VALUE_OPEN ? DOOR_OPEN : DOOR_CLOSED);
}
// door sensor B
if (int_states & (1 << _reader_wiring[ACS_READER_B_IDX].sensor_pin))
{
// TODO inform terminal
// Lock door after open
Chip_GPIO_SetPinState(LPC_GPIO, _reader_wiring[ACS_READER_B_IDX].relay_port, _reader_wiring[ACS_READER_B_IDX].relay_pin, LOG_HIGH);
// update current state
uint8_t sensor_value = Chip_GPIO_ReadPortBit(LPC_GPIO, port, _reader_wiring[ACS_READER_B_IDX].sensor_pin);
reader_conf[ACS_READER_B_IDX].door_open = (sensor_value == DOOR_SENSOR_VALUE_OPEN ? DOOR_OPEN : DOOR_CLOSED);
}
}
......
......@@ -6,6 +6,7 @@
#define BSP_READER_H_
#include <stdint.h>
#include <stdbool.h>
#include "board.h"
#include "FreeRTOS.h"
#include "stream_buffer.h"
......@@ -26,6 +27,7 @@ typedef struct
uint16_t open_time_sec;
uint16_t gled_time_sec;
uint8_t enabled;
uint8_t door_open;
} reader_conf_t;
typedef struct
......@@ -53,9 +55,10 @@ void reader_init(uint8_t idx);
void reader_deinit(uint8_t idx);
// user_id is 24-bit number
int8_t reader_get_request_from_buffer(uint32_t * user_id);
int8_t reader_get_request_from_buffer(uint32_t * user_id, uint16_t time_to_wait_ms);
void reader_unlock(uint8_t idx, bool with_beep, bool with_ok_led);
bool reader_is_door_open(uint8_t reader_idx);
#endif /* BSP_READER_H_ */
Markdown is supported
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