PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

Adafruit_NeoPixel.h 7.4KB

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*--------------------------------------------------------------------
  2. This file is part of the Adafruit NeoPixel library.
  3. NeoPixel is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation, either version 3 of
  6. the License, or (at your option) any later version.
  7. NeoPixel is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with NeoPixel. If not, see
  13. <http://www.gnu.org/licenses/>.
  14. --------------------------------------------------------------------*/
  15. #ifndef ADAFRUIT_NEOPIXEL_H
  16. #define ADAFRUIT_NEOPIXEL_H
  17. #if (ARDUINO >= 100)
  18. #include <Arduino.h>
  19. #else
  20. #include <WProgram.h>
  21. #include <pins_arduino.h>
  22. #endif
  23. // The order of primary colors in the NeoPixel data stream can vary
  24. // among device types, manufacturers and even different revisions of
  25. // the same item. The third parameter to the Adafruit_NeoPixel
  26. // constructor encodes the per-pixel byte offsets of the red, green
  27. // and blue primaries (plus white, if present) in the data stream --
  28. // the following #defines provide an easier-to-use named version for
  29. // each permutation. e.g. NEO_GRB indicates a NeoPixel-compatible
  30. // device expecting three bytes per pixel, with the first byte
  31. // containing the green value, second containing red and third
  32. // containing blue. The in-memory representation of a chain of
  33. // NeoPixels is the same as the data-stream order; no re-ordering of
  34. // bytes is required when issuing data to the chain.
  35. // Bits 5,4 of this value are the offset (0-3) from the first byte of
  36. // a pixel to the location of the red color byte. Bits 3,2 are the
  37. // green offset and 1,0 are the blue offset. If it is an RGBW-type
  38. // device (supporting a white primary in addition to R,G,B), bits 7,6
  39. // are the offset to the white byte...otherwise, bits 7,6 are set to
  40. // the same value as 5,4 (red) to indicate an RGB (not RGBW) device.
  41. // i.e. binary representation:
  42. // 0bWWRRGGBB for RGBW devices
  43. // 0bRRRRGGBB for RGB
  44. // RGB NeoPixel permutations; white and red offsets are always same
  45. // Offset: W R G B // HEX representation
  46. #define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) // 0x06
  47. #define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) // 0x09
  48. #define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) // 0x52
  49. #define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) // 0xA1
  50. #define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) // 0x58
  51. #define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) // 0xA4
  52. // RGBW NeoPixel permutations; all 4 offsets are distinct
  53. // Offset: W R G B // HEX representation
  54. #define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) // 0x1B
  55. #define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) // 0x1E
  56. #define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) // 0x27
  57. #define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) // 0x36
  58. #define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) // 0x2D
  59. #define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) // 0x39
  60. #define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) // 0x4B
  61. #define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) // 0x4E
  62. #define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) // 0x87
  63. #define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) // 0xC6
  64. #define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) // 0x8D
  65. #define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) // 0xC9
  66. #define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) // 0x63
  67. #define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) // 0x72
  68. #define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) // 0x93
  69. #define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) // 0xD2
  70. #define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) // 0xB1
  71. #define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) // 0xE1
  72. #define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) // 0x6C
  73. #define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) // 0x78
  74. #define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) // 0x9C
  75. #define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) // 0xD8
  76. #define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) // 0xB4
  77. #define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) // 0xE4
  78. // Add NEO_KHZ400 to the color order value to indicate a 400 KHz
  79. // device. All but the earliest v1 NeoPixels expect an 800 KHz data
  80. // stream, this is the default if unspecified. Because flash space
  81. // is very limited on ATtiny devices (e.g. Trinket, Gemma), v1
  82. // NeoPixels aren't handled by default on those chips, though it can
  83. // be enabled by removing the ifndef/endif below -- but code will be
  84. // bigger. Conversely, can disable the NEO_KHZ400 line on other MCUs
  85. // to remove v1 support and save a little space.
  86. #define NEO_KHZ800 0x0000 // 800 KHz datastream
  87. #ifndef __AVR_ATtiny85__
  88. #define NEO_KHZ400 0x0100 // 400 KHz datastream
  89. #endif
  90. // If 400 KHz support is enabled, the third parameter to the constructor
  91. // requires a 16-bit value (in order to select 400 vs 800 KHz speed).
  92. // If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value
  93. // is sufficient to encode pixel color order, saving some space.
  94. #ifdef NEO_KHZ400
  95. typedef uint16_t neoPixelType;
  96. #else
  97. typedef uint8_t neoPixelType;
  98. #endif
  99. class Adafruit_NeoPixel {
  100. public:
  101. // Constructor: number of LEDs, pin number, LED type
  102. Adafruit_NeoPixel(uint16_t n, uint8_t p=6, neoPixelType t=NEO_GRB + NEO_KHZ800);
  103. Adafruit_NeoPixel(void);
  104. ~Adafruit_NeoPixel();
  105. void
  106. begin(void),
  107. show(void),
  108. setPin(uint8_t p),
  109. setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b),
  110. setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w),
  111. setPixelColor(uint16_t n, uint32_t c),
  112. fill(uint32_t c=0, uint16_t first=0, uint16_t count=0),
  113. setBrightness(uint8_t),
  114. clear(),
  115. updateLength(uint16_t n),
  116. updateType(neoPixelType t);
  117. uint8_t
  118. *getPixels(void) const,
  119. getBrightness(void) const,
  120. sine8(uint8_t) const,
  121. gamma8(uint8_t) const;
  122. int8_t
  123. getPin(void) { return pin; };
  124. uint16_t
  125. numPixels(void) const;
  126. static uint32_t
  127. Color(uint8_t r, uint8_t g, uint8_t b),
  128. Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
  129. uint32_t
  130. getPixelColor(uint16_t n) const;
  131. inline bool
  132. canShow(void) { return (micros() - endTime) >= 300L; }
  133. protected:
  134. boolean
  135. #ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
  136. is800KHz, // ...true if 800 KHz pixels
  137. #endif
  138. begun; // true if begin() previously called
  139. uint16_t
  140. numLEDs, // Number of RGB LEDs in strip
  141. numBytes; // Size of 'pixels' buffer below (3 or 4 bytes/pixel)
  142. int8_t
  143. pin; // Output pin number (-1 if not yet set)
  144. uint8_t
  145. brightness,
  146. *pixels, // Holds LED color values (3 or 4 bytes each)
  147. rOffset, // Index of red byte within each 3- or 4-byte pixel
  148. gOffset, // Index of green byte
  149. bOffset, // Index of blue byte
  150. wOffset; // Index of white byte (same as rOffset if no white)
  151. uint32_t
  152. endTime; // Latch timing reference
  153. #ifdef __AVR__
  154. volatile uint8_t
  155. *port; // Output PORT register
  156. uint8_t
  157. pinMask; // Output PORT bitmask
  158. #endif
  159. };
  160. #endif // ADAFRUIT_NEOPIXEL_H