From c5b29bdee20c9d9301b9876ef6ec7d4cefcef70a Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Wed, 21 Apr 2021 23:22:11 +0200 Subject: [PATCH] Moved VFD buttons to separate module --- display/firmware/Makefile | 2 +- display/firmware/button.c | 25 +++++++++++++++++++++++++ display/firmware/button.h | 5 +++++ display/firmware/main.c | 25 +++---------------------- 4 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 display/firmware/button.c create mode 100644 display/firmware/button.h diff --git a/display/firmware/Makefile b/display/firmware/Makefile index 9532ff5..f4f3e14 100644 --- a/display/firmware/Makefile +++ b/display/firmware/Makefile @@ -1,7 +1,7 @@ PRG = display PROJ_ROOT = ../.. AVRCANLIB_PATH = $(PROJ_ROOT)/avr-can-lib -OBJ = main.o display.o \ +OBJ = main.o display.o button.o \ $(AVRCANLIB_PATH)/src/at90can_buffer.o \ $(AVRCANLIB_PATH)/src/at90can.o \ $(AVRCANLIB_PATH)/src/at90can_disable_dyn_filter.o \ diff --git a/display/firmware/button.c b/display/firmware/button.c new file mode 100644 index 0000000..9c48e45 --- /dev/null +++ b/display/firmware/button.c @@ -0,0 +1,25 @@ +#include +#include "button.h" + +void button_init() { + // PB2, PB3, PB4, PB5, PB6, PC0, PC1, PC4: input, no pullup (button0..7) + PORTB &= ~(_BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5) | _BV(PB6)); + DDRB &= ~(_BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5) | _BV(PB6)); + PORTC &= ~(_BV(PC0) | _BV(PC1) | _BV(PC4)); + DDRC &= ~(_BV(PC0) | _BV(PC1) | _BV(PC4)); +} + +uint8_t button_get() { + uint8_t pinb = PINB; + uint8_t pinc = PINC; + uint8_t buttons = 0; + if (pinb & _BV(PB2)) buttons |= _BV(0); + if (pinb & _BV(PB3)) buttons |= _BV(1); + if (pinb & _BV(PB4)) buttons |= _BV(2); + if (pinb & _BV(PB5)) buttons |= _BV(3); + if (pinb & _BV(PB6)) buttons |= _BV(4); + if (pinc & _BV(PC0)) buttons |= _BV(5); + if (pinc & _BV(PC1)) buttons |= _BV(6); + if (pinc & _BV(PC4)) buttons |= _BV(7); + return buttons; +} diff --git a/display/firmware/button.h b/display/firmware/button.h new file mode 100644 index 0000000..8c3da51 --- /dev/null +++ b/display/firmware/button.h @@ -0,0 +1,5 @@ +#include + +void button_init(); + +uint8_t button_get(); diff --git a/display/firmware/main.c b/display/firmware/main.c index 8226ad7..f622ed5 100644 --- a/display/firmware/main.c +++ b/display/firmware/main.c @@ -6,11 +6,11 @@ #include #include #include "display.h" +#include "button.h" static bool report_change = false; static void init(); -static uint8_t get_buttons(); static void send_status(); static void loop(); @@ -40,11 +40,7 @@ static void loop(); static void init() { // initialize buttons - // PB2, PB3, PB4, PB5, PB6, PC0, PC1, PC4: input, no pullup (button0..7) - PORTB &= ~(_BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5) | _BV(PB6)); - DDRB &= ~(_BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5) | _BV(PB6)); - PORTC &= ~(_BV(PC0) | _BV(PC1) | _BV(PC4)); - DDRC &= ~(_BV(PC0) | _BV(PC1) | _BV(PC4)); + button_init(); // initialize VFD display_init(); @@ -79,23 +75,8 @@ static void init() { sei(); } -static uint8_t get_buttons() { - uint8_t pinb = PINB; - uint8_t pinc = PINC; - uint8_t buttons = 0; - if (pinb & _BV(PB2)) buttons |= _BV(0); - if (pinb & _BV(PB3)) buttons |= _BV(1); - if (pinb & _BV(PB4)) buttons |= _BV(2); - if (pinb & _BV(PB5)) buttons |= _BV(3); - if (pinb & _BV(PB6)) buttons |= _BV(4); - if (pinc & _BV(PC0)) buttons |= _BV(5); - if (pinc & _BV(PC1)) buttons |= _BV(6); - if (pinc & _BV(PC4)) buttons |= _BV(7); - return buttons; -} - static void send_status() { - uint8_t status = get_buttons(); + uint8_t status = button_get(); if (can_check_free_buffer()) { can_t msg = {