Files @ 9294a623e8e5
Branch filter:

Location: therm/display.c

Ethan Zonca
Added support for both heaters and coolers as well as thermostatic control
  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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "display.h"


// Private function prototypes
static void draw_setpoint(therm_status_t* status);


// Button transition variables
static uint8_t sw_btn_last = 0;
static uint8_t sw_up_last = 0;
static uint8_t sw_down_last = 0;
static uint8_t sw_left_last = 0;
static uint8_t sw_right_last = 0;


// Buttonpress macros
#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)


// States
static uint8_t trigger_drawsetpoint = 1;
static int16_t last_temp = 21245;
static int16_t last_temp_frac = 21245;
static int16_t last_state = STATE_IDLE;
static uint8_t goto_mode = MODE_HEAT;
static uint8_t reset_mode = RESET_REBOOT;



// Display state machine
void display_process(therm_settings_t* set, therm_status_t* status)
{
    uint8_t state_changed = status->state != last_state;
    last_state = status->state;
    
    uint8_t temp_changed = status->temp != last_temp || status->temp_frac != last_temp_frac;
    last_temp = status->temp;
    last_temp_frac = status->temp_frac;

    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 || state_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);
            }

            if (state_changed) {
            	ssd1306_drawlogo();
            }

            switch(goto_mode) {

                case MODE_HEAT:
                {
                    if(set->val.plant_type == PLANT_HEATER)
                        ssd1306_drawstring("-> heat     ", 1, 40);
                    else
                        ssd1306_drawstring("-> cool     ", 1, 40);
                } break;

                case MODE_SETUP:
                {
                    ssd1306_drawstring("-> setup    ", 1, 40);
                } break;

                case MODE_RESET:
                {
                    ssd1306_drawstring("-> reset    ", 1, 40);
                } break;

				#ifdef BOOTLOADER_SHORTCUT
                case MODE_BOOTLOADER:
                {
                    ssd1306_drawstring("-> dfu      ", 1, 40);
                }
				#endif
            }

            // Button handler
            if(SW_BTN_PRESSED) {
                switch(goto_mode) {
                    case MODE_HEAT:
                        status->state = STATE_PREHEAT;
                        break;
                    case MODE_SETUP:
                        status->state = STATE_SETMODE;
                        break;
                    case MODE_RESET:
                        status->state = STATE_RESET;
                        reset_mode = RESET_REBOOT;
                        break;
					#ifdef BOOTLOADER_SHORTCUT
                    case MODE_BOOTLOADER:
                        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_RESET; // Just in case
                        break;
					#endif
                    default:
                        status->state = STATE_PREHEAT;
                }
            }
            else if(SW_DOWN_PRESSED && goto_mode < (MODE_SIZE - 1)) {
                goto_mode++;
            }
            else if(SW_UP_PRESSED && goto_mode > 0) {
                goto_mode--;
            }


            // Event Handler
            // N/A

        } break;



        case STATE_SETMODE:
        {
            // Write text to OLED
            // [ therm :: set mode ]
            // [ m =          ]
            ssd1306_drawstring("Control Mode", 0, 40);
            ssd1306_drawlogo();

            if(set->val.control_mode == MODE_PID)
                ssd1306_drawstring("PID       ", 1, 60);
            else
                ssd1306_drawstring("Thermostat", 1, 60);

            ssd1306_drawstring("Press to accept", 3, 40);
            
            // Button handler
            if(SW_BTN_PRESSED) {
                status->state = STATE_SETPLANTTYPE;
            }
            else if (!HAL_GPIO_ReadPin(SW_UP)) {
                set->val.control_mode = MODE_PID;
            }
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
                set->val.control_mode = MODE_THERMOSTAT;
            }
            // Event Handler
            // N/A
 
        } break;


        case STATE_SETPLANTTYPE:
        {
            // Write text to OLED
            // [ therm :: set mode ]
            // [ m =          ]
            ssd1306_drawstring("Plant Type", 0, 40);
            ssd1306_drawlogo();

            if(set->val.plant_type == PLANT_HEATER)
                ssd1306_drawstring("Heater", 1, 60);
            else
                ssd1306_drawstring("Cooler", 1, 60);

            ssd1306_drawstring("Press to accept", 3, 40);
            
            // Button handler
            if(SW_BTN_PRESSED) {
                if(set->val.control_mode == MODE_PID)
                    status->state = STATE_SETP;
                else
                    status->state = STATE_SETBOOTTOBREW;
            }
            else if (!HAL_GPIO_ReadPin(SW_UP)) {
                set->val.plant_type = PLANT_COOLER;
            }
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
                set->val.plant_type = PLANT_HEATER;
            }
            // 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->val.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((uint16_t*)&set->val.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->val.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((uint16_t*)&set->val.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->val.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((uint16_t*)&set->val.k_d);
            }

            // 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->val.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((uint16_t*)&set->val.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->val.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->val.boottobrew = 1;
            }
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
                set->val.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->val.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->val.temp_units = TEMP_UNITS_FAHRENHEIT;
            }
            else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
                set->val.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->val.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->val.temp_offset);
            }

            // Event Handler
            // N/A
 
        } break;


        case STATE_PREHEAT:
        {
            // 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->val.setpoint_brew;

            // Button handler
            if(SW_BTN_PRESSED) {
                status->state = STATE_IDLE;
            }
            else {
                user_input((uint16_t*)&set->val.setpoint_brew);
            }

            // Event Handler
            if(status->temp >= status->setpoint) {
                status->state = STATE_MAINTAIN;
            }
 
        } break;

        case STATE_MAINTAIN:
        {
            // Write text to OLED
            // [ therm : ready to brew ]
            // [ 30 => 120 C           ]

            if(set->val.plant_type == PLANT_HEATER)
                ssd1306_drawstring("Preheated!", 0, 0);
            else
                ssd1306_drawstring("Precooled!", 0, 0);

            draw_setpoint(status);
            status->pid_enabled = 1;
            status->setpoint = set->val.setpoint_brew;

            // Button handler
            if(SW_BTN_PRESSED) {
                status->state = STATE_IDLE;
            }
            else {
                user_input((uint16_t*)&set->val.setpoint_brew);
            }

            // Event Handler
            // N/A
 
        } break;

        // Thermocouple error
        case STATE_TC_ERROR:
        {
            // Write text to OLED
            // [ therm : ready to steam ]
            // [ 30 => 120 C            ]
            ssd1306_drawstring("Error:              ", 0, 0);

            char tempstr[6];
            itoa(status->error_code, tempstr, 10);
            ssd1306_drawstring(tempstr, 0, 57);

            //TODO: add RTD error codes

            if(status->error_code == 1)
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
            else if(status->error_code == 4)
                ssd1306_drawstring("    TC Short to GND", 1, 0);
            else if(status->error_code == 8)
                ssd1306_drawstring("    TC Short to VCC", 1, 0);
            else
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
            ssd1306_drawstring("                    ", 2, 0);

            ssd1306_drawstring("-> to ignore all or", 2, 0);
            ssd1306_drawstring("press to continue", 3, 0);

            // Button handler
            if(SW_BTN_PRESSED) {
                status->state = STATE_IDLE;
				#ifdef MAX31865_RTD_SENSOR
				max31865_clear_errors(spi_get());
				#endif
            }
            else if(SW_RIGHT_PRESSED) {
                set->val.ignore_error = 1;
                status->state = STATE_IDLE;
            }
            // Event Handler
            // Maybe handle if TC is plugged in
            // N/A
 
        } break;


        // Reset state
        case STATE_RESET:
        {
            // Write text to OLED
            // [ therm :: reset ]
            ssd1306_drawstring("therm :: reset ", 0, 40);
            status->pid_enabled = 0;

            ssd1306_drawlogo();

            switch(reset_mode) {
				case RESET_DEFAULTS:
				{
					ssd1306_drawstring("-> defaults   ", 1, 40);
				} break;
                case RESET_BOOTLOADER:
                {
                    ssd1306_drawstring("-> bootloader ", 1, 40);
                } break;
                case RESET_REBOOT:
                {
                    ssd1306_drawstring("-> reboot     ", 1, 40);
                } break;
                case RESET_EXIT:
                {
                    ssd1306_drawstring("-> exit       ", 1, 40);
                } break;
            }

            // Button handler
            if(SW_BTN_PRESSED) {
                switch(reset_mode) {
                    case RESET_BOOTLOADER:
                    {
                        ssd1306_clearscreen();
                        ssd1306_drawstring("Bootloader Entered", 0, 0);
                        ssd1306_drawstring("Device won't boot", 2, 0);
                        ssd1306_drawstring("until reflashed!", 3, 0);
                        HAL_Delay(1000);
                        bootloader_enter(); // Resets into bootloader
                        status->state = STATE_RESET; // Just in case
                    } break;
                    case RESET_DEFAULTS:
                    {
                        status->state = STATE_RESET;
                        flash_load_defaults(set);
                        flash_save(set);
                        NVIC_SystemReset();
                    } break;
                    case RESET_REBOOT:
                    {
                        status->state = STATE_RESET;
                        NVIC_SystemReset();
                    } break;
                    case RESET_EXIT:
                    {
                        status->state = STATE_IDLE;
                    } break;
                }
            }
            else if(SW_DOWN_PRESSED && reset_mode < (RESET_SIZE-1)) {
                reset_mode++;
            }
            else if(SW_UP_PRESSED && reset_mode > 0) {
                reset_mode--;
            }


            // Event Handler
            // 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 = MODE_HEAT;
        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;
}


static int32_t temp_last = 43002;
static int32_t temp_frac_last = 43002;
static int32_t setpoint_last = 10023;

// Draw current setpoint on display
static void draw_setpoint(therm_status_t* status) {
    // FIXME: need to do this when switching modes too
    if(status->temp != temp_last || status->temp_frac != temp_frac_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;
    temp_frac_last = status->temp_frac;
}

void display_startup_screen() {
    ssd1306_clearscreen();
    ssd1306_drawstring("therm v0.2", 1, 40);
    ssd1306_drawstring("protofusion.org/therm", 3, 0);
}

// vim:softtabstop=4 shiftwidth=4 expandtab