Library for multi-pin RGB leds that encapsulates three PwmOut objects.
MultipinRGB.h@3:327cc9243faf, 2017-03-28 (annotated)
- Committer:
- j3
- Date:
- Tue Mar 28 23:24:17 2017 +0000
- Revision:
- 3:327cc9243faf
- Parent:
- 2:ecd3840bb6a7
added toggle fx
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:327cc9243faf | 98 | ///@brief Sets duty cycle for led.\n |
j3 | 0:434f7d7a11d8 | 99 | /// |
j3 | 0:434f7d7a11d8 | 100 | ///On Entry: |
j3 | 3:327cc9243faf | 101 | ///@param[in] led - Led to update |
j3 | 3:327cc9243faf | 102 | ///@param[in] dc - Duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 103 | /// |
j3 | 0:434f7d7a11d8 | 104 | ///On Exit: |
j3 | 0:434f7d7a11d8 | 105 | ///@param[out] none |
j3 | 0:434f7d7a11d8 | 106 | /// |
j3 | 0:434f7d7a11d8 | 107 | ///@returns none |
j3 | 2:ecd3840bb6a7 | 108 | void writeLed(const Led_e led, const float dc); |
j3 | 0:434f7d7a11d8 | 109 | |
j3 | 0:434f7d7a11d8 | 110 | ///@brief Sets duty cycle for all three leds.\n |
j3 | 0:434f7d7a11d8 | 111 | /// |
j3 | 0:434f7d7a11d8 | 112 | ///On Entry: |
j3 | 0:434f7d7a11d8 | 113 | ///@param[in] r - Duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 114 | ///@param[in] g - Duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 115 | ///@param[in] b - Duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 116 | /// |
j3 | 0:434f7d7a11d8 | 117 | ///On Exit: |
j3 | 0:434f7d7a11d8 | 118 | ///@param[out] none |
j3 | 0:434f7d7a11d8 | 119 | /// |
j3 | 0:434f7d7a11d8 | 120 | ///@returns none |
j3 | 2:ecd3840bb6a7 | 121 | void writeLeds(const float r, const float g, const float b); |
j3 | 0:434f7d7a11d8 | 122 | |
j3 | 3:327cc9243faf | 123 | ///@brief Reads duty cycle for led.\n |
j3 | 0:434f7d7a11d8 | 124 | /// |
j3 | 0:434f7d7a11d8 | 125 | ///On Entry: |
j3 | 3:327cc9243faf | 126 | ///@param[in] led - Led to update |
j3 | 0:434f7d7a11d8 | 127 | /// |
j3 | 0:434f7d7a11d8 | 128 | ///On Exit: |
j3 | 0:434f7d7a11d8 | 129 | ///@param[out] none |
j3 | 0:434f7d7a11d8 | 130 | /// |
j3 | 0:434f7d7a11d8 | 131 | ///@returns Current duty cycle for led, 0.0 to 1.0 |
j3 | 2:ecd3840bb6a7 | 132 | float readLed(const Led_e led); |
j3 | 0:434f7d7a11d8 | 133 | |
j3 | 0:434f7d7a11d8 | 134 | ///@brief Reads duty cycle for all three leds.\n |
j3 | 0:434f7d7a11d8 | 135 | /// |
j3 | 0:434f7d7a11d8 | 136 | ///On Entry: |
j3 | 0:434f7d7a11d8 | 137 | ///@param[in] r - float reference for red led duty cycle. |
j3 | 0:434f7d7a11d8 | 138 | ///@param[in] g - float reference for green led duty cycle. |
j3 | 0:434f7d7a11d8 | 139 | ///@param[in] b - float reference for blue led duty cycle. |
j3 | 0:434f7d7a11d8 | 140 | /// |
j3 | 0:434f7d7a11d8 | 141 | ///On Exit: |
j3 | 0:434f7d7a11d8 | 142 | ///@param[out] r - Current duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 143 | ///@param[out] g - Current duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 144 | ///@param[out] b - Current duty cycle for led, 0.0 to 1.0 |
j3 | 0:434f7d7a11d8 | 145 | /// |
j3 | 0:434f7d7a11d8 | 146 | ///@returns none |
j3 | 2:ecd3840bb6a7 | 147 | void readLeds(float& r, float& g, float& b); |
j3 | 2:ecd3840bb6a7 | 148 | |
j3 | 3:327cc9243faf | 149 | ///@brief Toggles led from off to on, or on to off.\n |
j3 | 3:327cc9243faf | 150 | ///Duty Cycle will be 0% or 100% after this call.\n |
j3 | 3:327cc9243faf | 151 | /// |
j3 | 3:327cc9243faf | 152 | ///On Entry: |
j3 | 3:327cc9243faf | 153 | ///@param[in] led - Led to toggle |
j3 | 3:327cc9243faf | 154 | /// |
j3 | 3:327cc9243faf | 155 | ///On Exit: |
j3 | 3:327cc9243faf | 156 | ///@param[out] none |
j3 | 3:327cc9243faf | 157 | /// |
j3 | 3:327cc9243faf | 158 | ///@returns none |
j3 | 3:327cc9243faf | 159 | void toggleLed(const Led_e led); |
j3 | 3:327cc9243faf | 160 | |
j3 | 2:ecd3840bb6a7 | 161 | ///@brief Sets pwm period for all three leds.\n |
j3 | 2:ecd3840bb6a7 | 162 | /// |
j3 | 2:ecd3840bb6a7 | 163 | ///On Entry: |
j3 | 2:ecd3840bb6a7 | 164 | ///@param[in] p - PWM period in seconds |
j3 | 2:ecd3840bb6a7 | 165 | /// |
j3 | 2:ecd3840bb6a7 | 166 | ///On Exit: |
j3 | 2:ecd3840bb6a7 | 167 | ///@param[out] none |
j3 | 2:ecd3840bb6a7 | 168 | /// |
j3 | 2:ecd3840bb6a7 | 169 | ///@returns none |
j3 | 2:ecd3840bb6a7 | 170 | void setPeriod(const float p); |
j3 | 0:434f7d7a11d8 | 171 | |
j3 | 0:434f7d7a11d8 | 172 | private: |
j3 | 0:434f7d7a11d8 | 173 | |
j3 | 0:434f7d7a11d8 | 174 | PwmOut m_red; |
j3 | 0:434f7d7a11d8 | 175 | PwmOut m_green; |
j3 | 0:434f7d7a11d8 | 176 | PwmOut m_blue; |
j3 | 0:434f7d7a11d8 | 177 | |
j3 | 0:434f7d7a11d8 | 178 | LedLogic_e m_ledActiveState; |
j3 | 0:434f7d7a11d8 | 179 | }; |
j3 | 0:434f7d7a11d8 | 180 | |
j3 | 0:434f7d7a11d8 | 181 | |
j3 | 0:434f7d7a11d8 | 182 | ///@brief fx documentation template.\n |
j3 | 0:434f7d7a11d8 | 183 | /// |
j3 | 0:434f7d7a11d8 | 184 | ///On Entry: |
j3 | 0:434f7d7a11d8 | 185 | ///@param[in] none |
j3 | 0:434f7d7a11d8 | 186 | /// |
j3 | 0:434f7d7a11d8 | 187 | ///On Exit: |
j3 | 0:434f7d7a11d8 | 188 | ///@param[out] none |
j3 | 0:434f7d7a11d8 | 189 | /// |
j3 | 0:434f7d7a11d8 | 190 | ///@returns none |
j3 | 0:434f7d7a11d8 | 191 | |
j3 | 0:434f7d7a11d8 | 192 | #endif /*_MULTIPINRGB_H_ */ |