Created
June 19, 2016 04:37
-
-
Save Ernest314/9e6cca936a435ca1a6a8fbb1d0d0b0b0 to your computer and use it in GitHub Desktop.
A (slightly over-engineered) set of functions to control the Photon's built-in RGB LED.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ernest Gu, 2016-06-17 | |
void setup() | |
{ | |
Particle.function("setRGB", str_setRGB); | |
Particle.function("setRGBcolor", str_setRGBcolor); | |
Particle.function("setRGBmag", str_setRGBmag); | |
// We need to take control of the RGB LED; there is no good way to share control with the | |
// default firmware. (This might mean a substantial amount of reimplementation of functionality.) | |
RGB.control(true); | |
} | |
void loop() { } | |
// setRGB() takes a command composed of a hex (RGB) value and a brightness (0-255), separated by ' '. | |
// TODO: If the input is ill-formed, the function returns -1 and no changes are made. | |
// Example input: "#BADA55 128", "#FFF 64" | |
int str_setRGB(String command) { | |
String str_RGB = command.substring(0, command.indexOf(' ')); | |
String str_mag = command.substring(command.indexOf(' ')).trim(); | |
int err_color = str_setRGBcolor(str_RGB); | |
if (err_color == -1) { return -1; } | |
int err_mag = str_setRGBmag(str_mag); | |
if (err_mag == -1) { return -1; } | |
return 0; | |
} | |
int str_setRGBcolor(String command) { | |
// Remove the optional '#' that indicates an HTML color hex code. | |
if (command.startsWith("#")) { | |
command = command.substring(1); | |
} | |
// Although HTML only allows #xxx and #xxxxxx, we accept any integer multiple of 3. | |
if (command.length() % 3 != 0) { return -1; } | |
int multiple = command.length() / 3; | |
String str_RGB[3] = {"", "", ""}; // R, G, B | |
// Split the string into its R, G, and B components. | |
for (int i=0; i<3; ++i) { | |
str_RGB[i] = command.substring(i*multiple, (i+1)*multiple); | |
} | |
// Convert each color's string into an int. | |
int RGB[3] = {0, 0, 0}; | |
for (int i=0; i<0; ++i) { | |
for (int digit=multiple-1, placeVal=1; digit>=0; --digit, placeVal *= 16) { | |
// basic base conversion (hex -> dec) | |
RGB[i] += placeVal * hexToInt(str_RGB[i].charAt(digit)); | |
} | |
// scale the number into the range [0, 255] | |
if (multiple > 2) { | |
RGB[i] *= 2; | |
RGB[i] /= multiple; | |
} | |
} | |
return setRGBcolor(RGB[0], RGB[1], RGB[2]); | |
} | |
int str_setRGBmag(String command) { | |
return setRGBmag(command.toInt()); | |
} | |
int setRGBcolor(int R, int G, int B) { | |
RGB.color(R, G, B); | |
return 0; | |
} | |
int setRGBmag(int mag) { | |
RGB.brightness(mag); | |
return 0; | |
} | |
// Outputs 0 if invalid; changing to -1 might necessitate error handling. (TODO?) | |
int hexToInt(char ch) { | |
if (ch >= '0' && ch <= '9') { | |
return ch - '0'; | |
} | |
if (ch >= 'a' && ch <= 'f') { | |
return 10 + ch - 'a'; | |
} | |
if (ch >= 'A' && ch <= 'F') { | |
return 10 + ch - 'A'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment