Kaynağa Gözat

changed hal to use a sweepcounter for every flowLog

Sebastian 6 yıl önce
ebeveyn
işleme
81d2b16ec3
4 değiştirilmiş dosya ile 48 ekleme ve 5 silme
  1. 1 1
      CoffeeCode/buildno
  2. 2 0
      CoffeeCode/coffee.cpp
  3. 39 3
      CoffeeCode/hal.cpp
  4. 6 1
      CoffeeCode/hal.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-277
+279

+ 2 - 0
CoffeeCode/coffee.cpp

@@ -781,6 +781,7 @@ void coffeeClean(void) {
  */
 void coffeeBrew(void) {
 	coffeeIncreaseBrewCounter();
+	halIncreaseLogCycleCounter();
 	/*
 	 * Preinfusion
 	 */
@@ -825,6 +826,7 @@ void coffeeBrew(void) {
 		}
 	}
 	stopBrewing();
+	halIncreaseLogCycleCounter();
 	return;
 	//TODO: I want to see the total elapsed brewing time!!
 }

+ 39 - 3
CoffeeCode/hal.cpp

@@ -34,6 +34,7 @@ bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
 int pinState[4] = {1, 1, 1, 0};
 
 //sweep counter to log every brew
+uint16_t logcycle = 0;
 
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
@@ -92,6 +93,8 @@ void halInit(void) {
 	flagIgnoreRlsInt0 = false;
 	flagIgnoreRlsInt1 = false;
 
+	event_subscribe("terminate", &halTerminate);
+
 	if (wiringPiISR(PIN_INT0, INT_EDGE_BOTH, &halInt0) < 0) {
 		logger_error("Unable to setup ISR0: %s\n", strerror(errno));
 		return;
@@ -112,7 +115,12 @@ void halInit(void) {
 		logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
 		return;
 	}
-	// in long term a manual brew detection would be nice
+
+	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);
+	}
 }
 
 /**
@@ -276,7 +284,7 @@ void halIntFlow(void) {
 	clock_gettime(CLOCK_REALTIME, &flowTimestep[flowIndex]);
 	timespec_diff(&flowTimestep[((flowIndex + 1) % 2)], &flowTimestep[flowIndex], &deltaT);
 
-	if (sqlLogFlow(2, halGetFlow()*1000, deltaT.tv_sec * 1000 + deltaT.tv_nsec/1000000)) {
+	if (sqlLogFlow(logcycle, halGetFlow()*1000, deltaT.tv_sec * 1000 + deltaT.tv_nsec/1000000)) {
 		logger_error("hal.cpp: could not log flow to database!");
 		return;
 	}
@@ -453,6 +461,7 @@ void halMachineOff(void) {
 
 	idleCounter = 0;
 	idleTimer.start();
+	halWriteBackCache();
 	logger(V_HAL, "Turning machine off\n");
 }
 
@@ -480,12 +489,21 @@ void halLeaveIdle(void){
 /**
  * Wrapper function to turn the pump on
  * and to measure how long the pump is running
+ * @param cycle the number of the sweep in the database
  */
-void halPumpOn(void){
+void halPumpOn(){
 	halRelaisOn(RELAIS_PUMP);
 	clock_gettime(CLOCK_REALTIME, &pumpCycle[0]);
 }
 
+/**
+ *
+ */
+
+void halIncreaseLogCycleCounter(void){
+	logcycle++;
+}
+
 /**
  * Wrapper function to turn the pump off
  * and to measure how long the pump is running
@@ -543,3 +561,21 @@ void timespec_diff(timespec *start, timespec *stop, timespec *result) {
 	}
 	return;
 }
+
+/*
+ * Handler for Termination of the hal
+ */
+void halTerminate(event_t *event){
+	halWriteBackCache();
+}
+
+/*
+ * writing back non volatile variables of the hal to the database: SweepCounter
+ */
+void halWriteBackCache(){
+	if (sqlSetConf(CFGSweepCounter, logcycle)) {
+		logger_error("hall.cpp: Couldn't write logcycle to database");
+		return;
+	}
+	logger(V_BREW, "Writing back logcycle %d\n", logcycle);
+}

+ 6 - 1
CoffeeCode/hal.h

@@ -8,6 +8,8 @@
 #ifndef HAL_H_
 #define HAL_H_
 
+#include "events.h"
+
 #define RELAIS_HEAT		29
 #define RELAIS_PUMP		25
 #define RELAIS_POWER	28
@@ -84,9 +86,12 @@ void halInt1TimerHandler(void);
 void halIdleTimerHandler(void);
 void halEnterIdle (void);
 void halLeaveIdle (void);
-void halPumpOn(void);
+void halPumpOn();
 void halPumpOff(void);
+void halIncreaseLogCycleCounter(void);
 double halGetPumpTime(void);
 void timespec_diff(timespec *start, timespec *stop, timespec *result);
+void halTerminate(event_t *event);
+void halWriteBackCache(void);
 
 #endif /* HAL_H_ */