Files
@ c0c52bad69d7
Branch filter:
Location: therm/display.c
c0c52bad69d7
16.2 KiB
text/plain
Initial work on flash stuff. Reads and writes weird values...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | #include "stm32f0xx_hal.h"
#include "ssd1306.h"
#include "stringhelpers.h"
#include "display.h"
#include "config.h"
#include "states.h"
#include "bootlib.h"
#include "flash.h"
#include "gpio.h"
uint8_t goto_mode = 2;
// State machine
uint8_t sw_btn_last = 0;
uint8_t sw_up_last = 0;
uint8_t sw_down_last = 0;
uint8_t sw_left_last = 0;
uint8_t sw_right_last = 0;
#define SW_BTN_PRESSED (sw_btn_last == 0 && sw_btn == 1) // rising edge on buttonpress
#define SW_UP_PRESSED (sw_up_last == 0 && sw_up == 1)
#define SW_DOWN_PRESSED (sw_down_last == 0 && sw_down == 1)
#define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1)
#define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1)
///////////////////////////////////////////////////////////////////////////////////////
/// freaking multiple setpoint support ///
uint8_t step_duration[10] = {0,0,0,0,0,0,0,0,0,0};
int16_t step_setpoint[10] = {0,0,0,0,0,0,0,0,0,0};
uint8_t final_setpoint = 0;
// Multiple screens to set setpoint and duration on each screen
// press center to go to the next one, and press left or right or something to confirm
// When executing, complete on time AND(?) temperature. Maybe allow switching to OR via settings
////////////////////////////////////////////////////////////////////////////////////////////////
uint8_t trigger_drawsetpoint = 1;
int16_t last_temp = 21245;
void display_process(therm_settings_t* set, therm_status_t* status)
{
uint8_t last_state = status->state;
uint8_t temp_changed = status->temp != last_temp;
last_temp = status->temp;
uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN);
uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP);
uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN);
uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
switch(status->state)
{
// Idle state
case STATE_IDLE:
{
// Write text to OLED
// [ therm :: idle ]
ssd1306_DrawString("therm :: idle ", 0, 40);
status->pid_enabled = 0;
if(temp_changed) {
char tempstr[6];
itoa_fp(status->temp, status->temp_frac, tempstr);
ssd1306_DrawString("Temp: ", 3, 40);
ssd1306_DrawString(" ", 3, 72);
ssd1306_DrawString(tempstr, 3, 72);
}
ssd1306_drawlogo();
switch(goto_mode) {
case 2:
{
ssd1306_DrawString("-> heat ", 1, 40);
} break;
case 1:
{
ssd1306_DrawString("-> setup ", 1, 40);
} break;
case 0:
{
ssd1306_DrawString("-> loader ", 1, 40);
} break;
case 3:
{
ssd1306_DrawString("-> reset ", 1, 40);
}
}
// Button handler
if(SW_BTN_PRESSED) {
switch(goto_mode) {
case 3:
status->state = STATE_IDLE;
flash_erase(set);
case 2:
status->state = STATE_PREHEAT_BREW;
break;
case 1:
status->state = STATE_SETP;
break;
case 0:
{
ssd1306_clearscreen();
ssd1306_DrawString("Bootloader Entered", 0, 0);
ssd1306_DrawString("Device won't boot", 2, 0);
ssd1306_DrawString("until reflashed!", 3, 0);
bootloader_enter(); // Resets into bootloader
status->state = STATE_IDLE; // Just in case
} break;
default:
status->state = STATE_PREHEAT_BREW;
}
}
else if(SW_UP_PRESSED && goto_mode < 3) {
goto_mode++;
}
else if(SW_DOWN_PRESSED && goto_mode > 0) {
goto_mode--;
}
// Event Handler
// N/A
} break;
case STATE_SETP:
{
// Write text to OLED
// [ therm :: set p ]
// [ p = 12 ]
ssd1306_DrawString("Proportional", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->k_p, tempstr, 10);
ssd1306_DrawString("P=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_SETI;
}
else {
user_input(&set->k_p);
}
// Event Handler
// N/A
} break;
case STATE_SETI:
{
// Write text to OLED
// [ therm :: set i ]
// [ i = 12 ]
ssd1306_DrawString("Integral", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->k_i, tempstr, 10);
ssd1306_DrawString("I=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_SETD;
}
else {
user_input(&set->k_i);
}
// Event Handler
// N/A
} break;
case STATE_SETD:
{
// Write text to OLED
// [ therm :: set d ]
// [ d = 12 ]
ssd1306_DrawString("Derivative", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->k_d, tempstr, 10);
ssd1306_DrawString("D=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_SETWINDUP;
}
else {
user_input(&set->k_d);
}
// Event Handler
// N/A
} break;
case STATE_SETSTEPS:
{
// Write text to OLED
// [ step #1:: Duration: ### ]
// [ Setpoint: ### ]
char tempstr[6];
itoa(final_setpoint, tempstr, 10);
ssd1306_DrawString("Step #", 0, 0);
ssd1306_DrawString(tempstr, 0, 40);
ssd1306_DrawString("Duration: ", 0, 5);
itoa(step_duration[final_setpoint], tempstr, 10);
ssd1306_DrawString(tempstr, 0, 70);
ssd1306_DrawString("Setpoint: ", 0, 0);
itoa(step_setpoint[final_setpoint], tempstr, 10);
ssd1306_DrawString(tempstr, 0, 70);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler - TODO: increment max_step if pressed
// return and go to next state otherwise
if(SW_BTN_PRESSED) {
status->state = STATE_SETSTEPS;
final_setpoint++;
}
// else if(SW_LEFT_PRESSED) {
// state++; // go to next state or something
// }
else {
user_input(&set->k_p);
}
// Event Handler
// N/A
} break;
case STATE_SETWINDUP:
{
// Write text to OLED
// [ therm :: set windup ]
// [ g = 12 ]
ssd1306_DrawString("Windup Guard", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->windup_guard, tempstr, 10);
ssd1306_DrawString("G=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_SETBOOTTOBREW;
}
else {
user_input(&set->windup_guard);
}
// Event Handler
// N/A
} break;
case STATE_SETBOOTTOBREW:
{
// Write text to OLED
// [ therm :: set windup ]
// [ g = 12 ]
ssd1306_DrawString("Start on Boot", 0, 40);
ssd1306_drawlogo();
ssd1306_DrawString("sob=", 1, 45);
if(set->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) {
status->state = STATE_SETUNITS;
}
else if(!HAL_GPIO_ReadPin(SW_UP)) {
set->boottobrew = 1;
}
else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
set->boottobrew = 0;
}
// Event Handler
// N/A
} break;
case STATE_SETUNITS:
{
// Write text to OLED
// [ therm :: set windup ]
// [ g = 12 ]
ssd1306_DrawString("Units: ", 0, 40);
ssd1306_drawlogo();
if(set->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) {
status->state = STATE_SETTEMPOFFSET;
}
else if(!HAL_GPIO_ReadPin(SW_UP)) {
set->temp_units = TEMP_UNITS_FAHRENHEIT;
}
else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
set->temp_units = TEMP_UNITS_CELSIUS;
}
// Event Handler
// N/A
} break;
case STATE_SETTEMPOFFSET:
{
// Write text to OLED
// [ therm :: set temp offset ]
// [ g = 12 ]
ssd1306_DrawString("Temp Cal Offset", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->temp_offset, tempstr, 10);
ssd1306_DrawString("O=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
flash_save(&set);
status->state = STATE_IDLE;
}
else {
user_input_signed(&set->temp_offset);
}
// Event Handler
// N/A
} break;
case STATE_PREHEAT_BREW:
{
// Write text to OLED
// [ therm : preheating brew ]
// [ 30 => 120 C ]
ssd1306_DrawString("Preheating...", 0, 0);
//ssd1306_drawlogo();
draw_setpoint(status);
status->pid_enabled = 1;
status->setpoint = set->setpoint_brew;
// Button handler
if(SW_BTN_PRESSED) {
save_setpoints(&set); // TODO: Check for mod
status->state = STATE_IDLE;
}
else {
user_input(&set->setpoint_brew);
}
// Event Handler
if(status->temp >= status->setpoint) {
status->state = STATE_MAINTAIN_BREW;
}
} break;
case STATE_MAINTAIN_BREW:
{
// Write text to OLED
// [ therm : ready to brew ]
// [ 30 => 120 C ]
ssd1306_DrawString("Preheated!", 0, 0);
//ssd1306_drawlogo();
draw_setpoint(status);
status->pid_enabled = 1;
status->setpoint = set->setpoint_brew;
// Button handler
if(SW_BTN_PRESSED) {
save_setpoints(&set); // TODO: Check for mod
status->state = STATE_IDLE;
}
else {
user_input(&set->setpoint_brew);
}
// Event Handler
// N/A
} break;
case STATE_PREHEAT_STEAM:
{
// Write text to OLED
// [ therm : preheating steam ]
// [ 30 => 120 C ]
ssd1306_DrawString("Preheating...", 0, 0);
//ssd1306_drawlogo();
draw_setpoint(status);
status->pid_enabled = 1;
status->setpoint = set->setpoint_steam;
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_IDLE;
save_setpoints(&set); // TODO: Check for mod
}
else {
user_input(&set->setpoint_steam);
}
// Event Handler
if(status->temp >= status->setpoint) {
status->state = STATE_MAINTAIN_STEAM;
}
} break;
case STATE_MAINTAIN_STEAM:
{
// Write text to OLED
// [ therm : ready to steam ]
// [ 30 => 120 C ]
ssd1306_DrawString("Ready to Steam!", 0, 0);
//ssd1306_drawlogo();
draw_setpoint(status);
status->pid_enabled = 1;
status->setpoint = set->setpoint_steam;
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_IDLE;
save_setpoints(&set); // TODO: Check for mod
}
else {
user_input(&set->setpoint_steam);
}
// Event Handler
// N/A
} break;
case STATE_TC_ERROR:
{
// Write text to OLED
// [ therm : ready to steam ]
// [ 30 => 120 C ]
ssd1306_DrawString("Error: ", 0, 0);
if(status->tc_errno == 1)
ssd1306_DrawString(" Check Sensor (1)", 1, 0);
else if(status->tc_errno == 4)
ssd1306_DrawString(" Check Sensor (2)", 1, 0);
else
ssd1306_DrawString("#?, Unknown Error", 1, 0);
ssd1306_DrawString(" ", 2, 0);
ssd1306_DrawString("Press -> to ignore", 3, 0);
// Button handler
if(SW_BTN_PRESSED) {
status->state = STATE_IDLE;
}
else if(SW_RIGHT_PRESSED) {
set->ignore_tc_error = 1;
status->state = STATE_IDLE;
}
// Event Handler
// Maybe handle if TC is plugged in
// N/A
} break;
// Something is terribly wrong
default:
{
status->state = STATE_IDLE;
status->pid_enabled = 0;
} break;
}
if(last_state != status->state) {
// Clear screen on state change
goto_mode = 2;
trigger_drawsetpoint = 1;
ssd1306_clearscreen();
}
// Last buttonpress
sw_btn_last = sw_btn;
sw_up_last = sw_up;
sw_down_last = sw_down;
sw_left_last = sw_left;
sw_right_last = sw_right;
}
int32_t temp_last = 43002;
int32_t setpoint_last = 10023;
void draw_setpoint(therm_status_t* status) {
// FIXME: need to do this when switching modes too
if(status->temp != temp_last || trigger_drawsetpoint) {
char tempstr[3];
itoa_fp(status->temp, status->temp_frac, tempstr);
ssd1306_DrawStringBig(" ", 3, 0);
ssd1306_DrawStringBig(tempstr, 3, 0);
}
if(trigger_drawsetpoint)
ssd1306_DrawStringBig(">", 3, 74);
if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
char tempstr[3];
itoa(status->setpoint, tempstr, 10);
ssd1306_DrawStringBig(" ", 3, 90);
ssd1306_DrawStringBig(tempstr, 3, 90);
}
trigger_drawsetpoint = 0;
setpoint_last = status->setpoint;
temp_last = status->temp;
}
// vim:softtabstop=4 shiftwidth=4 expandtab
|