From 625f7b610772add0d943f5daf313060cfa46e605 Mon Sep 17 00:00:00 2001 From: Thammi Date: Fri, 30 Aug 2013 18:54:23 +0200 Subject: [PATCH] Add initial pentatonic support --- firmware/apps/tonictest.c | 22 +++++++ firmware/include/pentabug/pentatonic.h | 20 ++++++ firmware/lib/pentatonic.c | 85 ++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 firmware/apps/tonictest.c create mode 100644 firmware/include/pentabug/pentatonic.h create mode 100644 firmware/lib/pentatonic.c diff --git a/firmware/apps/tonictest.c b/firmware/apps/tonictest.c new file mode 100644 index 0000000..29c8abf --- /dev/null +++ b/firmware/apps/tonictest.c @@ -0,0 +1,22 @@ +#include +#include + +static void init(void) { + pentatonic_direction(ALL_OUT); +} + +static void run(void) { + pentatonic_led_on(0); + + for(uint8_t i = 0; i < 5; ++i) { + wait_ms(500); + + pentatonic_led_on((i + 1) % 5); + + wait_ms(200); + + pentatonic_led_off(i); + } +} + +REGISTER(run, init, NULL); diff --git a/firmware/include/pentabug/pentatonic.h b/firmware/include/pentabug/pentatonic.h new file mode 100644 index 0000000..0991b3d --- /dev/null +++ b/firmware/include/pentabug/pentatonic.h @@ -0,0 +1,20 @@ +#ifndef PENTATONIC_H +#define PENTATONIC_H + +#include + +#define ALL_OUT 0xff +#define ALL_IN 0x00 + +void pentatonic_direction(uint8_t direction); + +void pentatonic_led_on(uint8_t led); +void pentatonic_led_off(uint8_t led); + +void pentatonic_multi_led_on(uint8_t leds); +void pentatonic_multi_led_off(uint8_t leds); + +void pentatonic_led_set(uint8_t led, uint8_t state); +void pentatonic_all_led_set(uint8_t leds); + +#endif /* PENTATONIC_H */ diff --git a/firmware/lib/pentatonic.c b/firmware/lib/pentatonic.c new file mode 100644 index 0000000..b5ebea8 --- /dev/null +++ b/firmware/lib/pentatonic.c @@ -0,0 +1,85 @@ +#include + +#include + +// pins affected by pentatonic on each port +const uint8_t bits_c = (1 << 4) | (1 << 5); +const uint8_t bits_d = (1 << 5) | (1 << 6) | (1 << 7); + +static inline uint8_t bits_to_c(uint8_t direction) { + return ((direction & (1 << 3)) << 2) | (direction & (1 << 4)); +} + +static inline uint8_t bits_to_d(uint8_t direction) { + return (direction << 5) & bits_d; +} + +void pentatonic_direction(uint8_t direction) { + // map bits to pins + + uint8_t part_c = bits_to_c(direction); + uint8_t part_d = bits_to_d(direction); + + // reset everything + + PORTC &= ~bits_c; + DDRC &= ~bits_c; + + PORTD &= ~bits_d; + DDRD &= ~bits_d; + + // pullup if input, off if output (inverted) + + PORTC |= part_c; + PORTD |= part_d; + + // set new direction + + DDRC |= part_c; + DDRD |= part_d; +} + +void pentatonic_multi_led_on(uint8_t leds) { + // map bits to pins + + uint8_t part_c = bits_to_c(leds); + uint8_t part_d = bits_to_d(leds); + + // set leds on (inverted) + + PORTC &= ~(part_c & bits_c); + PORTD &= ~(part_d & bits_d); +} + +void pentatonic_multi_led_off(uint8_t leds) { + // map bits to pins + + uint8_t part_c = bits_to_c(leds); + uint8_t part_d = bits_to_d(leds); + + // set leds on (inverted) + + PORTC |= part_c; + PORTD |= part_d; +} + +void pentatonic_led_on(uint8_t led) { + pentatonic_multi_led_on(1 << led); +} + +void pentatonic_led_off(uint8_t led) { + pentatonic_multi_led_off(1 << led); +} + +void pentatonic_led_set(uint8_t led, uint8_t state) { + if(state) { + pentatonic_led_on(led); + } else { + pentatonic_led_off(led); + } +} + +void pentatonic_all_led_set(uint8_t leds) { + pentatonic_multi_led_off(0xff); + pentatonic_multi_led_on(leds); +}