Advertisement
Speeedfire

Untitled

Jan 24th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define NUM_LEDS 92
  4. #define LED_DATA_PIN 3
  5. #define NUM_BYTES (NUM_LEDS*3) // 3 colors  
  6.  
  7. #define BRIGHTNESS 50
  8. #define UPDATES_PER_SECOND 100
  9.  
  10. #define TIMEOUT 150000
  11.  
  12. #define MODE_ANIMATION 0
  13. #define MODE_AMBILIGHT 1
  14. uint8_t mode = MODE_ANIMATION;
  15.  
  16. byte MESSAGE_PREAMBLE[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
  17. uint8_t PREAMBLE_LENGTH = 10;
  18. uint8_t current_preamble_position = 0;
  19.  
  20. unsigned long last_serial_available = -1L;
  21.  
  22. uint8_t led_counter = 0;
  23. uint8_t byte_counter = 0;
  24.  
  25. CRGB leds[NUM_LEDS];
  26. byte buffer[NUM_BYTES];
  27.  
  28.  
  29. // Filler animation attributes
  30. CRGBPalette16 currentPalette = RainbowColors_p;
  31. TBlendType    currentBlending = LINEARBLEND;
  32. uint8_t startIndex = 0;
  33.  
  34. void setup()
  35. {
  36.   Serial.begin(460800);
  37.   FastLED.clear(true);
  38.   FastLED.addLeds<NEOPIXEL, LED_DATA_PIN>(leds, NUM_LEDS);
  39.   FastLED.setBrightness(BRIGHTNESS);
  40. }
  41.  
  42. void loop()
  43. {
  44.     switch (mode) {
  45.         case MODE_ANIMATION:
  46.             fillLEDsFromPaletteColors();
  47.             break;
  48.        
  49.         case MODE_AMBILIGHT:
  50.             processIncomingData();
  51.             break;
  52.     }
  53.    
  54. }
  55.  
  56. void processIncomingData()
  57. {
  58.     if (waitForPreamble(TIMEOUT))
  59.     {
  60.         Serial.readBytes(buffer, NUM_BYTES);
  61.  
  62.         /* DEBUG
  63.         for (int i = 0; i < NUM_BYTES; i++)
  64.         {
  65.             Serial.write((char)buffer[i]);
  66.         }
  67.         */
  68.  
  69.         while (byte_counter < NUM_BYTES)
  70.         {
  71.             byte blue = buffer[byte_counter++];
  72.             byte green = buffer[byte_counter++];
  73.             byte red = buffer[byte_counter++];
  74.  
  75.             leds[led_counter++] = CRGB(red, green, blue);
  76.         }
  77.  
  78.         FastLED.show();
  79.  
  80.         byte_counter = 0;
  81.         led_counter = 0;
  82.     }
  83.     else
  84.     {
  85.         mode = MODE_ANIMATION;
  86.     }
  87. }
  88.  
  89. bool waitForPreamble(int timeout)
  90. {
  91.     last_serial_available = millis();
  92.     while (current_preamble_position < PREAMBLE_LENGTH)
  93.     {
  94.         if (Serial.available() > 0)
  95.         {
  96.             last_serial_available = millis();
  97.  
  98.             if (Serial.read() == MESSAGE_PREAMBLE[current_preamble_position])
  99.             {
  100.                 current_preamble_position++;
  101.             }
  102.             else
  103.             {
  104.                 current_preamble_position = 0;
  105.             }
  106.         }
  107.  
  108.         if (millis() - last_serial_available > timeout)
  109.         {
  110.             return false;
  111.         }
  112.     }
  113.     current_preamble_position = 0;
  114.     return true;
  115. }
  116.  
  117. void fillLEDsFromPaletteColors()
  118. {
  119.     startIndex++; // speed
  120.  
  121.     uint8_t colorIndex = startIndex;
  122.     for( int i = 0; i < NUM_LEDS; i++) {
  123.         leds[i] = ColorFromPalette(currentPalette, colorIndex, BRIGHTNESS, currentBlending);
  124.             colorIndex += 3;
  125.     }
  126.  
  127.     FastLED.delay(1000 / UPDATES_PER_SECOND);
  128.     if (Serial.available() > 0)
  129.     {
  130.         mode = MODE_AMBILIGHT;
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement