Hotboards MX / Hotboards_switches

Dependents:   input dip_switch dipsw_change input ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hotboards_switches.h Source File

Hotboards_switches.h

00001 /*
00002   Hotboards_switches.h - Driver to read interrupts
00003   Hotboards Dip-switch board (http://hotboards.org)
00004   Created by Diego Perez, January 16, 2016.
00005   Released into the public domain.
00006 */
00007 #ifndef Hotboards_switches_h
00008 #define Hotboards_switches_h
00009 
00010 #include "mbed.h"
00011 
00012 /** Hotboards_switches class.
00013  *  Used to read general purpose dip switches
00014  *
00015  * Example:
00016  * @code
00017  * #include "Hotboards_switches.h"
00018  *
00019  * Hotboards_switches sw( 5 );
00020  *
00021  * int main( void )
00022  * {
00023  *     for(;;){
00024  *         bool var = sw.read( );
00025  *         wait( 0.2 );
00026  *     }
00027  * }
00028  * @endcode
00029  */
00030 class Hotboards_switches
00031 {
00032     public :
00033         /** Create Hotboards_switches instance for one sw
00034           * @param sw0 pin where the sw 0 will be read it
00035           * @param close logic level that gives you when the sw is closed
00036           *
00037           * Example:
00038           * @code
00039           *   // instance one sw on pin 5
00040           *   Hotboards_switches sw( 5 );
00041           * @endcode
00042           */
00043         Hotboards_switches( PinName sw0, bool close=0 );
00044 
00045         /** Create Hotboards_switches instance for two sw
00046           * @param sw1 pin where the sw 1 will be read it
00047           * @param sw0 pin where the sw 0 will be read it
00048           * @param close logic level that gives you when the sw is closed
00049           *
00050           * Example:
00051           * @code
00052           *   // instance one dip-sw with 2 sw on pin 5 and 6
00053           *   Hotboards_switches sw( 5, 6 );
00054           * @endcode
00055           */
00056         Hotboards_switches( PinName sw1, PinName sw0, bool close=0 );
00057 
00058         /** Create Hotboards_switches instance for three sw
00059           * @param sw2 pin where the sw 2 will be read it
00060           * @param sw1 pin where the sw 1 will be read it
00061           * @param sw0 pin where the sw 0 will be read it
00062           * @param close logic level that gives you when the sw is closed
00063           *
00064           * Example:
00065           * @code
00066           *   // instance one dip-sw with 3 sw on pin 5, 6 and 7
00067           *   Hotboards_switches sw( 5, 6, 7 );
00068           * @endcode
00069           */
00070         Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close=0 );
00071 
00072         /** Create Hotboards_switches instance for four sw
00073           * @param sw3 pin where the sw 3 will be read it
00074           * @param sw2 pin where the sw 2 will be read it
00075           * @param sw1 pin where the sw 1 will be read it
00076           * @param sw0 pin where the sw 0 will be read it
00077           * @param close logic level that gives you when the sw is closed
00078           *
00079           * Example:
00080           * @code
00081           *   // instance one dip-sw with 4 sw on pin 5, 6, 7 and 8
00082           *   Hotboards_switches sw( 5, 6, 7, 8 );
00083           * @endcode
00084           */
00085         Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
00086 
00087         /** Create Hotboards_switches instance for five sw
00088           * @param sw4 pin where the sw 4 will be read it
00089           * @param sw3 pin where the sw 3 will be read it
00090           * @param sw2 pin where the sw 2 will be read it
00091           * @param sw1 pin where the sw 1 will be read it
00092           * @param sw0 pin where the sw 0 will be read it
00093           * @param close logic level that gives you when the sw is closed
00094           *
00095           * Example:
00096           * @code
00097           *   // instance one dip-sw with 5 sw on pin 5, 6, 7, 8 and 9
00098           *   Hotboards_switches sw( 5, 6, 7, 8, 9 );
00099           * @endcode
00100           */
00101         Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
00102 
00103         /** Create Hotboards_switches instance for six sw
00104           * @param sw5 pin where the sw 5 will be read it
00105           * @param sw4 pin where the sw 4 will be read it
00106           * @param sw3 pin where the sw 3 will be read it
00107           * @param sw2 pin where the sw 2 will be read it
00108           * @param sw1 pin where the sw 1 will be read it
00109           * @param sw0 pin where the sw 0 will be read it
00110           * @param close logic level that gives you when the sw is closed
00111           *
00112           * Example:
00113           * @code
00114           *   // instance one dip-sw with 6 sw on pin 5, 6, 7, 8, 9 and 10
00115           *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10 );
00116           * @endcode
00117           */
00118         Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
00119 
00120         /** Create Hotboards_switches instance for seven sw
00121           * @param sw6 pin where the sw 6 will be read it
00122           * @param sw5 pin where the sw 5 will be read it
00123           * @param sw4 pin where the sw 4 will be read it
00124           * @param sw3 pin where the sw 3 will be read it
00125           * @param sw2 pin where the sw 2 will be read it
00126           * @param sw1 pin where the sw 1 will be read it
00127           * @param sw0 pin where the sw 0 will be read it
00128           * @param close logic level that gives you when the sw is closed
00129           *
00130           * Example:
00131           * @code
00132           *   // instance one dip-sw with 7 sw on pin 5, 6, 7, 8, 9, 10 and 11
00133           *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11 );
00134           * @endcode
00135           */
00136         Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0);
00137 
00138         /** Create Hotboards_switches instance for eight sw
00139           * @param sw7 pin where the sw 7 will be read it
00140           * @param sw6 pin where the sw 6 will be read it
00141           * @param sw5 pin where the sw 5 will be read it
00142           * @param sw4 pin where the sw 4 will be read it
00143           * @param sw3 pin where the sw 3 will be read it
00144           * @param sw2 pin where the sw 2 will be read it
00145           * @param sw1 pin where the sw 1 will be read it
00146           * @param sw0 pin where the sw 0 will be read it
00147           * @param close logic level that gives you when the sw is closed
00148           *
00149           * Example:
00150           * @code
00151           *   // instance one dip-sw with 8 sw on pin 5, 6, 7, 8, 9, 10, 11 and 12
00152           *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11, 12 );
00153           * @endcode
00154           */
00155         Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
00156 
00157         /** Read a single sw or the entire Dip-switch state (open=0 or close=1)
00158           * @return sw state(s)
00159           *
00160           * Example:
00161           * @code
00162           *   // instance one sw on pin 7 and read its state (0 o 1)
00163           *   Hotboards_leds sw ( 7 );
00164           *   bool val = sw.read( );
00165           *
00166           *   // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
00167           *   Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
00168           *   // read the sw values (from 0 to 255)
00169           *   uint8_t val = dipsw.read( );
00170           *
00171           *   // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
00172           *   Hotboards_switches dipsw( 2, 3, 4, 5 );
00173           *   // read sw 1 state (pin 4)
00174           *   bool val1 = dipsw.write( 1 );
00175           *   // read sw 0 state (pin 5)
00176           *   bool val2 = dipsw.write( 0 );
00177           * @endcode
00178           */
00179         uint8_t read( uint8_t sw=0xff );
00180 
00181         /** Tells you if one or more sw has been change
00182           * @return '1' if not changed
00183           *
00184           * Example:
00185           * @code
00186           *   // instance one sw on pin 7 and read its state (0 o 1)
00187           *   Hotboards_leds sw ( 7 );
00188           *   if( sw.hasItChange( ))
00189           *     bool val = sw.read( );
00190           *
00191           *   // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
00192           *   Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
00193           *   // read the sw values (from 0 to 255) if it changes
00194           *   if( dipsw.hasItChange( ) )
00195           *     uint8_t val = dipsw.read( );
00196           *
00197           *   // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
00198           *   Hotboards_switches dipsw( 2, 3, 4, 5 );
00199           *   // read sw 1 state (pin 4) if it changes
00200           *   if( sw.hasItChange( 1 ))
00201           *     bool val = sw.read( 1 );
00202           * @endcode
00203           */
00204         bool hasItChange( uint8_t sw=0xff );
00205 
00206     private :
00207         void begin( uint8_t sw, PinName pin );
00208         bool readSw( uint8_t sw );
00209         bool hasItChangeSw( uint8_t sw );
00210         DigitalIn *_sw[ 8 ];
00211         uint8_t _sws;
00212         uint8_t _lastSwState;
00213         bool _close;
00214 };
00215 
00216 #endif