stripe.cpp 7.2 KB

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