RGB LED connected to PCA9555
Revision 0:95a10b4db9ef, committed 2014-11-21
- Comitter:
- Reiko
- Date:
- Fri Nov 21 18:25:01 2014 +0000
- Commit message:
- RGB LED connected to PCA9555
Changed in this revision
rgb-led-pca9555.cpp | Show annotated file Show diff for this revision Revisions of this file |
rgb-led-pca9555.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 95a10b4db9ef rgb-led-pca9555.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgb-led-pca9555.cpp Fri Nov 21 18:25:01 2014 +0000 @@ -0,0 +1,95 @@ +#include "rgb-led-pca9555.h" + + +RgbLed::RgbLed (PCA9555 *ioExt, unsigned int redPin, unsigned int greenPin, unsigned int bluePin): + extIO(ioExt), ledR(redPin), ledG(greenPin), ledB(bluePin) + { + setColor(OFF); +} + +void RgbLed::toggle() { + extIO->togglePin(ledR); + extIO->togglePin(ledG); + extIO->togglePin(ledB); +} + +void RgbLed::setColor(Color color) { + switch (color) { + case RED: + extIO->clearPin(ledR); + extIO->setPin(ledG); + extIO->setPin(ledB); + break; + case GREEN: + extIO->setPin(ledR); + extIO->clearPin(ledG); + extIO->setPin(ledB); + break; + case BLUE: + extIO->setPin(ledR); + extIO->setPin(ledG); + extIO->clearPin(ledB); + break; + case CYAN: + extIO->setPin(ledR); + extIO->clearPin(ledG); + extIO->clearPin(ledB); + break; + case MAGENTA: + extIO->clearPin(ledR); + extIO->setPin(ledG); + extIO->clearPin(ledB); + break; + case YELLOW: + extIO->clearPin(ledR); + extIO->clearPin(ledG); + extIO->setPin(ledB); + break; + case WHITE: + extIO->clearPin(ledR); + extIO->clearPin(ledG); + extIO->clearPin(ledB); + break; + case OFF: + extIO->setPin(ledR); + extIO->setPin(ledG); + extIO->setPin(ledB); + break; + } +} + +void RgbLed::setRed(bool ledState) { + if (ledState) { + extIO->clearPin(ledR); + } else { + extIO->setPin(ledR); + } +} + +void RgbLed::setGreen(bool ledState) { + if (ledState) { + extIO->clearPin(ledG); + } else { + extIO->setPin(ledG); + } +} + +void RgbLed::setBlue(bool ledState) { + if (ledState) { + extIO->clearPin(ledB); + } else { + extIO->setPin(ledB); + } +} + +void RgbLed::toggleRed() { + extIO->togglePin(ledR); +} + +void RgbLed::toggleGreen() { + extIO->togglePin(ledG); +} + +void RgbLed::toggleBlue() { + extIO->togglePin(ledB); +} \ No newline at end of file
diff -r 000000000000 -r 95a10b4db9ef rgb-led-pca9555.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgb-led-pca9555.h Fri Nov 21 18:25:01 2014 +0000 @@ -0,0 +1,28 @@ +#ifndef RGBLEDPCA9555_H +#define RGBLEDPCA9555_H + +#include "mbed.h" +#include "PCA9555.h" + +class RgbLed { +public: + RgbLed(PCA9555 *ioExt, unsigned int redPin, unsigned int greenPin, unsigned int bluePin); + + enum Color {RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, OFF}; + void setColor(Color color); + void setRed(bool ledState); + void setGreen(bool ledState); + void setBlue(bool ledState); + void toggleRed(); + void toggleGreen(); + void toggleBlue(); + void toggle(); + +private: + PCA9555 *extIO; + unsigned int ledR; + unsigned int ledG; + unsigned int ledB; +}; + +#endif \ No newline at end of file