// Net IO vO@

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
// hoAhXAl`bAhX͂ꂼ̃VXeɍ킹
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x9C, 0x64 };
IPAddress ip(192,168,0,100);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);


// telnet defaults to port 23
EthernetServer server(23);	// |[gԍ͎̃VXeɍ킹ďĂǂB
boolean alreadyConnected = false; // whether or not the client was connected previously
Servo servo9; //@g\

void setup() {
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  servo9.attach(9);
  pinMode(2,OUTPUT);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  


  Serial.print("Chat server address:");
  Serial.println(Ethernet.localIP());
}

// [|[gR}h
void RelayReport(){ // [ԃ|[g
  server.print("*R");
  if(digitalRead(2)) server.println('1');
  else server.println('0');
}
void Relay(int param){
  if(param>0)digitalWrite(2,1);
  else digitalWrite(2,0);
  RelayReport();
}



uint8_t seqLevel=0; //V[PXx 0:wb_[ 1:R}h 2:p[^ 
uint8_t commandNo=0; //R}hԍ 0: 1:[쓮 2:T[{ 3:ovl
uint8_t paramIndex=0; // p[^̓CfbNX
char paramString[10]; // p[^
int iParam; //p[^[
void loop() {
 
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (client.available() > 0) {
      //
      char ch = client.read();
      switch(seqLevel){
        case 0: // V[PXxOwb_[o
          if(ch == '*'){
            seqLevel = 1; // wb_[o@V[PXxPɃZbg
            Serial.print("*");
            paramIndex=0; // p[^CfbNXZbg
            paramString[0]='\0'; //p[^񃊃Zbg
          }
          break;
        case 1: // V[PXxPR}h
          switch(ch){
            case 'R':
            case 'r':  //[쓮R}h
              commandNo = 1;
              seqLevel = 2; // V[PXxQAp[^
              Serial.print("R");
              break;
            case 'S':
            case 's': //T[{R}h@g\
              commandNo = 2;
              seqLevel = 2; // V[PXxQAp[^
              Serial.print("S");
              break;
            case 'P':
            case 'p': // ovlR}h@g\
              commandNo = 3;
              seqLevel = 2; // V[PXxQAp[^
              Serial.print("P");
              break;
            default: // R}h̓Zbg
              seqLevel = 0;
              commandNo = 0;
          }// end of switch R}h
          break;
        case 2: // V[PXxQp[^
          if(paramIndex==0){// ŏ́̕f|fL
            if((isdigit(ch))||(ch=='-')){
              paramString[paramIndex]=ch;
              paramIndex++;
              paramString[paramIndex]='\0';
            }else{// p[^Ȃsi₢킹j
              Serial.println();
              switch(commandNo){
                case 1: // Relay
                  RelayReport();
                  break;
                case 2: // Servo
                case 3: // Pwm
                default:
                  ;
              }
              seqLevel=0;
              commandNo=0;
            }
          }else if(paramIndex<9){ // Qڈȍ~X܂ŗL
            if(isdigit(ch)){
              paramString[paramIndex]=ch;
              paramIndex++;
              paramString[paramIndex]='\0';
            }else{// ȊOȂp[^s
              if((paramString[0]=='-')&&(paramString[1]=='\0')){ // p[^[f|f̂ݕs
                Serial.println("-NG");
                seqLevel = 0;
                commandNo = 0;
                break;
              }else{ // p[^[Lsi쓮j
                int param;
                sscanf(paramString,"%d",&param);
                Serial.println(paramString);
                switch(commandNo){
                  case 1:
                    Relay(param);
                    break;
                  case 2:
                  case 3:
                  default:
                  ;
                }
                seqLevel = 0;
                commandNo = 0;
              }
            }
          }else{// I[o[s
            Serial.println(paramString);
          } 
          break;
        default:
          seqLevel = 0; // 
      }//end ot switch@seqLevel
    }
  }
}


