  * Note: This sketch DOES NOT reset the I2C address of the BlinkM.
  *       If you want to change the I2C address use the 'A<n>' command.
  *
- * 2007-14, Tod E. Kurt, ThingM, http://thingm.com/
+ *
+ * Wiring hookups:
+ *
+ * Arduino Uno to BlinkM:
+ * Gnd - Gnd, 5V - Vcc, A4 - SDA, A5 - SCL
+ *
+ * Sparkfun Pro Micro (ATmega32U4) to BlinkM:
+ * Gnd - Gnd, 5V - Vcc, D2 - SDA, D3 - SCL
+ *
+ * Adafruit Trinket M0 to BlinkM2:
+ * Gnd - Gnd, 3V - Vcc, D0 - SDA, D2 - SCL
+ *
+ * 
+ *
+ * 2007-20, Tod E. Kurt, ThingM, http://thingm.com/
  *
  */
 

 *  and you'll see a menu of things to do.
 *
 *
 * BlinkM connections to Arduino
 * PWR - -- gnd -- black -- Gnd
 * PWR + -- +5V -- red   -- 5V
 * I2C d -- SDA -- green -- Analog In 4

{
  int32_t num;
  //read the serial port and create a string out of what you read
  if( !readSerialString() ) {
      return;
  }
  
  Serial.println(serInStr);
  char cmd = serInStr[0];  // first char is command
  
  // argument parsing
  char* str = serInStr;
  while( *++str == ' ' );  // go past any intervening whitespace
  if( strlen(str) >= 6 ) { // 6 chars in a hex color code
      str[6] = '\0';
      num = strtol(str, NULL, 16); // parse color
  } else { 
      num = atoi(str);
  }
  
  // command parsing
  if( cmd == '?' ) {
      help();
  }
  else if( cmd == 'c' || cmd=='h' || cmd == 'C' || cmd == 'H' ) {
      byte a = (num >> 16) & 0xff;
      byte b = (num >>  8) & 0xff;
      byte c = (num >>  0) & 0xff;
      
      if( cmd == 'c' ) {
          Serial.print(F("Fade to r,g,b:"));
          blinkm.fadeToRGB( a,b,c );
      } 
      else if( cmd == 'h' ) {
          Serial.print(F("Fade to h,s,b:"));
          blinkm.fadeToHSB( a,b,c );
      } 
      else if( cmd == 'C' ) {
          Serial.print(F("Random by r,g,b:"));
          blinkm.fadeToRandomRGB( a,b,c );
      } 
      else if( cmd == 'H' ) {
          Serial.print(F("Random by h,s,b:"));
          blinkm.fadeToRandomHSB( a,b,c );
      }
      Serial.print(a,HEX); Serial.print(",");
      Serial.print(b,HEX); Serial.print(",");
      Serial.print(c,HEX); Serial.println();
  }
  else if( cmd == 'f' ) {
      Serial.print(F("Set fade speed to:")); Serial.println(num,DEC);
      blinkm.setFadeSpeed(num);
  }
  else if( cmd == 't' ) {
      Serial.print(F("Set time adj:")); Serial.println(num,DEC);
      blinkm.setTimeAdj(num);
  }
  else if( cmd == 'p' ) {
      Serial.print(F("Play script #"));
      Serial.println(num,DEC);
      blinkm.playScript(num,0,0 );
  }
  else if( cmd == 'o' ) {
      Serial.println(F("Stop script"));
      blinkm.stopScript();
  }
  else if( cmd == 'g' ) {
      Serial.print(F("Current color: "));
      byte r,g,b;
      blinkm.getRGBColor(&r,&g,&b);
      Serial.print("r,g,b:"); Serial.print(r,HEX);
      Serial.print(",");      Serial.print(g,HEX);
      Serial.print(",");      Serial.println(b,HEX);
  }
  else if( cmd == 'W' ) {
      Serial.println(F("Writing new eeprom script"));
      /* for(int i=0; i<6; i++) { */
      /*     blinkm_script_line l = script_lines[i]; */
      /*     blinkm.writeScriptLine( blinkm_addr, 0, i, l.dur, */
      /*                             l.cmd[0],l.cmd[1],l.cmd[2],l.cmd[3]); */
      /* } */
  }
  else if( cmd == 'A' ) {
      if( num>0 && num<0xff ) {
          Serial.print(F("Setting address to: "));
          Serial.println(num,DEC);
          blinkm.changeAddress(num);
          //blinkm_addr = num;
      } else if ( num == 0 ) {
          Serial.println(F("Resetting address to default 9: "));
          //blinkm_addr = 9;
          blinkm.changeAddress( blinkm_addr_default );
      }
  }
  else if( cmd == 'a' ) {
      Serial.print(F("Address: "));
      //num = blinkm.getAddress(0); 
      num = blinkm.getAddress(); 
      Serial.println(num);
  }
  else if( cmd == '@' ) {
      Serial.print(F("Will now talk on blinkm address: "));
      Serial.println(num,DEC);
      blinkm.talkTo( num );
  }
  else if( cmd == 'Z' ) { 
      Serial.print(F("BlinkM version: "));
      num = blinkm.getVersion();
      if( num == -1 )
          Serial.println(F("couldn't get version"));
      Serial.print( (char)(num>>8) ); 
      Serial.println( (char)(num&0xff) );
  }
  else if( cmd == 'B' ) {
      Serial.print(F("Set startup mode:")); Serial.println(num,DEC);
      blinkm.setStartupParams(num, 0,0,1,0);
  }
  else if( cmd == 'i' ) {
      Serial.print(F("get Inputs: "));
      byte inputs[4];
      blinkm.getInputs(inputs); 
      for( byte i=0; i<4; i++ ) {
          Serial.print(inputs[i],HEX);
          Serial.print( (i<3)?',':'\n');
      }
  }
  else if( cmd == 's' ) { 
      lookForBlinkM();
  }
  else if( cmd == 'S' ) {
      Serial.println(F("Scanning I2C bus from 1-100:"));
      blinkm.ScanI2CBus(1,100, scanfunc);
      Serial.println();
  }
  else if( cmd == 'R' ) {
      Serial.println(F("Doing Factory Reset"));
      blinkm.doFactoryReset();
  }
  else if( cmd == '.' ) { 
      Serial.print(F("Set LEDn: "));
      Serial.println(num, BIN);
      blinkm.setLEDn( num );
  }
  else if( cmd == 'b' ) { // set brightness BLINKM2
      Serial.print(F("Set brightness: "));
      Serial.println(num);
      blinkm.setBrightness(num);
  }
  else if( cmd == '!' ) {
      Serial.print("");
  }
  else { 
      Serial.println(F("Unrecognized cmd"));
  }
  serInStr[0] = 0;  // say we've used the string
  
  Serial.print(F("cmd> "));
  
}

