Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_LiquidCrystal.h>
  3. #include <FastLED.h>
  4.  
  5. // Connect via i2c, default address #0 (A0-A2 not jumpered)
  6. Adafruit_LiquidCrystal lcd(0);
  7.  
  8. #define ANALOG_MODE_AVERAGE 0
  9. #define ANALOG_MODE_LAST_LED 1
  10.  
  11. #define INITIAL_LED_TEST_ENABLED true
  12. #define INITIAL_LED_TEST_BRIGHTNESS 255 // 0..255
  13. #define INITIAL_LED_TEST_TIME_MS 500 // 10..
  14.  
  15. #define MAX_LEDS 227
  16. #define LED_TYPE WS2812B
  17. // 3 wire (pwm): NEOPIXEL BTM1829 TM1812 TM1809 TM1804 TM1803 UCS1903 UCS1903B UCS1904 UCS2903 WS2812 WS2852
  18. // S2812B SK6812 SK6822 APA106 PL9823 WS2811 WS2813 APA104 WS2811_40 GW6205 GW6205_40 LPD1886 LPD1886_8BIT
  19. // 4 wire (spi): LPD8806 WS2801 WS2803 SM16716 P9813 APA102 SK9822 DOTSTAR
  20.  
  21. // DATA_PIN, or DATA_PIN, CLOCK_PIN
  22. // For 3 wire led stripes line Neopixel/Ws2812, which have a data line, ground, and power, you just need to define DATA_PIN.
  23. // For led chipsets that are SPI based (four wires - data, clock, ground, and power), both defines DATA_PIN and CLOCK_PIN are needed
  24. #define LED_PINS 5 // 3 wire leds
  25. //#define LED_PINS 6, 13 // 4 wire leds
  26.  
  27. #define COLOR_ORDER BRG
  28. #define OFF_TIMEOUT 15000 // ms to switch off after no data was received, set 0 to deactivate
  29.  
  30. const int ch_2 = A0; // Inputs from HDMI switcher
  31. const int ch_3 = A1;
  32. const int ch_4 = A2;
  33. const int ch_5 = A3;
  34.  
  35. int curr_source = 0; // Used to store which source is currently displayed
  36. boolean strip_status = true; // Used to chose if the LED strip is ON or OFF (depending on button choice)
  37.  
  38. // analog rgb uni color led stripe - using of hyperion smoothing is recommended
  39. // ATTENTION this pin config is default for atmega328 based arduinos, others might work to
  40. // if you have flickering analog leds this might be caused by unsynced pwm signals
  41. // try other pins is more or less the only thing that helps
  42. #define ANALOG_OUTPUT_ENABLED false
  43. #define ANALOG_MODE ANALOG_MODE_LAST_LED // use ANALOG_MODE_AVERAGE or ANALOG_MODE_LAST_LED
  44. #define ANALOG_GROUND_PIN 8 // additional ground pin to make wiring a bit easier
  45. #define ANALOG_RED_PIN 9
  46. #define ANALOG_GREEN_PIN 10
  47. #define ANALOG_BLUE_PIN 11
  48.  
  49. // overall color adjustments
  50. #define ANALOG_BRIGHTNESS_RED 255 // maximum brightness for analog 0-255
  51. #define ANALOG_BRIGHTNESS_GREEN 255 // maximum brightness for analog 0-255
  52. #define ANALOG_BRIGHTNESS_BLUE 255 // maximum brightness for analog 0-255
  53.  
  54. #define BRIGHTNESS 255 // maximum brightness 0-255
  55. #define DITHER_MODE BINARY_DITHER // BINARY_DITHER or DISABLE_DITHER
  56. #define COLOR_TEMPERATURE CRGB(255,255,255) // RGB value describing the color temperature
  57. #define COLOR_CORRECTION TypicalLEDStrip // predefined fastled color correction
  58. //#define COLOR_CORRECTION CRGB(255,255,255) // or RGB value describing the color correction
  59.  
  60. // Baudrate, higher rate allows faster refresh rate and more LEDs
  61. #define serialRate 500000
  62.  
  63. /**************************************
  64. A D A L I G H T C O D E
  65. no user changes needed unless modifying the code for the LCD or on/off button
  66. **************************************/
  67.  
  68. // Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
  69. uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
  70.  
  71. unsigned long endTime;
  72.  
  73. // Define the array of leds
  74. CRGB leds[MAX_LEDS];
  75.  
  76. // set rgb to analog led stripe
  77. void showAnalogRGB(const CRGB& led) {
  78. if (ANALOG_OUTPUT_ENABLED) {
  79. byte r = map(led.r, 0,255,0,ANALOG_BRIGHTNESS_RED);
  80. byte g = map(led.g, 0,255,0,ANALOG_BRIGHTNESS_GREEN);
  81. byte b = map(led.b, 0,255,0,ANALOG_BRIGHTNESS_BLUE);
  82. analogWrite(ANALOG_RED_PIN , r);
  83. analogWrite(ANALOG_GREEN_PIN, g);
  84. analogWrite(ANALOG_BLUE_PIN , b);
  85. }
  86. }
  87.  
  88. // set color to all leds
  89. void showColor(const CRGB& led) {
  90. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  91. LEDS.showColor(led);
  92. #endif
  93. showAnalogRGB(led);
  94. }
  95.  
  96. // switch of digital and analog leds
  97. void switchOff() {
  98. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  99. memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
  100. FastLED.show();
  101. #endif
  102. showAnalogRGB(leds[0]);
  103. }
  104.  
  105. // function to check if serial data is available
  106. // if timeout occured leds switch of, if configured
  107. bool checkIncommingData() {
  108. boolean dataAvailable = true;
  109. while (!Serial.available()) {
  110. if ( OFF_TIMEOUT > 0 && endTime < millis()) {
  111. switchOff();
  112. dataAvailable = false;
  113. endTime = millis() + OFF_TIMEOUT;
  114. }
  115. }
  116.  
  117. return dataAvailable;
  118. }
  119.  
  120. // main function that setups and runs the code
  121. void setup() {
  122.  
  123. pinMode(led_button, INPUT);
  124. pinMode(ch_2, INPUT);
  125. pinMode(ch_3, INPUT);
  126. pinMode(ch_4, INPUT);
  127. pinMode(ch_5, INPUT);
  128. pinMode(status_led, OUTPUT);
  129. digitalWrite(led_button, HIGH);
  130. digitalWrite(status_led, HIGH);
  131.  
  132. Serial.begin(serialRate);
  133.  
  134. // set up the LCD's number of rows and columns and print welcome message:
  135. lcd.begin(16, 2);
  136. lcd.setCursor(0,0);
  137. lcd.print(" Ambilight ");
  138. lcd.setCursor(0,1);
  139. lcd.print(" Controller ");
  140. delay(5000);
  141.  
  142. // analog output
  143. if (ANALOG_OUTPUT_ENABLED) {
  144. // additional ground pin to make wiring a bit easier
  145. pinMode(ANALOG_GROUND_PIN, OUTPUT);
  146. digitalWrite(ANALOG_GROUND_PIN, LOW);
  147. pinMode(ANALOG_BLUE_PIN , OUTPUT);
  148. pinMode(ANALOG_RED_PIN , OUTPUT);
  149. pinMode(ANALOG_GREEN_PIN, OUTPUT);
  150. }
  151.  
  152. int ledCount = MAX_LEDS;
  153. if (ANALOG_MODE == ANALOG_MODE_LAST_LED) {
  154. ledCount--;
  155. }
  156.  
  157. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  158. FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, ledCount);
  159. #endif
  160.  
  161. // color adjustments
  162. FastLED.setBrightness ( BRIGHTNESS );
  163. FastLED.setTemperature( COLOR_TEMPERATURE );
  164. FastLED.setCorrection ( COLOR_CORRECTION );
  165. FastLED.setDither ( DITHER_MODE );
  166.  
  167. // initial RGB flash
  168. #if INITIAL_LED_TEST_ENABLED == true
  169. for (int v=0;v<INITIAL_LED_TEST_BRIGHTNESS;v++)
  170. {
  171. showColor(CRGB(v,v,v));
  172. delay(INITIAL_LED_TEST_TIME_MS/2/INITIAL_LED_TEST_BRIGHTNESS);
  173. }
  174.  
  175. for (int v=0;v<INITIAL_LED_TEST_BRIGHTNESS;v++)
  176. {
  177. showColor(CRGB(v,v,v));
  178. delay(INITIAL_LED_TEST_TIME_MS/2/INITIAL_LED_TEST_BRIGHTNESS);
  179. }
  180. #endif
  181. showColor(CRGB(0, 0, 0));
  182.  
  183. Serial.print("Ada\n"); // Send "Magic Word" string to host
  184.  
  185.  
  186. boolean transmissionSuccess;
  187. unsigned long sum_r, sum_g, sum_b;
  188.  
  189. // loop() is avoided as even that small bit of function overhead
  190. // has a measurable impact on this code's overall throughput.
  191. for(;;) {
  192. // wait for first byte of Magic Word
  193. for (i = 0; i < sizeof prefix; ++i) {
  194. // If next byte is not in Magic Word, the start over
  195. if (!checkIncommingData() || prefix[i] != Serial.read()) {
  196. i = 0;
  197. }
  198. }
  199.  
  200. // Hi, Lo, Checksum
  201. if (!checkIncommingData()) continue;
  202. hi = Serial.read();
  203. if (!checkIncommingData()) continue;
  204. lo = Serial.read();
  205. if (!checkIncommingData()) continue;
  206. chk = Serial.read();
  207.  
  208. // if checksum does not match go back to wait
  209. if (chk != (hi ^ lo ^ 0x55)) continue;
  210.  
  211. memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));
  212. transmissionSuccess = true;
  213. sum_r = 0;
  214. sum_g = 0;
  215. sum_b = 0;
  216.  
  217. int num_leds = min ( MAX_LEDS, (hi<<8) + lo + 1 );
  218.  
  219. // read the transmission data and set LED values
  220. for (int idx = 0; idx < num_leds; idx++) {
  221. byte r, g, b;
  222. if (!checkIncommingData()) {
  223. transmissionSuccess = false;
  224. break;
  225. }
  226. r = Serial.read();
  227. if (!checkIncommingData()) {
  228. transmissionSuccess = false;
  229. break;
  230. }
  231. g = Serial.read();
  232. if (!checkIncommingData()) {
  233. transmissionSuccess = false;
  234. break;
  235. }
  236. b = Serial.read();
  237. leds[idx].r = r;
  238. leds[idx].g = g;
  239. leds[idx].b = b;
  240. #if ANALOG_OUTPUT_ENABLED == true && ANALOG_MODE == ANALOG_MODE_AVERAGE
  241. sum_r += r;
  242. sum_g += g;
  243. sum_b += b;
  244. #endif
  245. }
  246.  
  247. // shows new values
  248. if (transmissionSuccess) {
  249. check_source();
  250. endTime = millis() + OFF_TIMEOUT;
  251. #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false
  252. FastLED.show();
  253. #endif
  254.  
  255. #if ANALOG_OUTPUT_ENABLED == true
  256. #if ANALOG_MODE == ANALOG_MODE_LAST_LED
  257. showAnalogRGB(leds[MAX_LEDS-1]);
  258. #else
  259. showAnalogRGB(CRGB(sum_r/MAX_LEDS, sum_g/MAX_LEDS, sum_b/MAX_LEDS));
  260. #endif
  261. #endif
  262. }
  263. }
  264.  
  265. } // end of setup
  266.  
  267. void loop() {
  268. }
  269.  
  270. void check_source (void)
  271. {
  272.  
  273. if(digitalRead(ch_2) == HIGH) // Is source #2 HIGH
  274. {
  275. if(curr_source != 2) // Is the curr_source variable something other than "2"?
  276. {
  277. curr_source = 2; // Change curr_source variable to "2"
  278. lcd.clear(); // Clear LCD
  279. lcd.setCursor(0,0); // Set starting point for LCD cursor
  280. lcd.print(" Amazon "); // Print source 2 to LCD
  281. lcd.setCursor(0,1); // Set starting point for LCD cursor
  282. lcd.print(" Firestick "); // Print source 2 to LCD
  283. }
  284. }
  285. else if(digitalRead(ch_3) == HIGH) // If source #2 LED isn't HIGH, check to see if source #3 led is
  286. {
  287. if(curr_source != 3) // Is the curr_source variable something other than "3"?
  288. {
  289. curr_source = 3; // Change curr_source variable to "3"
  290. lcd.clear(); // Clear LCD
  291. lcd.setCursor(0,0); // Set starting point for LCD cursor
  292. lcd.print(" Apple "); // Print source 3 to LCD
  293. lcd.setCursor(0,1); // Set starting point for LCD cursor
  294. lcd.print(" TV "); // Print source 3 to LCD
  295. }
  296. }
  297. else if(digitalRead(ch_4) == HIGH) // If source #2 and #3 LEDs aren't HIGH, check to see if source #4 led is
  298. {
  299. if(curr_source != 4) // Is the curr_source variable something other than "4"?
  300. {
  301. curr_source = 4; // Change curr_source variable to "4"
  302. lcd.clear(); // Clear LCD
  303. lcd.setCursor(0,0); // Set starting point for LCD cursor
  304. lcd.print(" VGA "); // Print source 4 to LCD
  305. lcd.setCursor(0,1); // Set starting point for LCD cursor
  306. lcd.print(" Input "); // Print source 4 to LCD
  307. }
  308. }
  309. else if(digitalRead(ch_5) == HIGH) // If source #2, #3, and #4 LEDs aren't HIGH, check to see if source #5 led is
  310. {
  311. if(curr_source != 5) // Is the curr_source variable something other than "5"?
  312. {
  313. curr_source = 5; // Change curr_source variable to "5"
  314. lcd.clear(); // Clear LCD
  315. lcd.setCursor(0,0); // Set starting point for LCD cursor
  316. lcd.print(" DirecTV "); // Print source 5 to LCD
  317. lcd.setCursor(0,1); // Set starting point for LCD cursor
  318. lcd.print(" Receiver "); // Print source 5 to LCD
  319. }
  320. }
  321. else
  322. {
  323. if(curr_source != 1)
  324. {
  325. curr_source = 1; // Change curr_source variable to "1"
  326. lcd.clear();
  327. lcd.setCursor(0,0); // Set starting point for LCD cursor
  328. lcd.print(" RCA "); // Print source 1 to LCD
  329. lcd.setCursor(0,1); // Set starting point for LCD cursor
  330. lcd.print(" Input "); // Print source12 to LCD
  331. }
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement