Esempio n. 1
0
void BluetoothController::process() {
  if(Serial2.available())  {                            // data received from smartphone
    delay(2);
    cmd[0] = Serial2.read();
    if(cmd[0] == STX)  {
      int i=1;      
      while(Serial2.available())  {
        delay(1);
        cmd[i] = Serial2.read();
        if(cmd[i]>127 || i>7)                 break;     // Communication error
        if((cmd[i]==ETX) && (i==2 || i==7))   break;     // Button or Joystick data
        i++;
      }
      if     (i==2)          getButtonState(cmd[1]);    // 3 Bytes  ex: < STX "C" ETX >
      else if(i==7)          getJoystickState(cmd);     // 6 Bytes  ex: < STX "200" "180" ETX >
    }
  }
  sendBlueToothData();
  while(Serial2.available())  Serial2.read();         // empty RX buffer
}
void Joystick::main()
{
  if(Serial.available())  {                            // data received from smartphone
    delay(2);                                                              //  <<mod XXXXXXXX
    cmd[0] =  Serial.read();  
    if(cmd[0] == STX)  {
      i=1;      
      while(Serial.available())  {
        delay(1);                                                          //  <<mod XXXXXXXX
        cmd[i] = Serial.read();
        if(cmd[i]>127 || i>5)                               break;   // Communication error << XXX Mod
        if((cmd[i]==ETX) && ((i==2 && cmd[1]>2) || i==5))   break;   // Button or Joystick data
        i++;
      }
      if     (i==2 && cmd[1]>48 && cmd[1]<68)     getButtonState(cmd[1]);                         // 3 Bytes
      else if(i==5 && cmd[1]<3  && cmd[3]<3 )     getJoystickState(cmd);                          // 6 Bytes
    }
  }
  sendBlueToothData();                                                       //  <<mod XXXXXXXX
//    delay(5);                                                              //  <<mod XXXXXXXX
// your code here
}