Driver to read switches, one at a time or an entire dip-sw

Dependents:   input dip_switch dipsw_change input ... more

Committer:
Hotboards
Date:
Tue Mar 01 03:40:45 2016 +0000
Revision:
1:dfb1302f847d
Parent:
0:8e569e04a0fb
read pin method corrected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:8e569e04a0fb 1 /*
Hotboards 0:8e569e04a0fb 2 Hotboards_switches.h - Driver to read interrupts
Hotboards 0:8e569e04a0fb 3 Hotboards Dip-switch board (http://hotboards.org)
Hotboards 0:8e569e04a0fb 4 Created by Diego Perez, January 16, 2016.
Hotboards 0:8e569e04a0fb 5 Released into the public domain.
Hotboards 0:8e569e04a0fb 6 */
Hotboards 0:8e569e04a0fb 7 #ifndef Hotboards_switches_h
Hotboards 0:8e569e04a0fb 8 #define Hotboards_switches_h
Hotboards 0:8e569e04a0fb 9
Hotboards 0:8e569e04a0fb 10 #include "mbed.h"
Hotboards 0:8e569e04a0fb 11
Hotboards 0:8e569e04a0fb 12 /** Hotboards_switches class.
Hotboards 0:8e569e04a0fb 13 * Used to read general purpose dip switches
Hotboards 0:8e569e04a0fb 14 *
Hotboards 0:8e569e04a0fb 15 * Example:
Hotboards 0:8e569e04a0fb 16 * @code
Hotboards 0:8e569e04a0fb 17 * #include "Hotboards_switches.h"
Hotboards 0:8e569e04a0fb 18 *
Hotboards 0:8e569e04a0fb 19 * Hotboards_switches sw( 5 );
Hotboards 0:8e569e04a0fb 20 *
Hotboards 0:8e569e04a0fb 21 * int main( void )
Hotboards 0:8e569e04a0fb 22 * {
Hotboards 0:8e569e04a0fb 23 * for(;;){
Hotboards 0:8e569e04a0fb 24 * bool var = sw.read( );
Hotboards 0:8e569e04a0fb 25 * wait( 0.2 );
Hotboards 0:8e569e04a0fb 26 * }
Hotboards 0:8e569e04a0fb 27 * }
Hotboards 0:8e569e04a0fb 28 * @endcode
Hotboards 0:8e569e04a0fb 29 */
Hotboards 0:8e569e04a0fb 30 class Hotboards_switches
Hotboards 0:8e569e04a0fb 31 {
Hotboards 0:8e569e04a0fb 32 public :
Hotboards 0:8e569e04a0fb 33 /** Create Hotboards_switches instance for one sw
Hotboards 0:8e569e04a0fb 34 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 35 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 36 *
Hotboards 0:8e569e04a0fb 37 * Example:
Hotboards 0:8e569e04a0fb 38 * @code
Hotboards 0:8e569e04a0fb 39 * // instance one sw on pin 5
Hotboards 0:8e569e04a0fb 40 * Hotboards_switches sw( 5 );
Hotboards 0:8e569e04a0fb 41 * @endcode
Hotboards 0:8e569e04a0fb 42 */
Hotboards 0:8e569e04a0fb 43 Hotboards_switches( PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 44
Hotboards 0:8e569e04a0fb 45 /** Create Hotboards_switches instance for two sw
Hotboards 0:8e569e04a0fb 46 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 47 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 48 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 49 *
Hotboards 0:8e569e04a0fb 50 * Example:
Hotboards 0:8e569e04a0fb 51 * @code
Hotboards 0:8e569e04a0fb 52 * // instance one dip-sw with 2 sw on pin 5 and 6
Hotboards 0:8e569e04a0fb 53 * Hotboards_switches sw( 5, 6 );
Hotboards 0:8e569e04a0fb 54 * @endcode
Hotboards 0:8e569e04a0fb 55 */
Hotboards 0:8e569e04a0fb 56 Hotboards_switches( PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 57
Hotboards 0:8e569e04a0fb 58 /** Create Hotboards_switches instance for three sw
Hotboards 0:8e569e04a0fb 59 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 60 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 61 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 62 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 63 *
Hotboards 0:8e569e04a0fb 64 * Example:
Hotboards 0:8e569e04a0fb 65 * @code
Hotboards 0:8e569e04a0fb 66 * // instance one dip-sw with 3 sw on pin 5, 6 and 7
Hotboards 0:8e569e04a0fb 67 * Hotboards_switches sw( 5, 6, 7 );
Hotboards 0:8e569e04a0fb 68 * @endcode
Hotboards 0:8e569e04a0fb 69 */
Hotboards 0:8e569e04a0fb 70 Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 71
Hotboards 0:8e569e04a0fb 72 /** Create Hotboards_switches instance for four sw
Hotboards 0:8e569e04a0fb 73 * @param sw3 pin where the sw 3 will be read it
Hotboards 0:8e569e04a0fb 74 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 75 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 76 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 77 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 78 *
Hotboards 0:8e569e04a0fb 79 * Example:
Hotboards 0:8e569e04a0fb 80 * @code
Hotboards 0:8e569e04a0fb 81 * // instance one dip-sw with 4 sw on pin 5, 6, 7 and 8
Hotboards 0:8e569e04a0fb 82 * Hotboards_switches sw( 5, 6, 7, 8 );
Hotboards 0:8e569e04a0fb 83 * @endcode
Hotboards 0:8e569e04a0fb 84 */
Hotboards 0:8e569e04a0fb 85 Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 86
Hotboards 0:8e569e04a0fb 87 /** Create Hotboards_switches instance for five sw
Hotboards 0:8e569e04a0fb 88 * @param sw4 pin where the sw 4 will be read it
Hotboards 0:8e569e04a0fb 89 * @param sw3 pin where the sw 3 will be read it
Hotboards 0:8e569e04a0fb 90 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 91 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 92 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 93 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 94 *
Hotboards 0:8e569e04a0fb 95 * Example:
Hotboards 0:8e569e04a0fb 96 * @code
Hotboards 0:8e569e04a0fb 97 * // instance one dip-sw with 5 sw on pin 5, 6, 7, 8 and 9
Hotboards 0:8e569e04a0fb 98 * Hotboards_switches sw( 5, 6, 7, 8, 9 );
Hotboards 0:8e569e04a0fb 99 * @endcode
Hotboards 0:8e569e04a0fb 100 */
Hotboards 0:8e569e04a0fb 101 Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 102
Hotboards 0:8e569e04a0fb 103 /** Create Hotboards_switches instance for six sw
Hotboards 0:8e569e04a0fb 104 * @param sw5 pin where the sw 5 will be read it
Hotboards 0:8e569e04a0fb 105 * @param sw4 pin where the sw 4 will be read it
Hotboards 0:8e569e04a0fb 106 * @param sw3 pin where the sw 3 will be read it
Hotboards 0:8e569e04a0fb 107 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 108 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 109 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 110 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 111 *
Hotboards 0:8e569e04a0fb 112 * Example:
Hotboards 0:8e569e04a0fb 113 * @code
Hotboards 0:8e569e04a0fb 114 * // instance one dip-sw with 6 sw on pin 5, 6, 7, 8, 9 and 10
Hotboards 0:8e569e04a0fb 115 * Hotboards_switches sw( 5, 6, 7, 8, 9, 10 );
Hotboards 0:8e569e04a0fb 116 * @endcode
Hotboards 0:8e569e04a0fb 117 */
Hotboards 0:8e569e04a0fb 118 Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 119
Hotboards 0:8e569e04a0fb 120 /** Create Hotboards_switches instance for seven sw
Hotboards 0:8e569e04a0fb 121 * @param sw6 pin where the sw 6 will be read it
Hotboards 0:8e569e04a0fb 122 * @param sw5 pin where the sw 5 will be read it
Hotboards 0:8e569e04a0fb 123 * @param sw4 pin where the sw 4 will be read it
Hotboards 0:8e569e04a0fb 124 * @param sw3 pin where the sw 3 will be read it
Hotboards 0:8e569e04a0fb 125 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 126 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 127 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 128 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 129 *
Hotboards 0:8e569e04a0fb 130 * Example:
Hotboards 0:8e569e04a0fb 131 * @code
Hotboards 0:8e569e04a0fb 132 * // instance one dip-sw with 7 sw on pin 5, 6, 7, 8, 9, 10 and 11
Hotboards 0:8e569e04a0fb 133 * Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11 );
Hotboards 0:8e569e04a0fb 134 * @endcode
Hotboards 0:8e569e04a0fb 135 */
Hotboards 0:8e569e04a0fb 136 Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0);
Hotboards 0:8e569e04a0fb 137
Hotboards 0:8e569e04a0fb 138 /** Create Hotboards_switches instance for eight sw
Hotboards 0:8e569e04a0fb 139 * @param sw7 pin where the sw 7 will be read it
Hotboards 0:8e569e04a0fb 140 * @param sw6 pin where the sw 6 will be read it
Hotboards 0:8e569e04a0fb 141 * @param sw5 pin where the sw 5 will be read it
Hotboards 0:8e569e04a0fb 142 * @param sw4 pin where the sw 4 will be read it
Hotboards 0:8e569e04a0fb 143 * @param sw3 pin where the sw 3 will be read it
Hotboards 0:8e569e04a0fb 144 * @param sw2 pin where the sw 2 will be read it
Hotboards 0:8e569e04a0fb 145 * @param sw1 pin where the sw 1 will be read it
Hotboards 0:8e569e04a0fb 146 * @param sw0 pin where the sw 0 will be read it
Hotboards 0:8e569e04a0fb 147 * @param close logic level that gives you when the sw is closed
Hotboards 0:8e569e04a0fb 148 *
Hotboards 0:8e569e04a0fb 149 * Example:
Hotboards 0:8e569e04a0fb 150 * @code
Hotboards 0:8e569e04a0fb 151 * // instance one dip-sw with 8 sw on pin 5, 6, 7, 8, 9, 10, 11 and 12
Hotboards 0:8e569e04a0fb 152 * Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11, 12 );
Hotboards 0:8e569e04a0fb 153 * @endcode
Hotboards 0:8e569e04a0fb 154 */
Hotboards 0:8e569e04a0fb 155 Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
Hotboards 0:8e569e04a0fb 156
Hotboards 0:8e569e04a0fb 157 /** Read a single sw or the entire Dip-switch state (open=0 or close=1)
Hotboards 0:8e569e04a0fb 158 * @return sw state(s)
Hotboards 0:8e569e04a0fb 159 *
Hotboards 0:8e569e04a0fb 160 * Example:
Hotboards 0:8e569e04a0fb 161 * @code
Hotboards 0:8e569e04a0fb 162 * // instance one sw on pin 7 and read its state (0 o 1)
Hotboards 0:8e569e04a0fb 163 * Hotboards_leds sw ( 7 );
Hotboards 0:8e569e04a0fb 164 * bool val = sw.read( );
Hotboards 0:8e569e04a0fb 165 *
Hotboards 0:8e569e04a0fb 166 * // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
Hotboards 0:8e569e04a0fb 167 * Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
Hotboards 0:8e569e04a0fb 168 * // read the sw values (from 0 to 255)
Hotboards 0:8e569e04a0fb 169 * uint8_t val = dipsw.read( );
Hotboards 0:8e569e04a0fb 170 *
Hotboards 0:8e569e04a0fb 171 * // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
Hotboards 0:8e569e04a0fb 172 * Hotboards_switches dipsw( 2, 3, 4, 5 );
Hotboards 0:8e569e04a0fb 173 * // read sw 1 state (pin 4)
Hotboards 0:8e569e04a0fb 174 * bool val1 = dipsw.write( 1 );
Hotboards 0:8e569e04a0fb 175 * // read sw 0 state (pin 5)
Hotboards 0:8e569e04a0fb 176 * bool val2 = dipsw.write( 0 );
Hotboards 0:8e569e04a0fb 177 * @endcode
Hotboards 0:8e569e04a0fb 178 */
Hotboards 0:8e569e04a0fb 179 uint8_t read( uint8_t sw=0xff );
Hotboards 0:8e569e04a0fb 180
Hotboards 0:8e569e04a0fb 181 /** Tells you if one or more sw has been change
Hotboards 0:8e569e04a0fb 182 * @return '1' if not changed
Hotboards 0:8e569e04a0fb 183 *
Hotboards 0:8e569e04a0fb 184 * Example:
Hotboards 0:8e569e04a0fb 185 * @code
Hotboards 0:8e569e04a0fb 186 * // instance one sw on pin 7 and read its state (0 o 1)
Hotboards 0:8e569e04a0fb 187 * Hotboards_leds sw ( 7 );
Hotboards 0:8e569e04a0fb 188 * if( sw.hasItChange( ))
Hotboards 0:8e569e04a0fb 189 * bool val = sw.read( );
Hotboards 0:8e569e04a0fb 190 *
Hotboards 0:8e569e04a0fb 191 * // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
Hotboards 0:8e569e04a0fb 192 * Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
Hotboards 0:8e569e04a0fb 193 * // read the sw values (from 0 to 255) if it changes
Hotboards 0:8e569e04a0fb 194 * if( dipsw.hasItChange( ) )
Hotboards 0:8e569e04a0fb 195 * uint8_t val = dipsw.read( );
Hotboards 0:8e569e04a0fb 196 *
Hotboards 0:8e569e04a0fb 197 * // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
Hotboards 0:8e569e04a0fb 198 * Hotboards_switches dipsw( 2, 3, 4, 5 );
Hotboards 0:8e569e04a0fb 199 * // read sw 1 state (pin 4) if it changes
Hotboards 0:8e569e04a0fb 200 * if( sw.hasItChange( 1 ))
Hotboards 0:8e569e04a0fb 201 * bool val = sw.read( 1 );
Hotboards 0:8e569e04a0fb 202 * @endcode
Hotboards 0:8e569e04a0fb 203 */
Hotboards 0:8e569e04a0fb 204 bool hasItChange( uint8_t sw=0xff );
Hotboards 0:8e569e04a0fb 205
Hotboards 0:8e569e04a0fb 206 private :
Hotboards 0:8e569e04a0fb 207 void begin( uint8_t sw, PinName pin );
Hotboards 0:8e569e04a0fb 208 bool readSw( uint8_t sw );
Hotboards 0:8e569e04a0fb 209 bool hasItChangeSw( uint8_t sw );
Hotboards 0:8e569e04a0fb 210 DigitalIn *_sw[ 8 ];
Hotboards 0:8e569e04a0fb 211 uint8_t _sws;
Hotboards 0:8e569e04a0fb 212 uint8_t _lastSwState;
Hotboards 0:8e569e04a0fb 213 bool _close;
Hotboards 0:8e569e04a0fb 214 };
Hotboards 0:8e569e04a0fb 215
Hotboards 0:8e569e04a0fb 216 #endif