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