From 9f3062340c270880ba2e5df2c592fafb8d51cc44 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Wed, 5 Sep 2018 22:49:23 +0000 Subject: [PATCH] doorlockd, avr: Don't respond on commands No need to read back the AVR. Signed-off-by: Ralf Ramsauer --- avr-code/main.c | 17 ++++-------- doorlockd | 74 +++++++++++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/avr-code/main.c b/avr-code/main.c index e9525da..0ed9de1 100644 --- a/avr-code/main.c +++ b/avr-code/main.c @@ -123,22 +123,23 @@ static void update_state(unsigned char new_state, enum state_source source) switch (source) { case BUTTON: - ret = toupper(ret); + uart_putc(toupper(ret)); break; case EMERGENCY: - ret = 'E'; + uart_putc('E'); break; + case TIMEOUT: + uart_putc(ret); + break; + case COMM: default: break; } - - uart_putc(ret); } ISR(USART_RX_vect) { unsigned char c = UDR; - bool respond = true; switch (c) { case 'r': @@ -150,13 +151,7 @@ ISR(USART_RX_vect) case 'g': update_state(GREEN, COMM); break; - default: - respond = false; - break; } - - if (respond) - uart_putc(c); } ISR(TIMER1_OVF_vect) diff --git a/doorlockd b/doorlockd index 95b5663..8824f78 100755 --- a/doorlockd +++ b/doorlockd @@ -248,56 +248,50 @@ class DoorHandler: self.serial = Serial(device, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=0) self.thread = Thread(target=self.thread_worker) + log.debug('Spawning RS232 Thread') self.thread.start() - def handle_input(self, recv, expect=None): - if recv == DoorHandler.BUTTON_CLOSE: - playsound(wave_lock_button) - if self.state.is_open(): - run_lock() - self.state = DoorState.Closed - logic.emit_status(LogicResponse.ButtonLock) - elif recv == DoorHandler.BUTTON_OPEN: - playsound(wave_unlock_button) - self.state = DoorState.Open - logic.emit_status(LogicResponse.ButtonUnlock) - elif recv == DoorHandler.BUTTON_PRESENT: - # playsound... - self.state = DoorState.Present - logic.emit_status(LogicResponse.ButtonPresent) - elif recv == DoorHandler.CMD_EMERGENCY_SWITCH: - playsound(wave_emergency) - logic.emit_status(LogicResponse.EmergencyUnlock) - - if expect is None: - return True - return recv == expect - - def send_command(self, cmd): - self.serial.write(cmd) - self.serial.flush() - sleep(0.1) - char = self.serial.read(1) - if not self.handle_input(char, cmd): - log.warning('Sent serial command, got wrong response') - def thread_worker(self): while True: sleep(0.4) while True: - char = self.serial.read(1) - if len(char) == 0: + rx = self.serial.read(1) + if len(rx) == 0: break - self.handle_input(char) + if rx == DoorHandler.BUTTON_CLOSE: + self.close() + log.info('Closed due to Button press') + logic.emit_status(LogicResponse.ButtonLock) + elif rx == DoorHandler.BUTTON_OPEN: + self.open() + log.info('Opened due to Button press') + logic.emit_status(LogicResponse.ButtonUnlock) + elif rx == DoorHandler.BUTTON_PRESENT: + self.present() + log.info('Present due to Button press') + logic.emit_status(LogicResponse.ButtonPresent) + elif rx == DoorHandler.CMD_EMERGENCY_SWITCH: + log.warning('Emergency unlock') + logic.emit_status(LogicResponse.EmergencyUnlock) + else: + log.error('Received unknown message "%s" from AVR' % rx) if self.do_close: - self.send_command(DoorHandler.CMD_CLOSE) + tx = DoorHandler.CMD_CLOSE self.do_close = False - elif self.state.is_open(): - if self.state == DoorState.Present: - self.send_command(DoorHandler.CMD_PRESENT) - else: - self.send_command(DoorHandler.CMD_OPEN) + if self.state == DoorState.Present: + tx = DoorHandler.CMD_PRESENT + elif self.state == DoorState.Open: + tx = DoorHandler.CMD_OPEN + else: + tx = None + + if not tx: + continue + + self.serial.write(tx) + self.serial.flush() + sleep(0.1) def open(self): if self.state == DoorState.Open: