diff --git a/eeprom_min.h b/eeprom_min.h --- a/eeprom_min.h +++ b/eeprom_min.h @@ -13,6 +13,8 @@ #define EEPROM_ADDR_BREWTEMP 0x0030 #define EEPROM_ADDR_STEAMTEMP 0x0034 +#define EEPROM_ADDR_UNITS 0x0038 + void Minimal_EEPROM_Unlock(void); void Minimal_EEPROM_Lock(void); diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -23,13 +23,21 @@ uint8_t Send_Buffer[64]; uint32_t packet_sent=1; uint32_t packet_receive=1; +enum tempunits { + TEMP_UNITS_CELSIUS = 0, + TEMP_UNITS_FAHRENHEIT, +}; + // Globalish setting vars uint8_t boottobrew = 0; +uint8_t temp_units = TEMP_UNITS_CELSIUS; uint16_t windup_guard = 1; uint16_t k_p = 1; uint16_t k_i = 1; uint16_t k_d = 1; +uint8_t ignore_tc_error = 0; + // ISR ticks var volatile uint32_t ticks = 0; @@ -46,11 +54,14 @@ enum state { STATE_SETSTEPS, STATE_SETWINDUP, STATE_SETBOOTTOBREW, + STATE_SETUNITS, STATE_PREHEAT_BREW, STATE_MAINTAIN_BREW, STATE_PREHEAT_STEAM, STATE_MAINTAIN_STEAM, + + STATE_TC_ERROR }; uint8_t state = STATE_IDLE; @@ -145,10 +156,11 @@ void update_temp() { uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2); if(temp_pre & 0b0000000000000010) { - ssd1306_DrawString("Fatal Error", 2, 35); + ssd1306_DrawString("Fatal Error", 3, 35); + state = STATE_TC_ERROR; } - else if(temp_pre & 0b0000000000000001) { - ssd1306_DrawString("Error: No TC", 2, 40); + else if(temp_pre & 0b0000000000000001 && !ignore_tc_error) { + state = STATE_TC_ERROR; temp = 0; temp_frac = 0; } @@ -167,6 +179,13 @@ void update_temp() { else { temp = temp_pre; } + + if(temp_units == TEMP_UNITS_FAHRENHEIT) { + temp *= 10; // fixed point mul by 1.8 + temp *= 18; + temp /= 100; + temp += 32; + } } // Deassert CS @@ -317,6 +336,7 @@ void save_settings() Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_P, k_p); Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_I, k_i); Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_K_D, k_d); + Minimal_EEPROM_ProgramWord(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS, temp_units); Minimal_EEPROM_Lock(); } @@ -354,7 +374,9 @@ void restore_settings() while(Minimal_FLASH_GetStatus()==FLASH_BUSY); setpoint_steam = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_STEAMTEMP)); - + while(Minimal_FLASH_GetStatus()==FLASH_BUSY); + temp_units = (*(__IO uint32_t*)(EEPROM_BASE_ADDR + EEPROM_ADDR_UNITS)); + Minimal_EEPROM_Lock(); } @@ -529,7 +551,7 @@ void machine() // Button handler if(SW_BTN_PRESSED) { - state = STATE_SETSTEPS; + state = STATE_SETWINDUP; } else { user_input(&k_d); @@ -627,8 +649,7 @@ void machine() // Button handler if(SW_BTN_PRESSED) { - save_settings(); - state = STATE_IDLE; + state = STATE_SETUNITS; } else if(!GPIO_ReadInputDataBit(SW_UP)) { boottobrew = 1; @@ -642,6 +663,39 @@ void machine() } break; + case STATE_SETUNITS: + { + // Write text to OLED + // [ therm :: set windup ] + // [ g = 12 ] + ssd1306_DrawString("Units: ", 0, 40); + ssd1306_drawlogo(); + + if(temp_units == TEMP_UNITS_FAHRENHEIT) + ssd1306_DrawString("Fahrenheit", 1, 60); + else + ssd1306_DrawString("Celsius ", 1, 60); + + ssd1306_DrawString("Press to accept", 3, 40); + + // Button handler + if(SW_BTN_PRESSED) { + save_settings(); + state = STATE_IDLE; + } + else if(!GPIO_ReadInputDataBit(SW_UP)) { + temp_units = TEMP_UNITS_FAHRENHEIT; + } + else if(!GPIO_ReadInputDataBit(SW_DOWN)) { + temp_units = TEMP_UNITS_CELSIUS; + } + + // Event Handler + // N/A + + } break; + + case STATE_PREHEAT_BREW: { // Write text to OLED @@ -746,6 +800,29 @@ void machine() } break; + case STATE_TC_ERROR: + { + // Write text to OLED + // [ therm : ready to steam ] + // [ 30 => 120 C ] + ssd1306_DrawString("Error:", 0, 0); + ssd1306_DrawString("Connect thermocouple", 1, 0); + ssd1306_DrawString("Press -> to ignore", 3, 0); + + // Button handler + if(SW_BTN_PRESSED) { + state = STATE_IDLE; + } + else if(SW_RIGHT_PRESSED) { + ignore_tc_error = 1; + state = STATE_IDLE; + } + // Event Handler + // Maybe handle if TC is plugged in + // N/A + + } break; + // Something is terribly wrong default: {