Add buttons to pentatonic API
This commit is contained in:
parent
5dc11fa681
commit
c6adf3f044
|
@ -1,21 +1,30 @@
|
||||||
#include <pentabug/app.h>
|
#include <pentabug/app.h>
|
||||||
#include <pentabug/pentatonic.h>
|
#include <pentabug/pentatonic.h>
|
||||||
|
#include <pentabug/hal.h>
|
||||||
|
|
||||||
static void init(void) {
|
static void init(void) {
|
||||||
pentatonic_direction(ALL_OUT);
|
pentatonic_direction(ALL_IN);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void run(void) {
|
static void run(void) {
|
||||||
pentatonic_led_on(0);
|
pentatonic_direction(ALL_IN);
|
||||||
|
|
||||||
for(uint8_t i = 0; i < 5; ++i) {
|
for(;;) {
|
||||||
wait_ms(500);
|
uint8_t buttons = pentatonic_buttons();
|
||||||
|
|
||||||
pentatonic_led_on((i + 1) % 5);
|
if(buttons) {
|
||||||
|
pentatonic_direction(ALL_OUT);
|
||||||
|
|
||||||
wait_ms(200);
|
pentatonic_all_led_set(buttons);
|
||||||
|
|
||||||
pentatonic_led_off(i);
|
led_inv(RIGHT);
|
||||||
|
|
||||||
|
wait_ms(1000);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_ms(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,15 +6,31 @@
|
||||||
#define ALL_OUT 0xff
|
#define ALL_OUT 0xff
|
||||||
#define ALL_IN 0x00
|
#define ALL_IN 0x00
|
||||||
|
|
||||||
|
// every call affecting multiple LEDs/buttons expects a bitmap
|
||||||
|
// the first 5 bits of this bitmap repressent the LEDs/buttons (from left to right)
|
||||||
|
|
||||||
|
// set direction in which to use pentatonic extension, you might use one of the ALL_* from above
|
||||||
|
// IMPORTANT: you have to call this function before using any other function from this file
|
||||||
void pentatonic_direction(uint8_t direction);
|
void pentatonic_direction(uint8_t direction);
|
||||||
|
|
||||||
|
// direct access to single LED
|
||||||
void pentatonic_led_on(uint8_t led);
|
void pentatonic_led_on(uint8_t led);
|
||||||
void pentatonic_led_off(uint8_t led);
|
void pentatonic_led_off(uint8_t led);
|
||||||
|
|
||||||
|
// turn multiple LEDs on or off with one call
|
||||||
void pentatonic_multi_led_on(uint8_t leds);
|
void pentatonic_multi_led_on(uint8_t leds);
|
||||||
void pentatonic_multi_led_off(uint8_t leds);
|
void pentatonic_multi_led_off(uint8_t leds);
|
||||||
|
|
||||||
|
// set the state of a single LEDs
|
||||||
void pentatonic_led_set(uint8_t led, uint8_t state);
|
void pentatonic_led_set(uint8_t led, uint8_t state);
|
||||||
|
|
||||||
|
// sets the state of all LEDs
|
||||||
void pentatonic_all_led_set(uint8_t leds);
|
void pentatonic_all_led_set(uint8_t leds);
|
||||||
|
|
||||||
|
// get the state of all buttons
|
||||||
|
uint8_t pentatonic_buttons(void);
|
||||||
|
|
||||||
|
// get the state of a specific button
|
||||||
|
uint8_t pentatonic_button(uint8_t button);
|
||||||
|
|
||||||
#endif /* PENTATONIC_H */
|
#endif /* PENTATONIC_H */
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
|
||||||
// pins affected by pentatonic on each port
|
// pins affected by pentatonic on each port
|
||||||
const uint8_t bits_c = (1 << 4) | (1 << 5);
|
static const uint8_t bits_c = (1 << 4) | (1 << 5);
|
||||||
const uint8_t bits_d = (1 << 5) | (1 << 6) | (1 << 7);
|
static const uint8_t bits_d = (1 << 5) | (1 << 6) | (1 << 7);
|
||||||
|
|
||||||
|
// these functions assign the pentatonic bits to pins on a port
|
||||||
|
|
||||||
static inline uint8_t bits_to_c(uint8_t direction) {
|
static inline uint8_t bits_to_c(uint8_t direction) {
|
||||||
return ((direction & (1 << 3)) << 2) | (direction & (1 << 4));
|
return ((direction & (1 << 3)) << 2) | (direction & (1 << 4));
|
||||||
|
@ -17,21 +19,18 @@ static inline uint8_t bits_to_d(uint8_t direction) {
|
||||||
void pentatonic_direction(uint8_t direction) {
|
void pentatonic_direction(uint8_t direction) {
|
||||||
// map bits to pins
|
// map bits to pins
|
||||||
|
|
||||||
uint8_t part_c = bits_to_c(direction);
|
const uint8_t part_c = bits_to_c(direction);
|
||||||
uint8_t part_d = bits_to_d(direction);
|
const uint8_t part_d = bits_to_d(direction);
|
||||||
|
|
||||||
// reset everything
|
// reset direction
|
||||||
|
|
||||||
PORTC &= ~bits_c;
|
|
||||||
DDRC &= ~bits_c;
|
DDRC &= ~bits_c;
|
||||||
|
|
||||||
PORTD &= ~bits_d;
|
|
||||||
DDRD &= ~bits_d;
|
DDRD &= ~bits_d;
|
||||||
|
|
||||||
// pullup if input, off if output (inverted)
|
// pullup if input, off if output (inverted)
|
||||||
|
|
||||||
PORTC |= part_c;
|
PORTC |= bits_c;
|
||||||
PORTD |= part_d;
|
PORTD |= bits_d;
|
||||||
|
|
||||||
// set new direction
|
// set new direction
|
||||||
|
|
||||||
|
@ -42,8 +41,8 @@ void pentatonic_direction(uint8_t direction) {
|
||||||
void pentatonic_multi_led_on(uint8_t leds) {
|
void pentatonic_multi_led_on(uint8_t leds) {
|
||||||
// map bits to pins
|
// map bits to pins
|
||||||
|
|
||||||
uint8_t part_c = bits_to_c(leds);
|
const uint8_t part_c = bits_to_c(leds);
|
||||||
uint8_t part_d = bits_to_d(leds);
|
const uint8_t part_d = bits_to_d(leds);
|
||||||
|
|
||||||
// set leds on (inverted)
|
// set leds on (inverted)
|
||||||
|
|
||||||
|
@ -83,3 +82,19 @@ void pentatonic_all_led_set(uint8_t leds) {
|
||||||
pentatonic_multi_led_off(0xff);
|
pentatonic_multi_led_off(0xff);
|
||||||
pentatonic_multi_led_on(leds);
|
pentatonic_multi_led_on(leds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t pentatonic_buttons(void) {
|
||||||
|
// assign pins to pentatonic bits
|
||||||
|
|
||||||
|
uint8_t part_c = ((PINC & (1 << 5)) >> 2) | (PINC & (1 << 4));
|
||||||
|
uint8_t part_d = PIND >> 5 & 0b111;
|
||||||
|
|
||||||
|
// return inverted as switches are connected to ground
|
||||||
|
|
||||||
|
return ~(part_c | part_d) & 0b11111;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pentatonic_button(uint8_t button) {
|
||||||
|
return pentatonic_buttons() & (1 << button);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user