@@ -26,12 +26,15 @@ 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;
int16_t setpoint_brew = 0;
int16_t setpoint_steam = 0;
// State definition
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
@@ -54,12 +57,13 @@ void init_gpio();
void init_spi();
void process();
void machine();
void delay(__IO uint32_t nTime);
void restore_settings();
void save_settings();
void save_setpoints();
int main(void)
{
// Init clocks
SystemInit();
@@ -304,24 +308,35 @@ 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
#define EEPROM_ADDR_BREWTEMP 0x8080030
#define EEPROM_ADDR_STEAMTEMP 0x8080034
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 save_setpoints()
DATA_EEPROM_ProgramWord(EEPROM_ADDR_BREWTEMP, setpoint_brew);
DATA_EEPROM_ProgramWord(EEPROM_ADDR_STEAMTEMP, setpoint_steam);
// TODO: Save/restore temperature setpoint settings
void restore_settings()
while(FLASH_GetStatus()==FLASH_BUSY);
@@ -336,12 +351,18 @@ void restore_settings()
k_i = (*(__IO uint32_t*)EEPROM_ADDR_K_I);
k_d = (*(__IO uint32_t*)EEPROM_ADDR_K_D);
setpoint_brew = (*(__IO uint32_t*)EEPROM_ADDR_BREWTEMP);
setpoint_steam = (*(__IO uint32_t*)EEPROM_ADDR_STEAMTEMP);
void machine()
uint8_t last_state = state;
@@ -588,22 +609,24 @@ void machine()
// [ therm : preheating brew ]
// [ 30 => 120 C ]
ssd1306_DrawString("Preheating...", 0, 40);
ssd1306_drawlogo();
draw_setpoint();
pid_enabled = 1;
setpoint = setpoint_brew;
// Button handler
if(SW_BTN_PRESSED) {
save_setpoints(); // TODO: Check for mod
state = STATE_IDLE;
else if(!GPIO_ReadInputDataBit(SW_UP)) {
setpoint++;
setpoint_brew++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
setpoint--;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint_brew > 0) {
setpoint_brew--;
// Event Handler
if(temp >= setpoint) {
state = STATE_MAINTAIN_BREW;
@@ -617,22 +640,24 @@ void machine()
// [ therm : ready to brew ]
ssd1306_DrawString("Ready to Brew!", 0, 40);
// N/A
@@ -644,25 +669,26 @@ void machine()
// [ therm : preheating steam ]
setpoint = setpoint_steam;
setpoint_steam++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint_steam > 0) {
setpoint_steam--;
state = STATE_MAINTAIN_STEAM;
} break;
@@ -673,22 +699,24 @@ void machine()
// [ therm : ready to steam ]
ssd1306_DrawString("Ready to Steam!", 0, 40);
Status change: