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

Dependents:   MAX32630FTHR_RGB

Committer:
j3
Date:
Tue Mar 28 20:26:46 2017 +0000
Revision:
1:ce9726c385f1
Parent:
0:434f7d7a11d8
Child:
2:ecd3840bb6a7
fixed whitespace offset in docs

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 1:ce9726c385f1 53 * leds.writeRGB(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 0:434f7d7a11d8 74 enum LedLogic_e
j3 0:434f7d7a11d8 75 {
j3 0:434f7d7a11d8 76 ActiveLow,
j3 0:434f7d7a11d8 77 ActiveHigh
j3 0:434f7d7a11d8 78 };
j3 0:434f7d7a11d8 79
j3 0:434f7d7a11d8 80 ///Constructor
j3 0:434f7d7a11d8 81 ///@param[in] red - Pin that red led is connected to.
j3 0:434f7d7a11d8 82 ///@param[in] green - Pin that green led is connected to.
j3 0:434f7d7a11d8 83 ///@param[in] blue - Pin that blue led is connected to.
j3 0:434f7d7a11d8 84 ///@param[in] activeState - Active state of all three leds.
j3 0:434f7d7a11d8 85 MultipinRGB(PinName red, PinName green, PinName blue, LedLogic_e
j3 0:434f7d7a11d8 86 activeState = ActiveLow);
j3 0:434f7d7a11d8 87
j3 0:434f7d7a11d8 88 ///Destructor
j3 0:434f7d7a11d8 89 ~MultipinRGB();
j3 0:434f7d7a11d8 90
j3 0:434f7d7a11d8 91 ///@brief Sets duty cycle for red led.\n
j3 0:434f7d7a11d8 92 ///
j3 0:434f7d7a11d8 93 ///On Entry:
j3 0:434f7d7a11d8 94 ///@param[in] r - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 95 ///
j3 0:434f7d7a11d8 96 ///On Exit:
j3 0:434f7d7a11d8 97 ///@param[out] none
j3 0:434f7d7a11d8 98 ///
j3 0:434f7d7a11d8 99 ///@returns none
j3 0:434f7d7a11d8 100 void writeRed(const float r);
j3 0:434f7d7a11d8 101
j3 0:434f7d7a11d8 102 ///@brief Sets duty cycle for green led.\n
j3 0:434f7d7a11d8 103 ///
j3 0:434f7d7a11d8 104 ///On Entry:
j3 0:434f7d7a11d8 105 ///@param[in] g - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 106 ///
j3 0:434f7d7a11d8 107 ///On Exit:
j3 0:434f7d7a11d8 108 ///@param[out] none
j3 0:434f7d7a11d8 109 ///
j3 0:434f7d7a11d8 110 ///@returns none
j3 0:434f7d7a11d8 111 void writeGreen(const float g);
j3 0:434f7d7a11d8 112
j3 0:434f7d7a11d8 113 ///@brief Sets duty cycle for blue led.\n
j3 0:434f7d7a11d8 114 ///
j3 0:434f7d7a11d8 115 ///On Entry:
j3 0:434f7d7a11d8 116 ///@param[in] b - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 117 ///
j3 0:434f7d7a11d8 118 ///On Exit:
j3 0:434f7d7a11d8 119 ///@param[out] none
j3 0:434f7d7a11d8 120 ///
j3 0:434f7d7a11d8 121 ///@returns none
j3 0:434f7d7a11d8 122 void writeBlue(const float b);
j3 0:434f7d7a11d8 123
j3 0:434f7d7a11d8 124 ///@brief Sets duty cycle for all three leds.\n
j3 0:434f7d7a11d8 125 ///
j3 0:434f7d7a11d8 126 ///On Entry:
j3 0:434f7d7a11d8 127 ///@param[in] r - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 128 ///@param[in] g - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 129 ///@param[in] b - Duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 130 ///
j3 0:434f7d7a11d8 131 ///On Exit:
j3 0:434f7d7a11d8 132 ///@param[out] none
j3 0:434f7d7a11d8 133 ///
j3 0:434f7d7a11d8 134 ///@returns none
j3 0:434f7d7a11d8 135 void writeRGB(const float r, const float g, const float b);
j3 0:434f7d7a11d8 136
j3 0:434f7d7a11d8 137 ///@brief Sets pwm period for all three leds.\n
j3 0:434f7d7a11d8 138 ///
j3 0:434f7d7a11d8 139 ///On Entry:
j3 0:434f7d7a11d8 140 ///@param[in] p - PWM period in seconds
j3 0:434f7d7a11d8 141 ///
j3 0:434f7d7a11d8 142 ///On Exit:
j3 0:434f7d7a11d8 143 ///@param[out] none
j3 0:434f7d7a11d8 144 ///
j3 0:434f7d7a11d8 145 ///@returns none
j3 0:434f7d7a11d8 146 void setPeriod(const float p);
j3 0:434f7d7a11d8 147
j3 0:434f7d7a11d8 148 ///@brief Reads duty cycle for red led.\n
j3 0:434f7d7a11d8 149 ///
j3 0:434f7d7a11d8 150 ///On Entry:
j3 0:434f7d7a11d8 151 ///@param[in] none
j3 0:434f7d7a11d8 152 ///
j3 0:434f7d7a11d8 153 ///On Exit:
j3 0:434f7d7a11d8 154 ///@param[out] none
j3 0:434f7d7a11d8 155 ///
j3 0:434f7d7a11d8 156 ///@returns Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 157 float readRed();
j3 0:434f7d7a11d8 158
j3 0:434f7d7a11d8 159 ///@brief Reads duty cycle for green led.\n
j3 0:434f7d7a11d8 160 ///
j3 0:434f7d7a11d8 161 ///On Entry:
j3 0:434f7d7a11d8 162 ///@param[in] none
j3 0:434f7d7a11d8 163 ///
j3 0:434f7d7a11d8 164 ///On Exit:
j3 0:434f7d7a11d8 165 ///@param[out] none
j3 0:434f7d7a11d8 166 ///
j3 0:434f7d7a11d8 167 ///@returns Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 168 float readGreen();
j3 0:434f7d7a11d8 169
j3 0:434f7d7a11d8 170 ///@brief Reads duty cycle for blue led.\n
j3 0:434f7d7a11d8 171 ///
j3 0:434f7d7a11d8 172 ///On Entry:
j3 0:434f7d7a11d8 173 ///@param[in] none
j3 0:434f7d7a11d8 174 ///
j3 0:434f7d7a11d8 175 ///On Exit:
j3 0:434f7d7a11d8 176 ///@param[out] none
j3 0:434f7d7a11d8 177 ///
j3 0:434f7d7a11d8 178 ///@returns Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 179 float readBlue();
j3 0:434f7d7a11d8 180
j3 0:434f7d7a11d8 181 ///@brief Reads duty cycle for all three leds.\n
j3 0:434f7d7a11d8 182 ///
j3 0:434f7d7a11d8 183 ///On Entry:
j3 0:434f7d7a11d8 184 ///@param[in] r - float reference for red led duty cycle.
j3 0:434f7d7a11d8 185 ///@param[in] g - float reference for green led duty cycle.
j3 0:434f7d7a11d8 186 ///@param[in] b - float reference for blue led duty cycle.
j3 0:434f7d7a11d8 187 ///
j3 0:434f7d7a11d8 188 ///On Exit:
j3 0:434f7d7a11d8 189 ///@param[out] r - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 190 ///@param[out] g - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 191 ///@param[out] b - Current duty cycle for led, 0.0 to 1.0
j3 0:434f7d7a11d8 192 ///
j3 0:434f7d7a11d8 193 ///@returns none
j3 0:434f7d7a11d8 194 void readRGB(float& r, float& g, float& b);
j3 0:434f7d7a11d8 195
j3 0:434f7d7a11d8 196 private:
j3 0:434f7d7a11d8 197
j3 0:434f7d7a11d8 198 PwmOut m_red;
j3 0:434f7d7a11d8 199 PwmOut m_green;
j3 0:434f7d7a11d8 200 PwmOut m_blue;
j3 0:434f7d7a11d8 201
j3 0:434f7d7a11d8 202 LedLogic_e m_ledActiveState;
j3 0:434f7d7a11d8 203 };
j3 0:434f7d7a11d8 204
j3 0:434f7d7a11d8 205
j3 0:434f7d7a11d8 206 ///@brief fx documentation template.\n
j3 0:434f7d7a11d8 207 ///
j3 0:434f7d7a11d8 208 ///On Entry:
j3 0:434f7d7a11d8 209 ///@param[in] none
j3 0:434f7d7a11d8 210 ///
j3 0:434f7d7a11d8 211 ///On Exit:
j3 0:434f7d7a11d8 212 ///@param[out] none
j3 0:434f7d7a11d8 213 ///
j3 0:434f7d7a11d8 214 ///@returns none
j3 0:434f7d7a11d8 215
j3 0:434f7d7a11d8 216 #endif /*_MULTIPINRGB_H_ */