/* * BlinkMTester -- Simple command-line tool to play with BlinkMs * * * BlinkM connections to Arduino * PWR - -- gnd -- black -- Gnd * PWR + -- +5V -- red -- 5V * I2C d -- SDA -- green -- Analog In 4 * I2C c -- SCK -- blue -- Analog In 5 * * Note: This sketch resets the I2C address of the BlinkM. * If you don't want this behavior, comment out "BlinkM_setAddress()" * in setup() and change the variable "blink_addr" to your BlinkM's addr. * * 2007, Tod E. Kurt, ThingM, http://thingm.com/ * */ #include "Wire.h" #include "BlinkM_funcs.h" #define BLINKM_ARDUINO_POWERED 1 byte blinkm_addr = 0x00; // the address we're going to set the BlinkM to char serInStr[30]; // array that will hold the serial input string void help() { } void setup() { if( BLINKM_ARDUINO_POWERED ) { BlinkM_beginWithPower(); } else { BlinkM_begin(); } Serial.begin(9600); } void loop() { int num; //read the serial port and create a string out of what you read if( readSerialString() ) { char cmd = serInStr[0]; // first char is command char* str = serInStr; while( *++str == ' ' ); // got past any intervening whitespace num = atoi(str); // the rest is arguments (maybe) if( cmd == '?' ) { help(); } else if( cmd == 'c' || cmd=='h' || cmd == 'C' || cmd == 'H' ) { byte a = toHex( str[0],str[1] ); byte b = toHex( str[2],str[3] ); byte c = toHex( str[4],str[5] ); if( cmd == 'c' ) { Serial.print("Fade to r,g,b:"); BlinkM_fadeToRGB( blinkm_addr, a,b,c); } else if( cmd == 'h' ) { // Serial.print("Fade to h,s,b:"); BlinkM_fadeToHSB( blinkm_addr, a,b,c); } else if( cmd == 'C' ) { // Serial.print("Random by r,g,b:"); BlinkM_fadeToRandomRGB( blinkm_addr, a,b,c); } else if( cmd == 'H' ) { // Serial.print("Random by h,s,b:"); BlinkM_fadeToRandomHSB( blinkm_addr, a,b,c); } } else if( cmd == 'f' ) { BlinkM_setFadeSpeed( blinkm_addr, num); } else if( cmd == 't' ) { BlinkM_setTimeAdj( blinkm_addr, num); } else if( cmd == 'x' ) { blinkm_addr = num; } else if( cmd == 'p' ) { BlinkM_playScript( blinkm_addr, num,0,0 ); } else if( cmd == 'o' ) { BlinkM_stopScript( blinkm_addr ); } else if( cmd == 'g' ) { byte r,g,b; BlinkM_getRGBColor( blinkm_addr, &r,&g,&b); } else if( cmd == 'A' ) { if( num>0 && num<0xff ) { BlinkM_setAddress(num); blinkm_addr = num; } } else if( cmd == 'a' ) { num = BlinkM_getAddress(blinkm_addr); // zero means 'general call' } else if( cmd == 'Z' ) { num = BlinkM_getVersion(blinkm_addr); if( num == -1 ) BlinkM_setStartupParams( blinkm_addr, num, 0,0,1,0); } else { } serInStr[0] = 0; // say we've used the string } } // a really cheap strtol(s,NULL,16) #include uint8_t toHex(char hi, char lo) { uint8_t b; hi = toupper(hi); if( isxdigit(hi) ) { if( hi > '9' ) hi -= 7; // software offset for A-F hi -= 0x30; // subtract ASCII offset b = hi<<4; lo = toupper(lo); if( isxdigit(lo) ) { if( lo > '9' ) lo -= 7; // software offset for A-F lo -= 0x30; // subtract ASCII offset b = b + lo; return b; } // else error } // else error return 0; } //read a string from the serial and store it in an array //you must supply the array variable uint8_t readSerialString() { if(!Serial.available()) { return 0; } delay(10); // wait a little for serial data int i = 0; while (Serial.available()) { serInStr[i] = Serial.read(); // FIXME: doesn't check buffer overrun i++; } serInStr[i] = 0; // indicate end of read string return i; // return number of chars read }