diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -21,6 +21,14 @@ uint8_t Send_Buffer[64]; uint32_t packet_sent=1; uint32_t packet_receive=1; +// Globalish setting vars +uint8_t boottobrew = 0; +#define WINDUP_GUARD_GAIN 100 +uint16_t windup_guard = WINDUP_GUARD_GAIN; +uint16_t k_p = 1; +uint16_t k_i = 1; +uint16_t k_d = 1; + // State definition enum state { STATE_IDLE = 0, @@ -29,6 +37,7 @@ enum state { STATE_SETI, STATE_SETD, STATE_SETWINDUP, + STATE_SETBOOTTOBREW, STATE_PREHEAT_BREW, STATE_MAINTAIN_BREW, @@ -45,6 +54,8 @@ void init_spi(); void process(); void machine(); void delay(__IO uint32_t nTime); +void restore_settings(); +void save_settings(); int main(void) { @@ -64,7 +75,6 @@ int main(void) GPIO_SetBits(LED_POWER); // TODO: Awesome pwm of power LED (TIM4_CH4 or TIM11_CH1) - // TODO: PWM of stat led (TIM3_CH2) // Configure 1ms SysTick (change if more temporal resolution needed) RCC_ClocksTypeDef RCC_Clocks; @@ -83,6 +93,10 @@ int main(void) ssd1306_DrawString("protofusion.org/therm", 3, 0); delay(1500); ssd1306_clearscreen(); + restore_settings(); + delay(1500); + delay(1500); + ssd1306_clearscreen(); GPIO_ResetBits(LED_STAT); @@ -147,8 +161,6 @@ int16_t last_pid_temp = 0; uint8_t last_pid_temp_frac = 0; int16_t i_state = 0; -#define WINDUP_GUARD_GAIN 100 -uint16_t windup_guard = WINDUP_GUARD_GAIN; int16_t update_pid(uint16_t k_p, uint16_t k_i, uint16_t k_d, int16_t temp, uint8_t temp_frac, int16_t setpoint) { @@ -202,9 +214,6 @@ uint32_t ticks = 0; uint32_t last_ssr_on = 0; uint32_t last_led = 0; int32_t setpoint = 0; -uint16_t k_p = 1; -uint16_t k_i = 1; -uint16_t k_d = 1; int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD uint8_t pid_enabled = 0; @@ -284,6 +293,60 @@ uint8_t sw_right_last = 0; #define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1) #define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1) +/* + * uint8_t boottobrew = 0; +#define WINDUP_GUARD_GAIN 100 +uint16_t windup_guard = WINDUP_GUARD_GAIN; +uint16_t k_p = 1; +uint16_t k_i = 1; +uint16_t k_d = 1;*/ + +#define EEPROM_ADDR_WINDUP_GUARD 0x0808001C +#define EEPROM_ADDR_BOOTTOBREW 0x08080020 +#define EEPROM_ADDR_K_P 0x8080024 +#define EEPROM_ADDR_K_I 0x8080028 +#define EEPROM_ADDR_K_D 0x808002C + +void save_settings() +{ + DATA_EEPROM_Unlock(); + // Try programming a word at an address divisible by 4 + DATA_EEPROM_ProgramWord(EEPROM_ADDR_BOOTTOBREW, boottobrew); + DATA_EEPROM_ProgramWord(EEPROM_ADDR_WINDUP_GUARD, windup_guard); + DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_P, k_p); + DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_I, k_i); + DATA_EEPROM_ProgramWord(EEPROM_ADDR_K_D, k_d); + DATA_EEPROM_Lock(); + +} + +void restore_settings() +{ + DATA_EEPROM_Unlock(); + while(FLASH_GetStatus()==FLASH_BUSY); + boottobrew = (*(__IO uint32_t*)EEPROM_ADDR_BOOTTOBREW); + + while(FLASH_GetStatus()==FLASH_BUSY); + windup_guard = (*(__IO uint32_t*)EEPROM_ADDR_WINDUP_GUARD); + + while(FLASH_GetStatus()==FLASH_BUSY); + k_p = (*(__IO uint32_t*)EEPROM_ADDR_K_P); + + while(FLASH_GetStatus()==FLASH_BUSY); + k_i = (*(__IO uint32_t*)EEPROM_ADDR_K_I); + + while(FLASH_GetStatus()==FLASH_BUSY); + k_d = (*(__IO uint32_t*)EEPROM_ADDR_K_D); + + DATA_EEPROM_Lock(); + + char tempstr[6]; + itoa(windup_guard, tempstr); + ssd1306_DrawString("Read: ", 3, 40); + ssd1306_DrawString(" ", 3, 72); + ssd1306_DrawString(tempstr, 3, 72); +} + void machine() { uint8_t last_state = state; @@ -473,7 +536,7 @@ void machine() // Button handler if(SW_BTN_PRESSED) { - state = STATE_IDLE; + state = STATE_SETBOOTTOBREW; } else if(!GPIO_ReadInputDataBit(SW_UP)) { windup_guard++; @@ -487,6 +550,40 @@ void machine() } break; + case STATE_SETBOOTTOBREW: + { + // Write text to OLED + // [ therm :: set windup ] + // [ g = 12 ] + ssd1306_DrawString("Boot to Brew", 0, 40); + ssd1306_drawlogo(); + + ssd1306_DrawString("btb=", 1, 45); + + if(boottobrew) + ssd1306_DrawString("Enabled ", 1, 70); + else + ssd1306_DrawString("Disabled", 1, 70); + + ssd1306_DrawString("Press to accept", 3, 40); + + // Button handler + if(SW_BTN_PRESSED) { + save_settings(); + state = STATE_IDLE; + } + else if(!GPIO_ReadInputDataBit(SW_UP)) { + boottobrew = 1; + } + else if(!GPIO_ReadInputDataBit(SW_DOWN)) { + boottobrew = 0; + } + + // Event Handler + // N/A + + } break; + @@ -614,8 +711,11 @@ void machine() if(last_state != state) { // Clear screen on state change + goto_mode = 2; ssd1306_clearscreen(); } + + // Last buttonpress sw_btn_last = sw_btn; sw_up_last = sw_up; sw_down_last = sw_down;