Advertisement
Malle355

ArduinoAmbilightAdalightSketchHyperionBoblight.ino

Jul 26th, 2015
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. /* Modified and commented by ai.rs
  2. * t4a_boblight
  3. * (C) 2014 Hans Luijten, www.tweaking4all.com
  4. *
  5. * t4a_boblight is free software and can be distributed and/or modified
  6. * freely as long as the copyright notice remains in place.
  7. * Nobody is allowed to charge you for this code.
  8. * Use of this code is entirely at your own risk.
  9. */
  10.  
  11. #include "Adafruit_NeoPixel.h"
  12.  
  13. // DEFINITIONS
  14.  
  15. #define STARTCOLOR 0x333333 // LED colors at start
  16. #define BLACK 0x000000 // LED color BLACK
  17.  
  18. #define DATAPIN 6 // Datapin
  19. #define LEDCOUNT 183 // Number of LEDs used for boblight
  20. // LEDCOUNT value is local value in Arduino sketch, for hyperion it doesn't matter it sends prefx characters according to hyperion config
  21. #define SHOWDELAY 200 // Delay in micro seconds before showing default 200
  22. #define BAUDRATE 500000// Serial port speed, 460800 tested with Arduino Uno R3 23400 za MEGA, 115200 nano
  23.  
  24. #define BRIGHTNESS 90 // Max. brightness in %
  25. //Hyperion sends prefix characters based on number of LEDs in config file
  26. // e.g. for 181 LEDs it will send 0xB4 and cheksum 0xE1
  27. // keep in mind if you are using boblight config to calculate prefix that Boblight counts diodes from 1 and Hyperion from 0
  28. // if you have problems try +1 or -1 diodes when generating prefix characters
  29. // values to save some time: 178 B1 E4, 180 B3E6, 181 B4E1, 182 B5E0
  30. //hyperion code
  31. //_ledBuffer[3] = ((ledValues.size() - 1) >> 8) & 0xFF; // LED count high byte
  32. // _ledBuffer[4] = (ledValues.size() - 1) & 0xFF; // LED count low byte
  33. // _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
  34. const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xB7, 0xE2}; // Start prefix ADA
  35. char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
  36.  
  37. // Init LED strand, WS2811/WS2912 specific
  38.  
  39. // These might work for other configurations:
  40. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  41. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  42. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  43. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  44.  
  45. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
  46.  
  47. int state; // Define current state
  48. #define STATE_WAITING 1 // - Waiting for prefix
  49. #define STATE_DO_PREFIX 2 // - Processing prefix
  50. #define STATE_DO_DATA 3 // - Handling incoming LED colors
  51.  
  52. int readSerial; // Read Serial data (1)
  53. int currentLED; // Needed for assigning the color to the right LED
  54.  
  55. void setup()
  56. {
  57. strip.begin(); // Init LED strand, set all black, then all to startcolor
  58.  
  59. strip.setBrightness( (255 / 100) * BRIGHTNESS );
  60.  
  61. setAllLEDs(BLACK, 0);
  62. setAllLEDs(STARTCOLOR, 5);
  63.  
  64. Serial.begin(BAUDRATE); // Init serial speed
  65.  
  66. state = STATE_WAITING; // Initial state: Waiting for prefix
  67. }
  68.  
  69.  
  70. void loop()
  71. {
  72. switch(state)
  73. {
  74. case STATE_WAITING: // *** Waiting for prefix ***
  75. if( Serial.available()>0 )
  76. {
  77. readSerial = Serial.read(); // Read one character
  78.  
  79. if ( readSerial == prefix[0] ) // if this character is 1st prefix char
  80. { state = STATE_DO_PREFIX; } // then set state to handle prefix
  81. }
  82. break;
  83.  
  84.  
  85. case STATE_DO_PREFIX: // *** Processing Prefix ***
  86. if( Serial.available() > sizeof(prefix) - 2 )
  87. {
  88. Serial.readBytes(buffer, sizeof(prefix) - 1);
  89.  
  90. for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
  91. {
  92. if( buffer[Counter] == prefix[Counter+1] )
  93. {
  94. state = STATE_DO_DATA; // Received character is in prefix, continue
  95. currentLED = 0; // Set current LED to the first one
  96. }
  97. else
  98. {
  99. state = STATE_WAITING; // Crap, one of the received chars is NOT in the prefix
  100. break; // Exit, to go back to waiting for the prefix
  101. } // end if buffer
  102. } // end for Counter
  103. } // end if Serial
  104. break;
  105.  
  106.  
  107. case STATE_DO_DATA: // *** Process incoming color data ***
  108. if( Serial.available() > 2 ) // if we receive more than 2 chars
  109. {
  110. Serial.readBytes( buffer, 3 ); // Abuse buffer to temp store 3 charaters
  111. strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]); // and assing to LEDs
  112. }
  113.  
  114. if( currentLED > LEDCOUNT ) // Reached the last LED? Display it!
  115. {
  116. strip.show(); // Make colors visible
  117. delayMicroseconds(SHOWDELAY); // Wait a few micro seconds
  118.  
  119. state = STATE_WAITING; // Reset to waiting ...
  120. currentLED = 0; // and go to LED one
  121.  
  122. break; // and exit ... and do it all over again
  123. }
  124. break;
  125. } // switch(state)
  126.  
  127. } // loop
  128.  
  129.  
  130. // Sets the color of all LEDs in the strand to 'color'
  131. // If 'wait'>0 then it will show a swipe from start to end
  132. void setAllLEDs(uint32_t color, int wait)
  133. {
  134. for ( int Counter=0; Counter < LEDCOUNT; Counter++ ) // For each LED
  135. {
  136. strip.setPixelColor( Counter, color ); // .. set the color
  137.  
  138. if( wait > 0 ) // if a wait time was set then
  139. {
  140. strip.show(); // Show the LED color
  141. delay(wait); // and wait before we do the next LED
  142. } // if wait
  143.  
  144. } // for Counter
  145.  
  146. strip.show(); // Show all LEDs
  147. } // setAllLEDs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement