main.cpp 15.3 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
BrewControl brew_control(p10, p15, p12, p13, p20, p25, p26);
83 84

// Menu structures
85 86 87
//
// TODO portafilter size
// TODO powersaving
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
#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
#define M_BREW_TIME 1
#define M_BREW_YIELD 2
#define M_BREW_TIME_YIELD 3
#define M_BREW_MANUAL 4

// brew menus - 1 because we start at 0
#define NUM_BREW_MENUS 4 

const char* MENU_TITLES[5]= {
    "Brew Settings",
    "Brew Set Time",
    "Brew Set Yield",
    "Brew Set Time/Yield",
    "Manual Brew"
};

const uint8_t MENU_ITEMS[NUM_BREW_MENUS][5]= {
119
    {1, 3, TEMPERATURE, PRE_INFUSE_TIME, PRESSURE},
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    {1, 1, SHOT_TIME, EMPTY, EMPTY},
    {1, 1, YIELD, EMPTY, EMPTY},
    {1, 2, YIELD, SHOT_TIME, EMPTY}
};

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]= {
136
    { {92, 3, 9}, {0, 0, 0}, {0, 0, 0} },
137 138 139 140 141
    { {28, 0, 0}, {0, 0, 0}, {0, 0, 0} },
    { {55, 0, 0}, {0, 0, 0}, {0, 0, 0} },
    { {55, 28, 0}, {55, 28, 0}, {55, 28, 0} }
};

142
uint8_t active_menu = 0;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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);
160
	brew_control.set_preinfuse_time(0);
161 162 163
	return;
    }
    uint8_t i;
164 165
    for(i = 0; i < MENU_ITEMS[menu_id][1]; i++) {
	switch (MENU_ITEMS[menu_id][i+2])
166 167 168 169 170 171 172 173 174 175 176 177 178 179
	{
	    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;
180 181 182 183 184 185
	    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;
186 187 188 189 190 191 192 193
	}
    }
}

// Draw menu
void draw_menu()
{
    uint16_t i, j;
194
    uint8_t state;
195 196 197 198 199 200 201 202 203 204 205
    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) {
	LCD.set_font((unsigned char*) Small_6);
	if (menu_level == L_BREW)
	{
206
	    state = brew_control.get_state();
207
	    // Invert screen while brewing
208
	    if (state)
209
		LCD.invert(1);
210
	    if (active_menu == M_BREW_MANUAL)
211 212 213 214 215 216
	    {
		LCD.locate(5,16);
		LCD.printf("MANUAL");
		LCD.locate(9,23);
		LCD.printf("BREW");
	    }
217
	    else if (active_menu == M_BREW_TIME)
218
	    {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		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");
	    }
	    if (state == BREWING)
	    {
		LCD.locate(5,23);
235 236
		LCD.printf("BREW");
	    }
237 238 239 240 241 242 243 244 245 246
	    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");
	    }
247 248 249 250 251 252 253 254
	}
	else
	{
	    LCD.locate(5,20);
	    LCD.printf("STANDBY");
	}
	LCD.locate(4,4);
	LCD.set_font((unsigned char*) Arial12x12);
255
	LCD.printf("%.1f C %.1f bar %.1f ml/s", 
256 257 258 259
		brew_control.get_current_temperature(),
		brew_control.get_current_pressure(),
		brew_control.get_current_flow_rate());
	LCD.locate(55,17);
260
	LCD.printf("%.0f ml %.0f s",
261 262 263 264 265 266 267
		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);
268
	LCD.printf("%s  ", MENU_TITLES[active_menu]);
269 270 271 272 273
    }
    // Show second level menu items (brew parameters)
    else if (menu_level == L_MENU_SUB) {
	out_s.clear();
	// Display all menu items
274
	for(i = 2; i < sizeof(MENU_ITEMS[active_menu])/sizeof(*MENU_ITEMS[active_menu]); i++) {
275
	    // Get the type of current menu item (T, P, Y or ST)
276
	    int idx = MENU_ITEMS[active_menu][i];
277 278 279 280 281 282
	    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)
283
	    for (j = 0; j < MENU_ITEMS[active_menu][0]; j++) {
284 285
		// Add brackets if edit selection is over current parameter
		if (edit_mode == (i-2)*3+(j+1))
286
		    sprintf(default_n, "[%d]", brew_params[active_menu][j][i-2]);
287
		else
288
		    sprintf(default_n, "%d", brew_params[active_menu][j][i-2]);
289 290 291 292 293
		out_s += default_n;
		out_s += " ";
	    }
	    out_s += '\n';
	}
294
	if (MENU_ITEMS[active_menu][1] == 1)
295 296 297 298
	{
	    LCD.set_font((unsigned char*) Arial12x12);
	    LCD.locate(0,12);
	}
