Explorar el Código

Combinded coffee FSM states in enum and fixed a bug

Philipp Hinz hace 8 años
padre
commit
c383c6a884
Se han modificado 5 ficheros con 35 adiciones y 27 borrados
  1. 1 1
      CoffeeCode/buildno
  2. 7 6
      CoffeeCode/coffee.cpp
  3. 17 17
      CoffeeCode/coffee.h
  4. 8 2
      CoffeeCode/display.cpp
  5. 2 1
      CoffeeCode/display.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-85
+87

+ 7 - 6
CoffeeCode/coffee.cpp

@@ -24,13 +24,14 @@
 #include "database.h"
 #include "display.h"
 
-int state;
+coffee_status_t state;
 int sigValue;
 int brewTime; //Brew time in ms
 timer brewTimer(&brewTimeHandler);
 clock_t beginHeating;
 clock_t endHeating;
 double heatingTime;
+
 /**
  * Thread for the finite state machine
  * It represents the current state of the machine and handles signals coming from
@@ -247,7 +248,7 @@ bool SigValueEmpty(void) {
  * prints the change to the logger
  * @param newState
  */
-void changeState(int newState) {
+void changeState(coffee_status_t newState) {
 	logger(V_BREW, "Changing state to %d\n", newState);
 	state = newState;
 
@@ -257,7 +258,7 @@ void changeState(int newState) {
 /**
  * Returns the current state of the FSM
  */
-int getState(void) {
+coffee_status_t getState(void) {
 	return state;
 }
 
@@ -295,7 +296,7 @@ void coffeeBrew(void) {
 	brewTimer.start();
 	while (halGetFlow() < AMOUNT_PREINFUSION) {
 		usleep(100000);
-		if (getState() == SigInt1Psh)
+		if (getSigValue() == SigInt1Psh)
 			return;
 	}
 	brewTimer.stop();
@@ -309,7 +310,7 @@ void coffeeBrew(void) {
 	brewTimer.start();
 	while (brewTime < TIME_SOAK) {
 		usleep(100000);
-		if (getState() == SigInt1Psh)
+		if (getSigValue() == SigInt1Psh)
 			return;
 	}
 	brewTimer.stop();
@@ -323,7 +324,7 @@ void coffeeBrew(void) {
 	brewTimer.start();
 	while (brewTime < TIME_INFUSION && halGetFlow() < AMOUNT_DBLESPRESSO) {
 		usleep(100000);
-		if (getState() == SigInt1Psh)
+		if (getSigValue() == SigInt1Psh)
 			return;
 	}
 	halRelaisOff(RELAIS_PUMP);

+ 17 - 17
CoffeeCode/coffee.h

@@ -2,37 +2,37 @@
  * coffee.h
  *
  *  Created on: Sep 25, 2017
- *      Author: sebastian
+ *      Author: Sebastian
  */
 
 #ifndef COFFEE_H_
 #define COFFEE_H_
 #include <csignal>
 
-
 //define status
-#define	STATE_OFF	0
-#define	STATE_HEATING	1
-#define STATE_INITALHEATING	2
-#define STATE_IDLE	3
-#define STATE_BREW	4
-#define STATE_BREWMANUAL	5
-#define STATE_CLEANING	6
-#define STATE_ERROR	7
+typedef enum {
+	STATE_OFF,
+	STATE_HEATING,
+	STATE_INITALHEATING,
+	STATE_IDLE,
+	STATE_BREW,
+	STATE_BREWMANUAL,
+	STATE_CLEANING,
+	STATE_ERROR
+} coffee_status_t;
 
 #define AMOUNT_PREINFUSION	3 //Preinfusion amount in ml
-#define TIME_SOAK	3000//Time between preinfusion and infusion in ms
-#define TIME_INFUSION 25000	//Infusion time in ms
+#define TIME_SOAK			3000 //Time between preinfusion and infusion in ms
+#define TIME_INFUSION 		25000	//Infusion time in ms
 #define AMOUNT_DBLESPRESSO	25.0	//Size of a double espresso in ml
-
 void *coffeeThread(void *threadid);
 
-void coffeeHandler (int signum, siginfo_t *siginfo, void *context);
+void coffeeHandler(int signum, siginfo_t *siginfo, void *context);
 int getSigValue(void);
 bool SigValueEmpty(void);
-void changeState(int newState);
-int getState(void);
-void brewTimeHandler (void);
+void changeState(coffee_status_t newState);
+coffee_status_t getState(void);
+void brewTimeHandler(void);
 void coffeeTerminate(void);
 void coffeeBrew(void);
 void coffeeIncreaseBrewCounter(void);

+ 8 - 2
CoffeeCode/display.cpp

@@ -24,7 +24,7 @@ timer displayTimer(displayTimerHandler);
 int lcd = 0;
 volatile int timerScaler = 0;
 volatile int elapsedCnt = 0;
-int coffeeState = STATE_OFF;
+coffee_status_t coffeeState = STATE_OFF;
 
 /**
  * Prints out the current time in a centered position
@@ -138,6 +138,9 @@ void* displayTimerHandler(void* threadid) {
 		case STATE_CLEANING:
 		case STATE_ERROR:
 			displayRefresh();
+			break;
+		default:
+			break;
 		}
 	}
 	if (scale5Hz) {
@@ -145,6 +148,9 @@ void* displayTimerHandler(void* threadid) {
 		case STATE_BREW:
 		case STATE_BREWMANUAL:
 			displayRefresh();
+			break;
+		default:
+			break;
 		}
 	}
 	if (elapsedCnt < (5 * REFRESH_RATE)) // Don't let it grow too large
@@ -260,7 +266,7 @@ const char* displayGetString(display_strings_t string) {
  * @param state New state
  */
 
-void displayPushState(int state) {
+void displayPushState(coffee_status_t state) {
 	if (state != coffeeState) {
 		coffeeState = state;
 		timerScaler--;

+ 2 - 1
CoffeeCode/display.h

@@ -8,6 +8,7 @@
 #ifndef DISPLAY_H_
 #define DISPLAY_H_
 #include <string>
+#include "coffee.h"
 
 typedef enum {
 	lang_de,
@@ -94,7 +95,7 @@ void displayInit(void);
 void displayTerminate(void);
 void displaySetLang(display_lang_t lang);
 
-void displayPushState(int state);
+void displayPushState(coffee_status_t state);
 
 void displayRefresh(void);
 const char* displayGetString(display_strings_t string);