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

Dependents:   input dip_switch dipsw_change input ... more

Files at this revision

API Documentation at this revision

Comitter:
Hotboards
Date:
Sun Feb 28 01:32:55 2016 +0000
Child:
1:dfb1302f847d
Commit message:
first official release

Changed in this revision

Hotboards_switches.cpp Show annotated file Show diff for this revision Revisions of this file
Hotboards_switches.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_switches.cpp	Sun Feb 28 01:32:55 2016 +0000
@@ -0,0 +1,164 @@
+/*
+  Hotboards_switches.cpp - Driver to control interrupts
+  Hotboards Dip-switch board (http://hotboards.org)
+  Created by Diego Perez, January 16, 2016.
+  Released into the public domain.
+*/
+
+#include "Hotboards_switches.h"
+
+#define bitRead( var, bit )           (((var) >> (bit)) & 0x01)
+#define bitWrite( var, bit, val )     (val) ? (var) |= (1<<(bit)) : (var) &= ~(1<<(bit))
+
+Hotboards_switches::Hotboards_switches( PinName sw0, bool close )
+{
+    _sws = 1;
+    _close = close;
+    begin( 0, sw0 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw1, PinName sw0, bool close )
+{
+    _sws = 2;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 3;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 4;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+    begin( 3, sw3 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 5;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+    begin( 3, sw3 );
+    begin( 4, sw4 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 6;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+    begin( 3, sw3 );
+    begin( 4, sw4 );
+    begin( 5, sw5 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 7;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+    begin( 3, sw3 );
+    begin( 4, sw4 );
+    begin( 5, sw5 );
+    begin( 6, sw6 );
+}
+
+Hotboards_switches::Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
+{
+    _sws = 8;
+    _close = close;
+    begin( 0, sw0 );
+    begin( 1, sw1 );
+    begin( 2, sw2 );
+    begin( 3, sw3 );
+    begin( 4, sw4 );
+    begin( 5, sw5 );
+    begin( 6, sw6 );
+    begin( 7, sw7 );
+}
+
+uint8_t Hotboards_switches::read( uint8_t sw )
+{
+    uint8_t i;
+    uint8_t val = 0;
+
+    if( sw == 0xff )
+    {
+        for( i=0 ; i<_sws ; i++ )
+        {
+            bitWrite( val, i, readSw( i ) );
+        }
+    }
+    else
+    {
+        val = readSw( sw );
+    }
+    return val;
+}
+
+bool Hotboards_switches::hasItChange( uint8_t sw )
+{
+    uint8_t i;
+    bool isChanged = 0;
+
+    if( sw == 0xff )
+    {
+        for( i=0 ; i<_sws ; i++ )
+        {
+            isChanged |= hasItChangeSw( i );
+        }
+    }
+    else
+    {
+        isChanged = hasItChangeSw( sw );
+    }
+
+    return isChanged;
+}
+
+void Hotboards_switches::begin( uint8_t sw, PinName pin )
+{
+    _sw[ sw ] = new DigitalIn( pin );;
+}
+
+bool Hotboards_switches::readSw( uint8_t sw )
+{
+    bool val = _sw[ sw ];
+    if( _close == 0 ) val = !val;
+    return val;
+}
+
+bool Hotboards_switches::hasItChangeSw( uint8_t sw )
+{
+    bool swState;
+    bool isChanged = 0;
+    // leemos el boton de la tarjeta
+    swState = read( sw );
+
+    // comparemos el estado actual con la anterior lectura
+    if( swState != bitRead( _lastSwState, sw ) )
+    {
+        // si el estado cambio esque recien se presiono
+        isChanged = 1;
+    }
+    // respaldamos el esatdo actual para compararlo la siguiente vez que preguntemos
+    bitWrite( _lastSwState, sw, swState );
+    return isChanged;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_switches.h	Sun Feb 28 01:32:55 2016 +0000
@@ -0,0 +1,216 @@
+/*
+  Hotboards_switches.h - Driver to read interrupts
+  Hotboards Dip-switch board (http://hotboards.org)
+  Created by Diego Perez, January 16, 2016.
+  Released into the public domain.
+*/
+#ifndef Hotboards_switches_h
+#define Hotboards_switches_h
+
+#include "mbed.h"
+
+/** Hotboards_switches class.
+ *  Used to read general purpose dip switches
+ *
+ * Example:
+ * @code
+ * #include "Hotboards_switches.h"
+ *
+ * Hotboards_switches sw( 5 );
+ *
+ * int main( void )
+ * {
+ *     for(;;){
+ *         bool var = sw.read( );
+ *         wait( 0.2 );
+ *     }
+ * }
+ * @endcode
+ */
+class Hotboards_switches
+{
+    public :
+        /** Create Hotboards_switches instance for one sw
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one sw on pin 5
+          *   Hotboards_switches sw( 5 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for two sw
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 2 sw on pin 5 and 6
+          *   Hotboards_switches sw( 5, 6 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw1, PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for three sw
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 3 sw on pin 5, 6 and 7
+          *   Hotboards_switches sw( 5, 6, 7 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for four sw
+          * @param sw3 pin where the sw 3 will be read it
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 4 sw on pin 5, 6, 7 and 8
+          *   Hotboards_switches sw( 5, 6, 7, 8 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for five sw
+          * @param sw4 pin where the sw 4 will be read it
+          * @param sw3 pin where the sw 3 will be read it
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 5 sw on pin 5, 6, 7, 8 and 9
+          *   Hotboards_switches sw( 5, 6, 7, 8, 9 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for six sw
+          * @param sw5 pin where the sw 5 will be read it
+          * @param sw4 pin where the sw 4 will be read it
+          * @param sw3 pin where the sw 3 will be read it
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 6 sw on pin 5, 6, 7, 8, 9 and 10
+          *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
+
+        /** Create Hotboards_switches instance for seven sw
+          * @param sw6 pin where the sw 6 will be read it
+          * @param sw5 pin where the sw 5 will be read it
+          * @param sw4 pin where the sw 4 will be read it
+          * @param sw3 pin where the sw 3 will be read it
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 7 sw on pin 5, 6, 7, 8, 9, 10 and 11
+          *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0);
+
+        /** Create Hotboards_switches instance for eight sw
+          * @param sw7 pin where the sw 7 will be read it
+          * @param sw6 pin where the sw 6 will be read it
+          * @param sw5 pin where the sw 5 will be read it
+          * @param sw4 pin where the sw 4 will be read it
+          * @param sw3 pin where the sw 3 will be read it
+          * @param sw2 pin where the sw 2 will be read it
+          * @param sw1 pin where the sw 1 will be read it
+          * @param sw0 pin where the sw 0 will be read it
+          * @param close logic level that gives you when the sw is closed
+          *
+          * Example:
+          * @code
+          *   // instance one dip-sw with 8 sw on pin 5, 6, 7, 8, 9, 10, 11 and 12
+          *   Hotboards_switches sw( 5, 6, 7, 8, 9, 10, 11, 12 );
+          * @endcode
+          */
+        Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close=0 );
+
+        /** Read a single sw or the entire Dip-switch state (open=0 or close=1)
+          * @return sw state(s)
+          *
+          * Example:
+          * @code
+          *   // instance one sw on pin 7 and read its state (0 o 1)
+          *   Hotboards_leds sw ( 7 );
+          *   bool val = sw.read( );
+          *
+          *   // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
+          *   Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
+          *   // read the sw values (from 0 to 255)
+          *   uint8_t val = dipsw.read( );
+          *
+          *   // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
+          *   Hotboards_switches dipsw( 2, 3, 4, 5 );
+          *   // read sw 1 state (pin 4)
+          *   bool val1 = dipsw.write( 1 );
+          *   // read sw 0 state (pin 5)
+          *   bool val2 = dipsw.write( 0 );
+          * @endcode
+          */
+        uint8_t read( uint8_t sw=0xff );
+
+        /** Tells you if one or more sw has been change
+          * @return '1' if not changed
+          *
+          * Example:
+          * @code
+          *   // instance one sw on pin 7 and read its state (0 o 1)
+          *   Hotboards_leds sw ( 7 );
+          *   if( sw.hasItChange( ))
+          *     bool val = sw.read( );
+          *
+          *   // instance an 8 sw dip-sw (pin9->sw7 ..... pin2->sw0)
+          *   Hotboards_switches dipsw( 9, 8, 7, 6, 5, 4, 3, 2 );
+          *   // read the sw values (from 0 to 255) if it changes
+          *   if( dipsw.hasItChange( ) )
+          *     uint8_t val = dipsw.read( );
+          *
+          *   // instance a 4 dip-sw (pin2->sw3 ..... pin5->sw0)
+          *   Hotboards_switches dipsw( 2, 3, 4, 5 );
+          *   // read sw 1 state (pin 4) if it changes
+          *   if( sw.hasItChange( 1 ))
+          *     bool val = sw.read( 1 );
+          * @endcode
+          */
+        bool hasItChange( uint8_t sw=0xff );
+
+    private :
+        void begin( uint8_t sw, PinName pin );
+        bool readSw( uint8_t sw );
+        bool hasItChangeSw( uint8_t sw );
+        DigitalIn *_sw[ 8 ];
+        uint8_t _sws;
+        uint8_t _lastSwState;
+        bool _close;
+};
+
+#endif
\ No newline at end of file