Skip to content

Instantly share code, notes, and snippets.

@ttseng
Created November 1, 2019 03:10
Show Gist options
  • Save ttseng/9e4be52216f48282ab454e1c511810a6 to your computer and use it in GitHub Desktop.
Save ttseng/9e4be52216f48282ab454e1c511810a6 to your computer and use it in GitHub Desktop.
uint16_t getFillColor(uint8_t r, uint8_t g, uint8_t b)
{
return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}
void loop()
{
M5.update();
// get the latest color
colorHttp.begin("https://m5stack.glitch.me/getColor");
int colorHttpCode = colorHttp.GET();
if(colorHttpCode > 0){
if(colorHttpCode == HTTP_CODE_OK){
String rgb = colorHttp.getString();
M5.Lcd.setTextSize(3);
M5.Lcd.println("GET " + rgb);
M5.Lcd.println("rgb");
M5.Lcd.println(rgb);
// this returns a string in the format [r, g, b];
// extra out the r g b components
int rEnd = rgb.indexOf(",");
M5.Lcd.println("rEnd " + rEnd);
int gEnd = rgb.lastIndexOf(",");
M5.Lcd.println("gEnd " + gEnd);
int r = rgb.substring(1, rEnd).toInt();
int g = rgb.substring(rEnd+1, gEnd).toInt();
int b = rgb.substring(gEnd+1, rgb.length()-1).toInt();
M5.Lcd.print("red ");
M5.Lcd.print(r);
M5.Lcd.print(" green ");
M5.Lcd.print(g);
M5.Lcd.print(" blue ");
M5.Lcd.print(b);
// convert type to uint16_t color
uint16_t color = getFillColor(r, g, b);
M5.Lcd.println("color ");
M5.Lcd.println(color);
// fill the m5Stack display with the color
M5.Lcd.fillScreen(color);
}
}else{
M5.Lcd.println(colorHttp.errorToString(colorHttpCode).c_str());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment