summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-06-14 12:10:08 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-06-14 12:10:08 +0800
commit603876b7bbe5b7530ba345704fa2ed36a714b3c8 (patch)
treef8f35b7cc97a4a150bbc6907ca6b0ad194934a11
parenta4aced6cde7afacc2c045c0c444dcf721189db78 (diff)
downloadfpm-door-lock-603876b7bbe5b7530ba345704fa2ed36a714b3c8.tar.gz
Revert "Spread the buttons out to different ports: no improvement."
This reverts commit a4aced6cde7afacc2c045c0c444dcf721189db78.
-rw-r--r--main.c117
1 files changed, 42 insertions, 75 deletions
diff --git a/main.c b/main.c
index d460012..7329e11 100644
--- a/main.c
+++ b/main.c
@@ -7,6 +7,24 @@
#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
+
#define SERVO_PIN PB1
#define SERVO_DDR DDRB
@@ -44,13 +62,9 @@ uint16_t getvcc(void)
return vcc;
}
-static volatile int is_lock = 0;
-static volatile int is_unlock = 0;
-static volatile int is_sig_led = 0;
-
int main(void)
{
- /* disable WDT */
+ /* disable watchdog timer */
cli();
wdt_reset();
MCUSR &= ~(1 << WDRF);
@@ -61,34 +75,23 @@ int main(void)
LED_DDR |= (1 << LED_PIN);
LED_PORT &= ~(1 << LED_PIN);
- for (int i = 0; i < 4; i++) {
- LED_PORT ^= (1 << LED_PIN);
- _delay_ms(80);
- }
+ /* init input ports */
+ INPUT_DDR &= ~((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) |
+ (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) |
+ (1 << ENROLL_PIN));
- /* PD2: FPM unlock, PD3: outside lock, PD4: FPM enroll */
- DDRD &= ~((1 << PD2) | (1 << PD3) | (1 << PD4));
- PORTD |= (1 << PD2) | (1 << PD3) | (1 << PD4);
+ INPUT_PORT |= ((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) |
+ (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) |
+ (1 << ENROLL_PIN));
- /* internal lock btn */
- DDRB &= ~(1 << PB2);
- PORTB |= (1 << PB2);
-
- /* internal unlock btn */
- DDRC &= ~(1 << PC5);
- PORTC |= (1 << PC5);
-
- /* INT0 and INT1 from PD2 and PD3 */
EICRA = 0b00000000;
- EIMSK = (1 << INT0) | (1 << INT1);
-
- PCICR |= (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
+ EIMSK = (1 << FPM_UNLOCK_INT);
- PCMSK0 |= (1 << PCINT2); /* internal lock int */
- PCMSK1 |= (1 << PCINT13); /* internal unlock int */
- PCMSK2 |= (1 << PCINT20); /* FPM enroll int */
+ PCICR |= (1 << PCIE2);
+ PCMSK2 |= ((1 << FRONT_LOCK_INT) | (1 << ENROLL_INT) |
+ (1 << BACK_LOCK_INT) | (1 << BACK_UNLOCK_INT));
- /* servo */
+ /* init servo */
ICR1 = PWM_TOP;
TCCR1A |= (1 << WGM11) | (1 << COM1A1);
TCCR1B |= (1 << WGM13) | (1 << CS11);
@@ -96,7 +99,6 @@ int main(void)
SERVO_DDR |= (1 << SERVO_PIN);
fpm_init();
-
sei();
for (;;) {
@@ -125,79 +127,44 @@ static inline void unlock(void)
OCR1A = PWM_TOP;
}
-static inline int is_pressed(uint8_t pinx, uint8_t btn)
+static inline int is_pressed(uint8_t btn)
{
- if (!((pinx >> btn) & 0x01)) {
+ if (!((PIND >> btn) & 0x01)) {
_delay_ms(50);
return !((PIND >> btn) & 0x01);
}
return 0;
}
-ISR(INT0_vect)
+ISR(FPM_INT_VEC)
{
- cli();
-
if (fpm_match()) {
unlock();
fpm_led(BREATHE, BLUE, 1);
- } else
+ } else {
fpm_led(BREATHE, RED, 1);
-
- sei();
+ }
}
-ISR(INT1_vect)
+ISR(BTN_INT_VEC)
{
- cli();
+ uint16_t id;
- if (is_pressed(PIND, PD3)) {
+ if (is_pressed(FRONT_LOCK_PIN)) {
lock();
fpm_led(FLASH, RED, 1);
- }
-
- sei();
-}
-
-ISR(PCINT0_vect)
-{
- cli();
-
- if (is_pressed(PINB, PB2))
+ } else if (is_pressed(BACK_LOCK_PIN)) {
lock();
-
- sei();
-}
-
-ISR(PCINT1_vect)
-{
- cli();
-
- if (is_pressed(PINC, PC5))
+ } else if (is_pressed(BACK_UNLOCK_PIN)) {
unlock();
-
- sei();
-}
-
-ISR(PCINT2_vect)
-{
- uint16_t id;
-
- cli();
-
- if (is_pressed(PIND, PD4)) {
+ } 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);
}
-
- sei();
}
-