You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_leds.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Bibliothèque pour l'annimation de leds sur une bande de leds.
  3. */
  4. #include <Adafruit_NeoPixel.h>
  5. #define PIN_LED D1
  6. // NeoPixel brightness, 0 (min) to 255 (max)
  7. #define BRIGHTNESS 255 // Set BRIGHTNESS to about 1/5 (max = 255)
  8. // Le nombre de pixels déclaré ici n'est pas important @FIXME
  9. // Le nombre de pixels utilisé est celui qui est stocké en EEPROM, à savoir LED_COUNT
  10. Adafruit_NeoPixel strip = Adafruit_NeoPixel(500, PIN_LED, NEO_GRB + NEO_KHZ800);
  11. struct Couleur {
  12. int R = 0;
  13. int V = 0;
  14. int B = 0;
  15. };
  16. // Fill strip pixels one after another with a color. Strip is NOT cleared
  17. // first; anything there will be covered pixel by pixel. Pass in color
  18. // (as a single 'packed' 32-bit value, which you can get by calling
  19. // strip.Color(red, green, blue) as shown in the loop() function above),
  20. // and a delay time (in milliseconds) between pixels.
  21. void LED_colorWipe(uint32_t color, int wait) {
  22. for (int i = 0; i < LED_COUNT; i++) { // For each pixel in strip...
  23. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  24. strip.show(); // Update strip to match
  25. delay(wait); // Pause for a moment
  26. }
  27. }
  28. // --------------------------------------------------------------------------------
  29. // Allume la led dont le numéro est passé en paramètre
  30. // et éteind la led précédente.
  31. void LED_AllumeLedNum( int led, int R, int V, int B) {
  32. strip.setPixelColor(led, strip.Color(R, V, B)); // Blanc
  33. strip.show();
  34. }
  35. // --------------------------------------------------------------------------------
  36. // Vérifie que la chaine est est bien un integer entre 0 et 255
  37. // Si c'est le cas on renvoie True, False sinon
  38. //
  39. boolean LED_isADigit(char* s) {
  40. char chaine[] = "rrr";
  41. int i = 0;
  42. for (i = 0; s[i]; i++) {
  43. chaine[i] = s[i]; // On construit la copie de la chaine passée en parametre
  44. }
  45. chaine[i] = s[i]; // Pour ne pas oublier le \0 de la fin
  46. char* couleur = NULL;
  47. couleur = strtok(chaine, ","); // On travail sur la copie
  48. while (couleur != NULL) {
  49. // Convertion de la chaine en integer
  50. // Si l'integer n'est pas compris en 0 et 255 ...
  51. if (atoi( couleur ) < 0 or atoi( couleur ) > 255 ) {
  52. // ... on a pas une couleur, on sort du test
  53. return false;
  54. }
  55. couleur = strtok(NULL, ",");
  56. }
  57. return true;
  58. }
  59. // --------------------------------------------------------------------------------
  60. // Vérifie que la chaine est est bien une couleur du style R,V,B
  61. // Si c'est le cas on renvoie True, False sinon
  62. //
  63. boolean LED_isAColor(char* s) {
  64. char chaine[] = "rrr,bbb,vvv";
  65. // On compte les virgules dans la chaine
  66. int i, count = 0;
  67. for (i = 0; s[i]; i++) {
  68. if (s[i] == ',') {
  69. count++;
  70. }
  71. chaine[i] = s[i]; // On construit la copie de la chaine passée en parametre
  72. }
  73. chaine[i] = s[i]; // Pour na pas oublier le \0 de la fin
  74. // on a bien 2 virgules
  75. if (count == 2 ) {
  76. char* couleur = NULL;
  77. couleur = strtok(chaine, ","); // On travail sur la copie
  78. while (couleur != NULL) {
  79. // Convertion de la chaine en integer
  80. // Si l'integer n'est pas compris en 0 et 255 ...
  81. if (atoi( couleur ) < 0 or atoi( couleur ) > 255 ) {
  82. // ... on a pas une couleur, on sort du test
  83. return false;
  84. }
  85. couleur = strtok(NULL, ",");
  86. }
  87. } else {
  88. return false;
  89. }
  90. return true;
  91. }
  92. // --------------------------------------------------------------------------------
  93. // Convertie la chaine RVB en une couleur.
  94. // @return Color
  95. Couleur LED_ExtractRVB(char* s) {
  96. // Définition d'une couleur
  97. Couleur c;
  98. char* couleur = strtok(s, ",");
  99. int count = 0;
  100. while (couleur != NULL) {
  101. if (count == 0) {
  102. c.R = atoi(couleur);
  103. } else if (count == 1) {
  104. c.V = atoi(couleur);
  105. } else if (count == 2) {
  106. c.B = atoi(couleur);
  107. }
  108. count ++;
  109. couleur = strtok(NULL, ",");
  110. }
  111. return c;
  112. }
  113. // Input a value 0 to 255 to get a color value.
  114. // The colours are a transition r - g - b - back to r.
  115. uint32_t Wheel(byte WheelPos) {
  116. WheelPos = 255 - WheelPos;
  117. if (WheelPos < 85) {
  118. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  119. }
  120. if (WheelPos < 170) {
  121. WheelPos -= 85;
  122. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  123. }
  124. WheelPos -= 170;
  125. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  126. }
  127. void rainbow(uint8_t wait) {
  128. uint16_t i, j;
  129. for (j = 0; j < 256; j++) {
  130. for (i = 0; i < LED_COUNT; i++) {
  131. strip.setPixelColor(i, Wheel((i + j) & 255));
  132. }
  133. strip.show();
  134. delay(wait);
  135. }
  136. }
  137. // Slightly different, this makes the rainbow equally distributed throughout
  138. void rainbowCycle(uint8_t wait) {
  139. uint16_t i, j;
  140. for (j = 0; j < 256 * 1; j++) { // 5 cycles of all colors on wheel
  141. for (i = 0; i < LED_COUNT; i++) {
  142. strip.setPixelColor(i, Wheel(((i * 256 / LED_COUNT) + j) & 255));
  143. }
  144. strip.show();
  145. delay(wait);
  146. }
  147. }
  148. //Theatre-style crawling lights.
  149. void theaterChase(uint32_t c, uint8_t wait) {
  150. for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  151. for (int q = 0; q < 3; q++) {
  152. for (uint16_t i = 0; i < LED_COUNT; i = i + 3) {
  153. strip.setPixelColor(i + q, c); //turn every third pixel on
  154. }
  155. strip.show();
  156. delay(wait);
  157. for (uint16_t i = 0; i < LED_COUNT; i = i + 3) {
  158. strip.setPixelColor(i + q, 0); //turn every third pixel off
  159. }
  160. }
  161. }
  162. }
  163. //Theatre-style crawling lights with rainbow effect
  164. void theaterChaseRainbow(uint8_t wait) {
  165. DEBUG("LED_COUNT:"+String(LED_COUNT));
  166. for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
  167. for (int q = 0; q < 3; q++) {
  168. for (uint16_t i = 0; i < LED_COUNT; i = i + 3) {
  169. strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  170. }
  171. strip.show();
  172. delay(wait);
  173. for (uint16_t i = 0; i < LED_COUNT; i = i + 3) {
  174. strip.setPixelColor(i + q, 0); //turn every third pixel off
  175. }
  176. }
  177. }
  178. }
  179. // --------------------------------------------------------------------------------
  180. // Fait une animation sur les leds en fonction du numéro passé en paramètre.
  181. // [1..9]
  182. //
  183. void LED_Animation(int num) {
  184. g_BOO_AnimationSeconde = false;
  185. DEBUG(num);
  186. switch ( num ) {
  187. case 0:
  188. LED_colorWipe(strip.Color(255, 255, 255), 20); // Blanc
  189. break;
  190. case 1:
  191. LED_colorWipe(strip.Color(0, 0, 255), 20); // Bleu
  192. break;
  193. case 2:
  194. theaterChase(strip.Color(0, 0, 255), 50);
  195. break;
  196. case 3:
  197. theaterChaseRainbow(50);
  198. break;
  199. case 4:
  200. rainbow(50);
  201. break;
  202. case 5:
  203. rainbowCycle(10);
  204. break;
  205. case 6:
  206. g_BOO_AnimationSeconde = true;
  207. break;
  208. default:
  209. DEBUG("Animation inconnue ->" + String(num) );
  210. break;
  211. }
  212. }