考虑下面的函数串0 0 0
试图妥善字符串
虽然它对R:255,G:0,B:0,
或R:0,G:255,B:0,
正常工作。
int setColor(String command) {
//Parse the incoming command string
//Example command R:123,G:100,B:50,
//RGB values should be between 0 to 255
int red = getColorValue(command, "R:", "G");
int grn = getColorValue(command, "G:", "B");
int blu = getColorValue(command, "B:", ",");
// Set the color of the entire Neopixel ring.
uint16_t i;
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(red, grn, blu));
}
strip.show();
return 1;
}
int getColorValue(String command, String first, String second) {
int rgbValue;
String val = command.substring(command.indexOf(first)+2, command.indexOf(second));
val.trim();
rgbValue = val.toInt();
return rgbValue;
}
你正在使用什么'String'实现? – developerbmw
我们需要知道你正在使用的'String'类是如何工作来评估问题的。否则,我们只能猜测。是否有任何特定的原因,您没有使用C++标准库字符串实现? ('std :: string') – developerbmw
你应该通过引用传递所有字符串,或者更好的是,作为const引用。 – Lundin