|
@@ -2,14 +2,13 @@
|
|
|
* coffee.cpp
|
|
|
*
|
|
|
* Created on: Sep 25, 2017
|
|
|
- * Author: sebastian
|
|
|
+ * Author: Sebastian Vendt
|
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <errno.h>
|
|
|
-#include <stdint.h>
|
|
|
#include <wiringPi.h>
|
|
|
#include <pthread.h>
|
|
|
#include <unistd.h>
|
|
@@ -51,10 +50,15 @@ const char* PageName[] = {
|
|
|
"RESTART",
|
|
|
"SETTINGS",
|
|
|
"RESETKWH",
|
|
|
+ "RESETKWH_VAL",
|
|
|
"CHANGEPREINF",
|
|
|
+ "CHANGEPREINF_VAL",
|
|
|
"CHANGEBREWTIME",
|
|
|
+ "CHANGEBREWTIME_VAL",
|
|
|
"CHANGEBREWML",
|
|
|
+ "CHANGEBREWML_VAL",
|
|
|
"CHANGESOAKTIME",
|
|
|
+ "CHANGESOAKTIME_VAL",
|
|
|
"LEAVESETTINGS",
|
|
|
"Exit",
|
|
|
"PAGE_NULL"
|
|
@@ -74,6 +78,8 @@ const char* StateName[] = {
|
|
|
"STATE_NULL",
|
|
|
};
|
|
|
|
|
|
+struct coffeeConfig coffeeCFG = {AMOUNT_PREINFUSION, TIME_SOAK, TIME_INFUSION, AMOUNT_DBLESPRESSO};
|
|
|
+
|
|
|
/**
|
|
|
* Thread for the finite state machine
|
|
|
* It represents the current state of the machine and handles signals coming from
|
|
@@ -121,13 +127,25 @@ void *coffeeThread(void *threadid) {
|
|
|
//pthread_exit(EXIT_SUCCESS);
|
|
|
exit(EXIT_FAILURE);
|
|
|
}
|
|
|
+ if (!(coffeeCFG.amount_dblespresso = sqlGetConf(CFGAmountDblEspresso))) {
|
|
|
+ logger_error("coffee.cpp: Couldn't read config CFGAmountDblEspresso from database\nFalling back to default!\n");
|
|
|
+ }
|
|
|
+ if (!(coffeeCFG.amount_preinfusion = sqlGetConf(CFGAmountPreinfusion))) {
|
|
|
+ logger_error("coffee.cpp: Couldn't read config CFGAmountPreinfusion from database\nFalling back to default!\n");
|
|
|
+ }
|
|
|
+ if (!(coffeeCFG.time_infusion = sqlGetConf(CFGTimeInfusion))) {
|
|
|
+ logger_error("coffee.cpp: Couldn't read config CFGTimeInfusion from database\nFalling back to default!\n");
|
|
|
+ }
|
|
|
+ if (!(coffeeCFG.time_soak = sqlGetConf(CFGTimeSoak))) {
|
|
|
+ logger_error("coffee.cpp: Couldn't read config CFGTimeSoak from database\nFalling back to default!\n");
|
|
|
+ }
|
|
|
|
|
|
checkDescaling();
|
|
|
|
|
|
event_subscribe("terminate", &coffeeTerminate, "coffee.cpp");
|
|
|
|
|
|
logger(V_BREW, "Determining inital state\n");
|
|
|
- //determine inital state
|
|
|
+ //determine initial state
|
|
|
if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
|
|
|
&& !halGetRelaisState(RELAIS_PUMP)) {
|
|
|
//wait for heat relais to switch
|
|
@@ -335,7 +353,7 @@ void *coffeeThread(void *threadid) {
|
|
|
|
|
|
switch (getSigValue(MODE_MENU)) {
|
|
|
case SigInt0Rls:
|
|
|
-
|
|
|
+ changePage(PAGE_RESETKWH_VAL);
|
|
|
break;
|
|
|
case SigRotCW:
|
|
|
changePage(PAGE_CHANGEPREINF);
|
|
@@ -345,6 +363,28 @@ void *coffeeThread(void *threadid) {
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ case PAGE_RESETKWH_VAL:
|
|
|
+ if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
+ pause();
|
|
|
+
|
|
|
+ switch (getSigValue(MODE_MENU)) {
|
|
|
+ case SigInt0Rls:
|
|
|
+ //Reset the KWH counter
|
|
|
+
|
|
|
+ changePage(PAGE_RESETKWH);
|
|
|
+ break;
|
|
|
+ case SigInt1Rls:
|
|
|
+ //Abort reseting the KWH counter
|
|
|
+ changePage(PAGE_RESETKWH);
|
|
|
+ break;
|
|
|
+ case SigRotCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ case SigRotCCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case PAGE_CHANGEPREINF:
|
|
|
if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
pause();
|
|
@@ -361,6 +401,22 @@ void *coffeeThread(void *threadid) {
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ case PAGE_CHANGEPREINF_VAL:
|
|
|
+ if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
+ pause();
|
|
|
+
|
|
|
+ switch (getSigValue(MODE_MENU)) {
|
|
|
+ case SigInt0Rls:
|
|
|
+ changePage(PAGE_CHANGEPREINF);
|
|
|
+ break;
|
|
|
+ case SigRotCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ case SigRotCCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case PAGE_CHANGEBREWTIME:
|
|
|
if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
pause();
|
|
@@ -377,6 +433,22 @@ void *coffeeThread(void *threadid) {
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ case PAGE_CHANGEBREWTIME_VAL:
|
|
|
+ if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
+ pause();
|
|
|
+
|
|
|
+ switch (getSigValue(MODE_MENU)) {
|
|
|
+ case SigInt0Rls:
|
|
|
+ changePage(PAGE_CHANGEBREWTIME);
|
|
|
+ break;
|
|
|
+ case SigRotCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ case SigRotCCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case PAGE_CHANGEBREWML:
|
|
|
if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
pause();
|
|
@@ -393,6 +465,22 @@ void *coffeeThread(void *threadid) {
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ case PAGE_CHANGEBREWML_VAL:
|
|
|
+ if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
+ pause();
|
|
|
+
|
|
|
+ switch (getSigValue(MODE_MENU)) {
|
|
|
+ case SigInt0Rls:
|
|
|
+ changePage(PAGE_CHANGEBREWML);
|
|
|
+ break;
|
|
|
+ case SigRotCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ case SigRotCCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case PAGE_CHANGESOAKTIME:
|
|
|
if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
pause();
|
|
@@ -409,6 +497,22 @@ void *coffeeThread(void *threadid) {
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
+ case PAGE_CHANGESOAKTIME_VAL:
|
|
|
+ if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
+ pause();
|
|
|
+
|
|
|
+ switch (getSigValue(MODE_MENU)) {
|
|
|
+ case SigInt0Rls:
|
|
|
+ changePage(PAGE_CHANGESOAKTIME);
|
|
|
+ break;
|
|
|
+ case SigRotCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ case SigRotCCW:
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
case PAGE_LEAVESETTINGS:
|
|
|
if (SigValueEmpty() && mode == MODE_MENU)
|
|
|
pause();
|
|
@@ -909,6 +1013,13 @@ time_t * getDescTimestamp (void) {
|
|
|
return &descRawTimestamp;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Returns the preinfusion amount in ml of the coffee config
|
|
|
+ */
|
|
|
+uint16_t getAmountPreinfusion (void) {
|
|
|
+ return coffeeCFG.amount_preinfusion;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Counter for the menu timeout
|
|
@@ -963,20 +1074,33 @@ void writeBackCache(void) {
|
|
|
logger(V_BREW, "Writing back cache...\n");
|
|
|
logger(V_BREW, "Writing back brewCounter with %d\n", brewCounter);
|
|
|
if (sqlSetConf(CFGbrewcounter, brewCounter)) {
|
|
|
- logger_error("coffee.cpp: Couldn't write brewcounter to database");
|
|
|
- return;
|
|
|
+ logger_error("coffee.cpp: Couldn't write brewcounter to database!\n");
|
|
|
}
|
|
|
logger(V_BREW, "Writing back descaling brew counter %d\n", descBrewcount);
|
|
|
if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
|
|
|
- logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
|
|
|
- return;
|
|
|
+ logger_error("coffee.cpp: Couldn't write descaling brewcount to database!\n");
|
|
|
}
|
|
|
struct tm * timeinfo;
|
|
|
timeinfo = localtime(&descRawTimestamp);
|
|
|
logger(V_BREW, "Writing back descaling timestamp %d-%d-%d %d:%d\n", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year+1900, timeinfo->tm_hour, timeinfo->tm_min);
|
|
|
if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
|
|
|
- logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
|
|
|
- return;
|
|
|
+ logger_error("coffee.cpp: Couldn't write descaling timestamp to database!\n");
|
|
|
+ }
|
|
|
+ logger(V_BREW, "Writing back CFGAmountPreinfusion with %d", coffeeCFG.amount_preinfusion);
|
|
|
+ if (sqlSetConf(CFGAmountPreinfusion, coffeeCFG.amount_preinfusion)) {
|
|
|
+ logger_error("coffee.cpp: Couldn't write CFGAmountPreinfusion to database!\n");
|
|
|
+ }
|
|
|
+ logger(V_BREW, "Writing back CFGTimeSoak with %d", coffeeCFG.time_soak);
|
|
|
+ if (sqlSetConf(CFGTimeSoak, coffeeCFG.time_soak)) {
|
|
|
+ logger_error("coffee.cpp: Couldn't write CFGTimeSoak to database!\n");
|
|
|
+ }
|
|
|
+ logger(V_BREW, "Writing back CFGTimeInfusion with %d", coffeeCFG.time_infusion);
|
|
|
+ if (sqlSetConf(CFGTimeInfusion, coffeeCFG.time_infusion)) {
|
|
|
+ logger_error("coffee.cpp: Couldn't write CFGTimeInfusion to database!\n");
|
|
|
+ }
|
|
|
+ logger(V_BREW, "Writing back CFGAmountDblEspresso with %d", coffeeCFG.amount_dblespresso);
|
|
|
+ if (sqlSetConf(CFGAmountDblEspresso, coffeeCFG.amount_dblespresso)) {
|
|
|
+ logger_error("coffee.cpp: Couldn't write CFGAmountDblEspresso to database!\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1017,8 +1141,9 @@ void coffeeBrew(void) {
|
|
|
logger(V_BREW, "Starting preinfusion...\n");
|
|
|
halPumpOn();
|
|
|
while (halGetFlow() < AMOUNT_PREINFUSION && halGetFlowTime() < TIME_PREINFUSION) {
|
|
|
- //TODO don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
|
|
|
- coffeeNap(0, 50000000);
|
|
|
+ //TODO-Done don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
|
|
|
+ //coffeeNap(0, 50000000);
|
|
|
+ usleep(50000);
|
|
|
if (getSigValue(MODE_STATE) == SigInt0Rls) {
|
|
|
halPumpOff();
|
|
|
return;
|
|
@@ -1031,7 +1156,8 @@ void coffeeBrew(void) {
|
|
|
*/
|
|
|
brewTimer.start();
|
|
|
while (brewTime < TIME_SOAK) {
|
|
|
- coffeeNap(1, 100000000);
|
|
|
+ //coffeeNap(1, 100000000);
|
|
|
+ usleep(100000);
|
|
|
if (getSigValue(MODE_STATE) == SigInt0Rls) {
|
|
|
brewTimer.stop();
|
|
|
brewTime = 0;
|
|
@@ -1047,7 +1173,8 @@ void coffeeBrew(void) {
|
|
|
logger(V_BREW, "Starting infusion...\n");
|
|
|
halPumpOn();
|
|
|
while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
|
|
|
- coffeeNap(1, 100000000);
|
|
|
+ //coffeeNap(1, 100000000);
|
|
|
+ usleep(100000);
|
|
|
if (getSigValue(MODE_STATE) == SigInt0Rls){
|
|
|
halPumpOff();
|
|
|
return;
|