diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-06-14 15:57:09 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-06-14 15:57:09 +0800 |
| commit | d1c2274e12749be2db3096a29ee1a94b20d445ce (patch) | |
| tree | e7151c33779c79058777530c67bdb45fcb4acb7d | |
| parent | 0ed0e93d5a5ac88bc7c299e7f16b5e68bd70a66e (diff) | |
| download | fpm-door-lock-d1c2274e12749be2db3096a29ee1a94b20d445ce.tar.gz | |
Handle irq in main().
| -rw-r--r-- | main.c | 128 |
1 files changed, 80 insertions, 48 deletions
@@ -39,9 +39,36 @@ #define LED_DDR DDRB #define LED_PORT PORTB -/* Measure vcc by measuring known internal 1.1v bandgap - * reference voltage against AVCC. - */ +static volatile int frnt_lock = 0; +static volatile int back_lock = 0; +static volatile int frnt_unlock = 0; +static volatile int back_unlock = 0; + +enum { + NONE = 0, + FLOCK = 1, + BLOCK = 2, + FUNLOCK = 3, + BUNLOCK = 4, + ENROLL = 5 +}; + +static volatile int cmd = NONE; + +static inline void lock(void) +{ + OCR1A = PWM_MID; + _delay_ms(100); + OCR1A = PWM_TOP; +} + +static inline void unlock(void) +{ + OCR1A = PWM_MAX; + _delay_ms(100); + OCR1A = PWM_TOP; +} + uint16_t getvcc(void) { uint16_t vcc; @@ -62,22 +89,10 @@ uint16_t getvcc(void) return vcc; } -static inline void lock(void) -{ - OCR1A = PWM_MID; - _delay_ms(100); - OCR1A = PWM_TOP; -} - -static inline void unlock(void) -{ - OCR1A = PWM_MAX; - _delay_ms(100); - OCR1A = PWM_TOP; -} - int main(void) { + uint16_t id; + /* disable watchdog timer */ cli(); wdt_reset(); @@ -123,9 +138,48 @@ int main(void) sei(); for (;;) { + cli(); + + switch(cmd) { + case FLOCK: + lock(); + fpm_led(FLASH, RED, 1); + break; + case BLOCK: + lock(); + break; + case FUNLOCK: + if (fpm_match()) { + unlock(); + fpm_led(BREATHE, BLUE, 1); + } else { + fpm_led(BREATHE, RED, 1); + } + break; + case BUNLOCK: + unlock(); + break; + case ENROLL: + 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); + } + break; + default: + break; + } + + cmd = 0; + if (getvcc() < VCC_MIN) LED_PORT |= (1 << LED_PIN); + sei(); sleep_bod_disable(); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); @@ -146,44 +200,22 @@ static inline int is_pressed(uint8_t btn) ISR(FPM_INT_VEC) { cli(); - - if (fpm_match()) { - unlock(); - fpm_led(BREATHE, BLUE, 1); - } else { - fpm_led(BREATHE, RED, 1); - } - + cmd = FUNLOCK; sei(); } ISR(BTN_INT_VEC) { - uint16_t id; - cli(); - if (is_pressed(FRONT_LOCK_PIN)) { - lock(); - fpm_led(FLASH, RED, 1); - } else if (is_pressed(BACK_LOCK_PIN)) { - lock(); - fpm_led(FLASH, RED, 1); - } else if (is_pressed(BACK_UNLOCK_PIN)) { - unlock(); - fpm_led(FLASH, BLUE, 1); - } 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); - } else - fpm_led(BREATHE, RED, 1); - } + if (is_pressed(FRONT_LOCK_PIN)) + cmd = FLOCK; + else if (is_pressed(BACK_LOCK_PIN)) + cmd = BLOCK; + else if (is_pressed(BACK_UNLOCK_PIN)) + cmd = BUNLOCK; + else if (is_pressed(ENROLL_PIN)) + cmd = ENROLL; sei(); } |
