Library for multi-pin RGB leds that encapsulates three PwmOut objects.
Revision 3:327cc9243faf, committed 2017-03-28
- Comitter:
- j3
- Date:
- Tue Mar 28 23:24:17 2017 +0000
- Parent:
- 2:ecd3840bb6a7
- Commit message:
- added toggle fx
Changed in this revision
MultipinRGB.cpp | Show annotated file Show diff for this revision Revisions of this file |
MultipinRGB.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MultipinRGB.cpp Tue Mar 28 22:33:51 2017 +0000 +++ b/MultipinRGB.cpp Tue Mar 28 23:24:17 2017 +0000 @@ -33,20 +33,18 @@ //turn off all leds, use default period if(m_ledActiveState == MultipinRGB::ActiveLow) { - m_redDc = 1.0F; - m_grnDc = 1.0F; - m_bluDc = 1.0F; + m_red.write(1.0F); + m_green.write(1.0F); + m_blue.write(1.0F); } else { - m_redDc = 0.0F; - m_grnDc = 0.0F; - m_bluDc = 0.0F; + m_red.write(0.0F); + m_green.write(0.0F); + m_blue.write(0.0F); } - m_red.write(m_redDc); - m_green.write(m_grnDc); - m_blue.write(m_bluDc); + } @@ -63,18 +61,21 @@ switch(led) { case(Red): - m_redDc = dc; - (m_ledActiveState == MultipinRGB::ActiveLow) ? m_red.write(1.0F - m_redDc) : m_red.write(m_redDc); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + m_red.write(1.0F - dc) : m_red.write(dc); break; case(Green): - m_grnDc = dc; - (m_ledActiveState == MultipinRGB::ActiveLow) ? m_green.write(1.0F - m_grnDc) : m_green.write(m_grnDc); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + m_green.write(1.0F - dc) : m_green.write(dc); break; case(Blue): - m_bluDc = dc; - (m_ledActiveState == MultipinRGB::ActiveLow) ? m_blue.write(1.0F - m_bluDc) : m_blue.write(m_bluDc); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + m_blue.write(1.0F - dc) : m_blue.write(dc); break; default: @@ -89,20 +90,16 @@ { if(m_ledActiveState == MultipinRGB::ActiveLow) { - m_redDc = (1.0F - r); - m_grnDc = (1.0F - g); - m_bluDc = (1.0F - b); + m_red.write(1.0F - r); + m_green.write(1.0F - g); + m_blue.write(1.0F - b); } else { - m_redDc = r; - m_grnDc = g; - m_bluDc = b; + m_red.write(r); + m_green.write(g); + m_blue.write(b); } - - m_red.write(m_redDc); - m_green.write(m_grnDc); - m_blue.write(m_bluDc); } @@ -113,15 +110,21 @@ switch(led) { case(Red): - (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_red.read())) : (rtnVal = m_red.read()); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + (rtnVal = (1.0F - m_red.read())) : (rtnVal = m_red.read()); break; case(Green): - (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_green.read())) : (rtnVal = m_green.read()); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + (rtnVal = (1.0F - m_green.read())) : (rtnVal = m_green.read()); break; case(Blue): - (m_ledActiveState == MultipinRGB::ActiveLow) ? (rtnVal = (1.0F - m_blue.read())) : (rtnVal = m_blue.read()); + //if + (m_ledActiveState == MultipinRGB::ActiveLow) ? + (rtnVal = (1.0F - m_blue.read())) : (rtnVal = m_blue.read()); break; default: @@ -152,6 +155,64 @@ /*****************************************************************************/ +void MultipinRGB::toggleLed(const Led_e led) +{ + float currentDc, newDc; + + switch(led) + { + case(Red): + currentDc = m_red.read(); + break; + + case(Green): + currentDc = m_green.read(); + break; + + case(Blue): + currentDc = m_blue.read(); + break; + + default: + mbed_die(); + break; + }; + + if(currentDc >= 1.0F) + { + newDc = 0.0F; + } + else if(currentDc <= 0.0F) + { + newDc = 1.0F; + } + else + { + newDc = (m_ledActiveState == MultipinRGB::ActiveLow) ? 1.0F : 0.0F; + } + + switch(led) + { + case(Red): + m_red.write(newDc); + break; + + case(Green): + m_green.write(newDc); + break; + + case(Blue): + m_blue.write(newDc); + break; + + default: + mbed_die(); + break; + }; +} + + +/*****************************************************************************/ void MultipinRGB::setPeriod(const float p) { m_red.period(p);
--- a/MultipinRGB.h Tue Mar 28 22:33:51 2017 +0000 +++ b/MultipinRGB.h Tue Mar 28 23:24:17 2017 +0000 @@ -95,10 +95,11 @@ ///Destructor ~MultipinRGB(); - ///@brief Sets duty cycle for red led.\n + ///@brief Sets duty cycle for led.\n /// ///On Entry: - ///@param[in] r - Duty cycle for led, 0.0 to 1.0 + ///@param[in] led - Led to update + ///@param[in] dc - Duty cycle for led, 0.0 to 1.0 /// ///On Exit: ///@param[out] none @@ -119,10 +120,10 @@ ///@returns none void writeLeds(const float r, const float g, const float b); - ///@brief Reads duty cycle for red led.\n + ///@brief Reads duty cycle for led.\n /// ///On Entry: - ///@param[in] none + ///@param[in] led - Led to update /// ///On Exit: ///@param[out] none @@ -145,6 +146,18 @@ ///@returns none void readLeds(float& r, float& g, float& b); + ///@brief Toggles led from off to on, or on to off.\n + ///Duty Cycle will be 0% or 100% after this call.\n + /// + ///On Entry: + ///@param[in] led - Led to toggle + /// + ///On Exit: + ///@param[out] none + /// + ///@returns none + void toggleLed(const Led_e led); + ///@brief Sets pwm period for all three leds.\n /// ///On Entry: @@ -162,8 +175,6 @@ PwmOut m_green; PwmOut m_blue; - float m_redDc, m_grnDc, m_bluDc; - LedLogic_e m_ledActiveState; };