diff options
Diffstat (limited to 'door_lock/servo.c')
| -rw-r--r-- | door_lock/servo.c | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/door_lock/servo.c b/door_lock/servo.c index 3e78353..40e5ed7 100644 --- a/door_lock/servo.c +++ b/door_lock/servo.c @@ -1,5 +1,3 @@ -#include <stdint.h> - #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> @@ -11,20 +9,23 @@ #define PWM_MID 3000 #define PWM_MAX 5000 -#define SERVO_PIN PB1 -#define LOCK_BTN PD6 -#define ULOCK_BTN PD7 +#define SERVO_PIN PB1 +#define LOCK_BTN PD6 +#define UNLOCK_BTN PD7 -static inline void servo_init(void) +static inline void lock(void) { - DDRB |= 1 << SERVO_PIN; - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11); + OCR1A = PWM_MID; +} - ICR1 = 40000; +static inline void unlock(void) +{ + OCR1A = PWM_MIN; +} - DDRD &= ~((1 << LOCK_BTN) | (1 << ULOCK_BTN)); - PORTD |= (1 << LOCK_BTN) | (1 << ULOCK_BTN); +static inline int is_btn_pressed(unsigned char btn) +{ + return !((PIND >> btn) & 0x01); } static inline void pcint2_init(void) @@ -33,14 +34,21 @@ static inline void pcint2_init(void) PCMSK2 |= ((1 << PCINT22) | (1 << PCINT23)); } -static inline uint8_t is_btn_pressed(uint8_t btn) +static inline void servo_init(void) { - return !((PIND >> btn) & 0x01); + DDRB |= 1 << SERVO_PIN; + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11); + + ICR1 = 40000; + + DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN)); + PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); } int main(void) { - char *cmd; + char *s; servo_init(); pcint2_init(); @@ -49,12 +57,11 @@ int main(void) sei(); for(;;) { - cmd = get_cmd_hash(DOOR_UNLOCK); - - if (is_valid_cmd(cmd, DOOR_UNLOCK)) - serial_write_line("unlock door"); + s = cmd_hash(DOOR_UNLOCK); + if (cmd_cmp(s, DOOR_UNLOCK)) + serial_write_line("unlock"); else - serial_write_line("do not unlock door"); + serial_write_line("do not unlock"); _delay_ms(1000); } @@ -65,8 +72,8 @@ int main(void) ISR(PCINT2_vect) { if (is_btn_pressed(LOCK_BTN)) - OCR1A = PWM_MID; + lock(); - if (is_btn_pressed(ULOCK_BTN)) - OCR1A = PWM_MIN; + if (is_btn_pressed(UNLOCK_BTN)) + unlock(); } |
