Last active
April 29, 2023 03:33
-
-
Save arodland/d4f3ba7b5de236cbfacfb609f95c194e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
2023-04-06 | |
Amelia Vlahogiannis [AG2V] | |
WVTC RFID Access System | |
Uses UART to monitor an RDM6300 and trigger a relay to open the door | |
*/ | |
#include "rdm6300.c" | |
#include <stdio.h> | |
#include "pico/stdlib.h" | |
#include "hardware/uart.h" | |
#include "hardware/timer.h" | |
#define BUFFER_SIZE 14 | |
#define SN_LENGTH 14 | |
#define UART_ID uart0 | |
#define BAUD_RATE 9600 //RDM6300 outputs 9600 baud | |
#define UART_TX_PIN 0 | |
#define UART_RX_PIN 1 | |
#define LOCKED 0 | |
#define UNLOCK 1 | |
#define OPEN 2 | |
#define DOOR_PIN 15 | |
void on_uart_rx(void); | |
void door_handler(int state); | |
int check_card_sn(unsigned char *card_id); | |
int hex_to_int(unsigned char *hex_string); | |
char read_buffer[BUFFER_SIZE]; //stores the card sn | |
uint8_t ch; | |
int i, x; | |
int door_state; | |
int card; | |
absolute_time_t t0, t1; | |
char accepted_cards[2][2] = {{0x1C, 0x00, 0x9C, 0xD4, 0x4D}, {0x1C, 0x00, 0x9C, 0xAF, 0xDB}}; | |
int main() { | |
uart_init(UART_ID, BAUD_RATE); | |
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); | |
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART); | |
irq_set_exclusive_handler(UART0_IRQ, on_uart_rx); | |
irq_set_enabled(UART0_IRQ, true); | |
uart_set_irq_enables(UART_ID, true, false); | |
gpio_init(DOOR_PIN); | |
gpio_set_dir(DOOR_PIN, GPIO_OUT); | |
gpio_put(DOOR_PIN, 0); | |
gpio_init(7); | |
gpio_set_dir(7, GPIO_OUT); | |
gpio_put(7, 1); | |
gpio_init(25); | |
gpio_set_dir(25, GPIO_OUT); | |
gpio_put(25, 1); | |
for(;;) { | |
//int check_card_id(char *read_buffer); returns a 1 or 0 for valid or no | |
door_handler(door_state); | |
} | |
} | |
void on_uart_rx(void) { | |
i = 0; | |
while(uart_is_readable(UART_ID) && i < SN_LENGTH) { | |
read_buffer[i] = uart_getc(UART_ID); | |
i++; | |
} | |
read_buffer[i] = '\0'; | |
x = 0; | |
while(x < i) { | |
if(uart_is_writable(UART_ID)){ | |
uart_putc(UART_ID, read_buffer[x]); | |
} | |
x++; | |
} | |
card = hex_to_int(&read_buffer); | |
} | |
void door_handler(int state) { | |
switch(state) { | |
case LOCKED : | |
gpio_put(DOOR_PIN, 0); | |
break; | |
case UNLOCK: | |
gpio_put(DOOR_PIN, 1); | |
//timer = 0; | |
door_state = OPEN; | |
break; | |
case OPEN : | |
gpio_put(DOOR_PIN, 1); | |
//if(timer > 5000) door state = LOCKED; | |
break; | |
//case UNLATCHED : //for the open/closed sensor when implemented | |
} | |
} | |
int hex_to_int(unsigned char *hex_string) { | |
int result = 0; | |
int i = 0; | |
while (hex_string[i] != '\0') { | |
int digit = hex_string[i] - '0'; | |
if (digit > 9) { | |
digit = hex_string[i] - 'A' + 10; | |
} | |
result = result * 16 + digit; | |
i++; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment