Parcourir la source

coffee: added change state method to coffee
hal: exit program if no signals sent to coffee thread

pek72 il y a 8 ans
Parent
commit
0c7aadb919
3 fichiers modifiés avec 38 ajouts et 35 suppressions
  1. 33 33
      CoffeeCode/coffee.cpp
  2. 1 0
      CoffeeCode/coffee.h
  3. 4 2
      CoffeeCode/hal.cpp

+ 33 - 33
CoffeeCode/coffee.cpp

@@ -35,7 +35,7 @@ timer brewTimer(&brewTimeHandler);
  */
 
 void *coffeeThread(void *threadid) {
-	logger(V_BREW, "Initializing coffee thread...\n");
+	logger(V_BASIC, "Initializing coffee thread...\n");
 
 	//installing new Signal handler for coffeethread
 	struct sigaction action;
@@ -55,15 +55,13 @@ void *coffeeThread(void *threadid) {
 		//wait for heat relais to switch
 		sleep(1);
 		if (halIsHeating()) { //Heating is on
-			logger(V_BREW, "Change state: INITALHEATING\n");
-			state = STATE_INITALHEATING;
+			changeState(STATE_INITALHEATING);
 		} else {
-			logger(V_BREW, "Change state: IDLE\n");
-			state = STATE_IDLE;
+			changeState(STATE_IDLE);
 		}
 	} else if (halGetRelaisState(RELAIS_PUMP)) {
 		logger_error("Whoops, why is the pump running...\n");
-		state = STATE_ERROR;
+		changeState(STATE_ERROR);
 	}
 
 	logger(V_BREW, "Start Coffee FSM\n");
@@ -81,15 +79,12 @@ void *coffeeThread(void *threadid) {
 					halRelaisOn(RELAIS_POWER);
 					sleep(1);
 					if (halIsHeating()) { //check if System starts to heat when turned on
-						logger(V_BREW, "Change state: INITALHEATING\n");
-						state = STATE_INITALHEATING;
+						changeState(STATE_INITALHEATING);
 					} else {
-						logger(V_BREW, "Change state: IDLE\n");
-						state = STATE_IDLE;
+						changeState(STATE_IDLE);
 					}
 				} else {
-					logger(V_BREW, "Change state: ERROR\n");
-					state = STATE_ERROR;
+					changeState(STATE_ERROR);
 				}
 				break;
 			}
@@ -99,16 +94,15 @@ void *coffeeThread(void *threadid) {
 			switch (getSigValue()) {
 			case SigInt1Rls:
 				//Turn machine off again
+				logger(V_BREW, "Turn machine off\n");
 				halRelaisOff(RELAIS_HEAT);
 				halRelaisOff(RELAIS_PUMP);
 				halRelaisOff(RELAIS_POWER);
-				logger(V_BREW, "Change state: OFF\n");
-				state = STATE_OFF;
+				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
 				//Inital heating finished
-				logger(V_BREW, "Change state: IDLE\n");
-				state = STATE_IDLE;
+				changeState(STATE_IDLE);
 				break;
 			}
 			break;
@@ -117,25 +111,23 @@ void *coffeeThread(void *threadid) {
 			switch(getSigValue()){
 			case SigInt1Rls:
 				//Turn machine off again
+				logger(V_BREW, "Turn machine off\n");
 				halRelaisOff(RELAIS_HEAT);
 				halRelaisOff(RELAIS_PUMP);
 				halRelaisOff(RELAIS_POWER);
-				logger(V_BREW, "Change state: OFF\n");
-				state = STATE_OFF;
+				changeState(STATE_OFF);
 				break;
 			case SigPressOpn:
 				logger(V_BREW, "Change state: IDLE\n");
-				state = STATE_IDLE;
+				changeState(STATE_IDLE);
 				break;
 			case SigInt0Psh:
 				//start to brew a delicious coffee
-				logger(V_BREW, "Change state: BREW\n");
-				state = STATE_BREW;
+				changeState(STATE_BREW);
 				break;
 			case SigBrewOn:
 				//someone brews manually
-				logger(V_BREW, "Change state: BREWMANUAL\n");
-				state = STATE_BREWMANUAL;
+				changeState(STATE_BREWMANUAL);
 				break;
 			}
 			break;
@@ -144,25 +136,22 @@ void *coffeeThread(void *threadid) {
 			switch(getSigValue()){
 			case SigInt1Rls:
 				//Turn machine off again
+				logger(V_BREW, "Turn machine off\n");
 				halRelaisOff(RELAIS_HEAT);
 				halRelaisOff(RELAIS_PUMP);
 				halRelaisOff(RELAIS_POWER);
-				logger(V_BREW, "Change state: OFF\n");
-				state = STATE_OFF;
+				changeState(STATE_OFF);
 				break;
 			case SigPressCls:
-				logger(V_BREW, "Change state: HEATING\n");
-				state = STATE_HEATING;
+				changeState(STATE_HEATING);
 				break;
 			case SigInt0Psh:
 				//start to brew a delicious coffee
-				logger(V_BREW, "Change state: BREW\n");
-				state = STATE_BREW;
+				changeState(STATE_BREW);
 				break;
 			case SigBrewOn:
 				//someone brews manually
-				logger(V_BREW, "Change state: BREWMANUAL\n");
-				state = STATE_BREWMANUAL;
+				changeState(STATE_BREWMANUAL);
 				break;
 			}
 			break;
@@ -190,8 +179,7 @@ void *coffeeThread(void *threadid) {
 			brewTimer.stop();
 			brewTime = 0;
 			logger(V_BREW, "Finish brewing\n");
-			logger(V_BREW, "Change state: IDLE\n");
-			state = STATE_IDLE;
+			changeState(STATE_IDLE);
 			break;
 		case STATE_BREWMANUAL:
 			pause();
@@ -228,6 +216,18 @@ int getSigValue(void){
 	sigValue = 0;
 	return tmp;
 }
+
+/**
+ * Changes the state of the machine to newState
+ * prints the change to the logger
+ * @param newState
+ */
+void changeState(int newState){
+	logger(V_BREW, "Change state to %d\n", newState);
+	state = newState;
+}
+
+
 /**
  * Counter for the brew time
  * refresh every 200ms

+ 1 - 0
CoffeeCode/coffee.h

@@ -29,6 +29,7 @@ void *coffeeThread(void *threadid);
 
 void coffeeHandler (int signum, siginfo_t *siginfo, void *context);
 int getSigValue(void);
+void changeState(int newState);
 void brewTimeHandler (void);
 
 #endif /* COFFEE_H_ */

+ 4 - 2
CoffeeCode/hal.cpp

@@ -87,7 +87,7 @@ void halRelaisSet(int relais, int state) {
 	case RELAIS_POWER:
 	case RELAIS_HEAT:
 	case RELAIS_PUMP:
-		//digitalWrite(relais, state);
+		digitalWrite(relais, state);
 		break;
 	}
 }
@@ -202,6 +202,7 @@ bool halIsHeating(void) {
  * @return 1 if the proximity switch is covered and 0 if uncovered
  */
 bool halProxSensorCovered(void){
+	//for legacy till sensor is installed
 	/*if(digitalRead(PROXIMITY_SENSOR) == 0){
 		return false;
 	} else {
@@ -236,7 +237,8 @@ void halSendSignal(int val){
 
 	if(pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
 		logger_error("Failed to queue signal %d %s", val, strerror(errno));
-		//exit program?
+		//No Signals reach the state machine anymore...
+		exit(EXIT_FAILURE);
 	}
 }