Selaa lähdekoodia

added the descaling feature

Sebastian 7 vuotta sitten
vanhempi
commit
6bdf54b98d
5 muutettua tiedostoa jossa 96 lisäystä ja 7 poistoa
  1. 1 1
      CoffeeCode/buildno
  2. 69 4
      CoffeeCode/coffee.cpp
  3. 4 1
      CoffeeCode/coffee.h
  4. 3 1
      CoffeeCode/database.h
  5. 19 0
      CoffeeCode/display.cpp

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-236
+239

+ 69 - 4
CoffeeCode/coffee.cpp

@@ -35,6 +35,8 @@ uint64_t totalHeatingTime; //local copies of the corresponding database entries
 uint16_t brewCounter;
 bool initalHeating;
 bool descaling; //flag to indicate descaling and cleaning
+uint16_t descBrewcount;
+time_t descRawTimestamp;
 
 const char* PageName[] = { "SoftOff", "Kill", "Stats", "Temp", "Clean", "Demo",
 		"Exit" };
@@ -67,19 +69,30 @@ void *coffeeThread(void *threadid) {
 	page = PAGE_SOFTOFF;
 	descaling = false;
 
+
 	//read the database values
 	if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
-		logger_error(
-				"coffee.cpp: Couldn't read the heating time from the database");
+		logger_error("coffee.cpp: Couldn't read the heating time from the database");
 		//pthread_exit(EXIT_SUCCESS);
 		exit(EXIT_FAILURE);
 	}
 	if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
-		logger_error(
-				"coffee.cpp: Couldn't read the brew counter from the database");
+		logger_error("coffee.cpp: Couldn't read the brew counter from the database");
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
+	}
+	if (!(descRawTimestamp = (time_t) sqlGetConf(CFGDescTimestamp))) {
+		logger_error("coffee.cpp: Couldn't read the descaling time from the database");
 		//pthread_exit(EXIT_SUCCESS);
 		exit(EXIT_FAILURE);
 	}
+	if (!(descBrewcount = sqlGetConf(CFGDescBrewCount))) {
+		logger_error("coffee.cpp: Couldn't read the descaling brewcount time from the database");
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
+	}
+
+	checkDescaling();
 
 	event_subscribe("terminate", &coffeeTerminate);
 