299
	else if (MENU_ITEMS[active_menu][1] == 2)
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	{  
	    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
319 320 321 322 323 324 325
	osEvent event = Thread::signal_wait(0, 500);
	if (event.status == osEventTimeout)
	{
	    draw_menu();
	    continue;
	}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	// 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())
341 342 343 344
				{
				    // set general settings
				    set_params(M_BREW_SETTINGS);
				    // set brew mode settings
345
				    set_params(active_menu);
346
				}
347 348 349 350 351 352 353 354 355 356 357 358 359 360
				brew_control.toggle();
				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
361 362
				    if(active_menu == M_BREW_SETTINGS)
					set_params(active_menu);
363 364
				}
				else
365
				    brew_params[active_menu]
366 367 368
					[conversion_table[edit_mode-1][0]]
					[conversion_table[edit_mode-1][1]]--;
				break;
369 370 371 372 373 374
	    case L_BREW: 	if(active_menu == M_BREW_MANUAL)
				    // skip sub-menu for manual mode
				    menu_level = L_MENU;
				else
				    menu_level = L_MENU_SUB;
				break;
375 376 377 378 379 380 381
	    }
	}
	// up
	else if (event.value.signals == 0x02) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;
382
	    case L_MENU: 	active_menu = (!active_menu)?active_menu:active_menu-1;
383 384 385
				break;
	    case L_MENU_SUB: 	if(edit_mode > 1)
				    // sub 1 or 3 based on number of phases
386 387 388
				    edit_mode -= (MENU_ITEMS[active_menu][0] == 1)?3:1;
				break;
	    case L_BREW: 	if(active_menu == M_BREW_MANUAL)
389
				    brew_control.pressure_up(5);
390 391 392 393 394 395 396 397
				break;
	    }
	}
	// right
	else if (event.value.signals == 0x03) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;	
398 399 400 401 402 403 404
	    case L_MENU: 	if(active_menu == M_BREW_MANUAL)
				    // skip sub-menu for manual mode
				    menu_level = L_BREW;
				else
				    menu_level = L_MENU_SUB;
				break;
	    case L_MENU_SUB: 	if(!edit_mode && active_menu != M_BREW_SETTINGS)
405 406 407 408
				{
				    menu_level = L_BREW;
				}
				else
409
				    brew_params[active_menu]
410 411 412 413 414 415 416 417 418 419 420
					[conversion_table[edit_mode-1][0]]
					[conversion_table[edit_mode-1][1]]++;
				break;
	    case L_BREW: 	break;
	    }
	}
	// down
	else if (event.value.signals == 0x04) {
	    switch (menu_level)
	    {
	    case L_STANDBY:	menu_level = L_MENU; break;
421
	    case L_MENU: 	active_menu = (active_menu < NUM_BREW_MENUS)?active_menu+1:active_menu;
422
				break;
423
	    case L_MENU_SUB: 	if(edit_mode < (MENU_ITEMS[active_menu][1]-1)*3
424 425
					&& edit_mode > 0)
				    // move fwd by 1 or 3 based on number of phases
426 427 428
				    edit_mode += (MENU_ITEMS[active_menu][0] == 1)?3:1;
				break;
	    case L_BREW:	if(active_menu == M_BREW_MANUAL)
429
				    brew_control.pressure_down(5);
430 431 432 433 434 435 436 437 438
				break;
	    } 
	}

	// draw next menu
	draw_menu();
    }
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
// 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;
    }
}

454 455 456
// Joystick ISRs just send signals to unblock menu thread 
void cycle_up()
{
457 458
    if(check_js_timer())
	menu_thread.signal_set(0x02);
459 460 461
}
void cycle_down()
{
462 463
    if(check_js_timer())
	menu_thread.signal_set(0x04);
464 465 466
}
void cycle_left()
{
467 468
    if(check_js_timer())
	menu_thread.signal_set(0x01);
469 470 471
}
void cycle_right()
{
472 473
    if(check_js_timer())
	menu_thread.signal_set(0x03);
474 475 476
}
void fire_away()
{
477 478
    if(check_js_timer())
	menu_thread.signal_set(0x05);
479 480 481 482 483 484 485 486
}

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

    // 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);
		brew_control.toggle();
		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);
		brew_control.start();
	    }

	    // 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");
	    }
591 592
	    else if (c == '=')
		brew_control.toggle_solenoid();
593
	}
594 595
	/*
	pc.printf("%d %.3f ml %.2f bar %.1f C %.1f C %.1f s %.2f ml/s\n", 
596 597 598
		brew_control.get_pump_level(),
		brew_control.get_current_volume(),
		brew_control.get_current_pressure(),
599 600
		brew_control.get_current_temperature_side(),
		brew_control.get_current_temperature_top(),
601
		brew_control.get_current_time(),
602 603
		brew_control.get_current_flow_rate());
		*/
604
	Thread::wait(100);   // wait 0.1s
605
#endif
606 607
    }
}