diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-29 17:21:31 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-29 17:21:31 +0800 |
| commit | 0398894a39406ead5921df64892e3a1c82cc25b7 (patch) | |
| tree | 58425972a8ed8c72b268c41ce75a23b4e8e1648f /sleep/main.c | |
| parent | a9ce750c420813b1e7dab5eb4e0340d107a34b20 (diff) | |
| download | smart-home-0398894a39406ead5921df64892e3a1c82cc25b7.tar.gz | |
Sleep test.
Diffstat (limited to 'sleep/main.c')
| -rw-r--r-- | sleep/main.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/sleep/main.c b/sleep/main.c new file mode 100644 index 0000000..007dc18 --- /dev/null +++ b/sleep/main.c @@ -0,0 +1,57 @@ +#include <avr/interrupt.h> +#include <avr/sleep.h> +#include <util/delay.h> + +#include "uart.h" + +#define LOCK_PIN PD2 +#define UNLOCK_PIN PD3 + +static inline void init_btns(void) +{ + DDRD &= ~((1 << LOCK_PIN) | (1 << UNLOCK_PIN)); + PORTD |= ((1 << LOCK_PIN) | (1 << UNLOCK_PIN)); + EICRA = 0b00000000; + EIMSK = (1 << INT0) | (1 << INT1); +} + +int main(void) +{ + init_btns(); + uart_init(); + + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sei(); + + for (;;) { + uart_write_line("Going to sleep"); + sleep_enable(); + sleep_bod_disable(); + sleep_cpu(); + sleep_disable(); + uart_write_line("It's alive!"); + _delay_ms(1500); + } + return 0; +} + +static inline int is_btn_pressed(uint8_t pin, uint8_t btn) +{ + if (!((pin >> btn) & 0x01)) { + _delay_ms(100); + return !((pin >> btn) & 0x01); + } + return 0; +} + +ISR(INT0_vect) +{ + if (is_btn_pressed(PIND, LOCK_PIN)) + uart_write_line("Locked"); +} + +ISR(INT1_vect) +{ + if (is_btn_pressed(PIND, UNLOCK_PIN)) + uart_write_line("Unlocked"); +} |
