summaryrefslogtreecommitdiffstats
path: root/lock
diff options
context:
space:
mode:
Diffstat (limited to 'lock')
-rw-r--r--lock/Bend.Makefile3
-rw-r--r--lock/Fend.Makefile3
-rw-r--r--lock/bend.c22
-rw-r--r--lock/fend.c6
-rw-r--r--lock/nrfm.c4
5 files changed, 18 insertions, 20 deletions
diff --git a/lock/Bend.Makefile b/lock/Bend.Makefile
index a88257e..4b5b222 100644
--- a/lock/Bend.Makefile
+++ b/lock/Bend.Makefile
@@ -1,5 +1,6 @@
CC = avr-gcc
MCU = atmega328p
+PORT = /dev/cuaU1
TARGET = bend
SRC = bend.c uart.c nrfm.c util.c
@@ -21,7 +22,7 @@ HEX_FLAGS += -j .text -j .data
AVRDUDE_FLAGS = -p $(MCU)
AVRDUDE_FLAGS += -c arduino
-AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -P $(PORT)
AVRDUDE_FLAGS += -D -U
%.o: %.c
diff --git a/lock/Fend.Makefile b/lock/Fend.Makefile
index a525854..e478e47 100644
--- a/lock/Fend.Makefile
+++ b/lock/Fend.Makefile
@@ -1,5 +1,6 @@
CC = avr-gcc
MCU = atmega328p
+PORT = /dev/cuaU1
TARGET = fend
SRC = fend.c uart.c nrfm.c util.c
@@ -21,7 +22,7 @@ HEX_FLAGS += -j .text -j .data
AVRDUDE_FLAGS = -p $(MCU)
AVRDUDE_FLAGS += -c arduino
-AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -P $(PORT)
AVRDUDE_FLAGS += -D -U
%.o: %.c
diff --git a/lock/bend.c b/lock/bend.c
index 759ba9e..d4e936d 100644
--- a/lock/bend.c
+++ b/lock/bend.c
@@ -1,6 +1,7 @@
/* Lock back, connected to the servo */
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#include <avr/interrupt.h>
@@ -86,27 +87,14 @@ static inline void init_btns(void)
static inline void init_servo(void)
{
- DDRB |= (1 << SERVO_PIN);
-
- TCCR1A |= (1 << WGM11) | (1 << COM1A1);
- TCCR1B |= (1 << WGM13) | (1 << CS11);
- ICR1 = PWM_TOP;
}
static inline void lock(void)
{
- OCR1A = PWM_MID;
- _delay_ms(100);
- OCR1A = PWM_TOP;
- uart_write_line("locked");
}
static inline void unlock(void)
{
- OCR1A = PWM_MAX - 50;
- _delay_ms(100);
- OCR1A = PWM_TOP;
- uart_write_line("unlocked");
}
int main(void)
@@ -115,6 +103,7 @@ int main(void)
uint8_t rxaddr[ADDRLEN] = { 194, 178, 83 };
uint8_t txaddr[ADDRLEN] = { 194, 178, 82 };
+ char s[3]; s[2] = 0;
char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1];
init_rx();
@@ -137,12 +126,19 @@ int main(void)
if (rxd) {
n = radio_recv(buf, WDLEN);
buf[n] = '\0';
+ uart_write("recv: ");
+ uart_write(buf);
+ itoa(n, s, 10);
+ uart_write(s);
+ uart_write_line(" bytes");
if (!sync) {
xor(KEY, buf, msg, WDLEN);
if (strncmp(msg, SYN, WDLEN) == 0) {
keygen(key, WDLEN + 1);
xor(KEY, key, buf, WDLEN);
sync = radio_sendto(txaddr, buf, WDLEN);
+ } else {
+ uart_write_line("not syn");
}
} else {
sync = 0;
diff --git a/lock/fend.c b/lock/fend.c
index 2215ec5..079b23d 100644
--- a/lock/fend.c
+++ b/lock/fend.c
@@ -63,13 +63,13 @@ int main(void)
for (;;) {
if ((islock || isunlock) && !sync) {
- sync = 1;
xor(KEY, SYN, buf, WDLEN);
do {
sync = radio_sendto(txaddr, buf, WDLEN);
_delay_ms(10);
} while (!sync);
sync = 1;
+ uart_write_line("sent syn");
}
if (rxd) {
@@ -77,6 +77,7 @@ int main(void)
n = radio_recv(buf, WDLEN);
buf[n] = '\0';
xor(KEY, buf, key, WDLEN);
+ uart_write_line("read key");
if (islock) {
islock = 0;
xor(key, LOCK, buf, WDLEN);
@@ -86,6 +87,7 @@ int main(void)
}
radio_sendto(txaddr, buf, WDLEN);
sync = 0;
+ uart_write_line("sent command");
} else {
radio_flush_rx();
}
@@ -104,12 +106,10 @@ ISR(INT0_vect)
{
if (is_btn_pressed(PIND, LOCK_PIN))
islock = 1;
- uart_write_line(" start lock");
}
ISR(INT1_vect)
{
if (is_btn_pressed(PIND, UNLOCK_PIN))
isunlock = 1;
- uart_write_line("start unlock");
}
diff --git a/lock/nrfm.c b/lock/nrfm.c
index 7a88d44..5da4937 100644
--- a/lock/nrfm.c
+++ b/lock/nrfm.c
@@ -319,13 +319,13 @@ uint8_t radio_recv(char *buf, uint8_t n)
return 0;
}
- readmax = (n - 1) < pdlen ? (n - 1) : pdlen;
+ readmax = n < pdlen ? n : pdlen;
SPI_PORT &= ~(1 << SPI_SS);
SPDR = 0b01100001;
while (!(SPSR & (1 << SPIF)))
;
- for (readlen = 0; readlen <= readmax; readlen++) {
+ for (readlen = 0; readlen < readmax; readlen++) {
SPDR = NOP;
while (!(SPSR & (1 << SPIF)))
;