stripe.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * stripe.cpp
  3. *
  4. * Created on: Sep 25, 2017
  5. * Author: Philipp Hinz
  6. */
  7. #include <wiringPiI2C.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <errno.h>
  11. #include <pthread.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include "stripe.h"
  15. #include "global.h"
  16. #include "timer.h"
  17. #include "logger.h"
  18. #include "coffee.h"
  19. stripe_color currentColor;
  20. stripe_color effectColor;
  21. int currentWhite = 0, currentDim = 0;
  22. stripe_transient_t currentTransient = TRANS_DIRECT;
  23. timer stripeTimer(stripeTimerHandler);
  24. int i2cfd;
  25. coffee_status_t lastState = STATE_OFF;
  26. /**
  27. * This is the handler for time based stripe effects
  28. * @param *threadid Thread ID
  29. */
  30. void *stripeTimerHandler(void *threadid) {
  31. if (currentColor.red == effectColor.red
  32. && currentColor.green == effectColor.green
  33. && currentColor.blue == effectColor.blue) {
  34. stripeSetRGB(0, 0, 0);
  35. } else {
  36. stripeSetColor(effectColor);
  37. }
  38. pthread_exit(NULL);
  39. }
  40. /**
  41. * Thread for the stripe control. Requests data from other threads or HAL
  42. * @param *threadid Thread ID
  43. */
  44. void *stripeThread(void *threadid) {
  45. logger(V_STRIPE, "Initializing Stripe thread...\n");
  46. stripeInit();
  47. stripeTimer.setDivider(ms2Divider(2000));
  48. stripeTimer.stop();
  49. logger(V_BASIC, "Initialized Stripe thread\n");
  50. stripeOn();
  51. stripeSetRGB(0, 0, 0);
  52. stripe_color col;
  53. //int tmp = 0, inc = 1;
  54. while (1) {
  55. if (getState() != lastState) {
  56. lastState = getState();
  57. switch (lastState) {
  58. case STATE_IDLE:
  59. case STATE_HEATING:
  60. case STATE_BREW:
  61. case STATE_BREWMANUAL:
  62. case STATE_CLEANING:
  63. case STATE_NULL:
  64. col.red = 255;
  65. col.green = 0;
  66. col.blue = 0;
  67. stripeEffectPulse(col);
  68. break;
  69. case STATE_FULLTANK:
  70. col.red = 255;
  71. col.green = 0;
  72. col.blue = 255;
  73. stripeEffectPulse(col);
  74. break;
  75. case STATE_INITALHEATING:
  76. col.red = 0;
  77. col.green = 0;
  78. col.blue = 255;
  79. stripeEffectPulse(col);
  80. break;
  81. case STATE_ERROR:
  82. stripeEffectDisable();
  83. stripeSetTransient(TRANS_FAST);
  84. stripeSetRGB(255, 0, 0);
  85. break;
  86. case STATE_OFF:
  87. usleep(50000);
  88. stripeEffectDisable();
  89. stripeSetTransient(TRANS_SLOW);
  90. stripeSetRGB(0, 0, 0);
  91. stripeSetRGB(0, 0, 0); // do it twice, else it doesn't work?! dafuq..
  92. break;
  93. case STATE_WAIT_OFF:
  94. col.red = 255;
  95. col.green = 0;
  96. col.blue = 0;
  97. stripeEffectPulse(col);
  98. stripeSetTransient(TRANS_FAST);
  99. break;
  100. }
  101. }
  102. usleep(100000);
  103. /*
  104. pause();
  105. stripeEffectHeating(tmp);
  106. logger(V_NONE, LOG_ERRORC, "Simulating %d%% percent heating\n", tmp);
  107. sleep(1);
  108. if (inc) {
  109. if (++tmp >= 100)
  110. inc = 0;
  111. } else {
  112. if (--tmp <= 0)
  113. inc = 1;
  114. }*/
  115. }
  116. pthread_exit(NULL);
  117. }
  118. /**
  119. * Initializes i2c communication to stripe controller
  120. */
  121. void stripeInit(void) {
  122. i2cfd = wiringPiI2CSetup(I2C_ADDRESS_STRIPE);
  123. if (i2cfd < 0) {
  124. logger_error("Could not initialize i2c with device ID 0x%.2x (%s)\n",
  125. I2C_ADDRESS_STRIPE, strerror(errno));
  126. pthread_exit(NULL);
  127. }
  128. logger(V_STRIPE, "Initialized i2c device on address 0x%.2x\n",
  129. I2C_ADDRESS_STRIPE);
  130. currentColor.red = 0;
  131. currentColor.green = 0;
  132. currentColor.blue = 0;
  133. currentWhite = 0;
  134. currentDim = 0xff;
  135. currentTransient = TRANS_DIRECT;
  136. }
  137. /**
  138. * Sends out command data to the stripe controller via i2c
  139. * @param len Length of the data array
  140. * @param *data Pointer to data array
  141. * @return Number of failed bytes, 0 if successful
  142. */
  143. int stripeCommand(int len, char* data) {
  144. int ret = 0;
  145. int i = 0;
  146. pthread_mutex_lock(&mutex_i2c);
  147. for (i = 0; i < len; i++) {
  148. ret -= wiringPiI2CWriteReg8(i2cfd, i, data[i]);
  149. }
  150. logger(V_STRIPE, "Wrote %d bytes to i2c device\n", len);
  151. if (ret)
  152. logger(V_NONE, LOG_WARN,
  153. "Failed to write %d of %d bytes on i2c device\n", ret, len);
  154. pthread_mutex_unlock(&mutex_i2c);
  155. return ret;
  156. }
  157. /**
  158. * Updates the color data and prepares the stripe command
  159. */
  160. void stripeUpdate(void) {
  161. char data[7];
  162. int len = 7;
  163. if (currentTransient == TRANS_DIRECT) {
  164. data[0] = STRIPE_CMD_SET;
  165. data[1] = currentDim;
  166. data[2] = currentColor.red;
  167. data[3] = currentColor.green;
  168. data[4] = currentColor.blue;
  169. data[5] = currentWhite;
  170. len = 6;
  171. } else {
  172. data[0] = STRIPE_CMD_FADETO;
  173. data[1] = currentColor.red;
  174. data[2] = currentColor.green;
  175. data[3] = currentColor.blue;
  176. data[4] = currentWhite;
  177. data[5] = (currentTransient >> 8) & 0xff;
  178. data[6] = currentTransient & 0xff;
  179. }
  180. stripeCommand(len, data);
  181. logger(V_STRIPE, "Updated stripe to color r %d g %d b %d\n", currentColor.red,
  182. currentColor.green, currentColor.blue);
  183. }
  184. /**
  185. * Updates the dimmer value of the stripe
  186. */
  187. void stripeUpdateDim(void) {
  188. char data[2];
  189. int len = 2;
  190. data[0] = STRIPE_CMD_SETDIM;
  191. data[1] = currentDim;
  192. stripeCommand(len, data);
  193. }
  194. /**
  195. * Let the stripe fade out and turn off
  196. */
  197. void stripeFadeOut(void) {
  198. char data[3];
  199. int len = 3;
  200. data[0] = STRIPE_CMD_FADEOUT;
  201. data[1] = (currentTransient >> 8) & 0xff;
  202. data[2] = currentTransient & 0xff;
  203. stripeCommand(len, data);
  204. }
  205. /**
  206. * Stops the current fading
  207. */
  208. void stripeFadeStop(void) {
  209. char data[1];
  210. int len = 1;
  211. data[0] = STRIPE_CMD_FADESTOP;
  212. stripeCommand(len, data);
  213. }
  214. /**
  215. * Updates the color of the stripe
  216. * @param red Red value (max 255)
  217. * @param green Green value (max 255)
  218. * @param blue Blue value (max 255)
  219. */
  220. void stripeSetRGB(int red, int green, int blue) {
  221. currentColor.red = red & 0xff;
  222. currentColor.green = green & 0xff;
  223. currentColor.blue = blue & 0xff;
  224. stripeUpdate();
  225. }
  226. /**
  227. * Updates the color of the stripe
  228. * @param color Color struct
  229. */
  230. void stripeSetColor(stripe_color color) {
  231. stripeSetRGB(color.red, color.green, color.blue);
  232. }
  233. /**
  234. * Updates the transient time for the stripe (fading)
  235. * @param transient Transient time in ticks
  236. */
  237. void stripeSetTransient(stripe_transient_t transient) {
  238. currentTransient = transient;
  239. }
  240. /**
  241. * Updates the dimmer value of the stripe
  242. * @param dim Dimmer value
  243. */
  244. void stripeSetDim(int dim) {
  245. dim = dim & 0xff;
  246. }
  247. /**
  248. * Updates the white value of the stripe (4th channel)
  249. * @param white White value
  250. */
  251. void stripeSetWhite(int white) {
  252. currentWhite = white & 0xff;
  253. }
  254. /**
  255. * Turns the stripe on (Sets dimmer to 100%)
  256. */
  257. void stripeOn(void) {
  258. if (!currentDim) {
  259. currentDim = 0xff;
  260. }
  261. stripeUpdateDim();
  262. }
  263. /**
  264. * Turns the stripe on (Sets dimmer to 0%)
  265. */
  266. void stripeOff(void) {
  267. currentDim = 0;
  268. stripeUpdateDim();
  269. }
  270. /**
  271. * Reads the current stripe color
  272. * @param *red Pointer to red variable
  273. * @param *green Pointer to green variable
  274. * @param *blue Pointer to blue variable
  275. */
  276. void stripeGetRGB(int* red, int* green, int* blue) {
  277. *red = currentColor.red;
  278. *green = currentColor.green;
  279. *blue = currentColor.blue;
  280. }
  281. /**
  282. * Reads the current stripe dimmer value
  283. * @param *dim Pointer to dimmer variable
  284. */
  285. void stripeGetDim(int* dim) {
  286. *dim = currentDim;
  287. }
  288. /**
  289. * Reads the current stripe white value (4th channel)
  290. * @param *white Pointer to white variable
  291. */
  292. void stripeGetWhite(int* white) {
  293. *white = currentWhite;
  294. }
  295. /**
  296. * Stops current running effects
  297. */
  298. void stripeEffectDisable(void) {
  299. stripeTimer.stop();
  300. stripeFadeStop();
  301. }
  302. /**
  303. * Sets the stripe to a correspondending color to the heating status
  304. * @param percent Heating level (0-100%)
  305. */
  306. void stripeEffectHeating(int percent) {
  307. long red, blue;
  308. if (percent > 100)
  309. percent = 100;
  310. red = percent * 255;
  311. blue = (100 - percent) * 255;
  312. stripeSetTransient(TRANS_FAST);
  313. stripeSetRGB(red / 100, 0, blue / 100);
  314. }
  315. /**
  316. * Enables pulsing with a given color of the stripe
  317. * @param color Effect color
  318. */
  319. void stripeEffectPulse(stripe_color color) {
  320. effectColor = color;
  321. stripeSetTransient(TRANS_MEDIUM);
  322. stripeTimer.setDivider(ms2Divider(2150));
  323. stripeTimer.start();
  324. }