Driver to control leds one at a time or in group

Dependents:   handling_leds leds toggle writing ... more

Committer:
Hotboards
Date:
Sat Feb 27 23:35:58 2016 +0000
Revision:
0:ea0715867677
first release , library complete, but is not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:ea0715867677 1 /*
Hotboards 0:ea0715867677 2 Hotboards_leds.cpp - Driver to control leds one at a time or in group
Hotboards 0:ea0715867677 3 Hotboards leds board (http://hotboards.org)
Hotboards 0:ea0715867677 4 Created by Diego Perez, January 16, 2016.
Hotboards 0:ea0715867677 5 Released into the public domain.
Hotboards 0:ea0715867677 6 */
Hotboards 0:ea0715867677 7 #ifndef Hotboards_leds_h
Hotboards 0:ea0715867677 8 #define Hotboards_leds_h
Hotboards 0:ea0715867677 9
Hotboards 0:ea0715867677 10 #include "mbed.h"
Hotboards 0:ea0715867677 11
Hotboards 0:ea0715867677 12 /** Hotboards_leds class.
Hotboards 0:ea0715867677 13 * Used to control general purpose leds
Hotboards 0:ea0715867677 14 *
Hotboards 0:ea0715867677 15 * Example:
Hotboards 0:ea0715867677 16 * @code
Hotboards 0:ea0715867677 17 * #include "mbed.h"
Hotboards 0:ea0715867677 18 * #include "Hotboards_leds.h"
Hotboards 0:ea0715867677 19 *
Hotboards 0:ea0715867677 20 * Hotboards_leds led( PB_5 );
Hotboards 0:ea0715867677 21 *
Hotboards 0:ea0715867677 22 * int main( void )
Hotboards 0:ea0715867677 23 * {
Hotboards 0:ea0715867677 24 * for(;;){
Hotboards 0:ea0715867677 25 * led.toggle( );
Hotboards 0:ea0715867677 26 * wait( 0.2 );
Hotboards 0:ea0715867677 27 * }
Hotboards 0:ea0715867677 28 * }
Hotboards 0:ea0715867677 29 * @endcode
Hotboards 0:ea0715867677 30 */
Hotboards 0:ea0715867677 31 class Hotboards_leds
Hotboards 0:ea0715867677 32 {
Hotboards 0:ea0715867677 33 public :
Hotboards 0:ea0715867677 34 /** Create Hotboards_leds instance for one led
Hotboards 0:ea0715867677 35 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 36 * @param on logic level to turn on the led
Hotboards 0:ea0715867677 37 *
Hotboards 0:ea0715867677 38 * Example:
Hotboards 0:ea0715867677 39 * @code
Hotboards 0:ea0715867677 40 * // instance one led on pin 5
Hotboards 0:ea0715867677 41 * Hotboards_leds led( 5 );
Hotboards 0:ea0715867677 42 * @endcode
Hotboards 0:ea0715867677 43 */
Hotboards 0:ea0715867677 44 Hotboards_leds( PinName led0, bool on=1 );
Hotboards 0:ea0715867677 45
Hotboards 0:ea0715867677 46 /** Create Hotboards_leds instance for two leds
Hotboards 0:ea0715867677 47 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 48 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 49 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 50 *
Hotboards 0:ea0715867677 51 * Example:
Hotboards 0:ea0715867677 52 * @code
Hotboards 0:ea0715867677 53 * // one instance with 2 leds on pins 5 and 6
Hotboards 0:ea0715867677 54 * Hotboards_leds led( 5, 6 );
Hotboards 0:ea0715867677 55 * @endcode
Hotboards 0:ea0715867677 56 */
Hotboards 0:ea0715867677 57 Hotboards_leds( PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 58
Hotboards 0:ea0715867677 59 /** Create Hotboards_leds instance for three leds
Hotboards 0:ea0715867677 60 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 61 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 62 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 63 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 64 *
Hotboards 0:ea0715867677 65 * Example:
Hotboards 0:ea0715867677 66 * @code
Hotboards 0:ea0715867677 67 * // one instance with 3 leds on pins 5, 6 and 7
Hotboards 0:ea0715867677 68 * Hotboards_leds led( 5, 6, 7 );
Hotboards 0:ea0715867677 69 * @endcode
Hotboards 0:ea0715867677 70 */
Hotboards 0:ea0715867677 71 Hotboards_leds( PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 72
Hotboards 0:ea0715867677 73 /** Create Hotboards_leds instance for four leds
Hotboards 0:ea0715867677 74 * @param led3 pin where the led 3 will be connected
Hotboards 0:ea0715867677 75 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 76 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 77 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 78 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 79 *
Hotboards 0:ea0715867677 80 * Example:
Hotboards 0:ea0715867677 81 * @code
Hotboards 0:ea0715867677 82 * // one instance with 4 leds on pins 5, 6, 7 and 8
Hotboards 0:ea0715867677 83 * Hotboards_leds led( 5, 6, 7, 8 );
Hotboards 0:ea0715867677 84 * @endcode
Hotboards 0:ea0715867677 85 */
Hotboards 0:ea0715867677 86 Hotboards_leds( PinName led3, PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 87
Hotboards 0:ea0715867677 88 /** Create Hotboards_leds instance for five leds
Hotboards 0:ea0715867677 89 * @param led4 pin where the led 4 will be connected
Hotboards 0:ea0715867677 90 * @param led3 pin where the led 3 will be connected
Hotboards 0:ea0715867677 91 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 92 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 93 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 94 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 95 *
Hotboards 0:ea0715867677 96 * Example:
Hotboards 0:ea0715867677 97 * @code
Hotboards 0:ea0715867677 98 * // one instance with 5 leds on pins 5, 6, 7, 8 and 9
Hotboards 0:ea0715867677 99 * Hotboards_leds led( 5, 6, 7, 8, 9 );
Hotboards 0:ea0715867677 100 * @endcode
Hotboards 0:ea0715867677 101 */
Hotboards 0:ea0715867677 102 Hotboards_leds( PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 103
Hotboards 0:ea0715867677 104 /** Create Hotboards_leds instance for six leds
Hotboards 0:ea0715867677 105 * @param led5 pin where the led 5 will be connected
Hotboards 0:ea0715867677 106 * @param led4 pin where the led 4 will be connected
Hotboards 0:ea0715867677 107 * @param led3 pin where the led 3 will be connected
Hotboards 0:ea0715867677 108 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 109 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 110 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 111 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 112 *
Hotboards 0:ea0715867677 113 * Example:
Hotboards 0:ea0715867677 114 * @code
Hotboards 0:ea0715867677 115 * // one instance with 6 leds on pins 5, 6, 7, 8, 9 and 10
Hotboards 0:ea0715867677 116 * Hotboards_leds led( 5, 6, 7, 8, 9, 10 );
Hotboards 0:ea0715867677 117 * @endcode
Hotboards 0:ea0715867677 118 */
Hotboards 0:ea0715867677 119 Hotboards_leds( PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 120
Hotboards 0:ea0715867677 121 /** Create Hotboards_leds instance for seven leds
Hotboards 0:ea0715867677 122 * @param led6 pin where the led 6 will be connected
Hotboards 0:ea0715867677 123 * @param led5 pin where the led 5 will be connected
Hotboards 0:ea0715867677 124 * @param led4 pin where the led 4 will be connected
Hotboards 0:ea0715867677 125 * @param led3 pin where the led 3 will be connected
Hotboards 0:ea0715867677 126 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 127 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 128 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 129 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 130 *
Hotboards 0:ea0715867677 131 * Example:
Hotboards 0:ea0715867677 132 * @code
Hotboards 0:ea0715867677 133 * // one instance with 7 leds on pins 5, 6, 7, 8, 9, 10 and 11
Hotboards 0:ea0715867677 134 * Hotboards_leds led( 5, 6, 7, 8, 9, 10, 11 );
Hotboards 0:ea0715867677 135 * @endcode
Hotboards 0:ea0715867677 136 */
Hotboards 0:ea0715867677 137 Hotboards_leds( PinName led6, PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 138
Hotboards 0:ea0715867677 139 /** Create Hotboards_leds instance for eight leds
Hotboards 0:ea0715867677 140 * @param led7 pin where the led 7 will be connected
Hotboards 0:ea0715867677 141 * @param led6 pin where the led 6 will be connected
Hotboards 0:ea0715867677 142 * @param led5 pin where the led 5 will be connected
Hotboards 0:ea0715867677 143 * @param led4 pin where the led 4 will be connected
Hotboards 0:ea0715867677 144 * @param led3 pin where the led 3 will be connected
Hotboards 0:ea0715867677 145 * @param led2 pin where the led 2 will be connected
Hotboards 0:ea0715867677 146 * @param led1 pin where the led 1 will be connected
Hotboards 0:ea0715867677 147 * @param led0 pin where the led 0 will be connected
Hotboards 0:ea0715867677 148 * @param on logic level to turn on the leds
Hotboards 0:ea0715867677 149 *
Hotboards 0:ea0715867677 150 * Example:
Hotboards 0:ea0715867677 151 * @code
Hotboards 0:ea0715867677 152 * // one instance with 8 leds on pins 5, 6, 7, 8, 9, 10, 11 and 12
Hotboards 0:ea0715867677 153 * Hotboards_leds led( 5, 6, 7, 8, 9, 10, 11, 12 );
Hotboards 0:ea0715867677 154 * @endcode
Hotboards 0:ea0715867677 155 */
Hotboards 0:ea0715867677 156 Hotboards_leds( PinName led7, PinName led6, PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on=1 );
Hotboards 0:ea0715867677 157
Hotboards 0:ea0715867677 158 /** Turn on a single led
Hotboards 0:ea0715867677 159 * @param led led number to be turn on
Hotboards 0:ea0715867677 160 *
Hotboards 0:ea0715867677 161 * Example:
Hotboards 0:ea0715867677 162 * @code
Hotboards 0:ea0715867677 163 * // instance one led on pin 5 and turn it on
Hotboards 0:ea0715867677 164 * Hotboards_leds led( 5 );
Hotboards 0:ea0715867677 165 * led.turnOn( );
Hotboards 0:ea0715867677 166 *
Hotboards 0:ea0715867677 167 * // instance a bus with 4 leds
Hotboards 0:ea0715867677 168 * Hotboards_leds leds( 8, 7, 6, 5 );
Hotboards 0:ea0715867677 169 * // turn on led 2 on pin 7
Hotboards 0:ea0715867677 170 * leds.turnOn( 2 );
Hotboards 0:ea0715867677 171 * // turn on led 0 on pin 5
Hotboards 0:ea0715867677 172 * leds.turnOn( 0 );
Hotboards 0:ea0715867677 173 * @endcode
Hotboards 0:ea0715867677 174 */
Hotboards 0:ea0715867677 175 void turnOn( uint8_t led=0 );
Hotboards 0:ea0715867677 176
Hotboards 0:ea0715867677 177 /** Turn off a single led
Hotboards 0:ea0715867677 178 * @param led led number to be turn off
Hotboards 0:ea0715867677 179 *
Hotboards 0:ea0715867677 180 * Example:
Hotboards 0:ea0715867677 181 * @code
Hotboards 0:ea0715867677 182 * // instance one led on pin 5 and turn it off
Hotboards 0:ea0715867677 183 * Hotboards_leds led( 5 );
Hotboards 0:ea0715867677 184 * led.turnOff( );
Hotboards 0:ea0715867677 185 *
Hotboards 0:ea0715867677 186 * // instance a bus with 4 leds
Hotboards 0:ea0715867677 187 * Hotboards_leds leds( 8, 7, 6, 5 );
Hotboards 0:ea0715867677 188 * // turn off led 3 on pin 8
Hotboards 0:ea0715867677 189 * leds.turnOff( 3 );
Hotboards 0:ea0715867677 190 * // turn off led 0 on pin 5
Hotboards 0:ea0715867677 191 * leds.turnOff( 0 );
Hotboards 0:ea0715867677 192 * @endcode
Hotboards 0:ea0715867677 193 */
Hotboards 0:ea0715867677 194 void turnOff( uint8_t led=0 );
Hotboards 0:ea0715867677 195
Hotboards 0:ea0715867677 196 /** Toggle a single led
Hotboards 0:ea0715867677 197 * @param led led number to be toggled
Hotboards 0:ea0715867677 198 *
Hotboards 0:ea0715867677 199 * Example:
Hotboards 0:ea0715867677 200 * @code
Hotboards 0:ea0715867677 201 * // instance one led on pin 7 and toggle it
Hotboards 0:ea0715867677 202 * Hotboards_leds led( 7 );
Hotboards 0:ea0715867677 203 * led.toggle( );
Hotboards 0:ea0715867677 204 *
Hotboards 0:ea0715867677 205 * // instance a bus with 3 leds
Hotboards 0:ea0715867677 206 * Hotboards_leds leds( 2, 3, 4 );
Hotboards 0:ea0715867677 207 * // toggle led 2 on pin 2
Hotboards 0:ea0715867677 208 * leds.toggle( 2 );
Hotboards 0:ea0715867677 209 * // toggle led 1 on pin 3
Hotboards 0:ea0715867677 210 * leds.toggle( 1 );
Hotboards 0:ea0715867677 211 * @endcode
Hotboards 0:ea0715867677 212 */
Hotboards 0:ea0715867677 213 void toggle( uint8_t led=0 );
Hotboards 0:ea0715867677 214
Hotboards 0:ea0715867677 215 /** Write to a single led or the entire bus
Hotboards 0:ea0715867677 216 * @param val value to be written
Hotboards 0:ea0715867677 217 *
Hotboards 0:ea0715867677 218 * Example:
Hotboards 0:ea0715867677 219 * @code
Hotboards 0:ea0715867677 220 * // instance one led on pin 7 and write a 1 (aceptara valores de 0 a 1)
Hotboards 0:ea0715867677 221 * Hotboards_leds led( 7 );
Hotboards 0:ea0715867677 222 * led.write( 1 );
Hotboards 0:ea0715867677 223 *
Hotboards 0:ea0715867677 224 * // instance a 4 leds bus (pin2->led3 ..... pin5->led0)
Hotboards 0:ea0715867677 225 * Hotboards_leds leds( 2, 3, 4, 5 );
Hotboards 0:ea0715867677 226 * // write the 10 value (it will accept values from 0 to 15)
Hotboards 0:ea0715867677 227 * leds.write( 10 );
Hotboards 0:ea0715867677 228 *
Hotboards 0:ea0715867677 229 * // instance an 8 leds bus (pin4->led7 ..... pin3->led0)
Hotboards 0:ea0715867677 230 * Hotboards_leds leds( 4, 3, 2, 6, 5, 7, 8, 9 );
Hotboards 0:ea0715867677 231 * // write the 134 value (it will accept values from 0 to 255)
Hotboards 0:ea0715867677 232 * leds.write( 134 );
Hotboards 0:ea0715867677 233 * @endcode
Hotboards 0:ea0715867677 234 */
Hotboards 0:ea0715867677 235 void write( uint8_t val );
Hotboards 0:ea0715867677 236
Hotboards 0:ea0715867677 237 /** Read a single led or the entire bus
Hotboards 0:ea0715867677 238 * @return led states
Hotboards 0:ea0715867677 239 *
Hotboards 0:ea0715867677 240 * Example:
Hotboards 0:ea0715867677 241 * @code
Hotboards 0:ea0715867677 242 * // instance on led on 7 and read its state (0 o 1)
Hotboards 0:ea0715867677 243 * Hotboards_leds led( 7 );
Hotboards 0:ea0715867677 244 * uint8_t var = led.read( );
Hotboards 0:ea0715867677 245 *
Hotboards 0:ea0715867677 246 * // instance an 8 leds bus (pin9->led7 ..... pin2->led0)
Hotboards 0:ea0715867677 247 * Hotboards_leds leds( 9, 8, 7, 6, 5, 4, 3, 2 );
Hotboards 0:ea0715867677 248 * // read the leds value (from 0 to 255)
Hotboards 0:ea0715867677 249 * uint8_t var = leds.read( );
Hotboards 0:ea0715867677 250 *
Hotboards 0:ea0715867677 251 * // nstance a 4 leds bus (pin2->led3 ..... pin5->led0)
Hotboards 0:ea0715867677 252 * Hotboards_leds leds( 2, 3, 4, 5 );
Hotboards 0:ea0715867677 253 * // read led 1 state (pin 4)
Hotboards 0:ea0715867677 254 * uint8_t val1 = leds.write( 1 );
Hotboards 0:ea0715867677 255 * // read led 0 state (pin 5)
Hotboards 0:ea0715867677 256 * uint8_t val2 = leds.write( 0 );
Hotboards 0:ea0715867677 257 * @endcode
Hotboards 0:ea0715867677 258 */
Hotboards 0:ea0715867677 259 uint8_t read( uint8_t led=0xff );
Hotboards 0:ea0715867677 260
Hotboards 0:ea0715867677 261 private :
Hotboards 0:ea0715867677 262 void begin( uint8_t led, PinName pin );
Hotboards 0:ea0715867677 263 DigitalOut *_pin[8];
Hotboards 0:ea0715867677 264 uint8_t _leds;
Hotboards 0:ea0715867677 265 uint8_t _state;
Hotboards 0:ea0715867677 266 bool _on;
Hotboards 0:ea0715867677 267 };
Hotboards 0:ea0715867677 268
Hotboards 0:ea0715867677 269 #endif