I'm putting together a little set of helper classes to make writing code for basic pedal related activities a little quicker and easier. I have classes for switches, LEDs, relays, and IO pins.
For example, the switch class has built in debouncing and can detect short and long presses. It handles all of the underlying hardware calls (data direction register, enabling/disabling pullup resistor, etc).
Since I write AVR code so rarely I find myself always having to go back and reference my old code to refresh my memory on pin/port/bit operations. This allows me to work with a much more intuitive set of functions and put something together in just a few seconds.
Right now I'm focusing on AVR but I'll be putting together an Arduino version as well. It's really nothing you can't find elsewhere, especially in the Arduino libraries, but this puts everything I commonly use together in a nice simple little package.
For example:
Code:
// Configure Footswitch on Pin 3 (PB4)
Button footswitch(IOPin(&PORTB, PB4, &PINB, PINB4, &DDRB));
// Configure LED on Pin 2 (PB3)
LED led(IOPin(&PORTB, PB0, &PINB, PINB0, &DDRB));
// Configure Relay on Pin 7 (PB2), Default to Open, Do not invert coil polarity
Relay relay(IOPin(&PORTB, PB2, &PINB, PINB2, &DDRB), OPEN, FALSE);
while (1)
{
footswitch.Update();
if (footswitch.OnPress())
{
led.Toggle();
relay.Toggle();
}
}
Instead of:
Code:
DDRB &= ~(1 << PB4); // INPUT - Footswitch
PORTB |= (1 << PB4); // Enable pull-up resistor
DDRB |= (1 << PB3); // OUTPUT - LED
DDRB |= (1 << PB2); // OUTPUT - Relay
uint8_t lastState;
uint8_t currentState;
while (1)
{
currentState = (PINB & (1 << PINB4));
if ((!currentState) && (lastState))
{
lastState = currentState;
PORTB ^= (1 << PB3); // Toggle LED
PORTB ^= (1 << PB2); // Toggle Relay
}
}