diff --git a/src/main.c b/src/main.c index 35e534d..70b917e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,3 @@ -/* - * Author: Jake G Email: jakegoodwin@gorge.works - * FileName: main.c - * Date: 2025 - * Descripton: Program for Atiny13A controllers - */ - #ifndef MCU #define MCU atiny13a #endif @@ -13,9 +6,8 @@ #define F_CPU 4800000UL #endif -#include "stdio.h" - -#include +#include "main.h" +#include #include #include @@ -24,23 +16,17 @@ //Globals //############################# +/* This line isn't really quite right.*/ +//uint8_t saved_position EEMEM; // EEPROM storage for fader position -//Debounce check number. -#define MAX_CHECKS 10 +btn_state btn1; -#define LED_PORT (1<<5) -#define LED_PIN PB5 -#define BLINK_DELAY_MS 5000 +/*Some variables for managing the debouncing*/ +volatile uint8_t debounced_state; +volatile uint8_t db_state[MAX_CHECKS]; +volatile uint8_t idx; -#define PWM_PIN1 PB0 // Pin 5 - Motor PWM 1 -#define PWM_PIN2 PB1 // Pin 6 - Motor PWM 2 -#define ADC_PIN PB3 // Pin 2 - Fader position reading -#define BUTTON_PIN PB4 // Pin 3 - Button input - -#define LONG_PRESS_TIME 1000 // 1 second hold -#define SHORT_PRESS_TIME 50 // Debounce time - -uint8_t saved_position EEMEM; // EEPROM storage for fader position +volatile uint16_t tick_count; //############################# //Prototypes @@ -48,7 +34,6 @@ uint8_t saved_position EEMEM; // EEPROM storage for fader position void led_blink(void); -void setup(void); uint16_t readADC(void); void motorCoast(void); void motorMove(int direction); @@ -62,6 +47,7 @@ uint8_t readButton(void); int main(int argc, char **argv) { + /* while(1) { uint16_t fader_pos = readADC() >> 2; // Scale 10-bit ADC to 8-bit (0-255) uint8_t buttonState = readButton(); @@ -82,7 +68,8 @@ int main(int argc, char **argv) led_blink(); } - + */ + return 0; } @@ -92,6 +79,55 @@ int main(int argc, char **argv) //############################# +void init_prog(void) +{ + /*Set the debounced state to all high*/ + debounced_state = 0xFF; + + init_btn(&btn1, PIN_SW1, PIN_ACTIVE1); + + /*Wait 5ms for pull-up resistors voltage to become stable.*/ + _delay_ms(5); + + /*check if the eeprom has info. */ + while(! eeprom_is_ready()) {} //Blocks until eeprom is ready. + + //Checks against a bit pattern we defined to represent the start of data. + if(eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) { + //Reads the two bytes representing the two states. + btn1.is_active = eeprom_read_byte((uint8_t *)ROM_BP1_ADR); + } + else { + //otherwise we write the init values for the start pattern and states. + eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN); + eeprom_write_byte((uint8_t *)ROM_BP1_ADR, 0x0); + } + + btn1.is_pressed = ((PINB & (1<