I have to manage servos from a computer.
So I have to send manage messages from computer to Arduino. I need manage the number of servo and the corner. I\'m thinking
It's universal parser
struct servo
{
int iServoID;
int iAngle;
};
std::vector split(const std::string& str, const std::string& delim)
{
std::vector tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == std::string::npos) pos = str.length();
std::string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
std::vector getServoValues(const std::string& message)
{
std::vector servoList;
servo servoValue;
std::vector servoString;
std::vector values = split(message, ",");
for (const auto& v : values)
{
servoString.clear();
servoString = split(v, ";");
servoValue.iServoID = atoi(servoString[0].c_str()); //servoString[0].toInt();
servoValue.iAngle = atoi(servoString[1].c_str());// servoString[1].toInt();
servoList.emplace_back(servoValue);
}
return servoList;
}
to call:
std::string str = "1;233,2;123";
std::vector servos = getServoValues(str);
for (const auto & a : servos)
std::cout<
Result
1 233
2 123