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
Hotboards 0:ea0715867677 8 #include "Hotboards_leds.h"
Hotboards 0:ea0715867677 9
Hotboards 0:ea0715867677 10 #define bitRead( var, bit ) (((var) >> (bit)) & 0x01)
Hotboards 0:ea0715867677 11 #define bitWrite( var, bit, val ) (val) ? (var) |= (1<<(bit)) : (var) &= ~(1<<(bit))
Hotboards 0:ea0715867677 12
Hotboards 0:ea0715867677 13 Hotboards_leds::Hotboards_leds( PinName led0, bool on )
Hotboards 0:ea0715867677 14 {
Hotboards 0:ea0715867677 15 _leds = 1;
Hotboards 0:ea0715867677 16 _state = 0;
Hotboards 0:ea0715867677 17 _on = on;
Hotboards 0:ea0715867677 18 begin( 0, led0 );
Hotboards 0:ea0715867677 19 }
Hotboards 0:ea0715867677 20
Hotboards 0:ea0715867677 21 Hotboards_leds::Hotboards_leds( PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 22 {
Hotboards 0:ea0715867677 23 _leds = 2;
Hotboards 0:ea0715867677 24 _state = 0;
Hotboards 0:ea0715867677 25 _on = on;
Hotboards 0:ea0715867677 26 begin( 0, led0 );
Hotboards 0:ea0715867677 27 begin( 1, led1 );
Hotboards 0:ea0715867677 28 }
Hotboards 0:ea0715867677 29
Hotboards 0:ea0715867677 30 Hotboards_leds::Hotboards_leds( PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 31 {
Hotboards 0:ea0715867677 32 _leds = 3;
Hotboards 0:ea0715867677 33 _state = 0;
Hotboards 0:ea0715867677 34 _on = on;
Hotboards 0:ea0715867677 35 begin( 0, led0 );
Hotboards 0:ea0715867677 36 begin( 1, led1 );
Hotboards 0:ea0715867677 37 begin( 2, led2 );
Hotboards 0:ea0715867677 38 }
Hotboards 0:ea0715867677 39
Hotboards 0:ea0715867677 40 Hotboards_leds::Hotboards_leds( PinName led3, PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 41 {
Hotboards 0:ea0715867677 42 _leds = 4;
Hotboards 0:ea0715867677 43 _state = 0;
Hotboards 0:ea0715867677 44 _on = on;
Hotboards 0:ea0715867677 45 begin( 0, led0 );
Hotboards 0:ea0715867677 46 begin( 1, led1 );
Hotboards 0:ea0715867677 47 begin( 2, led2 );
Hotboards 0:ea0715867677 48 begin( 3, led3 );
Hotboards 0:ea0715867677 49 }
Hotboards 0:ea0715867677 50
Hotboards 0:ea0715867677 51 Hotboards_leds::Hotboards_leds( PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 52 {
Hotboards 0:ea0715867677 53 _leds = 5;
Hotboards 0:ea0715867677 54 _state = 0;
Hotboards 0:ea0715867677 55 _on = on;
Hotboards 0:ea0715867677 56 begin( 0, led0 );
Hotboards 0:ea0715867677 57 begin( 1, led1 );
Hotboards 0:ea0715867677 58 begin( 2, led2 );
Hotboards 0:ea0715867677 59 begin( 3, led3 );
Hotboards 0:ea0715867677 60 begin( 4, led4 );
Hotboards 0:ea0715867677 61 }
Hotboards 0:ea0715867677 62
Hotboards 0:ea0715867677 63 Hotboards_leds::Hotboards_leds( PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 64 {
Hotboards 0:ea0715867677 65 _leds = 6;
Hotboards 0:ea0715867677 66 _state = 0;
Hotboards 0:ea0715867677 67 _on = on;
Hotboards 0:ea0715867677 68 begin( 0, led0 );
Hotboards 0:ea0715867677 69 begin( 1, led1 );
Hotboards 0:ea0715867677 70 begin( 2, led2 );
Hotboards 0:ea0715867677 71 begin( 3, led3 );
Hotboards 0:ea0715867677 72 begin( 4, led4 );
Hotboards 0:ea0715867677 73 begin( 5, led5 );
Hotboards 0:ea0715867677 74 }
Hotboards 0:ea0715867677 75
Hotboards 0:ea0715867677 76 Hotboards_leds::Hotboards_leds( PinName led6, PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 77 {
Hotboards 0:ea0715867677 78 _leds = 7;
Hotboards 0:ea0715867677 79 _state = 0;
Hotboards 0:ea0715867677 80 _on = on;
Hotboards 0:ea0715867677 81 begin( 0, led0 );
Hotboards 0:ea0715867677 82 begin( 1, led1 );
Hotboards 0:ea0715867677 83 begin( 2, led2 );
Hotboards 0:ea0715867677 84 begin( 3, led3 );
Hotboards 0:ea0715867677 85 begin( 4, led4 );
Hotboards 0:ea0715867677 86 begin( 5, led5 );
Hotboards 0:ea0715867677 87 begin( 6, led6 );
Hotboards 0:ea0715867677 88 }
Hotboards 0:ea0715867677 89
Hotboards 0:ea0715867677 90 Hotboards_leds::Hotboards_leds( PinName led7, PinName led6, PinName led5, PinName led4, PinName led3, PinName led2, PinName led1, PinName led0, bool on )
Hotboards 0:ea0715867677 91 {
Hotboards 0:ea0715867677 92 _leds = 8;
Hotboards 0:ea0715867677 93 _state = 0;
Hotboards 0:ea0715867677 94 _on = on;
Hotboards 0:ea0715867677 95 begin( 0, led0 );
Hotboards 0:ea0715867677 96 begin( 1, led1 );
Hotboards 0:ea0715867677 97 begin( 2, led2 );
Hotboards 0:ea0715867677 98 begin( 3, led3 );
Hotboards 0:ea0715867677 99 begin( 4, led4 );
Hotboards 0:ea0715867677 100 begin( 5, led5 );
Hotboards 0:ea0715867677 101 begin( 6, led6 );
Hotboards 0:ea0715867677 102 begin( 7, led7 );
Hotboards 0:ea0715867677 103 }
Hotboards 0:ea0715867677 104
Hotboards 0:ea0715867677 105 void Hotboards_leds::turnOn( uint8_t led )
Hotboards 0:ea0715867677 106 {
Hotboards 0:ea0715867677 107 bitWrite( _state, led, _on );
Hotboards 0:ea0715867677 108 _pin[ led ]->write( bitRead( _state, led ) );
Hotboards 0:ea0715867677 109 //_pin[ led ]->write( 1 );
Hotboards 0:ea0715867677 110 }
Hotboards 0:ea0715867677 111 void Hotboards_leds::turnOff( uint8_t led )
Hotboards 0:ea0715867677 112 {
Hotboards 0:ea0715867677 113 bitWrite( _state, led, !_on );
Hotboards 0:ea0715867677 114 _pin[ led ]->write( bitRead( _state, led ) );
Hotboards 0:ea0715867677 115 //_pin[ led ]->write( 0 );
Hotboards 0:ea0715867677 116 }
Hotboards 0:ea0715867677 117
Hotboards 0:ea0715867677 118 void Hotboards_leds::toggle( uint8_t led )
Hotboards 0:ea0715867677 119 {
Hotboards 0:ea0715867677 120 if( bitRead( _state, led ) == _on )
Hotboards 0:ea0715867677 121 {
Hotboards 0:ea0715867677 122 turnOff( led );
Hotboards 0:ea0715867677 123 }
Hotboards 0:ea0715867677 124 else
Hotboards 0:ea0715867677 125 {
Hotboards 0:ea0715867677 126 turnOn( led );
Hotboards 0:ea0715867677 127 }
Hotboards 0:ea0715867677 128 }
Hotboards 0:ea0715867677 129
Hotboards 0:ea0715867677 130 uint8_t Hotboards_leds::read( uint8_t led )
Hotboards 0:ea0715867677 131 {
Hotboards 0:ea0715867677 132 uint8_t i;
Hotboards 0:ea0715867677 133 uint8_t val = 0;
Hotboards 0:ea0715867677 134
Hotboards 0:ea0715867677 135 if( led == 0xff )
Hotboards 0:ea0715867677 136 {
Hotboards 0:ea0715867677 137 for( i=0 ; i<_leds ; i++ )
Hotboards 0:ea0715867677 138 {
Hotboards 0:ea0715867677 139 bitWrite( val, i, bitRead( _state, i ) );
Hotboards 0:ea0715867677 140 }
Hotboards 0:ea0715867677 141 }
Hotboards 0:ea0715867677 142 else
Hotboards 0:ea0715867677 143 {
Hotboards 0:ea0715867677 144 val = bitRead( _state, led );
Hotboards 0:ea0715867677 145 }
Hotboards 0:ea0715867677 146 return val;
Hotboards 0:ea0715867677 147 }
Hotboards 0:ea0715867677 148
Hotboards 0:ea0715867677 149 void Hotboards_leds::write( uint8_t val )
Hotboards 0:ea0715867677 150 {
Hotboards 0:ea0715867677 151 uint8_t i;
Hotboards 0:ea0715867677 152 for( i=0 ; i<_leds ; i++ )
Hotboards 0:ea0715867677 153 {
Hotboards 0:ea0715867677 154 if( bitRead( val, i ) )
Hotboards 0:ea0715867677 155 {
Hotboards 0:ea0715867677 156 turnOn( i );
Hotboards 0:ea0715867677 157 }
Hotboards 0:ea0715867677 158 else
Hotboards 0:ea0715867677 159 {
Hotboards 0:ea0715867677 160 turnOff( i );
Hotboards 0:ea0715867677 161 }
Hotboards 0:ea0715867677 162 }
Hotboards 0:ea0715867677 163 }
Hotboards 0:ea0715867677 164
Hotboards 0:ea0715867677 165 void Hotboards_leds::begin( uint8_t led, PinName pin )
Hotboards 0:ea0715867677 166 {
Hotboards 0:ea0715867677 167 // set the digital outpout that holds the led
Hotboards 0:ea0715867677 168 _pin[ led ] = new DigitalOut( pin );
Hotboards 0:ea0715867677 169 // turn off led (as initial state)
Hotboards 0:ea0715867677 170 bitWrite( _state, led, !_on );
Hotboards 0:ea0715867677 171 _pin[ led ]->write( !_on );
Hotboards 0:ea0715867677 172 }