Skip to content

Instantly share code, notes, and snippets.

@bsatrom
Last active July 13, 2018 18:23
Show Gist options
  • Save bsatrom/3a123f461235e232dc7e75203a5d8216 to your computer and use it in GitHub Desktop.
Save bsatrom/3a123f461235e232dc7e75203a5d8216 to your computer and use it in GitHub Desktop.
Mirror the onboard RGB LED on Particle devices to an external RGB LED
// Automatically mirror the onboard RGB LED (Photon, Electron) to an external RGB LED
// LEDMirror.cpp
// Class set-up
class LEDMirror {
public:
LEDMirror(pin_t r, pin_t g, pin_t b) : pin_r(r), pin_g(g), pin_b(b) {
pinMode(pin_r, OUTPUT);
pinMode(pin_g, OUTPUT);
pinMode(pin_b, OUTPUT);
RGB.onChange(&LEDMirror::handler, this);
}
void handler(uint8_t r, uint8_t g, uint8_t b) {
analogWrite(pin_r, 255 - r);
analogWrite(pin_g, 255 - g);
analogWrite(pin_b, 255 - b);
}
private:
pin_t pin_r;
pin_t pin_g;
pin_t pin_b;
};
// myProject.ino
// Example Usage: Connect an external RGB LED to D0, D1 and D2 (R, G, and B) and place this in your user firmware
LEDMirror myLED(D0, D1, D2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment