/*
  Arduino フルカラーLED シリアルコントロール
  ColorLED_serial4    2009/6/26  puw2
    c***,***,*** r:0-255 g:0-255 b:0-255
    r*** Red   LED ****:0-255
    g*** Green LED ****:0-255
    b*** Blue  LED ****:0-255
    o:LED on
    f:LED off
 */
//------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include "WProgram.h"
#include "WString.h"
 
//------------------------------------------------
int ledPinG = 9;     // LED-G connected to digital pin 9
int ledPinR = 10;    // LED-R connected to digital pin 10
int ledPinB = 11;    // LED-B connected to digital pin 11
char flg_c,flg_r,flg_g,flg_b;
int r,g,b,v;
char buff[20];
int pt=0;
int no;

//------------------------------------------------
void led_off(){
  digitalWrite(9, LOW);    // sets the motor off
  digitalWrite(10, LOW);   // sets the motor off
  digitalWrite(11, LOW);   // sets the motor off
} 

//------------------------------------------------
void led_on(){
  digitalWrite(9, HIGH);    // sets the motor off
  digitalWrite(10, HIGH);   // sets the motor off
  digitalWrite(11, HIGH);   // sets the motor off
} 
//------------------------------------------------
// 検索文字の位置を戻り値に返す '0'は見つからない
int strchar(char *s, char ch){
int i=0;
    while (s[i]!='\0'){
      if (s[i]==ch) return i+1; else i++;
    }
    return 0;
}

//------------------------------------------------
void setup()
{
  pinMode(ledPinG, OUTPUT);   // sets the pin as output
  pinMode(ledPinR, OUTPUT);   // sets the pin as output
  pinMode(ledPinB, OUTPUT);   // sets the pin as output
  led_off();
  Serial.begin(9600);
  Serial.println("ColorLED_serial_4 Ready");
  r=g=b=127;
}

//------------------------------------------------
void loop()
{
  if( Serial.available()) {
    char ch = Serial.read();
    if(ch=='o') r=g=b=255;    // All on
    if(ch=='f') r=g=b=0;      // All off

    if(ch=='\r'){
      buff[pt]='\0';
      //Serial.print(buff);
      Serial.print("\n\r");
      pt=0;

      if(buff[0]=='c'){       // Red,Green,Blue
          buff[0]='0';
          r=atoi(buff);
          Serial.println(r,DEC);
        no=strchar(buff,',');  // ','文字位置サーチ
        if(no>0){
          buff[no-1]='0';
          g=atoi(&buff[no]);
          Serial.println(g,DEC);
        }
        no=strchar(buff,',');   // ','文字位置サーチ
        if(no>0){
          buff[no-1]='0';
          b=atoi(&buff[no]);
          Serial.println(b,DEC);
        }
      }
      
      if(buff[0]=='r'){      // Red
        buff[0]='0';
        r=atoi(buff);
        Serial.println(r,DEC);
      }
      if(buff[0]=='g'){      // Green
        buff[0]='0';
        g=atoi(buff);
        Serial.println(g,DEC);
      }
      if(buff[0]=='b'){      //Blue
        buff[0]='0';
        b=atoi(buff);
        Serial.println(b,DEC);
      }
      
    }else{
      Serial.print(ch);
      buff[pt]=ch;
      pt++;
      if(pt>19) pt=0;
    }
    
  }else{    // loop
    analogWrite(ledPinR, r);   //analogWrite values from 0 to 255
    analogWrite(ledPinG, g);
    analogWrite(ledPinB, b);
  }
}