From fdfeabbcaa62c2a5b5235b2c148a363b4876f993 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sun, 18 Dec 2016 21:34:59 +0200 Subject: [PATCH] lab06 functionality done --- src/hmi_msg.h | 4 ++-- src/main.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/rfid.c | 2 +- src/rfid.h | 1 + 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/hmi_msg.h b/src/hmi_msg.h index 3b83739..6ea81fa 100644 --- a/src/hmi_msg.h +++ b/src/hmi_msg.h @@ -21,9 +21,9 @@ #define READ_CMD "read" #define READ_HELP "Read and print out card info that is currently in proximity of the reader" #define ADD_CMD "add" -#define ADD_HELP "Add a new card to the system" +#define ADD_HELP "Add a new card to the system. Usage: add " #define REMOVE_CMD "remove" -#define REMOVE_HELP "Remove a card from the system" +#define REMOVE_HELP "Remove a card from the system. Usage: remove " #define LIST_CMD "list" #define LIST_HELP "List all added cards" diff --git a/src/main.c b/src/main.c index 882ff50..88d6641 100644 --- a/src/main.c +++ b/src/main.c @@ -13,13 +13,17 @@ #include "../lib/helius_microrl/microrl.h" #include "cli_microrl.h" #include "../lib/matejx_avr_lib/mfrc522.h" +#include "rfid.h" #define BAUDRATE 9600 // For configuring arduino mega pin 25 -#define LED_INIT DDRA |= _BV(DDA3); +#define LED_INIT DDRA |= _BV(DDA3) +#define DOOR_INIT DDRA |= _BV(DDA1) #define LED_TOGGLE PORTA ^= _BV(PORTA3) +#define DOOR_OPEN PORTA |= _BV(PORTA1) +#define DOOR_CLOSE PORTA &= ~_BV(PORTA1) #define UART_STATUS_MASK 0x00FF // Current system time @@ -59,8 +63,11 @@ static inline void init_rfid_reader(void) static inline void init_hw (void) { // IO init - /// Set arduino pin 25 as output + // Set arduino pin 25 as output LED_INIT; + + // Set Arduino pin 23 as output + DOOR_INIT; // System clock init_system_clock(); @@ -112,6 +119,48 @@ static inline void heartbeat (void) LED_TOGGLE; } +static inline void handle_door() { + Uid uid; + card_t card; + uint32_t time_cur = time(); + static uint32_t message_start; + static uint32_t door_open_start; + if (PICC_IsNewCardPresent()) { + PICC_ReadCardSerial(&uid); + card.uid_size = uid.size; + memcpy(&card.uid, &uid.uidByte, uid.size); + card.user = NULL; + card_t *found_card = rfid_find_card(&card); + if (found_card) { + lcd_goto(0x40); + lcd_puts(found_card->user); + for (int8_t i=16-strlen(found_card->user); i > -1; i--) { + lcd_putc(' '); + } + DOOR_OPEN; + } else { + DOOR_CLOSE; + lcd_goto(0x40); + lcd_puts("Access denied!"); + for (int8_t i=4; i > -1; i--) { + lcd_putc(' '); + } + } + door_open_start = time_cur; + message_start = time_cur; + } + + if ((message_start+5) < time_cur) { + lcd_goto(0x40); + for (int8_t i=16; i > -1; i--) { + lcd_putc(' '); + } + } + + if ((door_open_start+2) < time_cur) { + DOOR_CLOSE; + } +} int main (void) { @@ -123,6 +172,7 @@ int main (void) heartbeat(); // CLI commands are handled in cli_execute() microrl_insert_char (prl, cli_get_char()); + handle_door(); } } diff --git a/src/rfid.c b/src/rfid.c index 1edb9eb..e4cd58c 100644 --- a/src/rfid.c +++ b/src/rfid.c @@ -22,7 +22,7 @@ card_t* rfid_find_card(const card_t *card) while (current != NULL) { if ((current->uid_size != card->uid_size) || !memcmp(current->uid, card->uid, current->uid_size) || - !strcmp(current->user, card->user)) { + ((card->user != NULL) && !strcmp(current->user, card->user))) { return current; } diff --git a/src/rfid.h b/src/rfid.h index 29f54ef..aed583a 100644 --- a/src/rfid.h +++ b/src/rfid.h @@ -9,6 +9,7 @@ typedef struct card { } card_t; extern card_t *head; +extern card_t* rfid_find_card(const card_t *card); extern void rfid_add_card(const card_t *card); extern void rfid_list_cards(void); extern void rfid_remove_card_by_user(const char *user);