generated from TDD-Templates/cmake_cpputest_template_avr
added button code and methods
This commit is contained in:
parent
491df6d407
commit
3d259b179e
187
src/main.c
187
src/main.c
|
@ -40,12 +40,37 @@ void motorMove(int direction);
|
||||||
uint8_t readButton(void);
|
uint8_t readButton(void);
|
||||||
|
|
||||||
|
|
||||||
|
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin);
|
||||||
|
static void ToggleOutput(btn_state *b);
|
||||||
|
static void ClearButtonTimer(btn_state *b);
|
||||||
|
static void StartButtonTimer(btn_state *b);
|
||||||
|
static void CheckButtonLongpress(btn_state *b);
|
||||||
|
static void UpdateButtonInput(btn_state *b);
|
||||||
|
static void UpdateButtonOutput(btn_state *b);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//#############################
|
//#############################
|
||||||
//MAIN
|
//MAIN
|
||||||
//#############################
|
//#############################
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
InitProg();
|
||||||
|
|
||||||
|
uint16_t fader_pos = ReadADC() >> 2; // Scale 10-bit ADC to 8-bit (0-255)
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
fader_pos = ReadADC() >> 2;
|
||||||
|
|
||||||
|
/*
|
||||||
|
UpdateButtonOutput(&btn1);
|
||||||
|
UpdateButtonInput(&btn1);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
while(1) {
|
while(1) {
|
||||||
|
@ -79,7 +104,7 @@ int main(int argc, char **argv)
|
||||||
//#############################
|
//#############################
|
||||||
|
|
||||||
|
|
||||||
void init_prog(void)
|
void InitProg(void)
|
||||||
{
|
{
|
||||||
/*Set the debounced state to all high*/
|
/*Set the debounced state to all high*/
|
||||||
debounced_state = 0xFF;
|
debounced_state = 0xFF;
|
||||||
|
@ -105,28 +130,142 @@ void init_prog(void)
|
||||||
|
|
||||||
btn1.is_pressed = ((PINB & (1<<btn1.input_pin)) == 0);
|
btn1.is_pressed = ((PINB & (1<<btn1.input_pin)) == 0);
|
||||||
|
|
||||||
/*This is to read if the user wants to change the saved states.*/
|
InitTimer0();
|
||||||
/*Manually read the current switch state*/
|
|
||||||
/*
|
|
||||||
if(btn1.is_pressed){
|
|
||||||
btn1.is_active = ! btn1.is_active;
|
|
||||||
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, btn1.is_active);
|
|
||||||
blink_bypass1();
|
|
||||||
}
|
|
||||||
if(btn2.is_pressed){
|
|
||||||
btn2.is_active = ! btn2.is_active;
|
|
||||||
eeprom_write_byte((uint8_t *)ROM_SS2_ADR, btn2.is_active);
|
|
||||||
blink_bypass2();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(btn1.is_active){PORTB |= (1<<btn1.output_pin);}
|
|
||||||
if(btn2.is_active){PORTB |= (1<<btn2.output_pin);}
|
|
||||||
*/
|
|
||||||
|
|
||||||
init_timer0();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t ReadADC(void);
|
||||||
|
{
|
||||||
|
// Initialize ADC
|
||||||
|
ADMUX = (1 << MUX1) | (1 << MUX0); // Select ADC3 (PB3)
|
||||||
|
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
|
||||||
|
|
||||||
|
ADCSRA |= (1 << ADSC); // Start conversion
|
||||||
|
while (ADCSRA & (1 << ADSC)); // Wait for conversion to finish
|
||||||
|
return ADC;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* Motor methods
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
void MotorCoast(void)
|
||||||
|
{
|
||||||
|
PORTB &= ~((1 << PWM_PIN1) | (1 << PWM_PIN2)); // Disable motor drive
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* BUTTON methods
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*This is kinda like our button constructor*/
|
||||||
|
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin)
|
||||||
|
{
|
||||||
|
b->is_long_pressed = 0;
|
||||||
|
b->is_pressed = 0;
|
||||||
|
b->is_bypassed = 0;
|
||||||
|
b->pressed_ticks = 0;
|
||||||
|
b->timer_enabled = 0;
|
||||||
|
b->input_pin = input_pin;
|
||||||
|
b->output_pin = output_pin;
|
||||||
|
|
||||||
|
/*Configure the buttons inputs and outputs*/
|
||||||
|
DDRB &= ~(1<<b->input_pin);
|
||||||
|
PORTB |= (1<<b->input_pin);
|
||||||
|
|
||||||
|
DDRB |= (1<<b->output_pin);
|
||||||
|
PORTB &= ~(1<<b->output_pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*This is the fancy function to toggle output pins*/
|
||||||
|
static void ToggleOutput(btn_state *b)
|
||||||
|
{
|
||||||
|
if(!b->is_bypassed){
|
||||||
|
b->is_bypassed = 1;
|
||||||
|
PORTB |= (1<<b->output_pin);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
b->is_bypassed = 0;
|
||||||
|
PORTB &= ~(1<<b->output_pin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ClearButtonTimer(btn_state *b)
|
||||||
|
{
|
||||||
|
b->timer_enabled = 0;
|
||||||
|
b->is_long_pressed = 0;
|
||||||
|
b->pressed_ticks = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void StartButtonTimer(btn_state *b)
|
||||||
|
{
|
||||||
|
ClearButtonTimer(b);
|
||||||
|
b->timer_enabled = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CheckButtonLongpress(btn_state *b)
|
||||||
|
{
|
||||||
|
if(b->pressed_ticks >= LONG_PRESS_TICKS) {
|
||||||
|
b->is_long_pressed = 1;
|
||||||
|
b->timer_enabled = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void UpdateButtonInput(btn_state *b)
|
||||||
|
{
|
||||||
|
/*Check from the global debounced port input*/
|
||||||
|
|
||||||
|
/*check for pin HIGH*/
|
||||||
|
if(debounced_state & (1<<b->input_pin)) {
|
||||||
|
b->is_pressed = 0;
|
||||||
|
}
|
||||||
|
/*otherwise assume pin LOW*/
|
||||||
|
else{
|
||||||
|
b->is_pressed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void UpdateButtonOutput(btn_state *b)
|
||||||
|
{
|
||||||
|
/*If the button is actually pressed.*/
|
||||||
|
if(b->is_pressed){
|
||||||
|
|
||||||
|
/*If this is a new event.*/
|
||||||
|
if(!b->is_long_pressed && !b->timer_enabled){
|
||||||
|
/*Then start the timer and update the output*/
|
||||||
|
ToggleOutput(b);
|
||||||
|
StartButtonTimer(b);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*If the timer is already going.*/
|
||||||
|
else if(b->timer_enabled){
|
||||||
|
/*Then just check if it's hit the threshold.*/
|
||||||
|
CheckButtonLongpress(b);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
else{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Else the button was realeased*/
|
||||||
|
else if(!b->is_pressed){
|
||||||
|
/*If the button was released on a long press.*/
|
||||||
|
if(b->is_long_pressed){
|
||||||
|
ToggleOutput(b);
|
||||||
|
}
|
||||||
|
ClearButtonTimer(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ############################
|
* ############################
|
||||||
|
@ -140,7 +279,8 @@ void init_prog(void)
|
||||||
* DESCRIPTION: Updates the global debounced state. This function
|
* DESCRIPTION: Updates the global debounced state. This function
|
||||||
* should be called in a ISR a set rate using the hardware timer.
|
* should be called in a ISR a set rate using the hardware timer.
|
||||||
*/
|
*/
|
||||||
static inline void debounce_switch() {
|
static inline void DebounceSwitch()
|
||||||
|
{
|
||||||
uint8_t i, j;
|
uint8_t i, j;
|
||||||
db_state[idx] = PINB & 0xFF;
|
db_state[idx] = PINB & 0xFF;
|
||||||
idx+=1;
|
idx+=1;
|
||||||
|
@ -159,7 +299,8 @@ static inline void debounce_switch() {
|
||||||
|
|
||||||
|
|
||||||
/*Setup the timer0 on the AVR*/
|
/*Setup the timer0 on the AVR*/
|
||||||
static inline void init_timer0() {
|
static inline void InitTimer0()
|
||||||
|
{
|
||||||
/*Zero out the tick_count var*/
|
/*Zero out the tick_count var*/
|
||||||
tick_count = 0;
|
tick_count = 0;
|
||||||
|
|
||||||
|
@ -190,7 +331,7 @@ ISR(TIM0_OVF_vect)
|
||||||
cli();
|
cli();
|
||||||
|
|
||||||
/*Check the state of the switches*/
|
/*Check the state of the switches*/
|
||||||
debounce_switch();
|
DebounceSwitch();
|
||||||
|
|
||||||
/*Update the tick_count*/
|
/*Update the tick_count*/
|
||||||
if(btn1.timer_enabled && btn1.pressed_ticks <= UINT8_MAX){
|
if(btn1.timer_enabled && btn1.pressed_ticks <= UINT8_MAX){
|
||||||
|
|
Loading…
Reference in New Issue