Library for multi-pin RGB leds that encapsulates three PwmOut objects.

Dependents:   MAX32630FTHR_RGB

Revision:
0:434f7d7a11d8
Child:
1:ce9726c385f1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MultipinRGB.h	Tue Mar 28 20:22:56 2017 +0000
@@ -0,0 +1,216 @@
+/******************************************************************************
+* MIT License
+*
+* Copyright (c) 2017 Justin J. Jordan
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+ 
+* The above copyright notice and this permission notice shall be included in all
+* copies or substantial portions of the Software.
+ 
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+* SOFTWARE.
+******************************************************************************/
+
+
+#ifndef _MULTIPINRGB_H_
+#define _MULTIPINRGB_H_
+
+#include "mbed.h"
+
+
+/**
+* @brief MutlipinRGB Library
+*
+* @details Library for multi-pin RGB leds that encapsulates three PwmOut objects 
+* and provides access to the floating point read/write mbr fxs of the PwmOut 
+* objects.\n\n
+* Duty cycles should always be written as 0.0F for off, and 1.0F as 100% on,
+* regardless of led active state.\n\n
+* Duty cycles are reported in the same manner.\n\n 
+*
+* @code
+* #include "mbed.h"
+* #include "MultipinRGB.h"
+*
+*  int main ()
+*  {
+*    MultipinRGB leds(LED1, LED2, LED3);
+*    float redDutyCycle(0.5F), grnDutyCycle(0.0F), bluDutyCycle(0.0F), temp;
+*    
+*    while(1)
+*    {
+*      leds.writeRGB(redDutyCycle, grnDutyCycle, bluDutyCycle);
+*
+*      printf("RGB Duty Cycles = %3.1f, %3.1f, %3.1f\r\n", 
+*              redDutyCycle, grnDutyCycle, bluDutyCycle);
+*
+*      //shift r->g->b->r
+*      temp = bluDutyCycle;
+*      bluDutyCycle = grnDutyCycle;
+*      grnDutyCycle = redDutyCycle;
+*      redDutyCycle = temp;
+*      
+*      wait(0.25);
+*    }
+*  }
+* @endcode
+*
+*/
+class MultipinRGB
+{
+    public:
+    
+    enum LedLogic_e
+    {
+        ActiveLow,
+        ActiveHigh
+    };
+    
+    ///Constructor
+    ///@param[in] red - Pin that red led is connected to.
+    ///@param[in] green - Pin that green led is connected to.
+    ///@param[in] blue - Pin that blue led is connected to.
+    ///@param[in] activeState - Active state of all three leds.
+    MultipinRGB(PinName red, PinName green, PinName blue, LedLogic_e 
+    activeState = ActiveLow);
+    
+    ///Destructor
+    ~MultipinRGB();
+    
+    ///@brief Sets duty cycle for red led.\n
+    ///
+    ///On Entry:
+    ///@param[in] r - Duty cycle for led, 0.0 to 1.0 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    void writeRed(const float r);
+    
+    ///@brief Sets duty cycle for green led.\n
+    ///
+    ///On Entry:
+    ///@param[in] g - Duty cycle for led, 0.0 to 1.0 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    void writeGreen(const float g);
+    
+    ///@brief Sets duty cycle for blue led.\n
+    ///
+    ///On Entry:
+    ///@param[in] b - Duty cycle for led, 0.0 to 1.0  
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    void writeBlue(const float b);
+    
+    ///@brief Sets duty cycle for all three leds.\n
+    ///
+    ///On Entry:
+    ///@param[in] r - Duty cycle for led, 0.0 to 1.0  
+    ///@param[in] g - Duty cycle for led, 0.0 to 1.0  
+    ///@param[in] b - Duty cycle for led, 0.0 to 1.0  
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    void writeRGB(const float r, const float g, const float b);
+    
+    ///@brief Sets pwm period for all three leds.\n
+    ///
+    ///On Entry:
+    ///@param[in] p - PWM period in seconds 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns none
+    void setPeriod(const float p);
+    
+    ///@brief Reads duty cycle for red led.\n
+    ///
+    ///On Entry:
+    ///@param[in] none 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns Current duty cycle for led, 0.0 to 1.0  
+    float readRed();
+    
+    ///@brief Reads duty cycle for green led.\n
+    ///
+    ///On Entry:
+    ///@param[in] none 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns Current duty cycle for led, 0.0 to 1.0  
+    float readGreen();
+    
+    ///@brief Reads duty cycle for blue led.\n
+    ///
+    ///On Entry:
+    ///@param[in] none 
+    ///
+    ///On Exit:
+    ///@param[out] none
+    ///
+    ///@returns Current duty cycle for led, 0.0 to 1.0  
+    float readBlue();
+    
+    ///@brief Reads duty cycle for all three leds.\n
+    ///
+    ///On Entry:
+    ///@param[in] r - float reference for red led duty cycle.
+    ///@param[in] g - float reference for green led duty cycle.
+    ///@param[in] b - float reference for blue led duty cycle.
+    ///
+    ///On Exit:
+    ///@param[out] r - Current duty cycle for led, 0.0 to 1.0   
+    ///@param[out] g - Current duty cycle for led, 0.0 to 1.0   
+    ///@param[out] b - Current duty cycle for led, 0.0 to 1.0   
+    ///
+    ///@returns none
+    void  readRGB(float& r, float& g, float& b);
+    
+    private:
+    
+    PwmOut m_red;
+    PwmOut m_green;
+    PwmOut m_blue;
+    
+    LedLogic_e m_ledActiveState;
+};
+
+
+///@brief fx documentation template.\n
+///
+///On Entry:
+///@param[in] none 
+///
+///On Exit:
+///@param[out] none
+///
+///@returns none
+
+#endif /*_MULTIPINRGB_H_ */
\ No newline at end of file