#include <serialStr.h>
#include <strTools.h>
#include <neoPixel.h>
#include "RGBLED.h"

RGBLED    theLED(3,5,6);              // Our RGB LED create it by passing in 3 PWM pin numbers R G & B
RGBLED    theOtherLED(9,10,11,false); // Another RGB LED but this one is neg common.
serialStr listener;                   // serialStr does the work of getting commands for us.
neoPixel  theNeoPixel(1,2);           // Just for fun we'll also have a neoPixel.

void setup() {

  colorObj  aColor;
  Serial.begin(9600);                                   // Fire up serial stuff.
  Serial.print("Commands? type three 0..255 values");   // Quick instructions for user.
  Serial.println(" sepreated by blanks R G B.");        //
  listener.setCallback(gotCommand);                     // When a command arrives, call this function with it.
  aColor.setColor(LC_GREEN);                            // Make up a default color. Start with green..
  aColor.blend(&blue,50);                               // Blend in 50% blue.
  theNeoPixel.begin();                                  // Fire up the neoPixel.
  theNeoPixel.setPixelColor(0,&theLED);                 // WHAT! You pass in the LED to set the neoPixel?!?
  theNeoPixel.show();                                   // That's right, the RGBLED derives from colorObj. It's a color.

void gotCommand(char* inStr) {

  int         red;                            // Make life easy. Set up the new color values here.
  int         green;                          //
  int         blue;                           //
  const char  delim[] = " \t\n";              // Delimiters for breaking up the inputted commands space, tab, newline.
  char*       token;                          // A pointer for saying, your broken off bit starts here.
  char*       comStr = NULL;                  // We're going to store a copy of the inputted command string here.
  red   = 0;                                  // If it fails? We'll turn it black/off.
  green = 0;                                  //                      
  blue  = 0;                                  //
  heapStr(&comStr,inStr);                     // Allocate and save a local copy of our command string.
  token = strtok(comStr,delim);               // Bust off the first token (number) from the command.
  if (token) red = atoi(token);               // If we got a token, decode it as an int use it for red.
  token = strtok(NULL,delim);                 // Bust off the next token (number) from the command.
  if (token) green = atoi(token);             // If we got a token, decode it as an int use it for green.
  token = strtok(NULL,delim);                 // Grab the last token (number) from the command.
  if (token) blue = atoi(token);              // If we got a token, decode it as an int use it for blue.
  freeStr(&comStr);                           // Recycle the local copy of the command string.
  Serial.print("Color set to : ");            // Amuse the user with what we are up to.
  Serial.print(red);Serial.print("\t");       //
  Serial.print(green);Serial.print("\t");     //
  Serial.println(blue);                       //
  theLED.setColor(red,green,blue);            // Set the RGB LED to the new R G B values.
  theNeoPixel.setPixelColor(0,&theLED);       // Set the neoPixel using the RGB LED as a color object.
  theNeoPixel.show();                         // Make the neoPixel show it's new color.

void loop() { idle(); } // idle() lets the magic happen.