summaryrefslogtreecommitdiffstats
path: root/lock
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-05-05 11:51:40 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-05-05 11:51:40 +0800
commit605423d76e1f73c5a8590163b841c2db523626f7 (patch)
treec97b89849d8fbf5d6d542db889cfe35d2cc44fe5 /lock
parent96458bdb51704320216335f859bbd55e6594222f (diff)
downloadsmart-home-605423d76e1f73c5a8590163b841c2db523626f7.tar.gz
LEDs working.
Diffstat (limited to 'lock')
-rw-r--r--lock/bend.c36
-rw-r--r--lock/fend.c25
-rw-r--r--lock/util.c43
-rw-r--r--lock/util.h8
4 files changed, 61 insertions, 51 deletions
diff --git a/lock/bend.c b/lock/bend.c
index 823fddd..7e3ce7e 100644
--- a/lock/bend.c
+++ b/lock/bend.c
@@ -32,12 +32,6 @@
#define VCC_MIN 4900
-#define LOCK_LED PC3
-#define UNLOCK_LED PC4
-#define BATLOW_LED PC5
-#define LED_DDR DDRC
-#define LED_PORT PORTC
-
static char tab[] = {
'0', '8', '3', '6', 'a', 'Z', '$', '4', 'v', 'R', '@',
'E', '1', 'o', '#', ')', '2', '5', 'q', ';', '.', 'I',
@@ -99,16 +93,6 @@ static inline void init_btns(void)
EIMSK = (1 << INT0) | (1 << INT1);
}
-static inline void init_leds(void)
-{
- LED_DDR |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
- LED_DDR |= (1 << BATLOW_LED);
-
- LED_PORT &= ~(1 << LOCK_LED);
- LED_PORT &= ~(1 << UNLOCK_LED);
- LED_PORT &= ~(1 << BATLOW_LED);
-}
-
static inline void init_servo(void)
{
ICR1 = PWM_TOP;
@@ -141,11 +125,11 @@ int main(void)
init_wdt();
init_rx();
- init_leds();
init_btns();
init_servo();
uart_init();
+ led_init();
radio_init(rxaddr);
radio_print_config();
@@ -195,13 +179,7 @@ ISR(INT0_vect)
{
if (is_btn_pressed(PIND, LOCK_PIN)) {
lock();
- LED_PORT |= (1 << LOCK_LED);
- _delay_ms(70);
- LED_PORT &= ~(1 << LOCK_LED);
- _delay_ms(70);
- LED_PORT |= (1 << LOCK_LED);
- _delay_ms(70);
- LED_PORT &= ~(1 << LOCK_LED);
+ led_locked();
}
}
@@ -209,18 +187,12 @@ ISR(INT1_vect)
{
if (is_btn_pressed(PIND, UNLOCK_PIN)) {
unlock();
- LED_PORT |= (1 << UNLOCK_LED);
- _delay_ms(70);
- LED_PORT &= ~(1 << UNLOCK_LED);
- _delay_ms(70);
- LED_PORT |= (1 << UNLOCK_LED);
- _delay_ms(70);
- LED_PORT &= ~(1 << UNLOCK_LED);
+ led_unlocked();
}
}
ISR(WDT_vect)
{
if (getvcc() < VCC_MIN)
- LED_PORT ^= (1 << BATLOW_LED);
+ led_bat();
}
diff --git a/lock/fend.c b/lock/fend.c
index 2324dc5..78e42a1 100644
--- a/lock/fend.c
+++ b/lock/fend.c
@@ -26,12 +26,6 @@
#define VCC_MIN 4000
-#define LOCK_LED PC3
-#define UNLOCK_LED PC4
-#define BATLOW_LED PC5
-#define LED_DDR DDRC
-#define LED_PORT PORTC
-
static volatile uint8_t rxd = 0;
static volatile uint8_t sync = 0;
static volatile uint8_t islock = 0;
@@ -62,16 +56,6 @@ static inline void init_btns(void)
EIMSK = (1 << INT0) | (1 << INT1);
}
-static inline void init_leds(void)
-{
- LED_DDR |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
- LED_DDR |= (1 << BATLOW_LED);
-
- LED_PORT &= ~(1 << LOCK_LED);
- LED_PORT &= ~(1 << UNLOCK_LED);
- LED_PORT &= ~(1 << BATLOW_LED);
-}
-
int main(void)
{
int i, retries;
@@ -82,9 +66,9 @@ int main(void)
wdt_off();
uart_init();
+ led_init();
init_rx();
- init_leds();
init_btns();
radio_init(rxaddr);
@@ -118,18 +102,21 @@ int main(void)
if (islock) {
islock = 0;
xor(key, LOCK, buf, WDLEN);
+ if (radio_sendto(txaddr, buf, WDLEN))
+ led_locked();
} else if (isunlock) {
isunlock = 0;
xor(key, UNLOCK, buf, WDLEN);
+ if (radio_sendto(txaddr, buf, WDLEN))
+ led_unlocked();
}
- radio_sendto(txaddr, buf, WDLEN);
}
}
if (!sync) {
if (getvcc() < VCC_MIN) {
for (i = 0; i < 5; i++) {
- LED_PORT ^= (1 << BATLOW_LED);
+ led_bat();
_delay_ms(100);
}
}
diff --git a/lock/util.c b/lock/util.c
index ebcf9fe..5a22ef1 100644
--- a/lock/util.c
+++ b/lock/util.c
@@ -4,6 +4,12 @@
#include "util.h"
+#define LOCK_LED PC3
+#define UNLOCK_LED PC4
+#define BATLOW_LED PC5
+#define LED_DDR DDRC
+#define LED_PORT PORTC
+
int is_btn_pressed(uint8_t pin, uint8_t btn)
{
if (!((pin >> btn) & 0x01)) {
@@ -44,3 +50,40 @@ uint16_t getvcc(void)
ADCSRA &= ~(1 << ADEN);
return vcc;
}
+
+void led_init(void)
+{
+ LED_DDR |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
+ LED_DDR |= (1 << BATLOW_LED);
+
+ LED_PORT &= ~(1 << LOCK_LED);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+ LED_PORT &= ~(1 << BATLOW_LED);
+}
+
+void led_locked(void)
+{
+ LED_PORT |= (1 << LOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << LOCK_LED);
+ _delay_ms(70);
+ LED_PORT |= (1 << LOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << LOCK_LED);
+}
+
+void led_unlocked(void)
+{
+ LED_PORT |= (1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT |= (1 << UNLOCK_LED);
+ _delay_ms(70);
+ LED_PORT &= ~(1 << UNLOCK_LED);
+}
+
+void led_bat(void)
+{
+ LED_PORT ^= (1 << BATLOW_LED);
+}
diff --git a/lock/util.h b/lock/util.h
index 730beae..84584aa 100644
--- a/lock/util.h
+++ b/lock/util.h
@@ -16,4 +16,12 @@ void xor(const char *k, const char *s, char *d, uint8_t n);
uint16_t getvcc(void);
+void led_init(void);
+
+void led_locked(void);
+
+void led_unlocked(void);
+
+void led_bat(void);
+
#endif /* MY_UTIL_H */