Esempio n. 1
0
void BluetoothController::sendBlueToothData()  {
  static long previousMillis = 0;                             
  long currentMillis = millis();
  if(currentMillis - previousMillis > sendInterval) {   // send data back to smartphone
    previousMillis = currentMillis; 

    // Data frame transmitted back from Arduino to Android device:
    // < 0X02   Buttons state   0X01   DataField#1   0x04   DataField#2   0x05   DataField#3    0x03 >  
    // < 0X02      "01011"      0X01     "120.00"    0x04     "-4500"     0x05  "Motor enabled" 0x03 >    // example

    Serial2.print((char)STX);                                             // Start of Transmission
    Serial2.print(getButtonStatusString());  Serial2.print((char)0x1);    // buttons status feedback
    Serial2.print(getDataInt());             Serial2.print((char)0x4);    // datafield #1
    Serial2.print(getDataFloat());           Serial2.print((char)0x5);    // datafield #2
    Serial2.print(getDataString());                                       // datafield #3
    Serial2.print((char)ETX);                                             // End of Transmission
  }  
}
bool JsonActionResolver::resolve(std::string jsonMessage, ClientAction &action) {
  const char *json = jsonMessage.c_str();
  picojson::value v;
  std::string err;
  picojson::parse(v, json, json + strlen(json), &err);
  if (!err.empty()) {
    std::cout << "Error while parsing json: " << err << std::endl;
    return false;
  }
  if (!v.is<picojson::object>()) {
    std::cout << "JSON is not an object" << jsonMessage << std::endl;
    return false;
  }

  std::string message;
  if (!getString(v, "message", message)) {
    return false;
  }

  std::transform(message.begin(), message.end(), message.begin(), ::tolower);
  if (message == "start_tournament") {
    // Requires password
    if (!getDataString(v, "password", action)) return false;
    if (!getDataInt(v, "numberOfGames", action)) action.ints.insert(std::pair<std::string, int>("numberOfGames", 1));
    action.type = ClientAction::START_TOURNAMENT;
    return true;
  } else if (message == "end_tournament") {
    // Requires password
    if (!getDataString(v, "password", action)) return false;
    action.type = ClientAction::END_TOURNAMENT;
    return true;
  } else if (message == "end_game") {
    // Requires password
    if (!getDataString(v, "password", action)) return false;
    action.type = ClientAction::END_GAME;
    return true;
  } else if (message == "authenticate_admin") {
    // Requires password
    if (!getDataString(v, "password", action)) return false;
    action.type = ClientAction::AUTHENTICATE_ADMIN;
    return true;
  } else if (message == "update_settings") {
    // Requires password
    if (!getDataString(v, "password", action)) return false;
    getDataFloat(v, "wormWidth", action);
    getDataFloat(v, "wormSpeed", action);
    getDataFloat(v, "turnSpeed", action);
    getDataInt(v, "countdown", action);
    getDataInt(v, "minTimeBetweenGaps", action);
    getDataInt(v, "maxTimeBetweenGaps", action);
    getDataInt(v, "minTimeInGap", action);
    getDataInt(v, "maxTimeInGap", action);
    action.type = ClientAction::UPDATE_SETTINGS;
    return true;
  }
  else if (message == "register") {
    // Requires name
    if (!getDataString(v, "name", action)) return false;
    action.type = ClientAction::REGISTER;
		return true;
  } else if (message == "unregister") {
    action.type = ClientAction::UNREGISTER;
    return true;
  } else if (message == "start_moving") {
    action.type = ClientAction::START_MOVING;
		return true;
	} else if (message == "left_down") {
    action.type = ClientAction::LEFT_DOWN;
		return true;
	} else if (message == "left_up") {
    action.type = ClientAction::LEFT_UP;
		return true;
	} else if (message == "right_down") {
    action.type = ClientAction::RIGHT_DOWN;
		return true;
	} else if (message == "right_up") {
    action.type = ClientAction::RIGHT_UP;
		return true;
	}
	std::cout << "Message not recognized: " << message << std::endl;
	return false;
}