Pārlūkot izejas kodu

full integration of state STATE_FULLTANK and bugfix with soft off. Included the display background power switch in hal

Sebastian 7 gadi atpakaļ
vecāks
revīzija
59e301fe74
4 mainītis faili ar 105 papildinājumiem un 52 dzēšanām
  1. 86 51
      CoffeeCode/coffee.cpp
  2. 1 1
      CoffeeCode/coffee.h
  3. 15 0
      CoffeeCode/hal.cpp
  4. 3 0
      CoffeeCode/hal.h

+ 86 - 51
CoffeeCode/coffee.cpp

@@ -31,7 +31,7 @@ timer brewTimer(&brewTimeHandler);
 
 uint64_t totalHeatingTime; //local copies of the corresponding database entries
 uint16_t brewCounter;
-coffee_status_t lastState;
+bool initalHeating;
 
 
 const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
@@ -58,6 +58,7 @@ void *coffeeThread(void *threadid) {
 	brewTime = 0;
 
 	lastState = STATE_OFF;
+	initalHeating = true;
 
 	//read the database values
 	if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
@@ -82,6 +83,7 @@ void *coffeeThread(void *threadid) {
 		if (halIsHeating()) { //Heating is on
 			changeState(STATE_INITALHEATING);
 		} else {
+			initalHeating = false;
 			changeState(STATE_IDLE);
 		}
 	} else if (halGetRelaisState(RELAIS_PUMP)) {
@@ -99,21 +101,25 @@ void *coffeeThread(void *threadid) {
 		 *
 		 */
 		case STATE_OFF:
+			halMachineOff();
+			writeBackCache();
+
 			if (SigValueEmpty())
 				pause();
 			if (getSigValue() == SigInt0Rls) {
-				if (!halProxSensorCovered()) { //Check waterlevel in gray water tank
-					//turn machine on
-					halMachineOn();
-					sleep(1);
-					if (halIsHeating()) { //check if System starts to heat when turned on
-						changeState(STATE_INITALHEATING);
-					} else {
-						changeState(STATE_IDLE);
-					}
+				//Check waterlevel in gray water tank
+				//turn machine on
+				halMachineOn();
+				sleep(1);
+				if (halIsHeating()) { //check if System starts to heat when turned on
+					initalHeating = true;
+					changeState(STATE_INITALHEATING);
 				} else {
-					logger_error("Empty Tank please!\n");
-					changeState(STATE_ERROR);
+					changeState(STATE_IDLE);
+				}
+				if (halProxSensorCovered()) {
+					logger_error("Empty Tank please!\n");//change the state again to go back to the last state afterwards
+					changeState(STATE_FULLTANK);
 				}
 				break;
 			}
@@ -122,25 +128,24 @@ void *coffeeThread(void *threadid) {
 			 *
 			*/
 		case STATE_WAIT_OFF:
-			if (halIsHeating()) {
-				halMachineOff();
-				coffeeIncreaseHeatingTime(halgetHeatingTime());
-				writeBackCache();
-				changeState(STATE_OFF);
-			}
 			if (SigValueEmpty())
 				pause();
 			switch (getSigValue()) {
 			case SigPressOpn:
-				halMachineOff();
+				usleep(100000);//wait so no load will be switched
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
-				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 
 			case SigInt0Psh:
 			case SigInt1Psh:
-				changeState(STATE_INITALHEATING);
+				if (halProxSensorCovered()) {
+					changeState(STATE_FULLTANK);
+				} else if (initalHeating) {
+					changeState(STATE_INITALHEATING);
+				} else {
+					changeState(STATE_HEATING);
+				}
 				break;
 			}
 			break;
@@ -148,14 +153,14 @@ void *coffeeThread(void *threadid) {
 			 *
 			 */
 		case STATE_INITALHEATING:
+			initalHeating = true;
+
 			if (SigValueEmpty())
 				pause();
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
 				//Turn machine off again
-				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
-				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 
@@ -163,8 +168,13 @@ void *coffeeThread(void *threadid) {
 				changeState(STATE_WAIT_OFF);
 				break;
 
+			case SigProxCvrd:
+				changeState(STATE_FULLTANK);
+				break;
+
 			case SigPressOpn:
 				//Inital heating finished
+				initalHeating = false;
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
@@ -180,9 +190,7 @@ void *coffeeThread(void *threadid) {
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
 				//Turn machine _immediately_ off again
-				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
-				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 
@@ -201,6 +209,10 @@ void *coffeeThread(void *threadid) {
 				changeState(STATE_BREW);
 				break;
 
+			case SigProxCvrd:
+				changeState(STATE_FULLTANK);
+				break;
+
 			case SigBrewOn:
 				//someone brews manually
 				changeState(STATE_BREWMANUAL);
@@ -216,14 +228,11 @@ void *coffeeThread(void *threadid) {
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
 				//turn machine _immediately_ off
-				halMachineOff();
-				writeBackCache();
 				changeState(STATE_OFF);
 				break;
 
 			case SigInt1Rls:
-				//turn machine off when heating is finished
-				changeState(STATE_WAIT_OFF);
+				changeState(STATE_OFF);
 				break;
 
 			case SigPressCls:
@@ -231,13 +240,11 @@ void *coffeeThread(void *threadid) {
 				break;
 
 			case SigInt0Psh:
-				//start to brew a delicious coffee
-				if(!halProxSensorCovered()){
-					changeState(STATE_BREW);
-				}
-				else {
-					changeState(STATE_ERROR);
-				}
+				changeState(STATE_BREW);
+				break;
+
+			case SigProxCvrd:
+				changeState(STATE_FULLTANK);
 				break;
 
 			case SigBrewOn:
@@ -250,13 +257,22 @@ void *coffeeThread(void *threadid) {
 			 *
 			 */
 		case STATE_BREW:
-			coffeeBrew();
-			logger(V_BREW, "Finishing brewing\n");
-			if(!halProxSensorCovered()){
-				changeState(STATE_IDLE);
-			}
-			else {
-				changeState(STATE_ERROR);
+			//make sure the tank is not full
+			if (halProxSensorCovered()) {
+				changeState(STATE_FULLTANK);
+				logger_error("coffee.cpp: Full tank detection failed..\n");
+			} else {
+				coffeeBrew();
+				logger(V_BREW, "Finishing brewing\n");
+				if (!halProxSensorCovered()) {
+					if (halIsHeating()) {
+						changeState(STATE_HEATING);
+					} else {
+						changeState(STATE_IDLE);
+					}
+				} else {
+					changeState(STATE_FULLTANK);
+				}
 			}
 			break;
 
@@ -276,6 +292,27 @@ void *coffeeThread(void *threadid) {
 				pause();
 			break;
 
+			/*
+			 * Full tank is detected at the beginning and the end of a brewing process, during
+			 * idle times, initial heating and heating
+			 */
+		case STATE_FULLTANK:
+			if (SigValueEmpty())
+				pause();
+			switch (getSigValue()) {
+			case SigInt1Psh:
+			case SigInt0Psh:
+				if(halIsHeating() && initalHeating){
+					changeState(STATE_INITALHEATING);
+				} else if (halIsHeating() && !initalHeating){
+					changeState(STATE_HEATING);
+				} else {
+					changeState(STATE_IDLE);
+				}
+				break;
+			}
+			break;
+
 			/*
 			 *
 			 */
@@ -283,9 +320,12 @@ void *coffeeThread(void *threadid) {
 			if (SigValueEmpty())
 				pause();
 		switch (getSigValue()) {
-		case SigInt1Psh:
-		case SigInt0Psh:
-			changeState(RETURN_STATE);
+		case SigInt1RlsLong:
+		case SigInt0RlsLong:
+			if(halIsHeating()){
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
+			}
+			changeState(STATE_OFF);
 			break;
 		}
 		}
@@ -329,11 +369,7 @@ bool SigValueEmpty(void) {
  * @param newState
  */
 void changeState(coffee_status_t newState) {
-	if(newState == RETURN_STATE)
-		newState = lastState;
-
 	logger(V_BREW, "Changing state to %s\n", StateName[newState]);
-	lastState = state;
 	state = newState;
 	event_trigger("statechange", &state, sizeof(state));
 }
@@ -385,7 +421,6 @@ void writeBackCache(void){
  */
 void coffeeBrew(void) {
 	coffeeIncreaseBrewCounter();
-
 	/*
 	 * Preinfusion
 	 */

+ 1 - 1
CoffeeCode/coffee.h

@@ -22,7 +22,7 @@ typedef enum {
 	STATE_CLEANING,
 	STATE_ERROR,
 	STATE_WAIT_OFF,
-	RETURN_STATE
+	STATE_FULLTANK
 } coffee_status_t;
 
 extern const char* StateName[];

+ 15 - 0
CoffeeCode/hal.cpp

@@ -44,6 +44,7 @@ void halInit(void) {
 	pinMode(PIN_INT0, INPUT);
 	pinMode(PIN_INT1, INPUT);
 	pinMode(PIN_FLOW, INPUT);
+	pinMode(PIN_DISP, OUTPUT);
 
 	if (optPower) {
 		halMachineOn();
@@ -89,6 +90,20 @@ void halRelaisOn(int relais) {
 	halRelaisSet(relais, LOW);
 }
 
+/**
+ * Turn the display off
+ */
+void halDisplayOff(){
+	digitalWrite(PIN_DISP, LOW);
+}
+
+/**
+ * Turn the display on
+ */
+void halDisplayOn(){
+	digitalWrite(PIN_DISP, HIGH);
+}
+
 /**
  * Switches relais off
  * @param relais Relais ID

+ 3 - 0
CoffeeCode/hal.h

@@ -16,6 +16,7 @@
 #define PIN_INT0		2 // Top button
 #define PIN_INT1		0 // Bottom button
 #define PIN_FLOW		3 // Flow sensor
+#define PIN_DISP		4
 #define FLOW_ML_PULSE	(1000.0/990) // Flow sensor: volume (ml) per pulse
 
 #define TIME_BUTTONLONGPRESS	3	//Time in s until a Signal for a long pressed button is sent
@@ -55,6 +56,8 @@ enum HalSig {
 
 
 void halInit(void);
+void halDisplayOff();
+void halDisplayOn();
 void halRelaisOn(int relais);
 void halRelaisOff(int relais);
 void halRelaisSet(int relais, int state);