Advertisement
Morydd

Untitled

Mar 31st, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 KB | None | 0 0
  1. /*
  2.  * Reciever Code for LED Panels using WS2812b strips.
  3.  * incoming data over serial connection is passed out to next unit
  4.  * Format for transitions from sender is:
  5.  *  F,rrr,ggg,bbb,XX - "F"ade where target color is RRR,GGG,BBB and time of fade is XX in seconds
  6.  *  X = immediate blackout
  7.  */
  8.  
  9.  
  10. //Libraries
  11.   #include <FastLED.h>
  12.   //#include <SoftwareSerial.h>        
  13.   //SoftwareSerial inSerial(12,13);     //Set Serial Data IN pins from controller/previous unit
  14.   //SoftwareSerial outSerial(10,11);    //Set Serial Data OUT pins to next unit
  15.  
  16. // Preset Variables
  17.   // FastLED Setup
  18.     #define NUM_LEDS 15                   //LEDs Per Panel
  19.     #define DATA_PIN 2                    //Data Pin for LEDS
  20.     CRGB leds[NUM_LEDS];                  //Build LED Array
  21.  
  22.   // Color Handling
  23.     uint8_t endColor[3] = {0, 0, 0};      //New Color RGB (Starts in Black)
  24.     uint8_t startColor[3] = {10, 150, 250};    //Previous Color RGB (Non-Zero for Debug)
  25.     //uint8_t startColor[3] = {0, 0, 0};    //Previous Color RGB (Starts in Black)
  26.     uint8_t curColor[3] = {0, 0, 0};      //Transitional Color Holder
  27.  
  28.   //Time Handling
  29.     int cueTime = 0;                      //Cue Run Time
  30.     float startTime = millis();           //Cue Start
  31.     float curTime = millis();             //Cue Elapsed
  32.  
  33.   //Math Handling
  34.       float colStep[3]  = {0, 0, 0};        //Number of steps between start/end color
  35.       float stepTime[3] = {0, 0, 0};        //Millis per step
  36.       bool cueRunning = false;              //Skip the math if it's already done
  37.       float timeStep = 0;                   //Number of steps at time
  38.       uint8_t t = 0;                        //Type conversion of timeStep
  39.  
  40.   //Serial Buffer Handling  
  41.     const int buflen = 32;                  //Serial Buffer Length Maximum
  42.     char inBuffer[buflen];                  //Serial Buffer
  43.     static int inlength = 0;                //Set beginning of buffer
  44.  
  45. void setup() {
  46.   // initialize serial:
  47.     Serial.begin(9600);                   // USB serial for debugging
  48.     //  inSerial.begin(9600);             // Input from controller or previous board
  49.     //  outSerial.begin(9600);            // Output to next board
  50.     Serial.setTimeout(3600000);           // Set all serial timeouts to 1 hour
  51.     //  inSerial.setTimeout(3600000);
  52.     //  outSerial.setTimeout(3600000);
  53.  
  54.   //Set Up LEDs
  55.     FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  56.       Serial.println(F("Setup Complete"));
  57.   //Populate buffer array for later manipulation
  58.     for (int i=0; i<buflen; i++) {
  59.         inBuffer[buflen] = 0;
  60.     }
  61. }
  62.  
  63. void loop() {
  64.   // Incoming Serial Data
  65.   if (Serial.available()) {                 // Read Input
  66.     int data = Serial.read();               // Read incoming byte
  67.     if (data == '\n') {                     // Newline means end of Command
  68.       Serial.println("Command");
  69.       inBuffer[inlength] = '\0';            // Terminate Command
  70.       Serial.print("inBuffer Array: ");
  71.       for(int i = 0; i < inlength; i++){
  72.         Serial.print(inBuffer[i]);
  73.       }
  74.       Serial.println(" ");
  75. //      Serial.print("inBuffer: ");
  76. //      Serial.println(inBuffer);
  77.       Serial.print("inBuffer0: ");
  78.       Serial.println(inBuffer[0]);
  79.      
  80.       if (inlength) {                       //Check for actual data
  81.         if (inBuffer[0]== 'F') {            //Command is for firing a fade to fade to solid color  
  82. //          Serial.println(inBuffer);
  83.           int ci = 0;                       //color increment
  84.           uint8_t colors[5];
  85.           char *val = strtok (inBuffer,","); //break the buffer in to its RGB Components
  86. //          Serial.println(val);
  87.           colors[ci] = atoi(val);
  88.           while ((val = strtok(NULL,",")) != NULL) {  //populate color array
  89. //            Serial.println(val);
  90.             colors[++ci] = atoi(val);
  91.           }
  92. //          Serial.print("Colors: R");
  93. //          Serial.print(colors[1]);
  94. //          Serial.print(" G");
  95. //          Serial.print(colors[2]);
  96. //          Serial.print(" B");
  97. //          Serial.print(colors[3]);
  98. //          Serial.print(" - Time:");
  99. //          Serial.println(colors[4]);
  100. //          Serial.print("End Color: ");
  101.           for (int ei=1; ei<4; ei++) {
  102.             endColor[ei] = colors[ei];
  103. //            Serial.print(ei);
  104. //            Serial.print("-");
  105. //            Serial.print(endColor[ei]);
  106. //            Serial.print(" ");
  107.           }
  108. //          Serial.println(" ");
  109.          
  110. //          Serial.print("startColor: 1-");
  111. //          Serial.print(startColor[0]);
  112. //          Serial.print(" G");
  113. //          Serial.print(startColor[1]);
  114. //          Serial.print(" B");
  115. //          Serial.println(startColor[2]);
  116.          
  117.           cueTime = colors[4];
  118.          
  119.           if (cueRunning == false) {
  120.               Serial.println(F("Cue Running..."));
  121.             // Compute Fade Amounts
  122.             for (int i = 0; i < 3; i++) {
  123. //             Serial.print(F("Color Math... "));
  124.               colStep[i] = startColor[i] - endColor[i+1];
  125.               if (colStep[i] == 0) {
  126.                 stepTime[i] = 0;
  127.               }
  128.               else {
  129.                 stepTime[i] = ((cueTime * 1000) / colStep[i]);
  130.               }
  131. //              Serial.print (F("Step Time: "));
  132. //              Serial.println (stepTime[i]);
  133.             }
  134.             startTime = millis();
  135.             cueRunning = true;
  136.           }
  137.           //Display Fades
  138.           curTime = millis();
  139. //          Serial.print("startTime: ");
  140. //          Serial.print(startTime);
  141. //          Serial.print(" curTime: ");
  142. //          Serial.println(curTime);
  143.           while (curTime <= (startTime+(cueTime*1000))){
  144.             for (int j = 0; j < 3; j++) {
  145. //              Serial.print (F(" "));
  146. //              Serial.print (j);
  147.               if (stepTime[j] == 0) {
  148.                 curColor[j] = startColor[j];
  149. //                Serial.print (F("t=0"));
  150.               }
  151.               else {
  152.                 timeStep = ((curTime-startTime) / stepTime[j]);
  153.                 t=(uint8_t) timeStep;
  154. //                Serial.print (F("t="));
  155. //                Serial.print (t);
  156.                 curColor[j] = startColor[j] - t;
  157.               }
  158. //              Serial.println(" ");
  159.             }
  160.             for(int i = (NUM_LEDS)-1; i >= 0; i--) {
  161.               leds[i] = CRGB(curColor[0],curColor[1],curColor[2]);
  162.               FastLED.show();
  163.             }
  164. //            Serial.print("LED show: ");
  165. //            Serial.print(curColor[0]);
  166. //            Serial.print("-");
  167. //            Serial.print(curColor[1]);
  168. //            Serial.print("-");
  169. //            Serial.println(curColor[2]);
  170.             curTime = millis();
  171.           }
  172.           //else {
  173.             cueRunning = false;
  174.             Serial.println("End Cue");
  175.             memset(inBuffer,0,buflen);
  176.             Serial.println("Cue Buffer Clear");
  177.      
  178.           //}
  179.          
  180.         }
  181.         else if (inBuffer[0]== 'X') {       //Blackout Shortcut
  182.         Serial.println(F("Blackout"));
  183.           for(int i = (NUM_LEDS)-1; i >= 0; i--) {
  184.             leds[i] = CRGB(0,0,0);
  185.             FastLED.show();
  186.             memset(inBuffer,0,buflen);
  187.             Serial.println("BO Buffer Clear");
  188.  
  189.           }
  190.         }  
  191.         else{                               //We don't know what to do with this data
  192.         Serial.println(F("*** BAD DATA ***"));
  193.                     memset(inBuffer,0,buflen);
  194.             Serial.println("XX Buffer Clear");
  195.  
  196.         }
  197.       }
  198.     }
  199.     else if (inlength < buflen - 1) {   //still data coming in? Add it to the buffer
  200.         inBuffer[inlength++] = data;
  201.     }
  202.   }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement