djvj

Untitled

Feb 13th, 2018
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define ANALOG_MODE_AVERAGE 0
  4. #define ANALOG_MODE_LAST_LED 1
  5.  
  6. /**************************************
  7. S E T U P
  8.  
  9. set following values to your needs
  10. **************************************/
  11.  
  12. #define INITIAL_LED_TEST_ENABLED true
  13. #define INITIAL_LED_TEST_BRIGHTNESS 32 // 0..255
  14. #define INITIAL_LED_TEST_TIME_MS 500 // 10..
  15.  
  16. // Number of leds in your strip. set to "1" and ANALOG_OUTPUT_ENABLED to "true" to activate analog only
  17. // As of 26/1/2017:
  18. // 582 leaves ZERO bytes free and this
  19. // 410 is ok
  20. // tested with 500 leds and is fine (despite the warning)
  21. #define MAX_LEDS 300
  22.  
  23. // type of your led controller, possible values, see below
  24. #define LED_TYPE WS2812B
  25.  
  26. // 3 wire (pwm): NEOPIXEL BTM1829 TM1812 TM1809 TM1804 TM1803 UCS1903 UCS1903B UCS1904 UCS2903 WS2812 WS2852
  27. // S2812B SK6812 SK6822 APA106 PL9823 WS2811 WS2813 APA104 WS2811_40 GW6205 GW6205_40 LPD1886 LPD1886_8BIT
  28. // 4 wire (spi): LPD8806 WS2801 WS2803 SM16716 P9813 APA102 SK9822 DOTSTAR
  29.  
  30. // For 3 wire led stripes line Neopixel/Ws2812, which have a data line, ground, and power, you just need to define DATA_PIN.
  31. // For led chipsets that are SPI based (four wires - data, clock, ground, and power), both defines DATA_PIN and CLOCK_PIN are needed
  32.  
  33. // DATA_PIN, or DATA_PIN, CLOCK_PIN
  34. #define LED_PINS 10 // 3 wire leds
  35. //#define LED_PINS 6, 13 // 4 wire leds
  36.  
  37. #define COLOR_ORDER GRB // colororder of the stripe, set RGB in hyperion
  38.  
  39. #define OFF_TIMEOUT 15000 // ms to switch off after no data was received, set 0 to deactivate
  40.  
  41. // analog rgb uni color led stripe - using of hyperion smoothing is recommended
  42. // ATTENTION this pin config is default for atmega328 based arduinos, others might work to
  43. // if you have flickering analog leds this might be caused by unsynced pwm signals
  44. // try other pins is more or less the only thing that helps
  45. #define ANALOG_OUTPUT_ENABLED false
  46. #define ANALOG_MODE ANALOG_MODE_LAST_LED // use ANALOG_MODE_AVERAGE or ANALOG_MODE_LAST_LED
  47. #define ANALOG_GROUND_PIN 8 // additional ground pin to make wiring a bit easier
  48. #define ANALOG_RED_PIN 9
  49. #define ANALOG_GREEN_PIN 10
  50. #define ANALOG_BLUE_PIN 11
  51.  
  52. // overall color adjustments
  53. #define ANALOG_BRIGHTNESS_RED 255 // maximum brightness for analog 0-255
  54. #define ANALOG_BRIGHTNESS_GREEN 255 // maximum brightness for analog 0-255
  55. #define ANALOG_BRIGHTNESS_BLUE 255 // maximum brightness for analog 0-255
  56.  
  57. #define BRIGHTNESS 255 // maximum brightness 0-255
  58. #define DITHER_MODE BINARY_DITHER // BINARY_DITHER or DISABLE_DITHER
  59. #define COLOR_TEMPERATURE CRGB(255,255,255) // RGB value describing the color temperature
  60. #define COLOR_CORRECTION TypicalLEDStrip // predefined fastled color correction
  61. //#define COLOR_CORRECTION CRGB(255,255,255) // or RGB value describing the color correction
  62.  
  63. // Baudrate, higher rate allows faster refresh rate and more LEDs
  64. //#define serialRate 460800 // use 115200 for ftdi based boards
  65. //#define serialRate 115200 // use 115200 for ftdi based boards
  66. #define serialRate 500000 // use 115200 for ftdi based boards
  67.  
  68.  
  69. /**************************************
  70. A D A L I G H T C O D E
  71.  
  72. no user changes needed
  73. **************************************/
  74.  
  75. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  76. uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
  77.  
  78. unsigned long endTime;
  79.  
  80. // Define the array of leds
  81. CRGB leds[MAX_LEDS];
  82.  
  83. // set rgb to analog led stripe
  84. void showAnalogRGB(const CRGB& led) {
  85. if (ANALOG_OUTPUT_ENABLED) {
  86. byte r = map(led.r, 0,255,0,ANALOG_BRIGHTNESS_RED);
  87. byte g = map(led.g, 0,255,0,ANALOG_BRIGHTNESS_GREEN);
  88. byte b = map(led.b, 0,255,0,ANALOG_BRIGHTNESS_BLUE);
  89. analogWrite(ANALOG_RED_PIN , r);
  90. analogWrite(ANALOG_GREEN_PIN, g);
  91. analogWrite(ANALOG_BLUE_PIN , b);
  92. }
  93. }
  94.  
  95. // set color to all leds
  96. void showColor(const CRGB& led) {
  97. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  98. LEDS.showColor(led);
  99. #endif
  100. showAnalogRGB(led);
  101. }
  102.  
  103. // switch of digital and analog leds
  104. void switchOff() {
  105. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  106. memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
  107. FastLED.show();
  108. #endif
  109. showAnalogRGB(leds[0]);
  110. }
  111.  
  112. // function to check if serial data is available
  113. // if timeout occured leds switch of, if configured
  114. bool checkIncommingData() {
  115. boolean dataAvailable = true;
  116. while (!Serial.available()) {
  117. if ( OFF_TIMEOUT > 0 && endTime < millis()) {
  118. switchOff();
  119. dataAvailable = false;
  120. endTime = millis() + OFF_TIMEOUT;
  121. }
  122. }
  123.  
  124. return dataAvailable;
  125. }
  126.  
  127. // main function that setups and runs the code
  128. void setup() {
  129. Serial.begin(serialRate);
  130.  
  131. // analog output
  132. if (ANALOG_OUTPUT_ENABLED) {
  133. // additional ground pin to make wiring a bit easier
  134. pinMode(ANALOG_GROUND_PIN, OUTPUT);
  135. digitalWrite(ANALOG_GROUND_PIN, LOW);
  136. pinMode(ANALOG_BLUE_PIN , OUTPUT);
  137. pinMode(ANALOG_RED_PIN , OUTPUT);
  138. pinMode(ANALOG_GREEN_PIN, OUTPUT);
  139. }
  140.  
  141. int ledCount = MAX_LEDS;
  142. if (ANALOG_MODE == ANALOG_MODE_LAST_LED) {
  143. ledCount--;
  144. }
  145.  
  146. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  147. FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, ledCount);
  148. #endif
  149.  
  150. // color adjustments
  151. FastLED.setBrightness ( BRIGHTNESS );
  152. FastLED.setTemperature( COLOR_TEMPERATURE );
  153. FastLED.setCorrection ( COLOR_CORRECTION );
  154. FastLED.setDither ( DITHER_MODE );
  155.  
  156. // initial RGB flash
  157. #if INITIAL_LED_TEST_ENABLED == true
  158. for (int v=0;v<INITIAL_LED_TEST_BRIGHTNESS;v++)
  159. {
  160. showColor(CRGB(v,v,v));
  161. delay(INITIAL_LED_TEST_TIME_MS/2/INITIAL_LED_TEST_BRIGHTNESS);
  162. }
  163.  
  164. for (int v=0;v<INITIAL_LED_TEST_BRIGHTNESS;v++)
  165. {
  166. showColor(CRGB(v,v,v));
  167. delay(INITIAL_LED_TEST_TIME_MS/2/INITIAL_LED_TEST_BRIGHTNESS);
  168. }
  169. #endif
  170. showColor(CRGB(0, 0, 0));
  171.  
  172. Serial.print("Ada\n"); // Send "Magic Word" string to host
  173.  
  174.  
  175. boolean transmissionSuccess;
  176. unsigned long sum_r, sum_g, sum_b;
  177.  
  178. // loop() is avoided as even that small bit of function overhead
  179. // has a measurable impact on this code's overall throughput.
  180. for(;;) {
  181. // wait for first byte of Magic Word
  182. for (i = 0; i < sizeof prefix; ++i) {
  183. // If next byte is not in Magic Word, the start over
  184. if (!checkIncommingData() || prefix[i] != Serial.read()) {
  185. i = 0;
  186. }
  187. }
  188.  
  189. // Hi, Lo, Checksum
  190. if (!checkIncommingData()) continue;
  191. hi = Serial.read();
  192. if (!checkIncommingData()) continue;
  193. lo = Serial.read();
  194. if (!checkIncommingData()) continue;
  195. chk = Serial.read();
  196.  
  197. // if checksum does not match go back to wait
  198. if (chk != (hi ^ lo ^ 0x55)) continue;
  199.  
  200. memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
  201. transmissionSuccess = true;
  202. sum_r = 0;
  203. sum_g = 0;
  204. sum_b = 0;
  205.  
  206. int num_leds = min ( MAX_LEDS, (hi<<8) + lo + 1 );
  207.  
  208. // read the transmission data and set LED values
  209. for (int idx = 0; idx < num_leds; idx++) {
  210. byte r, g, b;
  211. if (!checkIncommingData()) {
  212. transmissionSuccess = false;
  213. break;
  214. }
  215. r = Serial.read();
  216. if (!checkIncommingData()) {
  217. transmissionSuccess = false;
  218. break;
  219. }
  220. g = Serial.read();
  221. if (!checkIncommingData()) {
  222. transmissionSuccess = false;
  223. break;
  224. }
  225. b = Serial.read();
  226. leds[idx].r = r;
  227. leds[idx].g = g;
  228. leds[idx].b = b;
  229. #if ANALOG_OUTPUT_ENABLED == true && ANALOG_MODE == ANALOG_MODE_AVERAGE
  230. sum_r += r;
  231. sum_g += g;
  232. sum_b += b;
  233. #endif
  234. }
  235.  
  236. // shows new values
  237. if (transmissionSuccess) {
  238. endTime = millis() + OFF_TIMEOUT;
  239. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  240. FastLED.show();
  241. #endif
  242.  
  243. #if ANALOG_OUTPUT_ENABLED == true
  244. #if ANALOG_MODE == ANALOG_MODE_LAST_LED
  245. showAnalogRGB(leds[MAX_LEDS-1]);
  246. #else
  247. showAnalogRGB(CRGB(sum_r/MAX_LEDS, sum_g/MAX_LEDS, sum_b/MAX_LEDS));
  248. #endif
  249. #endif
  250. }
  251. }
  252. } // end of setup
  253.  
  254. void loop() {
  255. // Not used. See note in setup() function.
  256. }
Add Comment
Please, Sign In to add comment