stripe.cpp 7.4 KB

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