Jelajahi Sumber

LED stripe is now reacting to the current machine state

Philipp Hinz 7 tahun lalu
induk
melakukan
2fff9fb34b
3 mengubah file dengan 86 tambahan dan 17 penghapusan
  1. 1 1
      CoffeeCode/buildno
  2. 83 15
      CoffeeCode/stripe.cpp
  3. 2 1
      CoffeeCode/stripe.h

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-105
+110

+ 83 - 15
CoffeeCode/stripe.cpp

@@ -7,6 +7,7 @@
 
 #include <wiringPiI2C.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <errno.h>
 #include <pthread.h>
 #include <unistd.h>
@@ -15,6 +16,7 @@
 #include "global.h"
 #include "timer.h"
 #include "logger.h"
+#include "coffee.h"
 
 stripe_color currentColor;
 stripe_color effectColor;
@@ -22,6 +24,7 @@ int currentWhite = 0, currentDim = 0;
 stripe_transient_t currentTransient = TRANS_DIRECT;
 timer stripeTimer(stripeTimerHandler);
 int i2cfd;
+coffee_status_t lastState = STATE_OFF;
 
 /**
  * This is the handler for time based stripe effects
@@ -49,24 +52,60 @@ void *stripeThread(void *threadid) {
 	stripeTimer.stop();
 	logger(V_BASIC, "Initialized Stripe thread\n");
 
+	stripeOn();
+	stripeSetRGB(0, 0, 0);
 	stripe_color col;
-	col.red = 255;
-	col.green = 0;
-	col.blue = 0;
-	stripeEffectPulse(col);
-	int tmp = 0, inc = 1;
+	//int tmp = 0, inc = 1;
 	while (1) {
-		pause();
-		stripeEffectHeating(tmp);
-		logger(V_NONE, LOG_ERRORC, "Simulating %d%% percent heating\n", tmp);
-		sleep(1);
-		if (inc) {
-			if (++tmp >= 100)
-				inc = 0;
-		} else {
-			if (--tmp <= 0)
-				inc = 1;
+		if (getState() != lastState) {
+			lastState = getState();
+			switch (lastState) {
+			case STATE_IDLE:
+			case STATE_HEATING:
+			case STATE_BREW:
+			case STATE_BREWMANUAL:
+			case STATE_CLEANING:
+				col.red = 255;
+				col.green = 0;
+				col.blue = 0;
+				stripeEffectPulse(col);
+				break;
+
+			case STATE_INITALHEATING:
+				col.red = 0;
+				col.green = 0;
+				col.blue = 255;
+				stripeEffectPulse(col);
+				break;
+
+			case STATE_ERROR:
+				stripeEffectDisable();
+				stripeSetTransient(TRANS_FAST);
+				stripeSetRGB(255, 0, 0);
+				break;
+
+			case STATE_OFF:
+				usleep(50000);
+				stripeEffectDisable();
+				stripeSetTransient(TRANS_SLOW);
+				stripeSetRGB(0, 0, 0);
+				stripeSetRGB(0, 0, 0); // do it twice, else it doesn't work?! dafuq..
+				break;
+			}
 		}
+		usleep(100000);
+		/*
+		 pause();
+		 stripeEffectHeating(tmp);
+		 logger(V_NONE, LOG_ERRORC, "Simulating %d%% percent heating\n", tmp);
+		 sleep(1);
+		 if (inc) {
+		 if (++tmp >= 100)
+		 inc = 0;
+		 } else {
+		 if (--tmp <= 0)
+		 inc = 1;
+		 }*/
 	}
 
 	pthread_exit(NULL);
@@ -139,6 +178,8 @@ void stripeUpdate(void) {
 	}
 
 	stripeCommand(len, data);
+	logger(V_STRIPE, "Updated stripe to color r %d g %d b %d\n", currentColor.red,
+			currentColor.green, currentColor.blue);
 }
 
 /**
@@ -154,6 +195,32 @@ void stripeUpdateDim(void) {
 	stripeCommand(len, data);
 }
 
+/**
+ * Let the stripe fade out and turn off
+ */
+void stripeFadeOut(void) {
+	char data[3];
+	int len = 3;
+
+	data[0] = STRIPE_CMD_FADEOUT;
+	data[1] = (currentTransient >> 8) & 0xff;
+	data[2] = currentTransient & 0xff;
+
+	stripeCommand(len, data);
+}
+
+/**
+ * Stops the current fading
+ */
+void stripeFadeStop(void) {
+	char data[1];
+	int len = 1;
+
+	data[0] = STRIPE_CMD_FADESTOP;
+
+	stripeCommand(len, data);
+}
+
 /**
  * Updates the color of the stripe
  * @param red Red value (max 255)
@@ -250,6 +317,7 @@ void stripeGetWhite(int* white) {
  */
 void stripeEffectDisable(void) {
 	stripeTimer.stop();
+	stripeFadeStop();
 }
 
 /**

+ 2 - 1
CoffeeCode/stripe.h

@@ -27,7 +27,7 @@ typedef enum {
 	TRANS_DIRECT = 0,
 	TRANS_FAST = 6,
 	TRANS_MEDIUM = 35,
-	TRANS_SLOW = 200
+	TRANS_SLOW = 100
 } stripe_transient_t;
 
 struct stripe_color {
@@ -42,6 +42,7 @@ void stripeInit(void);
 int stripeCommand(int len, char* data);
 void stripeUpdate(void);
 void stripeUpdateDim(void);
+void stripeFadeOut(void);
 void stripeSetRGB(int red, int green, int blue);
 void stripeSetColor(stripe_color color);
 void stripeSetTransient(stripe_transient_t transient);