summaryrefslogtreecommitdiffstats
path: root/door_lock
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-11-06 17:57:09 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-11-06 17:57:09 +0800
commite6c41b84b88596d130de98919480afdb0dce3fcf (patch)
treee65ec4dd6918057854ab833edfd6f51504bb2af6 /door_lock
parent617474e23500edf91db1c35fc89d49f52a42afe7 (diff)
downloadsmart-home-e6c41b84b88596d130de98919480afdb0dce3fcf.tar.gz
Code clean up.
Diffstat (limited to 'door_lock')
-rw-r--r--door_lock/cmd.c7
-rw-r--r--door_lock/cmd.h4
-rw-r--r--door_lock/serial.c1
-rw-r--r--door_lock/servo.c53
4 files changed, 36 insertions, 29 deletions
diff --git a/door_lock/cmd.c b/door_lock/cmd.c
index b757e33..fb9ae9c 100644
--- a/door_lock/cmd.c
+++ b/door_lock/cmd.c
@@ -1,4 +1,3 @@
-#include <stdint.h>
#include <string.h>
#include "cmd.h"
@@ -11,7 +10,7 @@
static char cmd[XORLEN];
-static inline void xor(const char *s, char *d, uint8_t n)
+static inline void xor(const char *s, char *d, int n)
{
int i;
@@ -19,7 +18,7 @@ static inline void xor(const char *s, char *d, uint8_t n)
d[i] = s[i] ^ KEY[i];
}
-int is_valid_cmd(const char *s, enum command c)
+int cmd_cmp(const char *s, enum command c)
{
int rc;
char buf[XORLEN + 1];
@@ -42,7 +41,7 @@ int is_valid_cmd(const char *s, enum command c)
return rc;
}
-char * get_cmd_hash(enum command c)
+char * cmd_hash(enum command c)
{
switch (c) {
case DOOR_LOCK:
diff --git a/door_lock/cmd.h b/door_lock/cmd.h
index c31f197..ec21ed6 100644
--- a/door_lock/cmd.h
+++ b/door_lock/cmd.h
@@ -6,8 +6,8 @@ enum command {
DOOR_UNLOCK
};
-int is_valid_cmd(const char *s, enum command c);
+int cmd_cmp(const char *s, enum command c);
-char * get_cmd_hash(enum command c);
+char * cmd_hash(enum command c);
#endif /* SA_CMD_H */
diff --git a/door_lock/serial.c b/door_lock/serial.c
index 8a6cde2..0b9aecf 100644
--- a/door_lock/serial.c
+++ b/door_lock/serial.c
@@ -27,6 +27,7 @@ void serial_write_line(const char *s)
{
for (; *s; s++)
serial_write(*s);
+
serial_write('\r');
serial_write('\n');
}
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();
}