From da7a6e46e8c2a95c285eab595cc3fb65dcf70629 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 12 Feb 2025 14:35:56 -0800 Subject: [PATCH] cleaned up the main function --- src/main.c | 51 +++++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index 0432281..34f5441 100644 --- a/src/main.c +++ b/src/main.c @@ -31,8 +31,6 @@ volatile uint8_t idx; volatile uint16_t tick_count; -volatile uint8_t fader_pos1; - // ############################# // Prototypes // ############################# @@ -53,38 +51,11 @@ static void UpdateButtonOutput(btn_state *b); 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) { - uint16_t fader_pos = readADC() >> 2; // Scale 10-bit ADC to 8-bit (0-255) - uint8_t buttonState = readButton(); - - if (buttonState == 2) { // Long press - Save position - eeprom_update_byte(&saved_position, fader_pos); - } - else if (buttonState == 1) { // Short press - Move to saved position - uint8_t target_pos = eeprom_read_byte(&saved_position); - if (fader_pos < target_pos - 2) motorMove(1); // Move up - else if (fader_pos > target_pos + 2) motorMove(-1); // Move down - else motorCoast(); // Stop when reached position - } - else { - motorCoast(); // Default to coast mode - } - - led_blink(); - - } - */ - return 0; } @@ -115,8 +86,6 @@ void InitProg(void) { eeprom_write_byte((uint8_t *)ROM_SS1_ADR, 0x0); } - btn1.is_pressed = ((PINB & (1 << btn1.input_pin)) == 0); - InitTimer0(); } @@ -137,6 +106,17 @@ uint16_t ReadADC(void) { * ############################ */ +void MotorSetSavePos() +{ + uint8_t pos = (uint8_t)(ReadADC() >> 2); + eeprom_write_byte((uint8_t *)ROM_SS1_ADR, pos); + return; +} + +uint8_t MotorGetSavedPos(void) +{ + return (uint8_t) eeprom_read_byte((uint8_t *)ROM_SS1_ADR); +} void MotorMoveTo(uint8_t pos) { @@ -227,8 +207,7 @@ static void UpdateButtonOutput(btn_state *b) { /*If this is a new event.*/ if (!b->is_long_pressed && !b->timer_enabled) { /*Then start the timer and update the output*/ - ToggleOutput(b); - FaderMoveTo(fader_pos1); + //ToggleOutput(b); StartButtonTimer(b); return; } @@ -249,7 +228,11 @@ static void UpdateButtonOutput(btn_state *b) { else if (!b->is_pressed) { /*If the button was released on a long press.*/ if (b->is_long_pressed) { - ToggleOutput(b); + MotorSetSavePos(); + } + /*If the button pres was a short one.*/ + else if(!b->is_long_pressed) { + MotorMoveTo(MotorGetSavedPos()); } ClearButtonTimer(b); }