Explorar o código

added debouncing to both buttons and the proximity sensor, added a coffeestate History to go back to last state in the program

Sebastian %!s(int64=7) %!d(string=hai) anos
pai
achega
079d0a48de
Modificáronse 4 ficheiros con 60 adicións e 22 borrados
  1. 26 6
      CoffeeCode/coffee.cpp
  2. 2 1
      CoffeeCode/coffee.h
  3. 30 15
      CoffeeCode/hal.cpp
  4. 2 0
      CoffeeCode/hal.h

+ 26 - 6
CoffeeCode/coffee.cpp

@@ -31,6 +31,7 @@ timer brewTimer(&brewTimeHandler);
 
 uint64_t totalHeatingTime; //local copies of the corresponding database entries
 uint16_t brewCounter;
+coffee_status_t lastState;
 
 
 const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
@@ -56,6 +57,8 @@ void *coffeeThread(void *threadid) {
 	brewTimer.stop();
 	brewTime = 0;
 
+	lastState = STATE_OFF;
+
 	//read the database values
 	if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
 		logger_error("coffee.cpp: Couldn't read the heating time from the database");
@@ -99,7 +102,7 @@ void *coffeeThread(void *threadid) {
 			if (SigValueEmpty())
 				pause();
 			if (getSigValue() == SigInt0Rls) {
-				if (halProxSensorCovered()) { //Check Waterlevel
+				if (!halProxSensorCovered()) { //Check waterlevel in gray water tank
 					//turn machine on
 					halMachineOn();
 					sleep(1);
@@ -109,7 +112,7 @@ void *coffeeThread(void *threadid) {
 						changeState(STATE_IDLE);
 					}
 				} else {
-					logger_error("Not enough Water in tank detected\n");
+					logger_error("Empty Tank please!\n");
 					changeState(STATE_ERROR);
 				}
 				break;
@@ -229,7 +232,12 @@ void *coffeeThread(void *threadid) {
 
 			case SigInt0Psh:
 				//start to brew a delicious coffee
-				changeState(STATE_BREW);
+				if(!halProxSensorCovered()){
+					changeState(STATE_BREW);
+				}
+				else {
+					changeState(STATE_ERROR);
+				}
 				break;
 
 			case SigBrewOn:
@@ -244,7 +252,12 @@ void *coffeeThread(void *threadid) {
 		case STATE_BREW:
 			coffeeBrew();
 			logger(V_BREW, "Finishing brewing\n");
-			changeState(STATE_IDLE);
+			if(!halProxSensorCovered()){
+				changeState(STATE_IDLE);
+			}
+			else {
+				changeState(STATE_ERROR);
+			}
 			break;
 
 			/*
@@ -269,8 +282,13 @@ void *coffeeThread(void *threadid) {
 		case STATE_ERROR:
 			if (SigValueEmpty())
 				pause();
+		switch (getSigValue()) {
+		case SigInt1Psh:
+		case SigInt0Psh:
+			changeState(RETURN_STATE);
 			break;
 		}
+		}
 	}
 	pthread_exit(EXIT_SUCCESS);
 }
@@ -311,9 +329,12 @@ 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));
 }
 
@@ -410,7 +431,6 @@ void coffeeBrew(void) {
 	halResetFlow();
 	brewTimer.stop();
 	brewTime = 0;
-	changeState(STATE_IDLE);
 	return;
 }
 

+ 2 - 1
CoffeeCode/coffee.h

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

+ 30 - 15
CoffeeCode/hal.cpp

@@ -20,11 +20,17 @@ volatile int flowcnt = 0;
 int Int0Time, Int1Time;
 bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
 
+//storage for the last state of the buttons and the proximity sensor
+int pinState[3] = {0, 0, 0};
+
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 
 clock_t heatingCycle[2] = {0, 0};
 
+//delay of the debounce in milliseconds
+#define DELAY_DEBOUNCE	50
+
 /**
  * Initializes HAL
  */
@@ -128,8 +134,11 @@ int halGetRelaisState(int relais) {
  * Interrupt routine for Int0 (Top button)
  */
 void halInt0(void) {
-	if (halGetInt0()) { //released
+	//wait for a debounce
+	delay(DELAY_DEBOUNCE);
+	if (halGetInt0() && !pinState[0]) { //released
 		logger(V_HAL, "Int0 released\n");
+		pinState[0] = 1;
 		if (flagIgnoreRlsInt0) {
 			flagIgnoreRlsInt0 = false;
 		} else {
@@ -137,8 +146,9 @@ void halInt0(void) {
 			Int0Timer.stop();
 			halSendSignal(SigInt0Rls);
 		}
-	} else { //pressed
+	} else if(!halGetInt0() && pinState[0]) { //pressed
 		logger(V_HAL, "Int0 pushed\n");
+		pinState[0] = 0;
 		halSendSignal(SigInt0Psh);
 		Int0Time = 0;
 		Int0Timer.start();
@@ -162,8 +172,10 @@ void halInt0TimerHandler(void) {
  * Interrupt routine for Int1 (Bottom button)
  */
 void halInt1(void) {
-	if (halGetInt1()) {
+	delay(DELAY_DEBOUNCE);
+	if (halGetInt1() && !pinState[1]) {
 		logger(V_HAL, "Int1 released\n");
+		pinState[1] = 1;
 		if (flagIgnoreRlsInt1) {
 			flagIgnoreRlsInt1 = false;
 		} else {
@@ -171,8 +183,9 @@ void halInt1(void) {
 			Int0Timer.stop();
 			halSendSignal(SigInt1Rls);
 		}
-	} else {
+	} else if(!halGetInt1() && pinState[1]) {
 		logger(V_HAL, "Int1 pushed\n");
+		pinState[1] = 0;
 		halSendSignal(SigInt1Psh);
 		Int1Time = 0;
 		Int1Timer.start();
@@ -198,7 +211,7 @@ void halInt1TimerHandler(void) {
  */
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
-	//logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
+	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	if (flowcnt == 99) {
 		halRelaisOff(RELAIS_PUMP);
 	}
@@ -239,11 +252,14 @@ double halgetHeatingTime(void){
  * Method to handle toggle of the proximity sensor
  */
 void halIntProximity(void) {
-	//logger(V_HAL, "IntProximity triggered\n");
-	return;
-	if (halProxSensorCovered()) {
+	delay(DELAY_DEBOUNCE);
+	if (halProxSensorCovered() && !pinState[2]) {
+		logger(V_HAL, "IntProximity triggered\n");
+		pinState[2] = 1;
 		halSendSignal(SigProxCvrd);
-	} else {
+	} else if(!halProxSensorCovered() && pinState[2]){
+		logger(V_HAL, "IntProximity triggered\n");
+		pinState[2] = 0;
 		halSendSignal(SigProxOpn);
 	}
 }
@@ -280,12 +296,11 @@ bool halIsHeating(void) {
  * @return 1 if the proximity switch is covered and 0 if uncovered
  */
 bool halProxSensorCovered(void) {
-//	if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
-//	 return false;
-//	 } else {
-//	 return true;
-//	 }
-	return true;
+	if(digitalRead(PIN_PROXIMITY_SENSOR) == 0){
+	 return false;
+	 } else {
+	 return true;
+	 }
 }
 
 /**

+ 2 - 0
CoffeeCode/hal.h

@@ -52,6 +52,8 @@ enum HalSig {
 	SigBrewOff = 12
 };
 
+
+
 void halInit(void);
 void halRelaisOn(int relais);
 void halRelaisOff(int relais);