@@ -425,6 +438,7 @@ void *coffeeThread(void *threadid) {
 				logger_error("coffee.cpp: Full tank detection failed..\n");
 			} else {
 				coffeeBrew();
+				checkDescaling();
 				logger(V_BREW, "Finishing brewing\n");
 				if (!halProxSensorCovered()) {
 					if (halIsHeating()) {
@@ -455,6 +469,7 @@ void *coffeeThread(void *threadid) {
 			if (!halProxSensorCovered()) {
 				//execute the cleaning procedure
 				coffeeClean();
+				updateDescaling();
 				if (halIsHeating()) {
 					changeState(STATE_HEATING);
 				} else {
@@ -649,6 +664,14 @@ void writeBackCache(void) {
 		logger_error("coffee.cpp: Couldn't write heating time to database");
 		return;
 	}
+	if (sqlSetConf(CFGDescBrewCount, descBrewcount)) {
+		logger_error("coffee.cpp: Couldn't write descaling brewcount to database");
+		return;
+	}
+	if (sqlSetConf(CFGDescTimestamp, (uint64_t)descRawTimestamp)) {
+		logger_error("coffee.cpp: Couldn't write descaling timestamp to database");
+		return;
+	}
 }
 
 /*
@@ -663,6 +686,7 @@ void coffeeClean(void) {
 		sleep(15);
 	}
 	descaling = false;
+	event_trigger("descaling", &descaling, sizeof(bool));
 }
 
 /**
@@ -743,3 +767,44 @@ void coffeeIncreaseBrewCounter(void) {
 void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
 	totalHeatingTime += heatingTime;
 }
+
+
+
+/**
+ * Checks if the descaling is necessary
+ * uses descBrewcount and descTimestamp
+ */
+void checkDescaling(){
+	time_t rawtime;
+	time(&rawtime);
+	double diffseconds = difftime(rawtime, descRawTimestamp);
+	diffseconds /= 24*60*60;
+
+	if((brewCounter - descBrewcount) >= DIRTY_ESPRESSO) {
+		logger(V_BREW, "Descaling necessary due to quantity: %d\n", brewCounter - descBrewcount);
+		descaling = true;
+		event_trigger("descaling", &descaling, sizeof(bool));
+
+	}
+	if(diffseconds >= DIRTY_TIME) {
+		logger(V_BREW, "Descaling necessary due to time in days: %d\n", diffseconds);
+		descaling = true;
+		event_trigger("descaling", &descaling, sizeof(bool));
+	}
+}
+
+/**
+ * updates the corresponding variables after a descaling process
+ */
+void updateDescaling(){
+	descBrewcount = brewCounter;
+	time_t newDesTimestamp;
+	time(&newDesTimestamp);
+	if(newDesTimestamp == -1){
+		logger(V_BREW, "Whoops, couldn't retrieve new descaling timestamp\n");
+	}
+	else {
+		descRawTimestamp = newDesTimestamp;
+	}
+}
+

+ 4 - 1
CoffeeCode/coffee.h

@@ -47,7 +47,8 @@ extern const char* PageName[];
 #define TIME_SOAK			5000 	//Time between preinfusion and infusion in ms
 #define TIME_INFUSION 		25000	//Infusion time in ms
 #define AMOUNT_DBLESPRESSO	59.0	//Size of a double espresso in ml
-#define DIRTY_ESPRESSO		35		//Number of espressos till the next cleaning
+#define DIRTY_ESPRESSO		35		//Number of espressi until the next cleaning
+#define DIRTY_TIME			90		//Number of days until the next cleaning
 void *coffeeThread(void *threadid);
 
 void coffeeHandler(int signum, siginfo_t *siginfo, void *context);
@@ -67,5 +68,7 @@ void coffeeClean(void);
 void stopBrewing(void);
 void coffeeIncreaseBrewCounter(void);
 void coffeeIncreaseHeatingTime(uint64_t heatingTime);
+void checkDescaling(void);
+void updateDescaling(void);
 
 #endif /* COFFEE_H_ */

+ 3 - 1
CoffeeCode/database.h

@@ -21,7 +21,9 @@ using namespace std;
 typedef enum {
 	CFGbrewcounter = 1,
 	CFGHeatingTime = 2,
-	CFGdisplaylang = 3
+	CFGdisplaylang = 3,
+	CFGDescTimestamp = 4,
+	CFGDescBrewCount = 5
 } config_key_t;
 
 int sqlOpen();

+ 19 - 0
CoffeeCode/display.cpp

@@ -29,6 +29,7 @@ volatile int elapsedCnt = 0;
 coffee_status_t coffeeState = STATE_OFF;
 coffee_mode_t coffeeMode = MODE_STATE;
 coffee_menuPage_t coffeePage = PAGE_SOFTOFF;
+bool coffeeDescaling = false;
 
 /**
  * Prints out the current time in a centered position
@@ -179,6 +180,17 @@ void displayPageUpdated(event_t *event) {
 	}
 }
 
+/**
+ * Updates the current descaling status of the machine
+ */
+void displayDescaling(event_t *event) {
+	if (event->len != sizeof(bool)) {
+		logger_error("Invalid use of event %s\n", event->event);
+		return;
+	}
+	coffeeDescaling = (bool) event->data;
+}
+
 /**
  * Handles cleanup before program termination
  */
@@ -204,6 +216,7 @@ void* displayThread(void* threadid) {
 	event_subscribe("modechange", &displayModeUpdated);
 	event_subscribe("pagechange", &displayPageUpdated);
 	event_subscribe("terminate", &displayTerminate);
+	event_subscribe("descaling", &displayDescaling);
 	while (1) {
 		pause();
 		if (1) {
@@ -348,6 +361,12 @@ void displayRefresh(void) {
 			displayPrintTime(1);
 			break;
 		}
+		//draw the descaling star
+		if(coffeeDescaling){
+			lcdPosition(lcd, 15, 0);
+			lcdPutchar(lcd, '*');
+		}
+
 	} else if (coffeeMode == MODE_MENU) {
 		switch (coffeePage) {
 		case PAGE_SOFTOFF: