From 3d259b179ed4c713204bdb7b3d1d8fcb19b4652b Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 10 Feb 2025 15:06:22 -0800 Subject: [PATCH] added button code and methods --- src/main.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 164 insertions(+), 23 deletions(-) diff --git a/src/main.c b/src/main.c index 426a377..9583566 100644 --- a/src/main.c +++ b/src/main.c @@ -40,12 +40,37 @@ void motorMove(int direction); 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 //############################# 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) { @@ -79,7 +104,7 @@ int main(int argc, char **argv) //############################# -void init_prog(void) +void InitProg(void) { /*Set the debounced state to all high*/ debounced_state = 0xFF; @@ -105,28 +130,142 @@ void init_prog(void) btn1.is_pressed = ((PINB & (1<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<input_pin); + PORTB |= (1<input_pin); + + DDRB |= (1<output_pin); + PORTB &= ~(1<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<output_pin); + } + else{ + b->is_bypassed = 0; + PORTB &= ~(1<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<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 * 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; db_state[idx] = PINB & 0xFF; idx+=1; @@ -159,7 +299,8 @@ static inline void debounce_switch() { /*Setup the timer0 on the AVR*/ -static inline void init_timer0() { +static inline void InitTimer0() +{ /*Zero out the tick_count var*/ tick_count = 0; @@ -190,7 +331,7 @@ ISR(TIM0_OVF_vect) cli(); /*Check the state of the switches*/ - debounce_switch(); + DebounceSwitch(); /*Update the tick_count*/ if(btn1.timer_enabled && btn1.pressed_ticks <= UINT8_MAX){