Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.50 KB | None | 0 0
  1.  
  2. #include <pt.h>
  3. #include "FastLED.h"
  4.  
  5. /*==============================================================================*/
  6. /* LED und Arduino Variablen */
  7. /*==============================================================================*/
  8.  
  9.  
  10. #define NUM_LEDS             28                // Number of LEDs
  11. #define MAX_ARGS             10                 // Max Number of command arguments
  12. #define BAUDRATE             50000             // Baudrate
  13. #define SERIAL               Serial            // Serial port for communication
  14. #define SERIAL_DEBUG         Serial             // Serial port for debugging
  15. #define DATA_PIN             6                  // PIN where LEDs are connected/Used for TM1809/WS2811 chipsets, because they dont use SPI
  16. //#define CLOCK_PIN           4                  // used for some SPI chipsets, e.g. WS2801
  17. #define BRIGHTNESS           255                // define global brightness 0..255
  18.  
  19. //choose your LED chipset in void setup()
  20.  
  21. /*==============================================================================*/
  22. /* TPM2 Variablen */
  23. /*==============================================================================*/
  24.  
  25. enum Protocol
  26. {
  27.  // positions
  28.  posStart      = 0,
  29.  posType       = 1,
  30.  posFsHigh     = 2,
  31.  posFsLow      = 3,
  32.  posData       = 4,
  33.  
  34.  // bytes
  35.  tpm2Start     = 0xc9,
  36.  tpm2DataFrame = 0xda,
  37.  tpm2Command   = 0xc0,
  38.  tpm2Answer    = 0xaa,
  39.  tpm2End       = 0x36,
  40.  tpm2Ack       = 0xAC
  41. };
  42.  
  43. enum Mode
  44. {
  45.  mNone,
  46.  mCommunication,
  47.  mProgram
  48. };
  49.  
  50. struct Data
  51. {
  52.  int pos;
  53.  uint8_t type;
  54.  uint16_t fs;
  55.  uint8_t command;
  56.  CRGB rgb[NUM_LEDS];
  57. } data;
  58.  
  59. byte args[MAX_ARGS];
  60. unsigned long lastDataAt = 0;
  61. int program = 10;
  62. int mode = mNone;
  63. int effectDelay = 100;
  64. static struct pt pt1;
  65.  
  66. /*==============================================================================*/
  67. /* Variablen für Effekte */
  68. /*==============================================================================*/
  69.  
  70. int BOTTOM_INDEX = 0;
  71. int TOP_INDEX = int(NUM_LEDS/2);
  72. int EVENODD = NUM_LEDS%2;
  73.  
  74. /*==============================================================================*/
  75. /* debug code
  76. /*==============================================================================*/
  77.  
  78. // comment this line out to disable debugging
  79.  
  80. #define DEBUG
  81.  
  82. #ifdef DEBUG
  83.  
  84. void debug(const char* str)
  85. {
  86.  SERIAL_DEBUG.println(str);
  87. }
  88.  
  89. void debug(const char* str, uint16_t val, int fmt = DEC)
  90. {
  91.  SERIAL_DEBUG.print(str);
  92.  SERIAL_DEBUG.println(val, fmt);
  93. }
  94.  
  95. int freeRam()
  96. {
  97.  extern int __heap_start, *__brkval;
  98.  int v;
  99.  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  100. }
  101.  
  102. #else
  103. #  define debug( ... )
  104. #endif
  105.  
  106. void oneColorAll(uint8_t r, uint8_t g, uint8_t b);
  107. void playProgram();
  108. void setProgram();
  109. void showLeds();
  110. void resetVars();
  111. void parsePacket();
  112. void evaluateData(byte val);
  113. void doCommunication();
  114. void loopRGBPixel(int idelay);
  115. void rainbow_fade(int idelay);
  116. void rainbow_loop(int idelay);
  117. void random_burst(int idelay);
  118. void flicker(int thishue, int thissat);
  119. void colorBounce(int ihue, int idelay);
  120. void pulse_oneColorAll(int ahue, int idelay, int steps, int useSat);
  121. void police_light_strobo(int idelay);
  122. void police_lightsALL(int idelay);
  123. void police_lightsONE(int idelay);
  124. int antipodal_index(int i);
  125.  
  126. void setup()
  127. {
  128.  SERIAL.begin(BAUDRATE);
  129.  delay(1000);
  130.  memset(data.rgb, 0, sizeof(struct CRGB) * NUM_LEDS);
  131.  
  132.  // Uncomment one of the following lines for your leds arrangement.
  133.  // FastLED.addLeds<TM1803, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  134.  // FastLED.addLeds<TM1804, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  135.  // FastLED.addLeds<TM1809, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  136.  // FastSPI_LED2.addLeds<WS2811, DATA_PIN, GRB>(data.rgb+18, NUM_LEDS/3);
  137.  // FastLED.addLeds<WS2811, 8, RGB>(data.rgb + 225, NUM_LEDS/4);
  138.  // FastLED.addLeds<WS2812, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  139.  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  140.  // FastLED.addLeds<NEOPIXEL, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  141.  // FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  142.  // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  143.  // FastLED.addLeds<WS2801, RGB>(data.rgb, NUM_LEDS);
  144.  // FastLED.addLeds<SM16716, RGB>(data.rgb, NUM_LEDS);
  145.  // FastLED.addLeds<LPD8806, RGB>(data.rgb, NUM_LEDS);
  146.  // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(data.rgb, NUM_LEDS);
  147.  // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(data.rgb, NUM_LEDS);
  148.  // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(data.rgb, NUM_LEDS);
  149.  
  150.  FastLED.addLeds<WS2811, DATA_PIN, RGB>(data.rgb, NUM_LEDS);
  151.  FastLED.setBrightness(BRIGHTNESS);
  152.  
  153.  oneColorAll(255,0,0);
  154. #ifdef DEBUG
  155.  SERIAL_DEBUG.begin(BAUDRATE);
  156.  // wait for serial port to connect. Needed for Leonardo only
  157.  while (!SERIAL_DEBUG)
  158.     delay(1);
  159.  SERIAL_DEBUG.println("Setup done");
  160. #endif
  161.  memset(args, 0, MAX_ARGS);
  162.  resetVars();
  163.  PT_INIT(&pt1);
  164. }
  165.  
  166. /*==============================================================================*/
  167. /* Thread für Programm/Effekte
  168. /*==============================================================================*/
  169.  
  170. static int playProgramThread(struct pt *pt)
  171. {
  172.  static unsigned long timestamp = 0;
  173.  PT_BEGIN(pt);
  174.  while(1)
  175.  {
  176.     PT_WAIT_UNTIL(pt, millis() - timestamp > effectDelay);
  177.     playProgram();
  178.     timestamp = millis();
  179.  }
  180.  PT_END(pt);
  181. }
  182.  
  183. /*==============================================================================*/
  184. /* loop
  185. /*==============================================================================*/
  186.  
  187. void loop()
  188. {
  189. while (1)
  190. {
  191.  // if data available switch to communication mode
  192.  if (SERIAL.available() > 0)
  193.  {
  194.     if (mode != mCommunication)
  195.     {
  196.        debug("switching to communication mode");
  197.        mode = mCommunication;
  198.        resetVars();
  199.     }
  200.     doCommunication();
  201.  }
  202.  else
  203.  // communication timeout after 0.5 seconds
  204.  while (SERIAL.available() == 0 && millis()-lastDataAt > 1000)
  205.  {
  206.     if (mode != mProgram)
  207.     {
  208.        debug("switching to prg mode, ignoring ", data.pos);
  209.        mode = mProgram;
  210.        resetVars();
  211.     }
  212.     else
  213.       playProgramThread(&pt1);
  214.  }
  215. }
  216. }
  217.  
  218. /*==============================================================================*/
  219. /* do communication
  220. /*==============================================================================*/
  221.  
  222. void doCommunication()
  223. {
  224. #ifdef DEBUG
  225.  int count = 0;
  226. #endif
  227.  
  228.  // read ...
  229.  
  230.  while (SERIAL.available() > 0)
  231.  {
  232.  
  233.     byte val = SERIAL.read();
  234.     lastDataAt = millis();
  235.  
  236. #ifdef DEBUG
  237.     debug("got: ", val, HEX);
  238.     debug("at pos: ", data.pos, DEC);
  239.     count++;
  240. #endif
  241.  
  242.     if (data.pos == posStart && val == tpm2Start)                                    // Startbyte
  243.        resetVars();
  244.  
  245.     else if (data.pos == posType && (val == tpm2DataFrame || val == tpm2Command))    // Block-Art
  246.        data.type = val;
  247.  
  248.     else if (data.pos == posFsHigh)                                                  // Framesize (High Bit)
  249.     {
  250.        data.fs = (val << 8) & 0xFF00;
  251.        if (data.fs > NUM_LEDS*3)
  252.        {
  253.          debug("framsize too high: ", data.fs);
  254.          resetVars();
  255.          continue;
  256.        }
  257.     }
  258.     else if (data.pos == posFsLow)                                                   // Framesize (Low byte)
  259.     {
  260.        data.fs += val & 0x00FF;
  261.        if (data.fs > NUM_LEDS*3)
  262.        {
  263.          debug("framsize too high: ", data.fs);
  264.          resetVars();
  265.          continue;
  266.        }
  267.     }
  268.     else if (data.pos == posData + data.fs && val == tpm2End)                        // End Byte
  269.        parsePacket();
  270.  
  271.     else if (data.pos >= posData && data.pos < posData+data.fs)                      // Bytes zwischen Header und Ende lesen
  272.        evaluateData(val);
  273.  
  274.     else                                                                             // protocol violation ...
  275.     {
  276.        if (data.pos != 0)
  277.        {
  278.           debug("protocol violation at pos: ", data.pos);
  279.           debug("val was: ", val);
  280.        }
  281.         debug("Error");
  282.        resetVars();
  283.        continue;
  284.     }
  285.     data.pos++;
  286.  }
  287.  
  288. #ifdef DEBUG
  289.  if (count)
  290.     debug("received", count, DEC);
  291. #endif
  292. }
  293.  
  294. /*==============================================================================*/
  295. /* evaluate data
  296. /*==============================================================================*/
  297.  
  298. void evaluateData(byte val)
  299. {
  300.  if (data.type == tpm2DataFrame)        // frame data
  301.  {
  302.     uint8_t* rgb = (uint8_t*)data.rgb;
  303.     rgb[data.pos-posData] = val;
  304.  }
  305.  
  306.  else                                  // command data
  307.  {
  308.     if (data.pos == posData)
  309.     {
  310.        data.command = val;
  311.        memset(args, 0, sizeof(args));
  312.     }
  313.     else
  314.        args[data.pos-posData-1] = val;
  315.  }
  316. }
  317.  
  318. /*==============================================================================*/
  319. /* reset variables
  320. /*==============================================================================*/
  321.  
  322. void resetVars()
  323. {
  324.  debug("Reset");
  325.  memset(&data, 0, sizeof(Data));
  326.  //data.rgb = (struct CRGB*)FastSPI_LED.getRGBData();
  327.  memset(data.rgb, 0,  NUM_LEDS * sizeof(struct CRGB));
  328. }
  329.  
  330. /*==============================================================================*/
  331. /* parse packet
  332. /*==============================================================================*/
  333.  
  334. void parsePacket()
  335. {
  336.  debug("Parse");
  337.  
  338.  switch (data.type)
  339.  {
  340.     case tpm2DataFrame:
  341.     {
  342.        showLeds();
  343.        break;
  344.     }
  345.     case tpm2Command:
  346.     {
  347.        setProgram();
  348.        break;
  349.     }
  350.     default:
  351.        break;
  352.  }
  353.  
  354.  SERIAL.write(tpm2Ack);
  355.  resetVars();
  356.  data.pos = -1;
  357. }
  358.  
  359. /*==============================================================================*/
  360. /* set LED color
  361. /*==============================================================================*/
  362.  
  363. void setLedColor(int led, uint8_t r, uint8_t g, uint8_t b)
  364. {
  365.  data.rgb[led].r = r;
  366.  data.rgb[led].g = g;
  367.  data.rgb[led].b = b;
  368. }
  369.  
  370. /*==============================================================================*/
  371. /* one Color All
  372. /*==============================================================================*/
  373.  
  374. void oneColorAll(uint8_t r, uint8_t g, uint8_t b)
  375. {
  376.  memset(data.rgb, 0, NUM_LEDS * sizeof(struct CRGB));
  377.  
  378.  for (int led = 0; led < NUM_LEDS; led++)
  379.     setLedColor(led, r, g, b);
  380.  
  381.  showLeds();
  382.  effectDelay = 1000;
  383. }
  384.  
  385. /*==============================================================================*/
  386. /* Output Leds
  387. /*==============================================================================*/
  388.  
  389. void showLeds()
  390. {
  391.   FastLED.show();
  392. }
  393.  
  394.  
  395. void setProgram()
  396. {
  397.  program = data.command;
  398. }
  399.  
  400. void playProgram()
  401. {
  402.  switch (program)
  403.  {
  404.     case  0: oneColorAll(args[0],args[1],args[2]);   break;
  405.     case  1: loopRGBPixel(50);                       break;
  406.     case  2: rainbow_fade(20);                       break;
  407.     case  3: rainbow_loop(20);                       break;
  408.     case  4: random_burst(20);                       break;
  409.     case  5: flicker(200,255);                       break;
  410.     case  6: colorBounce(200,50);                    break;
  411.     case  7: pulse_oneColorAll(200,50,100,0);        break;
  412.     case  8: pulse_oneColorAll(0,50,100,1);          break;
  413.     case  9: police_light_strobo(50);                break;
  414.     case 10: police_lightsALL(20);                   break;
  415.     case 11: police_lightsONE(20);                   break;
  416.     default: oneColorAll(0,0,0);        break;
  417.  }
  418. }
  419.  
  420. /* Set LED Color of given LED */
  421.  
  422. void oneColorAllNOSHOW(int r, int g, int b)
  423. {
  424.  resetVars();
  425.  for (int led = 0; led < NUM_LEDS; led++)
  426.  {
  427.     setLedColor(led, r, g, b);
  428.  }
  429.  
  430. }
  431.  
  432. /*==============================================================================*/
  433. /* Effect 0: Fixed color - Arguments RR GG BB
  434. /*==============================================================================*/
  435.  
  436.  
  437. /*==============================================================================*/
  438. /* Effect 1: Loops each RGB color through each Pixel
  439. /*==============================================================================*/
  440.  
  441. void loopRGBPixel(int idelay) //OK
  442. {
  443.  static int c = 0;
  444.  static int i = 0;
  445.  
  446.  if (i > NUM_LEDS-1)
  447.  {
  448.     i = 0;
  449.     c++;
  450.  }
  451.  if (c == 3)
  452.     c = 0;
  453.  
  454.  memset(data.rgb, 0, NUM_LEDS*3);
  455.  
  456.  switch (c)
  457.  {
  458.     case 0:
  459.        data.rgb[i].r =200;
  460.        break;
  461.     case 1:
  462.        data.rgb[i].g =200;
  463.        break;
  464.     case 2:
  465.        data.rgb[i].b =200;
  466.        break;
  467.  }
  468.  showLeds();
  469.  effectDelay = idelay;
  470.  i++;
  471. }
  472.  
  473. /*==============================================================================*/
  474. /* Effect 2: Fade through raibow colors over all LEDs
  475. /*==============================================================================*/
  476.  
  477. void rainbow_fade(int idelay) { //OK //-FADE ALL LEDS THROUGH HSV RAINBOW
  478.  static int ihue = -1;
  479.  ihue++;
  480.  if (ihue >= 255) {
  481.     ihue = 0;
  482.  }
  483.  for(int idex = 0 ; idex < NUM_LEDS; idex++ ) {
  484.     data.rgb[idex] = CHSV(ihue, 255, 255);
  485.  }
  486.  showLeds();
  487.  effectDelay = idelay;
  488. }
  489.  
  490. /*==============================================================================*/
  491. /* Effect 3: Loops rainbow colors around the stripe
  492. /*==============================================================================*/
  493.  
  494. void rainbow_loop(int idelay) { //-LOOP HSV RAINBOW
  495.  static double idex = 0;
  496.  static double ihue = 0;
  497.  double steps = (double)255/NUM_LEDS;
  498.  
  499.  for(int led = 0 ; led < NUM_LEDS; led++ ) {
  500.     ihue = led * steps + idex;
  501.     if (ihue >= 255)
  502.        ihue -= 255;
  503.  
  504.     data.rgb[led] = CHSV((int)ihue, 255, 255);
  505.  
  506.     if (led == 0)
  507.        idex += steps;
  508.     if (idex >= 255)
  509.        idex = 0;
  510.  }
  511.  showLeds();
  512.  effectDelay = idelay;
  513. }
  514.  
  515. /*==============================================================================*/
  516. /* Effect 4: Random burst - Randowm colors on each LED
  517. /*==============================================================================*/
  518.  
  519. void random_burst(int idelay) { //-RANDOM INDEX/COLOR
  520.  static int idex = 0;
  521.  static int ihue = 0;
  522.  
  523.  idex = random(0,NUM_LEDS-1);
  524.  ihue = random(0,255);
  525.  
  526.  data.rgb[idex] = CHSV(ihue, 255, 255);
  527.  
  528.  showLeds();
  529.  effectDelay = idelay;
  530. }
  531.  
  532. /*==============================================================================*/
  533. /* Effect 5: Flicker effect - random flashing of all LEDs
  534. /*==============================================================================*/
  535.  
  536. void flicker(int thishue, int thissat) {
  537.  int ibright = random(0,255);
  538.  int random_delay = random(10,100);
  539.  
  540.  for(int i = 0 ; i < NUM_LEDS; i++ ) {
  541.     data.rgb[i] = CHSV(thishue, thissat, ibright);
  542.  }
  543.  
  544.  showLeds();
  545.  effectDelay = random_delay;
  546. }
  547.  
  548. /*==============================================================================*/
  549. /* Effect 6: Color bounce - bounce a color through whole stripe
  550. /*==============================================================================*/
  551.  
  552. void colorBounce(int ihue, int idelay) { //-BOUNCE COLOR (SINGLE LED)
  553. static int bouncedirection = 0;
  554. static int idex = 0;
  555.  
  556. if (bouncedirection == 0) {
  557.   idex = idex + 1;
  558.   if (idex == NUM_LEDS) {
  559.     bouncedirection = 1;
  560.     idex = idex - 1;
  561.   }
  562. }
  563. if (bouncedirection == 1) {
  564.   idex = idex - 1;
  565.   if (idex == 0) {
  566.     bouncedirection = 0;
  567.   }
  568. }
  569. for(int i = 0; i < NUM_LEDS; i++ ) {
  570.   if (i == idex) {
  571.     data.rgb[i] = CHSV(ihue, 255, 255);
  572.   }
  573.   else {
  574.     setLedColor(i, 0, 0, 0);
  575.   }
  576. }
  577. showLeds();
  578. effectDelay = idelay;
  579. }
  580.  
  581. /*==============================================================================*/
  582. /* Effect 7/8: Fade in/out a color using brightness/saturation
  583. /*==============================================================================*/
  584.  
  585. void pulse_oneColorAll(int ahue, int idelay, int steps, int useSat) { //-PULSE BRIGHTNESS ON ALL LEDS TO ONE COLOR
  586. static int bouncedirection = 0;
  587. static int idex = 0;
  588. int isteps = 255/steps;
  589. static int ival = 0;
  590.  
  591. static int xhue = 0;
  592.  
  593. if (bouncedirection == 0) {
  594.   ival += isteps;
  595.   if (ival >= 255) {
  596.     bouncedirection = 1;
  597.   }
  598. }
  599. if (bouncedirection == 1) {
  600.   ival -= isteps;
  601.   if (ival <= 0) {
  602.     bouncedirection = 0;
  603.    xhue = random(0, 255);
  604.   }
  605. }
  606.  
  607. for(int i = 0 ; i < NUM_LEDS; i++ ) {
  608.   if (useSat == 0)
  609.     data.rgb[i] = CHSV(xhue, 255, ival);
  610.   else
  611.     data.rgb[i] = CHSV(xhue, ival, 255);
  612. }
  613. showLeds();
  614. effectDelay = idelay;
  615. }
  616.  
  617.  
  618. /*==============================================================================*/
  619. /* Effect 9: Police light - red/blue strobo on each half of stripe
  620. /*==============================================================================*/
  621.  
  622. void police_light_strobo(int idelay)
  623. {
  624. int middle = NUM_LEDS/2;
  625. static int color = 0;
  626. static int left_right = 0;
  627.  
  628. if (left_right > 19)
  629.   left_right = 0;
  630.  
  631. if (color == 1)
  632.   color = 0;
  633. else
  634.   color = 1;
  635.  
  636. for (int i = 0; i < NUM_LEDS; i++)
  637. {
  638.   if (i <= middle && left_right < 10)
  639.   {
  640.     if (color)
  641.       setLedColor(i, 0, 0, 255);
  642.     else
  643.       setLedColor(i, 0, 0, 0);
  644.   }
  645.   else
  646.   if (i > middle && left_right >= 10)
  647.   {
  648.     if (color)
  649.       setLedColor(i, 255, 0, 0);
  650.    else
  651.       setLedColor(i, 0, 0, 0);
  652.   }
  653. }
  654. showLeds();
  655. effectDelay = idelay;
  656.  
  657. left_right++;
  658.  
  659. }
  660.  
  661. /*==============================================================================*/
  662. /* Effect 10: Police Light all LEDs
  663. /*==============================================================================*/
  664.  
  665. void police_lightsALL(int idelay) { //-POLICE LIGHTS (TWO COLOR SOLID)
  666. static int idex = 0;
  667.  
  668. int idexR = idex;
  669. int idexB = antipodal_index(idexR);
  670. setLedColor(idexR, 255, 0, 0);
  671. setLedColor(idexB, 0, 0, 255);
  672. showLeds();
  673. effectDelay = idelay;
  674. idex++;
  675. if (idex >= NUM_LEDS) {
  676.   idex = 0;
  677. }
  678. }
  679.  
  680. /*==============================================================================*/
  681. /* Effect 11: Police Light one LED blue and red
  682. /*==============================================================================*/
  683.  
  684. void police_lightsONE(int idelay) { //-POLICE LIGHTS (TWO COLOR SINGLE LED)
  685. static int idex = 0;
  686.  
  687. int idexR = idex;
  688. int idexB = antipodal_index(idexR);
  689. for(int i = 0; i < NUM_LEDS; i++ ) {
  690.   if (i == idexR) {
  691.     setLedColor(i, 255, 0, 0);
  692.   }
  693.   else if (i == idexB) {
  694.     setLedColor(i, 0, 0, 255);
  695.   }
  696.   else {
  697.     setLedColor(i, 0, 0, 0);
  698.   }
  699. }
  700. showLeds();
  701. effectDelay = idelay;
  702. idex++;
  703. if (idex >= NUM_LEDS) {
  704.   idex = 0;
  705. }
  706. }
  707.  
  708. /*==============================================================================*/
  709. /* Util func Effekte */
  710. /*==============================================================================*/
  711.  
  712. //-FIND INDEX OF ANTIPODAL OPPOSITE LED
  713. int antipodal_index(int i) {
  714. //int N2 = int(NUM_LEDS/2);
  715. int iN = i + TOP_INDEX;
  716. if (i >= TOP_INDEX) {
  717.   iN = ( i + TOP_INDEX ) % NUM_LEDS;
  718. }
  719. return iN;
  720. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement