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

Dependents:   MAX32630FTHR_RGB

Committer:
j3
Date:
Tue Mar 28 22:33:51 2017 +0000
Revision:
2:ecd3840bb6a7
Parent:
1:ce9726c385f1
Child:
3:327cc9243faf
reduced number of fxs and added Led enum

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:434f7d7a11d8 1 /******************************************************************************
j3 0:434f7d7a11d8 2 * MIT License
j3 0:434f7d7a11d8 3 *
j3 0:434f7d7a11d8 4 * Copyright (c) 2017 Justin J. Jordan
j3 0:434f7d7a11d8 5 *
j3 0:434f7d7a11d8 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
j3 0:434f7d7a11d8 7 * of this software and associated documentation files (the "Software"), to deal
j3 0:434f7d7a11d8 8 * in the Software without restriction, including without limitation the rights
j3 0:434f7d7a11d8 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
j3 0:434f7d7a11d8 10 * copies of the Software, and to permit persons to whom the Software is
j3 0:434f7d7a11d8 11 * furnished to do so, subject to the following conditions:
j3 0:434f7d7a11d8 12
j3 0:434f7d7a11d8 13 * The above copyright notice and this permission notice shall be included in all
j3 0:434f7d7a11d8 14 * copies or substantial portions of the Software.
j3 0:434f7d7a11d8 15
j3 0:434f7d7a11d8 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
j3 0:434f7d7a11d8 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
j3 0:434f7d7a11d8 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
j3 0:434f7d7a11d8 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
j3 0:434f7d7a11d8 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
j3 0:434f7d7a11d8 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
j3 0:434f7d7a11d8 22 * SOFTWARE.
j3 0:434f7d7a11d8 23 ******************************************************************************/
j3 0:434f7d7a11d8 24
j3 0:434f7d7a11d8 25
j3 0:434f7d7a11d8 26 #ifndef _MULTIPINRGB_H_
j3 0:434f7d7a11d8 27 #define _MULTIPINRGB_H_
j3 0:434f7d7a11d8 28
j3 0:434f7d7a11d8 29 #include "mbed.h"
j3 0:434f7d7a11d8 30
j3 0:434f7d7a11d8 31
j3 0:434f7d7a11d8 32 /**
j3 0:434f7d7a11d8 33 * @brief MutlipinRGB Library
j3 0:434f7d7a11d8 34 *
j3 0:434f7d7a11d8 35 * @details Library for multi-pin RGB leds that encapsulates three PwmOut objects
j3 0:434f7d7a11d8 36 * and provides access to the floating point read/write mbr fxs of the PwmOut
j3 0:434f7d7a11d8 37 * objects.\n\n
j3 0:434f7d7a11d8 38 * Duty cycles should always be written as 0.0F for off, and 1.0F as 100% on,
j3 0:434f7d7a11d8 39 * regardless of led active state.\n\n
j3 0:434f7d7a11d8 40 * Duty cycles are reported in the same manner.\n\n
j3 0:434f7d7a11d8 41 *
j3 0:434f7d7a11d8 42 * @code
j3 0:434f7d7a11d8 43 * #include "mbed.h"
j3 0:434f7d7a11d8 44 * #include "MultipinRGB.h"
j3 0:434f7d7a11d8 45 *
j3 1:ce9726c385f1 46 * int main ()
j3 1:ce9726c385f1 47 * {
j3 1:ce9726c385f1 48 * MultipinRGB leds(LED1, LED2, LED3);
j3 1:ce9726c385f1 49 * float redDutyCycle(0.5F), grnDutyCycle(0.0F), bluDutyCycle(0.0F), temp;
j3 0:434f7d7a11d8 50 *
j3 1:ce9726c385f1 51 * while(1)
j3 1:ce9726c385f1 52 * {
j3 2:ecd3840bb6a7 53 * leds.writeLeds(redDutyCycle, grnDutyCycle, bluDutyCycle);
j3 0:434f7d7a11d8 54 *
j3 1:ce9726c385f1 55 * printf("RGB Duty Cycles = %3.1f, %3.1f, %3.1f\r\n",
j3 1:ce9726c385f1 56 * redDutyCycle, grnDutyCycle, bluDutyCycle);
j3 0:434f7d7a11d8 57 *
j3 1:ce9726c385f1 58 * //shift r->g->b->r
j3 1:ce9726c385f1 59 * temp = bluDutyCycle;
j3 1:ce9726c385f1 60 * bluDutyCycle = grnDutyCycle;
j3 1:ce9726c385f1 61 * grnDutyCycle = redDutyCycle;
j3 1:ce9726c385f1 62 * redDutyCycle = temp;
j3 0:434f7d7a11d8 63 *
j3 1:ce9726c385f1 64 * wait(0.25);
j3 1:ce9726c385f1 65 * }
j3 1:ce9726c385f1 66 * }
j3 0:434f7d7a11d8 67 * @endcode
j3 0:434f7d7a11d8 68 *
j3 0:434f7d7a11d8 69 */
j3 0:434f7d7a11d8 70 class MultipinRGB
j3 0:434f7d7a11d8 71 {
j3 0:434f7d7a11d8 72 public:
j3 0:434f7d7a11d8 73
j3 2:ecd3840bb6a7 74 enum Led_e
j3 2:ecd3840bb6a7 75 {
j3 2:ecd3840bb6a7 76 Red,
j3 2:ecd3840bb6a7 77 Green,
j3 2:ecd3840bb6a7 78 Blue
j3 2:ecd3840bb6a7 79 };
j3 2:ecd3840bb6a7 80
j3 0:434f7d7a11d8 81 enum LedLogic_e
j3 0:434f7d7a11d8 82 {
j3 0:434f7d7a11d8 83 ActiveLow,
j3 0:434f7d7a11d8 84 ActiveHigh
j3 0:434f7d7a11d8 85 };
j3 0:434f7d7a11d8 86
j3 0:434f7d7a11d8 87 ///Constructor
j3 0:434f7d7a11d8 88 ///@param[in] red - Pin that red led is connected to.
j3 0:434f7d7a11d8 89 ///@param[in] green - Pin that green led is connected to.
j3 0:434f7d7a11d8 90 ///@param[in] blue - Pin that blue led is connected to.
j3 0:434f7d7a11d8 91 ///@param[in] activeState - Active state of all three leds.
j3 0:434f7d7a11d8 92 MultipinRGB(PinName red, PinName green, PinName blue, LedLogic_e
j3 0:434f7d7a11d8 93 activeState = ActiveLow);
j3 0:434f7d7a11d8 94
j3 0:434f7d7a11d8 95 ///Destructor
j3 0:434f7d7a11d8 96 ~MultipinRGB();
j3 0:434f7d7a11d8 97
j3 0:434f7d7a11d8 98 ///@brief Sets duty cycle for red led.\n
j3 0:434f7d7a11d8 99 ///
j3 0:434f7d7a11d8 100 ///On Entry:
j3 0:434f7d7a11d8 101 ///@param[in] r - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 102 ///
j3 0:434f7d7a11d8 103 ///On Exit:
j3 0:434f7d7a11d8 104 ///@param[out] none
j3 0:434f7d7a11d8 105 ///
j3 0:434f7d7a11d8 106 ///@returns none
j3 2:ecd3840bb6a7 107 void writeLed(const Led_e led, const float dc);
j3 0:434f7d7a11d8 108
j3 0:434f7d7a11d8 109 ///@brief Sets duty cycle for all three leds.\n
j3 0:434f7d7a11d8 110 ///
j3 0:434f7d7a11d8 111 ///On Entry:
j3 0:434f7d7a11d8 112 ///@param[in] r - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 113 ///@param[in] g - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 114 ///@param[in] b - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 115 ///
j3 0:434f7d7a11d8 116 ///On Exit:
j3 0:434f7d7a11d8 117 ///@param[out] none
j3 0:434f7d7a11d8 118 ///
j3 0:434f7d7a11d8 119 ///@returns none
j3 2:ecd3840bb6a7 120 void writeLeds(const float r, const float g, const float b);
j3 0:434f7d7a11d8 121
j3 0:434f7d7a11d8 122 ///@brief Reads duty cycle for red led.\n
j3 0:434f7d7a11d8 123 ///
j3 0:434f7d7a11d8 124 ///On Entry:
j3 0:434f7d7a11d8 125 ///@param[in] none
j3 0:434f7d7a11d8 126 ///
j3 0:434f7d7a11d8 127 ///On Exit:
j3 0:434f7d7a11d8 128 ///@param[out] none
j3 0:434f7d7a11d8 129 ///
j3 0:434f7d7a11d8 130 ///@returns Current duty cycle for led, 0.0 to 1.0
j3 2:ecd3840bb6a7 131 float readLed(const Led_e led);
j3 0:434f7d7a11d8 132
j3 0:434f7d7a11d8 133 ///@brief Reads duty cycle for all three leds.\n
j3 0:434f7d7a11d8 134 ///
j3 0:434f7d7a11d8 135 ///On Entry:
j3 0:434f7d7a11d8 136 ///@param[in] r - float reference for red led duty cycle.
j3 0:434f7d7a11d8 137 ///@param[in] g - float reference for green led duty cycle.
j3 0:434f7d7a11d8 138 ///@param[in] b - float reference for blue led duty cycle.
j3 0:434f7d7a11d8 139 ///
j3 0:434f7d7a11d8 140 ///On Exit:
j3 0:434f7d7a11d8 141 ///@param[out] r - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 142 ///@param[out] g - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 143 ///@param[out] b - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 144 ///
j3 0:434f7d7a11d8 145 ///@returns none
j3 2:ecd3840bb6a7 146 void readLeds(float& r, float& g, float& b);
j3 2:ecd3840bb6a7 147
j3 2:ecd3840bb6a7 148 ///@brief Sets pwm period for all three leds.\n
j3 2:ecd3840bb6a7 149 ///
j3 2:ecd3840bb6a7 150 ///On Entry:
j3 2:ecd3840bb6a7 151 ///@param[in] p - PWM period in seconds
j3 2:ecd3840bb6a7 152 ///
j3 2:ecd3840bb6a7 153 ///On Exit:
j3 2:ecd3840bb6a7 154 ///@param[out] none
j3 2:ecd3840bb6a7 155 ///
j3 2:ecd3840bb6a7 156 ///@returns none
j3 2:ecd3840bb6a7 157 void setPeriod(const float p);
j3 0:434f7d7a11d8 158
j3 0:434f7d7a11d8 159 private:
j3 0:434f7d7a11d8 160
j3 0:434f7d7a11d8 161 PwmOut m_red;
j3 0:434f7d7a11d8 162 PwmOut m_green;
j3 0:434f7d7a11d8 163 PwmOut m_blue;
j3 0:434f7d7a11d8 164
j3 2:ecd3840bb6a7 165 float m_redDc, m_grnDc, m_bluDc;
j3 2:ecd3840bb6a7 166
j3 0:434f7d7a11d8 167 LedLogic_e m_ledActiveState;
j3 0:434f7d7a11d8 168 };
j3 0:434f7d7a11d8 169
j3 0:434f7d7a11d8 170
j3 0:434f7d7a11d8 171 ///@brief fx documentation template.\n
j3 0:434f7d7a11d8 172 ///
j3 0:434f7d7a11d8 173 ///On Entry:
j3 0:434f7d7a11d8 174 ///@param[in] none
j3 0:434f7d7a11d8 175 ///
j3 0:434f7d7a11d8 176 ///On Exit:
j3 0:434f7d7a11d8 177 ///@param[out] none
j3 0:434f7d7a11d8 178 ///
j3 0:434f7d7a11d8 179 ///@returns none
j3 0:434f7d7a11d8 180
j3 0:434f7d7a11d8 181 #endif /*_MULTIPINRGB_H_ */