cleaned up the main function

This commit is contained in:
Jake Goodwin 2025-02-12 14:35:56 -08:00
parent 5b2ce9f4d8
commit da7a6e46e8
1 changed files with 17 additions and 34 deletions

View File

@ -31,8 +31,6 @@ volatile uint8_t idx;
volatile uint16_t tick_count; volatile uint16_t tick_count;
volatile uint8_t fader_pos1;
// ############################# // #############################
// Prototypes // Prototypes
// ############################# // #############################
@ -53,38 +51,11 @@ static void UpdateButtonOutput(btn_state *b);
int main(int argc, char **argv) { int main(int argc, char **argv) {
InitProg(); InitProg();
uint16_t fader_pos = ReadADC() >> 2; // Scale 10-bit ADC to 8-bit (0-255)
while (1) { while (1) {
fader_pos = ReadADC() >> 2;
UpdateButtonOutput(&btn1); UpdateButtonOutput(&btn1);
UpdateButtonInput(&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; return 0;
} }
@ -115,8 +86,6 @@ void InitProg(void) {
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, 0x0); eeprom_write_byte((uint8_t *)ROM_SS1_ADR, 0x0);
} }
btn1.is_pressed = ((PINB & (1 << btn1.input_pin)) == 0);
InitTimer0(); 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) void MotorMoveTo(uint8_t pos)
{ {
@ -227,8 +207,7 @@ static void UpdateButtonOutput(btn_state *b) {
/*If this is a new event.*/ /*If this is a new event.*/
if (!b->is_long_pressed && !b->timer_enabled) { if (!b->is_long_pressed && !b->timer_enabled) {
/*Then start the timer and update the output*/ /*Then start the timer and update the output*/
ToggleOutput(b); //ToggleOutput(b);
FaderMoveTo(fader_pos1);
StartButtonTimer(b); StartButtonTimer(b);
return; return;
} }
@ -249,7 +228,11 @@ static void UpdateButtonOutput(btn_state *b) {
else if (!b->is_pressed) { else if (!b->is_pressed) {
/*If the button was released on a long press.*/ /*If the button was released on a long press.*/
if (b->is_long_pressed) { 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); ClearButtonTimer(b);
} }