From a3a63e64d00a6554f935d574358c88afb453b09d Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 7 Jun 2025 14:26:01 +0800 Subject: FPM and the buttons. --- main.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..abc5b1b --- /dev/null +++ b/main.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include + +#include "fpm.h" + +#define FRONT_UNLOCK_PIN PD2 +#define FRONT_LOCK_PIN PD3 +#define ENROLL_PIN PD4 +#define BACK_LOCK_PIN PD5 +#define BACK_UNLOCK_PIN PD6 + +#define INPUT_DDR DDRD +#define INPUT_PORT PORTD + +#define FPM_UNLOCK_INT INT0 +#define FPM_INT_VEC INT0_vect + +#define FRONT_LOCK_INT PCINT19 +#define ENROLL_INT PCINT20 +#define BACK_LOCK_INT PCINT21 +#define BACK_UNLOCK_INT PCINT22 +#define BTN_INT_VEC PCINT2_vect + +int main(void) +{ + /* input ports */ + INPUT_DDR &= ~((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | + (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | + (1 << ENROLL_PIN)); + + INPUT_PORT |= ((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | + (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | + (1 << ENROLL_PIN)); + + EICRA = 0b00000000; + EIMSK = (1 << FPM_UNLOCK_INT); + + PCICR |= (1 << PCIE2); + PCMSK2 |= ((1 << FRONT_LOCK_INT) | (1 << ENROLL_INT) | + (1 << BACK_LOCK_INT) | (1 << BACK_UNLOCK_INT)); + + fpm_init(); + sei(); + + for (;;) { + } + + return 0; +} + +static inline int is_pressed(uint8_t btn) +{ + if (!((PIND >> btn) & 0x01)) { + _delay_ms(50); + return !((PIND >> btn) & 0x01); + } + return 0; +} + +ISR(FPM_INT_VEC) +{ + cli(); + + if (fpm_match()) { + fpm_led(BREATHE, BLUE, 1); + } else { + fpm_led(BREATHE, RED, 1); + } + + sei(); +} + +ISR(BTN_INT_VEC) +{ + uint16_t id; + + cli(); + + if (is_pressed(FRONT_LOCK_PIN)) { + fpm_led(FLASH, RED, 1); + } else if (is_pressed(BACK_LOCK_PIN)) { + } else if (is_pressed(BACK_UNLOCK_PIN)) { + } else if (is_pressed(ENROLL_PIN)) { + id = fpm_match(); + if (id == 1 || id == 2) { + fpm_led(BREATHE, BLUE, 1); + _delay_ms(1000); + if (fpm_enroll()) + fpm_led(BREATHE, BLUE, 1); + } else + fpm_led(BREATHE, RED, 1); + } + + sei(); +} -- cgit v1.2.3