main.cpp 16.6 KB
Newer Older
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
/* Copyright (c) 2017 Philippe Kalaf, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

// Espresso Machine Mods
// ---------------------
// Hardware
// --------
// Boiler temperature sensor (x2)
// Brewing pressure sensor
// Flowmeter
// Solenoid control

// Software
// --------
// PID temperature control (back-flush for temperature reduction)
// pressure control (phase control)
// auto-shot clock
// auto-shot volume
// auto-flowrate on fixed shot clock and shot volume
// manual mode
// brew soft-stop
// pre-infusion

#include "mbed.h"
#include "rtos.h"
#include "Small_6.h"
#include "Small_7.h"
#include "Arial_9.h"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "stdio.h"
#include "C12832_lcd.h"
#include "string"
#include "brewcontrol.h"

// This is for debugging and control without a LCD/joystick
Serial pc(USBTX, USBRX); // tx, rx

// LCD object
C12832_LCD LCD;

// Menu display thread
Thread menu_thread;

// mutex to make the lcd lib thread safe
Mutex lcd_mutex;

// Joystick for menu control
64 65 66 67 68
InterruptIn Fire(p9);	// JS_PUSH
InterruptIn Up(p30);   	// JS_UP
InterruptIn Down(p29); 	// JS_DOWN
InterruptIn Left(p28); 	// JS_LEFT
InterruptIn Right(p27);	// JS_RIGHT
69

70 71
Timer js_timer;

72 73 74 75 76 77 78
// Main Brew Control class holding all brewing features and logic
// parameters are (in order):
// valve/zcd/pump power control (VALVE_CTRL)
// flow sensor input pin (FLOW_IN)
// ZDC detector input (ZCD_IN)
// pump phasecontrol signal (PUMP_CTRL)
// pressure sensor (PRESSURE_IN)
79 80
// side temperature sensor (TEMP_IN)
// top temperature sensor (TEMP2_IN) - Not Implemented
81
// boiler control ssr (BOILER_CTRL)
82 83 84
#ifdef TEMP2
BrewControl brew_control(p10, p15, p12, p13, p20, p25, p14, p26);
#else
85
BrewControl brew_control(p10, p15, p12, p13, p20, p25, p26);
86 87
#endif

88 89

// Menu structures
90 91 92
//
// TODO portafilter size
// TODO powersaving
93 94 95 96 97 98 99 100 101 102 103 104 105 106
#define PRE_INFUSE_TIME 0
#define PRESSURE 1
#define YIELD 2
#define SHOT_TIME 3
#define TEMPERATURE 4
#define FLOW_RATE 5
#define EMPTY 6

#define L_STANDBY 0
#define L_MENU 1
#define L_MENU_SUB 2
#define L_BREW 3

#define M_BREW_SETTINGS 0
107 108 109 110 111
#define M_BREW_TIME MODE_TIME
#define M_BREW_YIELD MODE_YIELD
#define M_BREW_TIME_YIELD MODE_TIME_YIELD
#define M_BREW_MANUAL MODE_MANUAL
#define M_BREW_STEAM MODE_STEAM
112

Philippe Kalaf's avatar
Philippe Kalaf committed
113
#define NUM_BREW_MENUS 6
114

Philippe Kalaf's avatar
Philippe Kalaf committed
115
const char* MENU_TITLES[NUM_BREW_MENUS]= {
116 117 118 119
    "Brew Settings",
    "Brew Set Time",
    "Brew Set Yield",
    "Brew Set Time/Yield",
Philippe Kalaf's avatar
Philippe Kalaf committed
120 121
    "Manual Brew",
    "Steam"
122 123 124
};

const uint8_t MENU_ITEMS[NUM_BREW_MENUS][5]= {
125
    {1, 3, TEMPERATURE, PRE_INFUSE_TIME, PRESSURE},
126 127
    {1, 1, SHOT_TIME, EMPTY, EMPTY},
    {1, 1, YIELD, EMPTY, EMPTY},
Philippe Kalaf's avatar
Philippe Kalaf committed
128 129 130
    {1, 2, YIELD, SHOT_TIME, EMPTY},
    {1, 0, EMPTY, EMPTY, EMPTY}, // stub for manual brew (no sub menu)
    {1, 0, EMPTY, EMPTY, EMPTY}  // stub for steam (no sub menu)
131 132 133 134 135 136 137 138 139 140 141 142 143
};

const char* ITEM_STRINGS[7]= {
    "Pre-Infuse Time (s): ",
    "Pressure (bar): ",
    "Yield (ml): ",
    "Shot Time (s): ",
    "Temperature (C): ",
    "Flow rate (dl/s): ",
    ""
};

uint8_t brew_params[NUM_BREW_MENUS][3][3]= {
144
    { {92, 3, 9}, {0, 0, 0}, {0, 0, 0} },
145 146
    { {28, 0, 0}, {0, 0, 0}, {0, 0, 0} },
    { {55, 0, 0}, {0, 0, 0}, {0, 0, 0} },
Philippe Kalaf's avatar
Philippe Kalaf committed
147 148 149
    { {55, 28, 0}, {55, 28, 0}, {55, 28, 0} },
    { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }, // stub for manual brew (no sub menu)
    { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }  // stub for steam (no sub menu)
150 151
};

152
uint8_t active_menu = 0;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
uint8_t menu_level = L_STANDBY;

// 0 means not in edit mode - 1 to 9 are positions on a 3x3 square
int edit_mode = 0;
const uint8_t conversion_table[9][2] = {
    {0, 0}, {1, 0}, {2, 0},
    {0, 1}, {1, 1}, {2, 1},
    {0, 2}, {1, 2}, {2, 2}
};

// takes the params from given menu and give thems to brew control
void set_params(uint8_t menu_id)
{
    if (menu_id == M_BREW_MANUAL)
    {
	brew_control.set_shot_volume(0);
	brew_control.set_shot_time(0);
170
	brew_control.set_preinfuse_time(0);
171 172 173
	return;
    }
    uint8_t i;
174 175
    for(i = 0; i < MENU_ITEMS[menu_id][1]; i++) {
	switch (MENU_ITEMS[menu_id][i+2])
176 177 178 179 180 181 182 183 184 185 186 187 188 189
	{
	    case PRE_INFUSE_TIME: 
				brew_control.set_preinfuse_time(
					brew_params[menu_id][0][i]); 
				break;
	    case YIELD:		brew_control.set_shot_volume(
					brew_params[menu_id][0][i]);
				break;
	    case SHOT_TIME:	brew_control.set_shot_time(
					brew_params[menu_id][0][i]);
				break;
	    case TEMPERATURE:	brew_control.set_shot_temperature(
					brew_params[menu_id][0][i]);
				break;
190 191 192 193 194 195
	    case FLOW_RATE:	brew_control.set_shot_flow_rate(
					float(brew_params[menu_id][0][i]/10));
				break;
	    case PRESSURE:	brew_control.set_shot_pressure(
					brew_params[menu_id][0][i]);
				break;
196 197 198 199 200 201 202 203
	}
    }
}

// Draw menu
void draw_menu()
{
    uint16_t i, j;
204
    uint8_t state;
205 206 207 208 209 210 211 212
    string out_s;
    lcd_mutex.lock();
    // clear screen if required & set
    LCD.cls();
    LCD.invert(0);

    // Standby screen -> Temperature, Pressure, Yield, Shot Clock, Flow Rate
    if (menu_level == L_STANDBY || menu_level == L_BREW) {
213
	state = brew_control.get_state();
214 215 216
	LCD.set_font((unsigned char*) Small_6);
	if (menu_level == L_BREW)
	{
217
	    // Invert screen while brewing or steaming
218
	    if (state)
219
		LCD.invert(1);
220
	    if (active_menu == M_BREW_MANUAL)
221 222 223 224 225 226
	    {
		LCD.locate(5,16);
		LCD.printf("MANUAL");
		LCD.locate(9,23);
		LCD.printf("BREW");
	    }
227
	    else if (active_menu == M_BREW_TIME)
228
	    {
229 230 231 232 233 234 235 236 237 238 239 240 241
		LCD.locate(5,16);
		LCD.printf("TIME");
	    }
	    else if (active_menu == M_BREW_YIELD)
	    {
		LCD.locate(4,16);
		LCD.printf("YIELD");
	    }
	    else if (active_menu == M_BREW_TIME_YIELD)
	    {
		LCD.locate(2,16);
		LCD.printf("FLOWRATE");
	    }
Philippe Kalaf's avatar
Philippe Kalaf committed
242 243 244 245 246
	    else if (active_menu == M_BREW_STEAM)
	    {
		LCD.locate(4,16);
		LCD.printf("STEAM");
	    }
247 248 249
	    if (state == BREWING)
	    {
		LCD.locate(5,23);
250 251 252 253
		if (active_menu == M_BREW_STEAM)
		    LCD.printf("HOT");
		else
		    LCD.printf("BREW");
254
	    }
255 256 257 258 259 260 261 262 263 264
	    else if (state == PRE_INFUSING)
	    {
		LCD.locate(2,23);
		LCD.printf("PRE-INFUSE");
	    }
	    else if (state == SOFT_STOPPING)
	    {
		LCD.locate(2,23);
		LCD.printf("SOFT-STOP");
	    }
265 266 267 268 269 270 271 272
	}
	else
	{
	    LCD.locate(5,20);
	    LCD.printf("STANDBY");
	}
	LCD.locate(4,4);
	LCD.set_font((unsigned char*) Arial12x12);
273 274 275 276 277 278 279 280
	if (state == SOFT_STOPPING)
	    LCD.printf("Av, pressure: %.1f bar",
		    brew_control.get_average_pressure());
	else
	    LCD.printf("%.1fC %.1fbar %.1fml/s", 
		    brew_control.get_current_temperature(),
		    brew_control.get_current_pressure(),
		    brew_control.get_current_flow_rate());
281
	LCD.locate(55,17);
282
	LCD.printf("%.0f ml %.0f s",
283 284 285 286 287 288 289
		brew_control.get_current_volume(),
		brew_control.get_current_time());
    }
    // Show first level menu items (brew modes)
    else if (menu_level == L_MENU) {
	LCD.locate(0,12);
	LCD.set_font((unsigned char*) Arial12x12);
290
	LCD.printf("%s  ", MENU_TITLES[active_menu]);
291 292 293 294 295
    }
    // Show second level menu items (brew parameters)
    else if (menu_level == L_MENU_SUB) {
	out_s.clear();
	// Display all menu items
296
	for(i = 2; i < sizeof(MENU_ITEMS[active_menu])/sizeof(*MENU_ITEMS[active_menu]); i++) {
297
	    // Get the type of current menu item (T, P, Y or ST)
298
	    int idx = MENU_ITEMS[active_menu][i];
299 300 301 302 303 304
	    char default_n[8];
	    // break out if we have an empty element
	    if (idx == EMPTY)
		break;
	    out_s += ITEM_STRINGS[idx];
	    // For each menu item, get all the parameters (either 1 or 3)
305
	    for (j = 0; j < MENU_ITEMS[active_menu][0]; j++) {
306 307
		// Add brackets if edit selection is over current parameter
		if (edit_mode == (i-2)*3+(j+1))
308
		    sprintf(default_n, "[%d]", brew_params[active_menu][j][i-2]);
309
		else
310
		    sprintf(default_n, "%d", brew_params[active_menu][j][i-2]);
311 312 313 314 315
		out_s += default_n;
		out_s += " ";
	    }
	    out_s += '\n';
	}
316
	if (MENU_ITEMS[active_menu][1] == 1)
317 318 319 320
	{
	    LCD.set_font((unsigned char*) Arial12x12);
	    LCD.locate(0,12);
	}
321
	else if (MENU_ITEMS[active_menu][1] == 2)
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	{  
	    LCD.set_font((unsigned char*) Arial12x12);
	    LCD.locate(0,6);
	}
	else
	{
	    LCD.set_font((unsigned char*) Small_7);
	    LCD.locate(0,2);
	}
	LCD.printf("%s", out_s.c_str());
    }
    lcd_mutex.unlock();
}

// This thread waits for joystick commands, then sets parameters and then draws the menu
void menu_handler()
{
    while(true) {
	// wait on joystick action
341 342 343 344 345 346 347
	osEvent event = Thread::signal_wait(0, 500);
	if (event.status == osEventTimeout)
	{
	    draw_menu();
	    continue;
	}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	// fire
	if (event.value.signals == 0x05) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	break;
	    case L_MENU: 	break;
	    case L_MENU_SUB: 	if(!edit_mode)
				    // enter edit mode
				    edit_mode = 1;
				else
				    // exit edit mode
				    edit_mode = 0;
				break;
	    case L_BREW:	// send params to brew control b4 brewing
				if(!brew_control.get_state())
363 364 365 366
				{
				    // set general settings
				    set_params(M_BREW_SETTINGS);
				    // set brew mode settings
367
				    set_params(active_menu);
368
				}
369
				brew_control.toggle(active_menu);
370 371 372 373 374 375 376 377 378 379 380 381 382
				break;
	    }
	}
	// left
	else if (event.value.signals == 0x01) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	break;	
	    case L_MENU: 	menu_level = L_STANDBY; break;
	    case L_MENU_SUB: 	if(!edit_mode)
				{
				    menu_level = L_MENU;
				    // if we exiting settings, let's set them
383 384
				    if(active_menu == M_BREW_SETTINGS)
					set_params(active_menu);
385 386
				}
				else
387
				    brew_params[active_menu]
388 389 390
					[conversion_table[edit_mode-1][0]]
					[conversion_table[edit_mode-1][1]]--;
				break;
391 392
	    case L_BREW:        // skip sub-menu for manual or steam mode
				if(active_menu == M_BREW_MANUAL || active_menu == M_BREW_STEAM)
393
				    menu_level = L_MENU;
394 395
				// only exit brew menu if not in brewing mode
				else if(!brew_control.get_state())
396 397
				    menu_level = L_MENU_SUB;
				break;
398 399 400 401 402 403 404
	    }
	}
	// up
	else if (event.value.signals == 0x02) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;
405
	    case L_MENU: 	active_menu = (!active_menu)?active_menu:active_menu-1;
406 407 408
				break;
	    case L_MENU_SUB: 	if(edit_mode > 1)
				    // sub 1 or 3 based on number of phases
409 410 411
				    edit_mode -= (MENU_ITEMS[active_menu][0] == 1)?3:1;
				break;
	    case L_BREW: 	if(active_menu == M_BREW_MANUAL)
412
				    brew_control.pressure_up(5);
413 414 415 416 417 418 419 420
				break;
	    }
	}
	// right
	else if (event.value.signals == 0x03) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;	
421 422 423
	    case L_MENU: 	if(active_menu == M_BREW_MANUAL)
				    // skip sub-menu for manual mode
				    menu_level = L_BREW;
Philippe Kalaf's avatar
Philippe Kalaf committed
424 425
				else if(active_menu == M_BREW_STEAM)
				{
426
				    // skip sub-menu for steam mode
Philippe Kalaf's avatar
Philippe Kalaf committed
427 428
				    menu_level = L_BREW;
				}
429 430 431 432
				else
				    menu_level = L_MENU_SUB;
				break;
	    case L_MENU_SUB: 	if(!edit_mode && active_menu != M_BREW_SETTINGS)
433 434 435 436
				{
				    menu_level = L_BREW;
				}
				else
437
				    brew_params[active_menu]
438 439 440
					[conversion_table[edit_mode-1][0]]
					[conversion_table[edit_mode-1][1]]++;
				break;
Philippe Kalaf's avatar
Philippe Kalaf committed
441 442 443
	    case L_BREW: 	if(brew_control.get_state() == PRE_INFUSING)
				    brew_control.stop_preinfuse_now();
				break;
444 445 446 447 448 449 450
	    }
	}
	// down
	else if (event.value.signals == 0x04) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;
Philippe Kalaf's avatar
Philippe Kalaf committed
451
	    case L_MENU: 	active_menu = (active_menu < NUM_BREW_MENUS-1)?active_menu+1:active_menu;
452
				break;
453
	    case L_MENU_SUB: 	if(edit_mode < (MENU_ITEMS[active_menu][1]-1)*3
454 455
					&& edit_mode > 0)
				    // move fwd by 1 or 3 based on number of phases
456 457 458
				    edit_mode += (MENU_ITEMS[active_menu][0] == 1)?3:1;
				break;
	    case L_BREW:	if(active_menu == M_BREW_MANUAL)
459
				    brew_control.pressure_down(5);
460 461 462 463 464 465 466 467 468
				break;
	    } 
	}

	// draw next menu
	draw_menu();
    }
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
// timer to ignore double clicks
bool check_js_timer()
{
    if(js_timer.read_ms() < 250)
    {
	js_timer.reset();
	return 0;
    }
    else
    {
	js_timer.reset();
	return 1;
    }
}

484 485 486
// Joystick ISRs just send signals to unblock menu thread 
void cycle_up()
{
487 488
    if(check_js_timer())
	menu_thread.signal_set(0x02);
489 490 491
}
void cycle_down()
{
492 493
    if(check_js_timer())
	menu_thread.signal_set(0x04);
494 495 496
}
void cycle_left()
{
497 498
    if(check_js_timer())
	menu_thread.signal_set(0x01);
499 500 501
}
void cycle_right()
{
502 503
    if(check_js_timer())
	menu_thread.signal_set(0x03);
504 505 506
}
void fire_away()
{
507 508
    if(check_js_timer())
	menu_thread.signal_set(0x05);
509 510 511 512 513 514 515 516
}

// print the actual contrast
int main()
{
    // Initialize default mode - 60ml shots
    int target_volume = 60;
    brew_control.set_shot_volume(target_volume);
517
    js_timer.start();
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

    // We want to call the menu_handler in another thread
    menu_thread.start(callback(menu_handler));
    draw_menu();

    // Attach joystick ISR callbacks
    Up.rise(&cycle_up);
    Down.rise(&cycle_down);
    Left.rise(&cycle_left);
    Right.rise(&cycle_right);
    Fire.rise(&fire_away);

    // This loop will use the main thread to get commands from ttyUSB
    // it will also display debug/status info on ttyUSB
    while(true) { 
#if 1
	if (pc.readable()) {
	    char c = pc.getc();

	    // Menu navigation from ttyUSB
	    if(c == 'i')
		cycle_up();
	    else if(c == 'j')
		cycle_left();
	    else if(c == 'k')
		cycle_down();
	    else if(c == 'l')
		cycle_right();
	    else if(c == 'm')
		fire_away();

	    // b for Manual Brew Toggle
	    else if(c == 'b')
	    {
		brew_control.set_shot_volume(0);
		brew_control.set_shot_time(0);
554
		brew_control.toggle(MODE_MANUAL);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
		pc.printf("brew to %d\n", brew_control.get_state());
	    }

	    // q and a to control target pressure
	    else if(c == 'q')
	    {
		brew_control.pressure_up();
	    }
	    else if(c == 'a')
	    {
		brew_control.pressure_down();
	    }

	    // z for Automatic Volume Brew
	    else if(c == 'z')
	    {
		brew_control.set_shot_volume(target_volume);
572
		brew_control.start(MODE_YIELD);
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
	    }

	    // w and s to set target temperature
	    else if (c == 'w')
	    {
		brew_control.set_shot_temperature(brew_control.get_shot_temperature() + 0.5);
		pc.printf("Temp set to %.1f\n", brew_control.get_shot_temperature());
	    }
	    else if (c == 's')
	    {
		brew_control.set_shot_temperature(brew_control.get_shot_temperature() - 0.5);
		pc.printf("Temp set to %.1f\n", brew_control.get_shot_temperature());
	    }

	    // e and d to set pre-infuse time
	    else if (c == 'e')
	    {
		brew_control.set_preinfuse_time(brew_control.get_preinfuse_time() + 1);
		pc.printf("Pre-Infuse set to %d seconds\n", brew_control.get_preinfuse_time());
	    }
	    else if (c == 'd')
	    {
		brew_control.set_preinfuse_time(brew_control.get_preinfuse_time() - 1);
		pc.printf("Pre-Infuse set to %d seconds\n", brew_control.get_preinfuse_time());
	    }

	    // r and f to set target shot volume
	    else if (c == 'r')
	    {
		target_volume = brew_control.get_shot_volume() + 5;
		brew_control.set_shot_volume(target_volume);
		pc.printf("Target Volume set to %d\n", target_volume);
	    }
	    else if (c == 'f')
	    {
		target_volume = brew_control.get_shot_volume() - 5;
		brew_control.set_shot_volume(target_volume);
		pc.printf("Target Volume set to %d\n", target_volume);
	    }

	    // p to toggle PID control (if off there will be no heating at all)
	    else if (c == 'p')
	    {
		if (brew_control.toggle_boiler())
		    pc.printf("Enabling PID\n");
		else
		    pc.printf("Disabling PID\n");
	    }
621 622
	    else if (c == '=')
		brew_control.toggle_solenoid();
623
	}
624 625
	/*
	pc.printf("%d %.3f ml %.2f bar %.1f C %.1f C %.1f s %.2f ml/s\n", 
626 627 628
		brew_control.get_pump_level(),
		brew_control.get_current_volume(),
		brew_control.get_current_pressure(),
629 630
		brew_control.get_current_temperature_side(),
		brew_control.get_current_temperature_top(),
631
		brew_control.get_current_time(),
632 633
		brew_control.get_current_flow_rate());
		*/
634 635 636 637 638
	pc.printf("%.1f %.1f %d %d\n", brew_control.get_current_temperature_side(),
				brew_control.get_current_temperature_top(),
				brew_control.get_last_pulse_count_side(),
				brew_control.get_last_pulse_count_top()
				);
639
	Thread::wait(100);   // wait 0.1s
640
#endif
641 642
    }
}