浏览代码

changed idle handling in hal and fixed TODO in idle, changed process creation in main

Sebastian Vendt 5 年之前
父节点
当前提交
07b64f4572
共有 7 个文件被更改,包括 69 次插入58 次删除
  1. 1 1
      CoffeeCode/buildno
  2. 2 2
      CoffeeCode/coffee.cpp
  3. 0 1
      CoffeeCode/database.h
  4. 42 36
      CoffeeCode/hal.cpp
  5. 4 3
      CoffeeCode/hal.h
  6. 20 14
      CoffeeCode/main.cpp
  7. 0 1
      CoffeeCode/server.cpp

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-362
+367

+ 2 - 2
CoffeeCode/coffee.cpp

@@ -324,7 +324,7 @@ void *coffeeThread(void *threadid) {
 			if (mode == MODE_STATE) {
 				halMachineOff();
 				writeBackCache();
-				//TODO this might be a bit confusing to change the page here even if the menu isnt actually displayed
+				// his might be a bit confusing to change the page here even if the menu isn't actually displayed
 				changePage(PAGE_RESTART);
 				if (SigValueEmpty())
 					pause();
@@ -879,7 +879,7 @@ void coffeeBrew(void) {
 	while (halGetFlow() < AMOUNT_PREINFUSION && halGetFlowTime() < TIME_PREINFUSION) {
 		//TODO don't use coffeeNap here since we don't want to resume to sleep after a signal got caught...
 		coffeeNap(0, 50000000);
-		if (getSigValue(MODE_STATE) == SigInt0Rls){
+		if (getSigValue(MODE_STATE) == SigInt0Rls) {
 			halPumpOff();
 			return;
 		}

+ 0 - 1
CoffeeCode/database.h

@@ -24,7 +24,6 @@ typedef enum {
 	CFGdisplaylang = 3,
 	CFGDescTimestamp = 4,
 	CFGDescBrewCount = 5,
-	CFGSweepCounter = 6
 } config_key_t;
 
 int sqlOpen();

+ 42 - 36
CoffeeCode/hal.cpp

@@ -62,9 +62,6 @@ int pinState[4] = {1, 1, 1, 0};
 //3: rotary2 state at t
 int rotaryState[4] = {1, 0, 0, 1};
 
-//sweep counter to log every brew
-uint16_t logcycle = 1;
-
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 timer idleTimer(&halIdleTimerHandler);
@@ -310,8 +307,6 @@ void halInt0TimerHandler(void) {
  *
  */
 void halIdleTimerHandler(void) {
-	//TODO the idle counter is once resetted when a button is pressed. From this moment the machine
-	//will enter the idle state no matter what the user does -> reset Idle counter on every input!
 	if(++idleCounter == IDLE_TIME){
 		halEnterIdle();
 	}
@@ -568,31 +563,7 @@ void halSendSignal(HalSig val) {
 		system("reboot");
 	}
 
-	//catch if machine is idle and drop button event
-	switch (val) {
-	case SigInt0Psh:
-	case SigInt1Psh:
-		if (idle) {
-			return;
-		}
-		break;
-
-	case SigInt0Rls:
-	case SigInt0RlsLong:
-	case SigInt1Rls:
-	case SigRotCCW:
-	case SigRotCW:
-		if (idle) {
-			halLeaveIdle();
-			return;
-		}
-		break;
-
-	default:
-		break;
-	}
-
-
+	if (halIdle(val)) return;
 
 	sigval value = { 0 };
 	value.sival_int = (int) val;
@@ -649,7 +620,7 @@ void halEnterIdle(void){
 /**
  *
  */
-void halLeaveIdle(void){
+void halLeaveIdle(void) {
 	idleCounter = 0;
 	logger(V_HAL, "hal.cpp: Leaving Idle Mode\n");
 	halDisplayOn();
@@ -658,17 +629,52 @@ void halLeaveIdle(void){
 }
 
 /**
- * Wrapper function to turn the pump on.
+ * processes the signal value with respect to the idle state
+ * returns true, if the current signal should be dropped
  */
-void halPumpOn(){
-	halRelaisOn(RELAIS_PUMP);
+bool halIdle(HalSig val){
+	//idle processing filter for signals
+	if (idle) {
+		switch (val) {
+		case SigInt0Psh:
+		case SigInt1Psh:
+			return true;
+			break;
+
+		case SigInt0Rls:
+		case SigInt0RlsLong:
+		case SigInt1Rls:
+		case SigRotCCW:
+		case SigRotCW:
+			halLeaveIdle();
+			return true;
+			break;
+		case SigPowerUp:
+			halLeaveIdle();
+			return false;
+			break;
+		default:
+			return false;
+		}
+	} else {
+		//reset the idle time on every input
+		halResetIdleTimer();
+		return false;
+	}
 }
 
 /**
  *
  */
-void halIncreaseLogCycleCounter(void){
-	logcycle = logcycle + 1;
+void halResetIdleTimer(void) {
+	idleCounter = 0;
+}
+
+/**
+ * Wrapper function to turn the pump on.
+ */
+void halPumpOn(){
+	halRelaisOn(RELAIS_PUMP);
 }
 
 /**

+ 4 - 3
CoffeeCode/hal.h

@@ -104,11 +104,12 @@ void halMachineOff(void);
 void halInt0TimerHandler(void);
 void halInt1TimerHandler(void);
 void halIdleTimerHandler(void);
-void halEnterIdle (void);
-void halLeaveIdle (void);
+void halEnterIdle(void);
+void halLeaveIdle(void);
+bool halIdle(HalSig value);
+void halResetIdleTimer(void);
 void halPumpOn();
 void halPumpOff(void);
-void halIncreaseLogCycleCounter(void);
 void timespec_diff(timespec *start, timespec *stop, timespec *result);
 void halTerminate(event_t *event);
 void halWriteBackCache(void);

+ 20 - 14
CoffeeCode/main.cpp

@@ -151,19 +151,26 @@ int main(int argc, char *argv[]) {
 	if (rc != 0)
 		throw(L"pthread_mutexattr_destroy returns " + rc);
 
-	for (int i = 0; i < 5; i++) {//TODO this can be simplified!
-		if (i == THREAD_MAIN)
-			rc = pthread_create(&thread[i], &attr, mainLoop, (void *) i);
-		else if (i == THREAD_STRIPE)
-			rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
-		else if (i == THREAD_DISPLAY)
-			rc = pthread_create(&thread[i], NULL, displayThread, (void *) i);
-		else if (i == THREAD_COFFEE)
-			rc = pthread_create(&thread[i], NULL, coffeeThread, (void *) i);
-		else if (i == THREAD_SERVER && !optNoNet)
-			rc = pthread_create(&thread[i], NULL, serverThread, (void *) i);
-		if (rc) {
-			logger_error("Error:unable to create thread %d (%d)\n", i, rc);
+
+	if ((rc = pthread_create(&thread[THREAD_MAIN], &attr, mainLoop, (void *) THREAD_MAIN))) {
+		logger_error("Error:unable to create thread THREAD_MAIN %d (%d)\n", THREAD_MAIN, rc);
+		exit(-1);
+	}
+	if ((rc = pthread_create(&thread[THREAD_STRIPE], NULL, stripeThread, (void *) THREAD_STRIPE))) {
+		logger_error("Error:unable to create thread THREAD_STRIPE %d (%d)\n", THREAD_STRIPE, rc);
+		exit(-1);
+	}
+	if ((rc = pthread_create(&thread[THREAD_DISPLAY], NULL, displayThread, (void *) THREAD_DISPLAY))) {
+		logger_error("Error:unable to create thread THREAD_DISPLAY %d (%d)\n", THREAD_DISPLAY, rc);
+		exit(-1);
+	}
+	if ((rc = pthread_create(&thread[THREAD_COFFEE], NULL, coffeeThread, (void *) THREAD_COFFEE))) {
+		logger_error("Error:unable to create thread THREAD_COFFEE %d (%d)\n", THREAD_COFFEE, rc);
+		exit(-1);
+	}
+	if (!optNoNet) {
+		if ((rc = pthread_create(&thread[THREAD_SERVER], NULL, serverThread, (void *) THREAD_SERVER))) {
+			logger_error("Error:unable to create thread THREAD_SERVER %d (%d)\n", THREAD_SERVER, rc);
 			exit(-1);
 		}
 	}
@@ -195,7 +202,6 @@ void terminationHandler(int signum) {
 	logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
 	stopTimers();
 
-	//TODO This event is not fired anymore...
 	event_trigger("terminate");
 
 	/*logger(V_NONE, "Saving my state to the database..\n");

+ 0 - 1
CoffeeCode/server.cpp

@@ -132,7 +132,6 @@ void* serverThread(void* threadid) {
 
 			if (!strcmp(SERVER_CMD_ON, pcmd)) {
 				if (getState() == STATE_OFF) {
-					halLeaveIdle();
 					halSendSignal(SigPowerUp);
 					sendACK();
 				}