فهرست منبع

Signals zwischen hal und coffee implementiert
FSM in Coffee implementiert

pek72 8 سال پیش
والد
کامیت
e590c07f95
5فایلهای تغییر یافته به همراه172 افزوده شده و 49 حذف شده
  1. 1 1
      CoffeeCode/buildno
  2. 2 0
      CoffeeCode/global.h
  3. 120 24
      CoffeeCode/hal.cpp
  4. 35 11
      CoffeeCode/hal.h
  5. 14 13
      CoffeeCode/main.cpp

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-31
+55

+ 2 - 0
CoffeeCode/global.h

@@ -25,12 +25,14 @@ extern int verbose;
 extern bool optDate, optPower;
 extern pthread_mutex_t mutex;
 extern int lcd;
+extern pthread_t thread[];
 
 
 // Thread IDs
 #define THREAD_MAIN 	0
 #define THREAD_STRIPE	1
 #define THREAD_DISPLAY	2
+#define THREAD_COFFEE	3
 
 
 void killThread(int threadid, int sig);

+ 120 - 24
CoffeeCode/hal.cpp

@@ -8,6 +8,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
+#include <signal.h>
 #include "lcd.h"
 #include "hal.h"
 #include "global.h"
@@ -33,21 +34,29 @@ void halInit(void) {
 	pinMode(RELAIS_POWER, OUTPUT);
 	pinMode(PRESSURE_CTRL, INPUT);
 	pinMode(PROXIMITY_SENSOR, INPUT);
-	pinMode(HAL_INT0, INPUT);
-	pinMode(HAL_INT1, INPUT);
-	pinMode(HAL_FLOW, INPUT);
-	if (wiringPiISR(HAL_INT0, INT_EDGE_FALLING, &halInt0) < 0) {
+	pinMode(INT0, INPUT);
+	pinMode(INT1, INPUT);
+	pinMode(FLOW, INPUT);
+	if (wiringPiISR(INT0, INT_EDGE_BOTH, &halInt0) < 0) {
 		logger_error("Unable to setup ISR0: %s\n", strerror(errno));
 		return;
 	}
-	if (wiringPiISR(HAL_INT1, INT_EDGE_FALLING, &halInt1) < 0) {
+	if (wiringPiISR(INT1, INT_EDGE_BOTH, &halInt1) < 0) {
 		logger_error("Unable to setup ISR1: %s\n", strerror(errno));
 		return;
 	}
-	if (wiringPiISR(HAL_FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
+	if (wiringPiISR(FLOW, INT_EDGE_FALLING, &halIntFlow) < 0) {
 		logger_error("Unable to setup ISRFLOW: %s\n", strerror(errno));
 		return;
 	}
+	if(wiringPiISR(PRESSURE_CTRL, INT_EDGE_BOTH, &halIntPressure) < 0) {
+		logger_error("Unable to setup ISRPressure: %s\n", strerror(errno));
+		return;
+	}
+	if(wiringPiISR(PROXIMITY_SENSOR, INT_EDGE_BOTH, &halIntProximity) < 0) {
+		logger_error("Unable to setup ISRProximity: %s\n", strerror(errno));
+		return;
+	}
 }
 
 /*
@@ -78,33 +87,78 @@ void halRelaisSet(int relais, int state) {
 	case RELAIS_POWER:
 	case RELAIS_HEAT:
 	case RELAIS_PUMP:
-		digitalWrite(relais, state);
+		//digitalWrite(relais, state);
+		break;
+	}
+}
+
+/*
+ * Returns the state of the relais relais
+ * Returns HIGH when Relais is ON
+ * @param relais Relais ID
+ */
+int halGetRelaisState(int relais) {
+	switch (relais) {
+	case RELAIS_POWER:
+	case RELAIS_HEAT:
+	case RELAIS_PUMP:
+		return !digitalRead(relais);
 		break;
 	}
+	return -1;
 }
 
 void halInt0(void) {
-	//halRelaisOn(RELAIS_POWER);
 	logger(V_BASIC, "Int0 triggered\n");
-	flowcnt = 0;
-	halRelaisOn(RELAIS_PUMP);
+	if (halGetInt0()) {
+		halSendSignal(SigInt0Rls);
+	} else {
+		halSendSignal(SigInt0Psh);
+	}
 }
 
 void halInt1(void) {
-	//halRelaisOff(RELAIS_POWER);
 	logger(V_BASIC, "Int1 triggered\n");
-	flowcnt = 0;
+	if (halGetInt1()) {
+		halSendSignal(SigInt1Rls);
+	} else {
+		halSendSignal(SigInt1Psh);
+	}
 }
 
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
 	logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	lcdPosition(lcd, 0, 0);
-	lcdPrintf(lcd, "ml = %.2f   ", halGetFlow());
-	if (flowcnt == 99) {
-		halRelaisOff(RELAIS_PUMP);
+	 lcdPrintf(lcd, "ml = %.2f   ", halGetFlow());
+	 if (flowcnt == 99) {
+	 halRelaisOff(RELAIS_PUMP);
+	 }
+	 flowcnt++;
+}
+
+/*
+ * Method to handle toggle of the pressure control
+ */
+void halIntPressure(void) {
+	logger(V_BASIC, "IntPressure Control triggered\n");
+	if (halIsHeating()) {
+		halSendSignal(SigPressCls);
+	} else {
+		halSendSignal(SigPressOpn);
+	}
+}
+
+/*
+ * Method to handle toggle of the proximity sensor
+ */
+void halIntProximity(void) {
+	logger(V_BASIC, "IntProximity triggered\n");
+	if (halProxSensorCovered()) {
+		halSendSignal(SigProxCvrd);
+	} else {
+		halSendSignal(SigProxOpn);
 	}
-	flowcnt++;
 }
 
 /*
@@ -114,22 +168,64 @@ float halGetFlow(void) {
 	return flowcnt*FLOW_ML_PULSE;
 }
 
+/*
+ * Resets the Flow counter
+ */
+void halResetFlow(void){
+	flowcnt = 0;
+}
+
 /*
  * Reads the status of the Pressure Control
- * @return 1 for closed Pressure Control and 0 for open
+ * @return 0 for closed Pressure Control(heating) and 1 for open
  */
-int halGetPressureCtrl(void) {
-	if (digitalRead(PRESSURE_CTRL)) {
-		return 0;
-	} else
-		return 1;
+bool halIsHeating(void) {
+	if(digitalRead(PRESSURE_CTRL) == 0){
+		return true;
+	} else {
+		return false;
+	}
 }
 
 /*
  * Returns status of the proximity switch
  * @return 1 if the proximity switch is covered and 0 if uncovered
  */
-int halGetProximitySensor(void){
-	return digitalRead(PROXIMITY_SENSOR);
+bool halProxSensorCovered(void){
+	/*if(digitalRead(PROXIMITY_SENSOR) == 0){
+		return false;
+	} else {
+		return true;
+	}*/
+	return true;
+}
+
+/*
+ * Returns the value of the top button Int0 (low active)
+ * @return LOW or HIGH
+ */
+int halGetInt0(void){
+	return digitalRead(INT0);
+}
+
+/*
+ * Returns the value of the bottom button Int1 (low active)
+ * @return LOW or HIGH
+ */
+int halGetInt1(void){
+	return digitalRead(INT1);
+}
+
+/*
+ * send Signal to coffee thread
+ */
+void halSendSignal(int val){
+	sigval value = {0};
+	value.sival_int = (int) val;
+
+	if(pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
+		logger_error("Failed to queue signal %d %s", val, strerror(errno));
+		//exit program?
+	}
 }
 

+ 35 - 11
CoffeeCode/hal.h

@@ -11,28 +11,52 @@
 #define RELAIS_HEAT		29
 #define RELAIS_PUMP		25
 #define RELAIS_POWER	28
-#define PRESSURE_CTRL	24
+#define PRESSURE_CTRL	7
 #define PROXIMITY_SENSOR	6
 
-#define HAL_INT0		0 // bottom button
-#define HAL_INT1		2 // top button
-#define HAL_FLOW		3 // flow sensor
-
-#define HAL_PROX_COVERED	1 //proximity sensor
-
+#define INT0		0 // bottom button
+#define INT1		2 // top button
+#define FLOW		3 // flow sensor
+#define PROX_COVERED	1 //proximity sensor
 #define FLOW_ML_PULSE	(1000.0/990) // Flow sensor: volume (ml) per pulse
-
+/*
+ * Explanation for the signal levels
+ * SigInt_Psh 			Button is pushed
+ * SigInt_Rls 		Button is released
+ * SigPressCls 		Pressure control is closed, System is heating
+ * SigPressOpn		Pressure control is open, heating is off
+ * SigProxOpn		Proximity Sensor is uncovered
+ * SigProxCvrd	Proximity Sensor is covered
+ */
+enum HalSig {
+	SigInt0Psh = 1,
+	SigInt0Rls = 2,
+	SigInt1Psh = 3,
+	SigInt1Rls = 4,
+	SigPressCls = 5,
+	SigPressOpn = 6,
+	SigProxOpn = 7,
+	SigProxCvrd = 8,
+	SigBrewOn = 9,
+	SigBrewOff = 10
+};
 
 void halInit(void);
 void halRelaisOn(int relais);
 void halRelaisOff(int relais);
 void halRelaisSet(int relais, int state);
+int halGetRelaisState(int relais);
 void halInt0(void);
 void halInt1(void);
 void halIntFlow(void);
+void halIntPressure(void);
+void halIntProximity(void);
 float halGetFlow(void);
-int halGetPressureCtrl(void);
-int halGetProximitySensor(void);
-
+void halResetFlow(void);
+bool halIsHeating(void);
+bool halProxSensorCovered(void);
+int halGetInt0(void);
+int halGetInt1(void);
+void halSendSignal(int value);
 
 #endif /* HAL_H_ */

+ 14 - 13
CoffeeCode/main.cpp

@@ -23,6 +23,7 @@
 #include "lcd.h"
 #include "hal.h"
 #include "stripe.h"
+#include "coffee.h"
 
 const int buildno = (1+(int)(
 #include "buildno"
@@ -40,7 +41,7 @@ void usr1Handler(int signum);
 void hupHandler(int signum);
 void timeHandler(void);
 timer timeTimer(&timeHandler);
-pthread_t thread[3];
+pthread_t thread[4];
 pthread_mutex_t mutex;
 
 int main(int argc, char *argv[]) {
@@ -115,15 +116,6 @@ int main(int argc, char *argv[]) {
 		return 1;
 	}
 
-	logger_reset();
-	initTimers();
-	halInit();
-	lcd = lcdInit();
-	if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
-	lcdClear(lcd);
-	lcdHome(lcd);
-	lcdPrintf(lcd,"    CoffeePi   ", buildno);
-
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
 	// Initialize and set thread joinable
@@ -137,19 +129,28 @@ int main(int argc, char *argv[]) {
 	if (rc != 0)
 		throw(L"pthread_mutexattr_destroy returns " + rc);
 
-	for (int i = 0; i < 2; i++) {
+	for (int i = 0; i < 4; i++) {
 		if (i == THREAD_MAIN)
 			rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
 		else if (i == THREAD_STRIPE)
 			rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
-		/*else if (i == THREAD_CAN)
-			rc = pthread_create(&thread[i], NULL, canThread, (void *) i);*/
+		else if (i == THREAD_COFFEE)
+			rc = pthread_create(&thread[i], NULL, coffeeThread, (void *) i);
 		if (rc) {
 			logger_error("Error:unable to create thread, %d\n", rc);
 			exit(-1);
 		}
 	}
 
+	logger_reset();
+	initTimers();
+	halInit();
+	lcd = lcdInit();
+	if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
+	lcdClear(lcd);
+	lcdHome(lcd);
+	lcdPrintf(lcd,"    CoffeePi   ", buildno);
+
 	// free attribute and wait for the other threads
 	pthread_attr_destroy(&attr);