{"id":697,"date":"2019-03-07T19:39:36","date_gmt":"2019-03-07T18:39:36","guid":{"rendered":"https:\/\/www.anginf.de\/?p=697"},"modified":"2023-02-07T17:01:15","modified_gmt":"2023-02-07T16:01:15","slug":"ambilight","status":"publish","type":"post","link":"https:\/\/www.anginf.de\/?p=697","title":{"rendered":"Ambilight"},"content":{"rendered":"\n<p>Hier meine angepasste Version, die mit Ambibox (unter Windows) kommuniziert und ein Ambilight vergleichsweise einfach erm\u00f6glicht. Die LED-Anzahl und ggf. ein paar weitere Parameter m\u00fcssen nat\u00fcrlich noch angepasst werden.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-27979871-13c5-42cd-b647-eb12c583c112\" href=\"https:\/\/www.anginf.de\/wp-content\/uploads\/Ambilight-Kurze-Einf\u00fchrung.odp\">Ambilight Kurze Einf\u00fchrung<\/a><a href=\"https:\/\/www.anginf.de\/wp-content\/uploads\/Ambilight-Kurze-Einf\u00fchrung.odp\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-27979871-13c5-42cd-b647-eb12c583c112\">Herunterladen<\/a><\/div>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-color has-text-color\"><code lang=\"cpp\" class=\"language-cpp\">#include \"FastLED.h\"\n#include \"Arduino.h\"\n\n#define ANALOG_MODE_AVERAGE  0\n#define ANALOG_MODE_LAST_LED 1\n#define FASTLED_ESP8266_D1_PIN_ORDER\n\n\/**************************************\n   S E T U P\n   set following values to your needs\n **************************************\/\n\n#define INITIAL_LED_TEST_ENABLED true\n#define INITIAL_LED_TEST_BRIGHTNESS 32  \/\/ 0..255\n#define INITIAL_LED_TEST_TIME_MS 2000  \/\/ 10..\n\n\/\/ Number of leds in your strip. set to \"1\" and ANALOG_OUTPUT_ENABLED to \"true\" to activate analog only\n\/\/ As of 26\/1\/2017:\n\/\/ 582 leaves ZERO bytes free and this\n\/\/ 410 is ok\n\/\/ tested with 500 leds and is fine (despite the warning)\n#define MAX_LEDS 7+7+11+1\n\n\/\/ type of your led controller, possible values, see below\n\/\/#define LED_TYPE APA102\n#define LED_TYPE WS2812\n\n\/\/ 3 wire (pwm): NEOPIXEL BTM1829 TM1812 TM1809 TM1804 TM1803 UCS1903 UCS1903B UCS1904 UCS2903 WS2812 WS2852\n\/\/               S2812B SK6812 SK6822 APA106 PL9823 WS2811 WS2813 APA104 WS2811_40 GW6205 GW6205_40 LPD1886 LPD1886_8BIT \n\/\/ 4 wire (spi): LPD8806 WS2801 WS2803 SM16716 P9813 APA102 SK9822 DOTSTAR\n\n\/\/ For 3 wire led stripes line Neopixel\/Ws2812, which have a data line, ground, and power, you just need to define DATA_PIN.\n\/\/ For led chipsets that are SPI based (four wires - data, clock, ground, and power), both defines DATA_PIN and CLOCK_PIN are needed\n\n\/\/ DATA_PIN, or DATA_PIN, CLOCK_PIN\n#define LED_PINS D2        \/\/ 3 wire leds\n\/\/ #define LED_PINS D3, D2  \/\/ 4 wire leds\n\n\/\/ #define COLOR_ORDER RBG  \/\/ colororder of the stripe, set RGB in hyperion\n\/\/ #define COLOR_ORDER BGR  \/\/ colororder of the stripe, set RGB in hyperion\n#define COLOR_ORDER GRB  \/\/ colororder of the stripe, set RGB in hyperion\n\n#define OFF_TIMEOUT 0    \/\/ ms to switch off after no data was received, set 0 to deactivate\n\n\/\/ analog rgb uni color led stripe - using of hyperion smoothing is recommended\n\/\/ ATTENTION  this pin config is default for atmega328 based arduinos, others might work to\n\/\/            if you have flickering analog leds this might be caused by unsynced pwm signals\n\/\/            try other pins is more or less the only thing that helps\n#define ANALOG_OUTPUT_ENABLED false\n#define ANALOG_MODE           ANALOG_MODE_LAST_LED  \/\/ use ANALOG_MODE_AVERAGE or ANALOG_MODE_LAST_LED\n#define ANALOG_GROUND_PIN     8                     \/\/ additional ground pin to make wiring a bit easier\n#define ANALOG_RED_PIN        9\n#define ANALOG_GREEN_PIN      10\n#define ANALOG_BLUE_PIN       11\n\n\/\/ overall color adjustments\n#define ANALOG_BRIGHTNESS_RED   255              \/\/ maximum brightness for analog 0-255\n#define ANALOG_BRIGHTNESS_GREEN 255              \/\/ maximum brightness for analog 0-255\n#define ANALOG_BRIGHTNESS_BLUE  255              \/\/ maximum brightness for analog 0-255\n\n#define BRIGHTNESS 255                      \/\/ maximum brightness 0-255\n#define DITHER_MODE BINARY_DITHER           \/\/ BINARY_DITHER or DISABLE_DITHER\n#define COLOR_TEMPERATURE CRGB(255,255,255) \/\/ RGB value describing the color temperature\n#define COLOR_CORRECTION  TypicalLEDStrip   \/\/ predefined fastled color correction\n\/\/#define COLOR_CORRECTION  CRGB(255,255,255) \/\/ or RGB value describing the color correction\n\n\/\/ Baudrate, higher rate allows faster refresh rate and more LEDs\n\/\/#define serialRate 460800      \/\/ use 115200 for ftdi based boards\n#define serialRate 115200     \/\/ use 115200 for ftdi based boards\n\/\/#define serialRate 500000         \/\/ use 115200 for ftdi based boards\n\n\n\/**************************************\n   A D A L I G H T   C O D E\n   no user changes needed\n **************************************\/\n\n\/\/ Adalight sends a \"Magic Word\" (defined in \/etc\/boblight.conf) before sending the pixel data\nuint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;\n\nunsigned long endTime;\n\n\/\/ Define the array of leds\nCRGB leds[MAX_LEDS];\n\n\/\/ set rgb to analog led stripe\nvoid showAnalogRGB(const CRGB&amp; led) {\n  if (ANALOG_OUTPUT_ENABLED) {\n    byte r = map(led.r, 0,255,0,ANALOG_BRIGHTNESS_RED);\n    byte g = map(led.g, 0,255,0,ANALOG_BRIGHTNESS_GREEN);\n    byte b = map(led.b, 0,255,0,ANALOG_BRIGHTNESS_BLUE);\n    analogWrite(ANALOG_RED_PIN  , r);\n    analogWrite(ANALOG_GREEN_PIN, g);\n    analogWrite(ANALOG_BLUE_PIN , b);\n  }\n}\n\n\/\/ set color to all leds\nvoid showColor(const CRGB&amp; led) {\n  #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false\n  LEDS.showColor(led);\n  #endif\n  showAnalogRGB(led);\n}\n\n\/\/ switch of digital and analog leds\nvoid switchOff() {\n  #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false\n  memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));\n  FastLED.show();\n  #endif\n  showAnalogRGB(leds[0]);\n}\n\n\/\/ function to check if serial data is available\n\/\/ if timeout occured leds switch of, if configured\nbool checkIncommingData() {\n  boolean dataAvailable = true;\n  while (!Serial.available()) {\n    if ( OFF_TIMEOUT > 0 &amp;&amp; endTime &lt; millis()) {\n      switchOff();\n      dataAvailable = false;\n      endTime = millis() + OFF_TIMEOUT;\n    }\n  }\n\n  return dataAvailable;\n}\n\n\/\/ main function that setups and runs the code\nvoid setup() {\n  Serial.begin(serialRate);\n\n  \/\/ analog output\n  if (ANALOG_OUTPUT_ENABLED) {\n    \/\/ additional ground pin to make wiring a bit easier\n    pinMode(ANALOG_GROUND_PIN, OUTPUT);\n    digitalWrite(ANALOG_GROUND_PIN, LOW);\n    pinMode(ANALOG_BLUE_PIN , OUTPUT);\n    pinMode(ANALOG_RED_PIN  , OUTPUT);\n    pinMode(ANALOG_GREEN_PIN, OUTPUT);\n  }\n\n  int ledCount = MAX_LEDS;\n  if (ANALOG_MODE == ANALOG_MODE_LAST_LED) {\n    ledCount--;\n  }\n\n  #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false\n    FastLED.addLeds&lt;LED_TYPE, LED_PINS, COLOR_ORDER>(leds, ledCount);\n  #endif\n  \n  \/\/ color adjustments\n  FastLED.setBrightness ( BRIGHTNESS );\n  FastLED.setTemperature( COLOR_TEMPERATURE );\n  FastLED.setCorrection ( COLOR_CORRECTION );\n  FastLED.setDither     ( DITHER_MODE );\n\n  \/\/ initial RGB flash\n  #if INITIAL_LED_TEST_ENABLED == true\n  for (int v=0;v&lt;INITIAL_LED_TEST_BRIGHTNESS;v++)\n  {\n    showColor(CRGB(v,v,v));  \n    delay(INITIAL_LED_TEST_TIME_MS\/2\/INITIAL_LED_TEST_BRIGHTNESS);\n  }\n \n  for (int v=0;v&lt;INITIAL_LED_TEST_BRIGHTNESS;v++)\n  {\n    showColor(CRGB(v,v,v));  \n    delay(INITIAL_LED_TEST_TIME_MS\/2\/INITIAL_LED_TEST_BRIGHTNESS);\n  }\n  #endif\n  showColor(CRGB(0, 0, 0));\n\n  Serial.print(\"Ada\\n\"); \/\/ Send \"Magic Word\" string to host\n\n\n  boolean transmissionSuccess;\n  unsigned long sum_r, sum_g, sum_b;\n\n  \/\/ loop() is avoided as even that small bit of function overhead\n  \/\/ has a measurable impact on this code's overall throughput.\n  for(;;) {\n    \/\/ wait for first byte of Magic Word\n    for (i = 0; i &lt; sizeof prefix; ++i) {\n      \/\/ If next byte is not in Magic Word, the start over\n      if (!checkIncommingData() || prefix[i] != Serial.read()) {\n        i = 0;\n      }\n    }\n\n    \/\/ Hi, Lo, Checksum\n    if (!checkIncommingData()) continue;\n    hi = Serial.read();\n    if (!checkIncommingData()) continue;\n    lo = Serial.read();\n    if (!checkIncommingData()) continue;\n    chk = Serial.read();\n\n    \/\/ if checksum does not match go back to wait\n    if (chk != (hi ^ lo ^ 0x55)) continue;\n\n    memset(leds, 0, MAX_LEDS * sizeof(struct CRGB));\n    transmissionSuccess = true;\n    sum_r = 0;\n    sum_g = 0;\n    sum_b = 0;\n\n    int num_leds = min ( MAX_LEDS, (hi&lt;&lt;8) + lo + 1 );\n\n    \/\/ read the transmission data and set LED values\n    for (int idx = 0; idx &lt; num_leds; idx++) {\n      byte r, g, b;\n      if (!checkIncommingData()) {\n        transmissionSuccess = false;\n        break;\n      }\n      r = Serial.read();\n      if (!checkIncommingData()) {\n        transmissionSuccess = false;\n        break;\n      }\n      g = Serial.read();\n      if (!checkIncommingData()) {\n        transmissionSuccess = false;\n        break;\n      }\n      b = Serial.read();\n      leds[idx].r = r;\n      leds[idx].g = g;\n      leds[idx].b = b;\n      #if ANALOG_OUTPUT_ENABLED == true &amp;&amp; ANALOG_MODE == ANALOG_MODE_AVERAGE\n          sum_r += r;\n          sum_g += g;\n          sum_b += b;\n      #endif\n    }\n\n    \/\/ shows new values\n    if (transmissionSuccess) {\n      endTime = millis() + OFF_TIMEOUT;\n      #if MAX_LEDS > 1 || ANALOG_OUTPUT_ENABLED == false\n      FastLED.show();\n      #endif\n\n      #if ANALOG_OUTPUT_ENABLED == true\n        #if ANALOG_MODE == ANALOG_MODE_LAST_LED\n          showAnalogRGB(leds[MAX_LEDS-1]);\n        #else\n          showAnalogRGB(CRGB(sum_r\/MAX_LEDS, sum_g\/MAX_LEDS, sum_b\/MAX_LEDS));\n         #endif\n      #endif\n    }\n  }\n} \/\/ end of setup\n\nvoid loop() {\n  \/\/ Not used. See note in setup() function.\n}<\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hier meine angepasste Version, die mit Ambibox (unter Windows) kommuniziert und ein Ambilight vergleichsweise einfach erm\u00f6glicht. Die LED-Anzahl und ggf. ein paar weitere Parameter m\u00fcssen nat\u00fcrlich noch angepasst werden.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=697"}],"version-history":[{"count":7,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":784,"href":"https:\/\/www.anginf.de\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions\/784"}],"wp:attachment":[{"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anginf.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}