فهرست منبع

rotary encoder interrupt now robust against emps from the pump or the heater relais

Sebastian 6 سال پیش
والد
کامیت
d177b17198
3فایلهای تغییر یافته به همراه44 افزوده شده و 24 حذف شده
  1. 1 1
      CoffeeCode/buildno
  2. 40 22
      CoffeeCode/hal.cpp
  3. 3 1
      CoffeeCode/hal.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-293
+306

+ 40 - 22
CoffeeCode/hal.cpp

@@ -32,6 +32,12 @@ bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
 
 //storage for the last state of the buttons, the proximity sensor and the pressure sensor
 int pinState[4] = {1, 1, 1, 0};
+//state of the rotary encoder
+//0: rotary1 state at t-1
+//1: rotary2 state at t-1
+//2: rotary1 state at t
+//3: rotary2 state at t
+int rotaryState[4] = {1, 0, 0, 1};
 
 //sweep counter to log every brew
 uint16_t logcycle = 1;
@@ -49,6 +55,8 @@ timespec pumpCycle[] = {{0,0},{0,0}};
 
 //delay of the debounce in milliseconds
 #define DELAY_DEBOUNCE	50
+#define DELAY_MICRODEB	2
+
 
 //display turn off after idle time in min
 //minimal time is 2min
@@ -119,7 +127,7 @@ void halInit(void) {
 		logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
 		return;
 	}
-	if (wiringPiISR(PIN_ROTARY1, INT_EDGE_RISING, &halIntRotary) < 0) {
+	if (wiringPiISR(PIN_ROTARY1, INT_EDGE_BOTH, &halIntRotary) < 0) {
 		logger_error("Unable to setup ISRRotary2: %s\n", strerror(errno));
 		return;
 	}
@@ -197,30 +205,40 @@ int halGetRelaisState(int relais) {
 /**
  *
  */
-void halIntRotary(void){
-	//check for the status of the other pin
-	if(digitalRead(PIN_ROTARY2)) {
-		//clockwise rotation
-		tickCounter++;
-
-		if(!(tickCounter % ROTARY_STEPSIZE)) {
-			logger(V_HAL, "rotary encoder CW");
-			halSendSignal(SigRotCW);
-			tickCounter = 0;
+void halIntRotary(void) {
+	//delay(DELAY_MICRODEB);
+	rotaryState[2] = digitalRead(PIN_ROTARY1);
+	rotaryState[3] = digitalRead(PIN_ROTARY2);
+
+	if (rotaryState[0] != rotaryState[2]) {
+		//check for the status of the other pin
+		if (rotaryState[1] != rotaryState[3]) {
+			if ((rotaryState[2] == HIGH && rotaryState[3] == LOW) || (rotaryState[2] == LOW && rotaryState[3] == HIGH)) {
+				//clockwise rotation
+				tickCounter++;
+				if (!(abs(tickCounter) % ROTARY_STEPSIZE)) {
+					//logger(V_HAL, "rotary encoder CW \n");
+					halSendSignal(SigRotCW);
+					tickCounter = 0;
+				}
+			} else if ((rotaryState[2] == HIGH && rotaryState[3] == HIGH) || (rotaryState[2] == LOW && rotaryState[3] == LOW)) {
+				//counterclockwise rotation
+				tickCounter--;
+				if (!(abs(tickCounter) % ROTARY_STEPSIZE)) {
+					//logger(V_HAL, "rotary encoder CCW \n");
+					halSendSignal(SigRotCCW);
+					tickCounter = 0;
+				}
+			}
+			rotaryState[1] = rotaryState[3];
 		}
+		rotaryState[0] = rotaryState[2];
 	}
-	else {
-		//counterclockwise rotation
-		tickCounter--;
+}
+
+
 
-		if(!(abs(tickCounter) % ROTARY_STEPSIZE)) {
-			logger(V_HAL, "rotary encoder CCW");
-			halSendSignal(SigRotCCW);
-			tickCounter = 0;
-		}
-	}
 
-}
 
 
 /**
@@ -547,7 +565,7 @@ void halPumpOn(){
  */
 
 void halIncreaseLogCycleCounter(void){
-	logcycle++;
+	logcycle = logcycle + 1;
 }
 
 /**

+ 3 - 1
CoffeeCode/hal.h

@@ -26,7 +26,9 @@
 #define PIN_ROTARY2		16
 
 #define ROTARY_RESOLUTION	127
-#define ROTARY_STEPSIZE		7
+#define ROTARY_STEPSIZE		15
+
+
 
 #define TIME_BUTTONLONGPRESS	3	//Time in s until a Signal for a long pressed button is sent
 /*