Quellcode durchsuchen

created divider calculation function, fixed calculation of heating time

Sebastian Vendt vor 5 Jahren
Ursprung
Commit
b4712cf2bb
8 geänderte Dateien mit 85 neuen und 102 gelöschten Zeilen
  1. 1 1
      CoffeeCode/buildno
  2. 2 40
      CoffeeCode/coffee.cpp
  3. 0 2
      CoffeeCode/coffee.h
  4. 60 52
      CoffeeCode/hal.cpp
  5. 4 2
      CoffeeCode/hal.h
  6. 2 2
      CoffeeCode/stripe.cpp
  7. 13 1
      CoffeeCode/timer.cpp
  8. 3 2
      CoffeeCode/timer.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-359
+362

+ 2 - 40
CoffeeCode/coffee.cpp

@@ -32,7 +32,6 @@ int menuTimeout;
 timer menuTimer(&menuTimerHandler);
 timer brewTimer(&brewTimerHandler);
 
-uint64_t totalHeatingTime; //local copies of the corresponding database entries
 uint16_t brewCounter;
 uint16_t brewTime;
 uint16_t currentCleanCycle;
@@ -65,11 +64,11 @@ void *coffeeThread(void *threadid) {
 
 	currentCleanCycle = 0;
 
-	menuTimer.setDivider(4);
+	menuTimer.setDivider(ms2Divider(200));
 	menuTimer.stop();
 	menuTimeout = 0;
 
-	brewTimer.setDivider(2);
+	brewTimer.setDivider(ms2Divider(200));
 	brewTimer.stop();
 	brewTime = 0;
 
@@ -78,13 +77,6 @@ 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\n");
-		//pthread_exit(EXIT_SUCCESS);
-		exit(EXIT_FAILURE);
-	}
 	if (!(brewCounter = sqlGetConf(CFGbrewcounter))) {
 		logger_error("coffee.cpp: Couldn't read the brew counter from the database\n");
 		//pthread_exit(EXIT_SUCCESS);
@@ -166,9 +158,6 @@ void *coffeeThread(void *threadid) {
 
 			switch (getSigValue(MODE_MENU)) {
 			case SigInt0Rls:
-				if (halIsHeating()) {
-					coffeeIncreaseHeatingTime(halgetHeatingTime());
-				}
 				changeState(STATE_OFF);
 				leaveMenu();
 				break;
@@ -381,7 +370,6 @@ void *coffeeThread(void *threadid) {
 			switch (getSigValue(MODE_STATE)) {
 			case SigPressOpn:
 				coffeeNap(1,0); //wait so no load will be switched
-				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_OFF);
 				break;
 
@@ -425,7 +413,6 @@ void *coffeeThread(void *threadid) {
 			case SigPressOpn:
 				//Inital heating finished
 				initalHeating = false;
-				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
 
@@ -460,7 +447,6 @@ void *coffeeThread(void *threadid) {
 //			break;
 
 			case SigPressOpn:
-				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
 
@@ -626,9 +612,6 @@ void *coffeeThread(void *threadid) {
 				pause();
 			switch (getSigValue(MODE_STATE)) {
 			case SigInt0RlsLong:
-				if (halIsHeating()) {
-					coffeeIncreaseHeatingTime(halgetHeatingTime());
-				}
 				changeState(STATE_OFF);
 				break;
 			}
@@ -772,13 +755,6 @@ uint16_t getBrewCounter(void) {
 	return brewCounter;
 }
 
-/**
- * Returns the total heating time in seconds
- */
-uint64_t getTotalHeatingTime(void) {
-	return totalHeatingTime;
-}
-
 /**
  * Returns the value of the brewcounter when the last descaling happened
  */
@@ -850,11 +826,6 @@ void writeBackCache(void) {
 		logger_error("coffee.cpp: Couldn't write brewcounter to database");
 		return;
 	}
-	logger(V_BREW, "Writing back total heating Time with %lld sec\n", totalHeatingTime);
-	if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
-		logger_error("coffee.cpp: Couldn't write heating time to database");
-		return;
-	}
 	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");
@@ -958,15 +929,6 @@ void coffeeIncreaseBrewCounter(void) {
 	brewCounter++;
 }
 
-/**
- *
- */
-void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
-	totalHeatingTime += heatingTime;
-}
-
-
-
 /**
  * Checks if the descaling is necessary
  * uses descBrewcount and descTimestamp

+ 0 - 2
CoffeeCode/coffee.h

@@ -72,7 +72,6 @@ uint16_t getLastBrewTime(void);
 int getSigValue(coffee_mode_t mode);
 uint16_t getCurrentCleanCycle(void);
 uint16_t getBrewCounter(void);
-uint64_t getTotalHeatingTime(void);
 uint16_t getDescBrewCounter (void);
 time_t * getDescTimestamp (void);
 void brewTimerHandler(void);
@@ -86,7 +85,6 @@ void coffeeBrew(void);
 void coffeeClean(void);
 void stopBrewing(void);
 void coffeeIncreaseBrewCounter(void);
-void coffeeIncreaseHeatingTime(uint64_t heatingTime);
 void checkDescaling(void);
 int16_t checkDirtyEspresso (void);
 int16_t checkDirtyTime (void);

+ 60 - 52
CoffeeCode/hal.cpp

@@ -45,6 +45,9 @@ int lastFlowcnt = 0;
 int flowResetValue = -1;
 bool brewmanual = false;
 
+time_t heatingCycleStart = 0;
+uint64_t totalHeatingTime = 0; //local copies of the corresponding database entries
+
 int Int0Time, Int1Time;
 int idleCounter;
 bool idle;
@@ -68,10 +71,10 @@ timer idleTimer(&halIdleTimerHandler);
 timer flowResetTimer (&flowResetTimerHandler);
 timer flowTimer(&halFlowTimerHandler);
 
-time_t heatingCycle[] = {0, 0};
+
 timespec flowTimestep[] = {{0,0},{0,0}};
 uint8_t flowIndex = 0;
-int16_t tickCounter = 0;
+int16_t tickCounter = 0; //rotary encoder
 uint16_t flowtime = 0;
 uint16_t lastFlowTime = 0;
 bool brewSigFired = false;
@@ -104,13 +107,19 @@ void halInit(void) {
 	pinMode(PIN_ROTARY2, INPUT);
 
 
-	idleTimer.setDivider(1200); //1 min
+	idleTimer.setDivider(ms2Divider(60000));
 	idleCounter = 0;
 	idle = false;
 	clock_gettime(CLOCK_REALTIME, &flowTimestep[0]);
 	clock_gettime(CLOCK_REALTIME, &flowTimestep[1]);
 	halDisplayOn();
 
+	if (!(totalHeatingTime = sqlGetConf(CFGHeatingTime))) {
+		logger_error("hal.cpp: Couldn't read the total heating time from the database\n");
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
+	}
+
 	if (optPower) {
 		halMachineOn();
 	} else {
@@ -120,13 +129,14 @@ void halInit(void) {
 
 	pinState[3] = halIsHeating();
 
-	Int0Timer.setDivider(4); //200ms
-	Int1Timer.setDivider(4);
+	Int0Timer.setDivider(ms2Divider(200));
+	Int1Timer.setDivider(ms2Divider(200));
 	Int0Time = 0;
 	Int1Time = 0;
 
-	flowResetTimer.setDivider(20); //1000ms
-	flowTimer.setDivider(4); // 100ms
+
+	flowResetTimer.setDivider(ms2Divider(TIME_FLOWRESET));
+	flowTimer.setDivider(ms2Divider(200));
 
 	flagIgnoreRlsInt0 = false;
 	flagIgnoreRlsInt1 = false;
@@ -157,15 +167,7 @@ void halInit(void) {
 		logger_error("Unable to setup ISRRotary2: %s\n", strerror(errno));
 		return;
 	}
-
-	//TODO when machine is turned off above the logcycle 1 is written back from cache...
-	//TODO logcycle is not used atm
-	if (!(logcycle = sqlGetConf(CFGSweepCounter))) {
-		logger_error("hal.cpp: Couldn't read the  logcycle counter from the database\n");
-		//pthread_exit(EXIT_SUCCESS);
-		exit(EXIT_FAILURE);
-	}
-	logger(V_BASIC, "hal.cpp Initialized hal\n");
+	logger(V_BASIC, "hal.cpp: Initialized\n");
 }
 
 /**
@@ -411,36 +413,36 @@ void halIntFlow(void) {
  * Reading heating time via the getHeatingTime function
  */
 void halIntPressure(void) {
-	logger(V_HAL, "IntPressure Control triggered\n");
+	delay(DELAY_DEBOUNCE);
 	if (halIsHeating() && !pinState[3]) {
+		logger(V_HAL, "hal.cpp: Pressure Control closed\n");
 		pinState[3] = 1;
-		time(&heatingCycle[0]);
+		halStartHeatingTime();
 		halSendSignal(SigPressCls);
 	} else if(!halIsHeating() && pinState[3]) {
+		logger(V_HAL, "hal.cpp: Pressure Control opened\n");
 		pinState[3] = 0;
-		time(&heatingCycle[1]);
+		halStopHeatingTime();
 		halSendSignal(SigPressOpn);
 	}
 }
 
 /**
- * Function to read the heating time in sec
- * If called during a heating process, it returns the time elapsed since the heating started
- * If called after a heating process, it returns the total time elapsed during the heating cycle
+ *
  */
-double halgetHeatingTime(void){
-	//TODO check return value on negative times
-	//TODO move the tracking of the heating time into hal.cpp and fix issue of wrong tracking -> inital pressure close is not
-	//recognized and so heatingCycle has no time
-	//quickfix:
-	return 0;
-	if (halIsHeating()) {
-		logger(V_HAL, "Hot Heating Time: %f\n", difftime(time(NULL), heatingCycle[0]));
-		return difftime(time(0), heatingCycle[0]);
-	}
-	else {
-		logger(V_HAL, "Heating time: %f\n", difftime(heatingCycle[1], heatingCycle[0]));
-		return difftime(heatingCycle[1], heatingCycle[0]);
+void halStartHeatingTime(void) {
+	time(&heatingCycleStart);
+}
+
+/**
+ *
+ */
+void halStopHeatingTime(void) {
+	if (heatingCycleStart != 0) { //only track time if stopwatch was started!
+		uint64_t timediff = (uint64_t) difftime(time(0), heatingCycleStart);
+		logger(V_HAL, "Heating time: %f\n", timediff);
+		totalHeatingTime += timediff;
+		heatingCycleStart = 0;
 	}
 }
 
@@ -511,16 +513,21 @@ void halResetFlow(void) {
 
 /**
  * Reads the status of the Pressure Control
- * @return 1 (true) for closed Pressure Control(heating) and 0 (false) for open
+ * @return 1 (true) for closed Pressure Control (heating) and 0 (false) for open
  */
 bool halIsHeating(void) {
-	if (digitalRead(PIN_PRESSURE_CTRL) == 0) {
-		return true;
-	} else {
-		return false;
-	}
+	//TODO: eventually rely on the pinState variable instead of reading the pin
+	//then the state of the pin needs to be set at the very beginning of the initialization
+	if (digitalRead(PIN_PRESSURE_CTRL) == 0) return true;
+	else return false;
 }
 
+/**
+ *
+ */
+uint64_t getTotalHeatingTime() {
+	return totalHeatingTime;
+}
 /**
  * Returns status of the proximity switch
  * @return 1 if the proximity switch is covered and 0 if uncovered
@@ -599,7 +606,6 @@ void halSendSignal(HalSig val) {
 	} catch (int e) {
 		logger_error("Whoops.. %d\n", e);
 	}
-
 }
 
 /**
@@ -609,8 +615,10 @@ void halMachineOn(void) {
 	halRelaisOn(RELAIS_HEAT);
 	halRelaisOff(RELAIS_PUMP);
 	halRelaisOn(RELAIS_POWER);
+
 	idleTimer.stop();
-	logger(V_HAL, "Turning machine on\n");
+	if(halIsHeating()) halStartHeatingTime();
+	logger(V_HAL, "hal.cpp: Turning machine on\n");
 }
 
 /**
@@ -623,15 +631,16 @@ void halMachineOff(void) {
 
 	idleCounter = 0;
 	idleTimer.start();
+	halStopHeatingTime();
 	halWriteBackCache();
-	logger(V_HAL, "Turning machine off\n");
+	logger(V_HAL, "hal.cpp: Turning machine off\n");
 }
 
 /**
  *
  */
 void halEnterIdle(void){
-	logger(V_HAL, "Entering Idle Mode\n");
+	logger(V_HAL, "hal.cpp: Entering Idle Mode\n");
 	idleTimer.stop();
 	halDisplayOff();
 	idle = true;
@@ -642,7 +651,7 @@ void halEnterIdle(void){
  */
 void halLeaveIdle(void){
 	idleCounter = 0;
-	logger(V_HAL, "Leaving Idle Mode\n");
+	logger(V_HAL, "hal.cpp: Leaving Idle Mode\n");
 	halDisplayOn();
 	idleTimer.start();
 	idle = false;
@@ -705,18 +714,17 @@ void timespec_diff(timespec *start, timespec *stop, timespec *result) {
  * Handler for Termination of the hal
  */
 void halTerminate(event_t *event){
+	halStopHeatingTime();
 	halWriteBackCache();
 }
 
 /*
- * writing back non volatile variables of the hal to the database: SweepCounter
+ * writing back non volatile variables of the hal to the database: totalHeatingTime
  */
 void halWriteBackCache(){
-	/*
-	if (sqlSetConf(CFGSweepCounter, logcycle)) {
-		logger_error("hal.cpp: Couldn't write logcycle to database");
+	if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
+		logger_error("hal.cpp: Couldn't write heating time to database");
 		return;
 	}
-	logger(V_BREW, "Writing back logcycle %d\n", logcycle);
-	*/
+	logger(V_BREW, "Writing back heating time: %lld sec\n", totalHeatingTime);
 }

+ 4 - 2
CoffeeCode/hal.h

@@ -29,7 +29,7 @@
 #define ROTARY_STEPSIZE		15
 
 
-
+#define TIME_FLOWRESET			600	//Wait time in ms to reset flow counter after the last received flow signal
 #define TIME_BUTTONLONGPRESS	3	//Time in s until a Signal for a long pressed button is sent
 #define BREW_MANUAL_TRIGGER		26	//specifies the ml after which a brew_manual signal is triggered
 /*
@@ -85,8 +85,10 @@ void flowResetTimerHandler(void);
 void halFlowTimerHandler(void);
 void halIntFlow(void);
 void halIntPressure(void);
-double halgetHeatingTime(void);
+void halStartHeatingTime(void);
+void halStopHeatingTime(void);
 void halIntProximity(void);
+uint64_t getTotalHeatingTime(void);
 float halGetFlow(void);
 uint16_t halGetFlowTime(void);
 float halGetLastFlow(void);

+ 2 - 2
CoffeeCode/stripe.cpp

@@ -48,7 +48,7 @@ void *stripeTimerHandler(void *threadid) {
 void *stripeThread(void *threadid) {
 	logger(V_STRIPE, "Initializing Stripe thread...\n");
 	stripeInit();
-	stripeTimer.setDivider(40);
+	stripeTimer.setDivider(ms2Divider(2000));
 	stripeTimer.stop();
 	logger(V_BASIC, "Initialized Stripe thread\n");
 
@@ -353,7 +353,7 @@ void stripeEffectHeating(int percent) {
 void stripeEffectPulse(stripe_color color) {
 	effectColor = color;
 	stripeSetTransient(TRANS_MEDIUM);
-	stripeTimer.setDivider(43);
+	stripeTimer.setDivider(ms2Divider(2150));
 	stripeTimer.start();
 }
 

+ 13 - 1
CoffeeCode/timer.cpp

@@ -11,6 +11,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
 //#include <sys/time.h>
 #include <time.h>
 #include <pthread.h>
@@ -107,7 +108,7 @@ void timer::stop() {
  * @param divider integer timer divider
  */
 
-void timer::setDivider(int divider) {
+void timer::setDivider(unsigned int divider) {
 	this->divider = divider;
 }
 
@@ -207,6 +208,17 @@ void initTimers(void) {
 	counter = 0;
 }
 
+/**
+ * Calculates the divider corresponding to the given period.
+ * If the given period does not match exactly to a divider, the
+ * next higher is returned!
+ */
+uint32_t ms2Divider(uint64_t millisec) {
+	uint32_t divider = (millisec * 1000) / (uint64_t) TIMER_DELAY_US;
+	if (((uint64_t) divider * TIMER_DELAY_US) < (millisec * 1000)) return ++divider;
+	else return divider;
+}
+
 /**
  * Stops all existing timers
  */

+ 3 - 2
CoffeeCode/timer.h

@@ -18,7 +18,7 @@ class timer {
 public:
 	timer(void (*handler)(void));
 	timer(void *(*handler)(void *));
-	void setDivider(int divider);
+	void setDivider(unsigned int divider);
 	int getDivider();
 	void call();
 	void start();
@@ -28,7 +28,7 @@ public:
 private:
 	void (*handler)(void);
 	void *(*thandler)(void *);
-	int divider;
+	unsigned int divider;
 	int id = 0; /**< needed to identify the threads */
 	bool active;
 	bool asThread; /**< defines if the handler should be called or not */
@@ -37,6 +37,7 @@ private:
 
 void initTimers(void);
 void stopTimers(void);
+uint32_t ms2Divider(uint64_t millisec);
 void *nullThread(void *threadid);
 
 #define SIG SIGRTMIN