@@ -157,198 +157,198 @@ int main(void)
// probably just passed the actual port
// TODO: Grab buttonpresses with interrupts
//uint8_t sw_btn = GPIO_ReadInputDataBit(SW_BTN);
//uint8_t sw_up = GPIO_ReadInputDataBit(SW_UP);
//uint8_t sw_down = GPIO_ReadInputDataBit(SW_DOWN);
//uint8_t sw_left = GPIO_ReadInputDataBit(SW_LEFT);
//uint8_t sw_right = GPIO_ReadInputDataBit(SW_RIGHT);
GPIO_SetBits(LED_POWER);
Delay(50);
GPIO_ResetBits(LED_POWER);
}
// Read temperature and update global temp vars
int16_t temp = 0;
uint8_t temp_frac = 0;
void update_temp() {
// Assert CS
GPIO_ResetBits(MAX_CS);
Delay(1);
// This may not clock at all... might need to send 16 bits first
SPI_I2S_SendData(SPI2, 0xAAAA); // send dummy data
//SPI_I2S_SendData(SPI2, 0xAA); // send dummy data
uint16_t temp_pre = SPI_I2S_ReceiveData(SPI2);
if(temp_pre & 0b0000000000000010) {
ssd1306_DrawString("Fatal Error", 3, 35);
else if(temp_pre & 0b0000000000000001) {
ssd1306_DrawString("TC Fault", 3, 35);
uint8_t sign = temp >> 15;// top bit is sign
temp_pre = temp_pre >> 2; // Drop 2 lowest bits
temp_frac = temp_pre & 0b11; // get fractional part
temp_frac *= 25; // each bit is .25 a degree, up to fixed point
temp_pre = temp_pre >> 2; // Drop 2 fractional bits
if(sign) {
temp = -temp_pre;
else {
temp = temp_pre;
// Deassert CS
GPIO_SetBits(MAX_CS);
int32_t setpoint = 0;
uint16_t k_p = 1;
uint16_t k_i = 1;
uint16_t k_d = 1;
// Process things
void process()
{
update_temp(); // Read MAX31855
// TODO: Add calibration offset (linear)
// Perform PID calculations
//if(
GPIO_SetBits(LED_STAT);
// Write output to SSR
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_PREHEAT_BREW,
STATE_MAINTAIN_BREW,
STATE_PREHEAT_STEAM,
STATE_MAINTAIN_STEAM,
};
void draw_setpoint() {
char tempstr[3];
itoa_fp(temp, temp_frac, tempstr);
ssd1306_DrawString(" ", 3, 40);
//ssd1306_DrawString(" ", 3, 40);
ssd1306_DrawString(tempstr, 3, 40);
ssd1306_DrawString("-> ", 3, 40);
ssd1306_DrawString("-> ", 3, 80);
itoa(setpoint, tempstr);
ssd1306_DrawString(" ", 3, 70);
ssd1306_DrawString(tempstr, 3, 70);
ssd1306_DrawString(" ", 3, 95);
ssd1306_DrawString(tempstr, 3, 95);
uint8_t state = STATE_IDLE;
uint8_t goto_mode = 2;
// State machine
void machine()
uint8_t last_state = state;
switch(state)
// Idle state
case STATE_IDLE:
// Write text to OLED
// [ therm :: idle ]
ssd1306_DrawString("therm :: idle ", 0, 40);
char tempstr[6];
ssd1306_DrawString("Temp: ", 3, 40);
ssd1306_DrawString(tempstr, 3, 72);
ssd1306_drawlogo();
switch(goto_mode) {
case 2:
ssd1306_DrawString("-> brew ", 1, 40);
} break;
case 1:
ssd1306_DrawString("-> set P/I/D", 1, 40);
case 0:
ssd1306_DrawString("-> setup ", 1, 40);
// Button handler
if(!GPIO_ReadInputDataBit(SW_BTN)) {
state = STATE_PREHEAT_BREW;
break;
state = STATE_SETP;
default:
else if(!GPIO_ReadInputDataBit(SW_UP) && goto_mode < 2) {
goto_mode++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0 && goto_mode > 0) {
goto_mode--;
// Event Handler
// N/A
case STATE_SETP:
// [ therm :: set p ]
// [ p = 12 ]
ssd1306_DrawString("Proportional", 0, 40);
itoa(k_p, tempstr);
ssd1306_DrawString("P=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
state = STATE_SETI;
else if(!GPIO_ReadInputDataBit(SW_UP)) {
Status change: