考慮下面的函數串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