diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -5,11 +5,10 @@ #include "pid.h" #include "states.h" #include "ssd1306.h" -#ifdef MAX31855_TC_SENSOR -#include "max31855.h" -#endif #ifdef MAX31865_RTD_SENSOR #include "max31865.h" +#else +#include "max31855.h" #endif #include "gpio.h" #include "spi.h" @@ -22,6 +21,7 @@ therm_settings_t set; therm_status_t status; +pid_state_t pid_state; int main(void) { @@ -53,13 +53,16 @@ int main(void) // Init SPI busses spi_init(); + // Init RTD chip #ifdef MAX31865_RTD_SENSOR max31865_config(spi_get()); #endif // Init OLED over SPI ssd1306_init(); - ssd1306_clearscreen(); + + // Startup screen + display_startup_screen(); // Default status status.temp = 0; @@ -69,20 +72,16 @@ int main(void) status.setpoint = 70; status.pid_enabled = 0; + pid_init(&pid_state); + // Go to brew instead of idle if configured thusly if(set.val.boottobrew) status.state = STATE_PREHEAT; - // Startup screen - ssd1306_drawstring("therm v0.2", 1, 40); - ssd1306_drawstring("protofusion.org/therm", 3, 0); - - HAL_Delay(1000); - // Restore settings from flash memory flash_restore(&set); - HAL_Delay(1000); + HAL_Delay(2000); ssd1306_clearscreen(); // Soft timers @@ -104,21 +103,27 @@ int main(void) if((HAL_GetTick() - last_pid > PID_PERIOD)) { - #ifdef MAX31855_TC_SENSOR - max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 - #endif #ifdef MAX31865_RTD_SENSOR max31865_readtemp(spi_get(), &set, &status); - #endif + #else + max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 + #endif if(status.pid_enabled) { // Get ssr output for next time - int16_t power_percent = pid_update(set.val.k_p, set.val.k_i, set.val.k_d, status.temp, status.temp_frac, status.setpoint, &set, &status); + int16_t power_percent = pid_update(&set, &status, &pid_state); //power-percent is 0-1000? ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000; + + + // put ssr output on display + ssd1306_drawstring(" ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise + char tempstr[6]; + itoa(ssr_output, tempstr, 10); + ssd1306_drawstring(tempstr, 0, 90); } else { @@ -138,12 +143,10 @@ int main(void) // Every 200ms, set the SSR on unless output is 0 if(HAL_GetTick() - last_ssr_on > SSR_PERIOD) { + // Only support heating (ssr_output > 0) right now if(ssr_output > 0) { - char tempstr[6]; - itoa(ssr_output, tempstr, 10); - ssd1306_drawstring(tempstr, 0, 90); HAL_GPIO_WritePin(SSR_PIN, 1); HAL_GPIO_WritePin(LED_POWER, 1); @@ -157,7 +160,7 @@ int main(void) } - // Transmit temperature over USB-CDC on a regulat basis + // Transmit temperature over USB-CDC on a regular basis if(HAL_GetTick() - last_vcp_tx > VCP_TX_FREQ) { // Print temp to cdc