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.cpp Source File

Hotboards_switches.cpp

00001 /*
00002   Hotboards_switches.cpp - Driver to control 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 
00008 #include "Hotboards_switches.h"
00009 
00010 #define bitRead( var, bit )           (((var) >> (bit)) & 0x01)
00011 #define bitWrite( var, bit, val )     (val) ? (var) |= (1<<(bit)) : (var) &= ~(1<<(bit))
00012 
00013 Hotboards_switches::Hotboards_switches( PinName sw0, bool close )
00014 {
00015     _sws = 1;
00016     _close = close;
00017     begin( 0, sw0 );
00018 }
00019 
00020 Hotboards_switches::Hotboards_switches( PinName sw1, PinName sw0, bool close )
00021 {
00022     _sws = 2;
00023     _close = close;
00024     begin( 0, sw0 );
00025     begin( 1, sw1 );
00026 }
00027 
00028 Hotboards_switches::Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close )
00029 {
00030     _sws = 3;
00031     _close = close;
00032     begin( 0, sw0 );
00033     begin( 1, sw1 );
00034     begin( 2, sw2 );
00035 }
00036 
00037 Hotboards_switches::Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
00038 {
00039     _sws = 4;
00040     _close = close;
00041     begin( 0, sw0 );
00042     begin( 1, sw1 );
00043     begin( 2, sw2 );
00044     begin( 3, sw3 );
00045 }
00046 
00047 Hotboards_switches::Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
00048 {
00049     _sws = 5;
00050     _close = close;
00051     begin( 0, sw0 );
00052     begin( 1, sw1 );
00053     begin( 2, sw2 );
00054     begin( 3, sw3 );
00055     begin( 4, sw4 );
00056 }
00057 
00058 Hotboards_switches::Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
00059 {
00060     _sws = 6;
00061     _close = close;
00062     begin( 0, sw0 );
00063     begin( 1, sw1 );
00064     begin( 2, sw2 );
00065     begin( 3, sw3 );
00066     begin( 4, sw4 );
00067     begin( 5, sw5 );
00068 }
00069 
00070 Hotboards_switches::Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
00071 {
00072     _sws = 7;
00073     _close = close;
00074     begin( 0, sw0 );
00075     begin( 1, sw1 );
00076     begin( 2, sw2 );
00077     begin( 3, sw3 );
00078     begin( 4, sw4 );
00079     begin( 5, sw5 );
00080     begin( 6, sw6 );
00081 }
00082 
00083 Hotboards_switches::Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
00084 {
00085     _sws = 8;
00086     _close = close;
00087     begin( 0, sw0 );
00088     begin( 1, sw1 );
00089     begin( 2, sw2 );
00090     begin( 3, sw3 );
00091     begin( 4, sw4 );
00092     begin( 5, sw5 );
00093     begin( 6, sw6 );
00094     begin( 7, sw7 );
00095 }
00096 
00097 uint8_t Hotboards_switches::read( uint8_t sw )
00098 {
00099     uint8_t i;
00100     uint8_t val = 0;
00101 
00102     if( sw == 0xff )
00103     {
00104         for( i=0 ; i<_sws ; i++ )
00105         {
00106             bitWrite( val, i, readSw( i ) );
00107         }
00108     }
00109     else
00110     {
00111         val = readSw( sw );
00112     }
00113     return val;
00114 }
00115 
00116 bool Hotboards_switches::hasItChange( uint8_t sw )
00117 {
00118     uint8_t i;
00119     bool isChanged = 0;
00120 
00121     if( sw == 0xff )
00122     {
00123         for( i=0 ; i<_sws ; i++ )
00124         {
00125             isChanged |= hasItChangeSw( i );
00126         }
00127     }
00128     else
00129     {
00130         isChanged = hasItChangeSw( sw );
00131     }
00132 
00133     return isChanged;
00134 }
00135 
00136 void Hotboards_switches::begin( uint8_t sw, PinName pin )
00137 {
00138     _sw[ sw ] = new DigitalIn( pin );
00139 }
00140 
00141 bool Hotboards_switches::readSw( uint8_t sw )
00142 {
00143     bool val = _sw[ sw ]->read();
00144     if( _close == 0 ) val = !val;
00145     return val;
00146 }
00147 
00148 bool Hotboards_switches::hasItChangeSw( uint8_t sw )
00149 {
00150     bool swState;
00151     bool isChanged = 0;
00152     // leemos el boton de la tarjeta
00153     swState = read( sw );
00154 
00155     // comparemos el estado actual con la anterior lectura
00156     if( swState != bitRead( _lastSwState, sw ) )
00157     {
00158         // si el estado cambio esque recien se presiono
00159         isChanged = 1;
00160     }
00161     // respaldamos el esatdo actual para compararlo la siguiente vez que preguntemos
00162     bitWrite( _lastSwState, sw, swState );
00163     return isChanged;
00164 }