Mac OS X ambilight
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.

DisplayBacklight.ino 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Adafruit NeoPixel RGB-LED strand UART controller
  3. *
  4. * This simple sketch controls an RGB LED strand via data received over UART.
  5. * The protocol works as follows:
  6. *
  7. * A single 'frame' that can be displayed, on our N LED strand, is defined as:
  8. * uint8_t data[N * 3] = { r0, g0, b0, r1, g1, b1, r2, g2, b2, ... };
  9. * With consecutive 24-bit RGB pixels.
  10. * This is simply transferred, as is, from the PC to the Arduino. To distinguish
  11. * between start and end of different frames, a magic string is used, defined below.
  12. * Hopefully it won't appear in the data stream... :)
  13. */
  14. #include <Adafruit_NeoPixel.h>
  15. #ifdef __AVR__
  16. #include <avr/power.h>
  17. #endif
  18. #define DEBUG
  19. #ifdef DEBUG
  20. #define debugPrint(x) Serial.print(x)
  21. #define debugPrintln(x) Serial.println(x)
  22. #else
  23. #define debugPrint(x)
  24. #define debugPrintln(x)
  25. #endif
  26. #define LED_PIN 2
  27. #define LED_COUNT 156
  28. #define BAUDRATE 115200
  29. #define MAGIC "xythobuzRGBled"
  30. // Optional auto-turn-off after no data received
  31. //#define TURN_OFF_TIMEOUT 60000l // in ms
  32. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  33. void setup() {
  34. Serial.begin(BAUDRATE);
  35. strip.begin();
  36. strip.show(); // Initialize all pixels to 'off'
  37. debugPrintln("RGB LED controller ready!");
  38. debugPrint("Config: ");
  39. debugPrint(LED_COUNT);
  40. debugPrintln(" LEDs...");
  41. debugPrint("Magic-Word: \"");
  42. debugPrint(MAGIC);
  43. debugPrintln("\"");
  44. // Blink LED as init message
  45. strip.setPixelColor(0, 0x20, 0x20, 0x20);
  46. strip.show();
  47. delay(100);
  48. strip.setPixelColor(0, 0x00, 0x00, 0x00);
  49. strip.show();
  50. }
  51. #define WAIT_FOR_START 0
  52. #define RECEIVING_START 1
  53. #define RECEIVING_DATA 2
  54. uint8_t state = WAIT_FOR_START;
  55. uint8_t startPos = 0;
  56. uint16_t dataPos = 0;
  57. uint8_t frame[LED_COUNT * 3];
  58. #ifdef TURN_OFF_TIMEOUT
  59. unsigned long lastTime = millis();
  60. #endif
  61. static void setNewFrame() {
  62. for (uint16_t i = 0; i < LED_COUNT; i++) {
  63. strip.setPixelColor(i, frame[i * 3], frame[(i * 3) + 1], frame[(i * 3) + 2]);
  64. }
  65. strip.show();
  66. }
  67. void loop() {
  68. if (!Serial.available()) {
  69. #ifdef TURN_OFF_TIMEOUT
  70. if ((millis() - lastTime) >= TURN_OFF_TIMEOUT) {
  71. for (int i = 0; i < (LED_COUNT * 3); i++) {
  72. frame[i] = 0;
  73. }
  74. setNewFrame();
  75. }
  76. #endif
  77. return;
  78. }
  79. #ifdef TURN_OFF_TIMEOUT
  80. lastTime = millis();
  81. #endif
  82. uint8_t c;
  83. Serial.readBytes(&c, 1);
  84. if (state == WAIT_FOR_START) {
  85. if (c == MAGIC[0]) {
  86. state = RECEIVING_START;
  87. startPos = 1;
  88. debugPrintln("f");
  89. } else {
  90. debugPrintln("");
  91. }
  92. } else if (state == RECEIVING_START) {
  93. if (startPos < strlen(MAGIC)) {
  94. if (c == MAGIC[startPos]) {
  95. startPos++;
  96. debugPrintln("g");
  97. if (startPos >= strlen(MAGIC)) {
  98. state = RECEIVING_DATA;
  99. dataPos = 0;
  100. debugPrintln("w");
  101. }
  102. } else {
  103. state = WAIT_FOR_START;
  104. debugPrintln("x");
  105. }
  106. } else {
  107. // Should not happen
  108. state = RECEIVING_START;
  109. debugPrintln("s");
  110. }
  111. } else if (state == RECEIVING_DATA) {
  112. frame[dataPos++] = c;
  113. if (dataPos >= (LED_COUNT * 3)) {
  114. debugPrintln("d");
  115. setNewFrame();
  116. state = WAIT_FOR_START;
  117. }
  118. }
  119. }