stripe.cpp 5.7 KB

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