stripe.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <errno.h>
  10. #include <pthread.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include "stripe.h"
  14. #include "global.h"
  15. #include "timer.h"
  16. #include "logger.h"
  17. stripe_color currentColor;
  18. stripe_color effectColor;
  19. int currentWhite = 0, currentDim = 0;
  20. stripe_transient_t currentTransient = TRANS_DIRECT;
  21. timer stripeTimer(stripeTimerHandler);
  22. int i2cfd;
  23. /**
  24. * This is the handler for time based stripe effects
  25. * @param *threadid Thread ID
  26. */
  27. void *stripeTimerHandler(void *threadid) {
  28. if (currentColor.red == effectColor.red
  29. && currentColor.green == effectColor.green
  30. && currentColor.blue == effectColor.blue) {
  31. stripeSetRGB(0,0,0);
  32. } else {
  33. stripeSetColor(effectColor);
  34. }
  35. pthread_exit(NULL);
  36. }
  37. /**
  38. * Thread for the stripe control. Requests data from other threads or HAL
  39. * @param *threadid Thread ID
  40. */
  41. void *stripeThread(void *threadid) {
  42. logger(V_STRIPE, "Initializing Stripe thread...\n");
  43. stripeInit();
  44. stripeTimer.setDivider(40);
  45. stripeTimer.stop();
  46. logger(V_BASIC, "Initialized Stripe thread\n");
  47. stripe_color col;
  48. col.red = 255;
  49. col.green = 0;
  50. col.blue = 0;
  51. stripeEffectPulse(col);
  52. int tmp = 0, inc = 1;
  53. while (1) {
  54. pause();
  55. stripeEffectHeating(tmp);
  56. logger(V_NONE, LOG_ERRORC, "Simulating %d%% percent heating\n", tmp);
  57. sleep(1);
  58. if (inc) {
  59. if (++tmp >= 100) inc = 0;
  60. } else {
  61. if (--tmp <= 0) inc = 1;
  62. }
  63. }
  64. pthread_exit(NULL);
  65. }
  66. /**
  67. * Initializes i2c communication to stripe controller
  68. */
  69. void stripeInit() {
  70. i2cfd = wiringPiI2CSetup(I2C_ADDRESS_STRIPE);
  71. if (i2cfd < 0) {
  72. logger_error("Could not initialize i2c with device ID 0x%.2x (%s)\n",
  73. I2C_ADDRESS_STRIPE, strerror(errno));
  74. pthread_exit(NULL);
  75. }
  76. logger(V_STRIPE, "Initialized i2c device on address 0x%.2x\n",
  77. I2C_ADDRESS_STRIPE);
  78. currentColor.red = 0;
  79. currentColor.green = 0;
  80. currentColor.blue = 0;
  81. currentWhite = 0;
  82. currentDim = 0xff;
  83. currentTransient = TRANS_DIRECT;
  84. }
  85. /**
  86. * Sends out command data to the stripe controller via i2c
  87. * @param len Length of the data array
  88. * @param *data Pointer to data array
  89. */
  90. int stripeCommand(int len, char* data) {
  91. int ret = 0;
  92. int i = 0;
  93. pthread_mutex_lock(&mutex);
  94. for (i = 0; i < len; i++) {
  95. ret += wiringPiI2CWriteReg8(i2cfd, i, data[i]);
  96. }
  97. logger(V_STRIPE, "Wrote %d bytes to i2c device\n", len);
  98. if (ret)
  99. logger(V_NONE, LOG_WARN, "Failed to write %d of %d bytes on i2c device\n", ret, len);
  100. pthread_mutex_unlock(&mutex);
  101. return ret;
  102. }
  103. /**
  104. * Updates the color data and prepares the stripe command
  105. */
  106. void stripeUpdate(void) {
  107. char data[7];
  108. int len = 7;
  109. if (currentTransient == TRANS_DIRECT) {
  110. data[0] = STRIPE_CMD_SET;
  111. data[1] = currentDim;
  112. data[2] = currentColor.red;
  113. data[3] = currentColor.green;
  114. data[4] = currentColor.blue;
  115. data[5] = currentWhite;
  116. len = 6;
  117. } else {
  118. data[0] = STRIPE_CMD_FADETO;
  119. data[1] = currentColor.red;
  120. data[2] = currentColor.green;
  121. data[3] = currentColor.blue;
  122. data[4] = currentWhite;
  123. data[5] = (currentTransient >> 8) & 0xff;
  124. data[6] = currentTransient & 0xff;
  125. }
  126. stripeCommand(len, data);
  127. }
  128. /**
  129. * Updates the dimmer value of the stripe
  130. */
  131. void stripeUpdateDim(void) {
  132. char data[2];
  133. int len = 2;
  134. data[0] = STRIPE_CMD_SETDIM;
  135. data[1] = currentDim;
  136. stripeCommand(len, data);
  137. }
  138. /**
  139. * Updates the color of the stripe
  140. * @param red Red value (max 255)
  141. * @param green Green value (max 255)
  142. * @param blue Blue value (max 255)
  143. */
  144. void stripeSetRGB(int red, int green, int blue) {
  145. currentColor.red = red & 0xff;
  146. currentColor.green = green & 0xff;
  147. currentColor.blue = blue & 0xff;
  148. stripeUpdate();
  149. }
  150. /**
  151. * Updates the color of the stripe
  152. * @param color Color struct
  153. */
  154. void stripeSetColor(stripe_color color) {
  155. stripeSetRGB(color.red, color.green, color.blue);
  156. }
  157. /**
  158. * Updates the transient time for the stripe (fading)
  159. * @param transient Transient time in ticks
  160. */
  161. void stripeSetTransient(stripe_transient_t transient) {
  162. currentTransient = transient;
  163. }
  164. /**
  165. * Updates the dimmer value of the stripe
  166. * @param dim Dimmer value
  167. */
  168. void stripeSetDim(int dim) {
  169. dim = dim & 0xff;
  170. }
  171. /**
  172. * Updates the white value of the stripe (4th channel)
  173. * @param white White value
  174. */
  175. void stripeSetWhite(int white) {
  176. currentWhite = white & 0xff;
  177. }
  178. /**
  179. * Turns the stripe on (Sets dimmer to 100%)
  180. */
  181. void stripeOn(void) {
  182. if (!currentDim) {
  183. currentDim = 0xff;
  184. }
  185. stripeUpdateDim();
  186. }
  187. /**
  188. * Turns the stripe on (Sets dimmer to 0%)
  189. */
  190. void stripeOff(void) {
  191. currentDim = 0;
  192. stripeUpdateDim();
  193. }
  194. /**
  195. * Reads the current stripe color
  196. * @param *red Pointer to red variable
  197. * @param *green Pointer to green variable
  198. * @param *blue Pointer to blue variable
  199. */
  200. void stripeGetRGB(int* red, int* green, int* blue) {
  201. *red = currentColor.red;
  202. *green = currentColor.green;
  203. *blue = currentColor.blue;
  204. }
  205. /**
  206. * Reads the current stripe dimmer value
  207. * @param *dim Pointer to dimmer variable
  208. */
  209. void stripeGetDim(int* dim) {
  210. *dim = currentDim;
  211. }
  212. /**
  213. * Reads the current stripe white value (4th channel)
  214. * @param *white Pointer to white variable
  215. */
  216. void stripeGetWhite(int* white) {
  217. *white = currentWhite;
  218. }
  219. /**
  220. * Stops current running effects
  221. */
  222. void stripeEffectDisable(void) {
  223. stripeTimer.stop();
  224. }
  225. /**
  226. * Sets the stripe to a correspondending color to the heating status
  227. * @param percent Heating level (0-100%)
  228. */
  229. void stripeEffectHeating(int percent) {
  230. long red, blue;
  231. if (percent > 100) percent = 100;
  232. red = percent * 255;
  233. blue = (100-percent) * 255;
  234. stripeSetTransient(TRANS_FAST);
  235. stripeSetRGB(red/100,0,blue/100);
  236. }
  237. /**
  238. * Enables pulsing with a given color of the stripe
  239. * @param color Effect color
  240. */
  241. void stripeEffectPulse(stripe_color color) {
  242. effectColor = color;
  243. stripeSetTransient(TRANS_MEDIUM);
  244. stripeTimer.setDivider(40);
  245. stripeTimer.start();
  246. }