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.cpp - Driver to control 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
Hotboards 0:8e569e04a0fb 8 #include "Hotboards_switches.h"
Hotboards 0:8e569e04a0fb 9
Hotboards 0:8e569e04a0fb 10 #define bitRead( var, bit ) (((var) >> (bit)) & 0x01)
Hotboards 0:8e569e04a0fb 11 #define bitWrite( var, bit, val ) (val) ? (var) |= (1<<(bit)) : (var) &= ~(1<<(bit))
Hotboards 0:8e569e04a0fb 12
Hotboards 0:8e569e04a0fb 13 Hotboards_switches::Hotboards_switches( PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 14 {
Hotboards 0:8e569e04a0fb 15 _sws = 1;
Hotboards 0:8e569e04a0fb 16 _close = close;
Hotboards 0:8e569e04a0fb 17 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 18 }
Hotboards 0:8e569e04a0fb 19
Hotboards 0:8e569e04a0fb 20 Hotboards_switches::Hotboards_switches( PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 21 {
Hotboards 0:8e569e04a0fb 22 _sws = 2;
Hotboards 0:8e569e04a0fb 23 _close = close;
Hotboards 0:8e569e04a0fb 24 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 25 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 26 }
Hotboards 0:8e569e04a0fb 27
Hotboards 0:8e569e04a0fb 28 Hotboards_switches::Hotboards_switches( PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 29 {
Hotboards 0:8e569e04a0fb 30 _sws = 3;
Hotboards 0:8e569e04a0fb 31 _close = close;
Hotboards 0:8e569e04a0fb 32 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 33 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 34 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 35 }
Hotboards 0:8e569e04a0fb 36
Hotboards 0:8e569e04a0fb 37 Hotboards_switches::Hotboards_switches( PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 38 {
Hotboards 0:8e569e04a0fb 39 _sws = 4;
Hotboards 0:8e569e04a0fb 40 _close = close;
Hotboards 0:8e569e04a0fb 41 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 42 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 43 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 44 begin( 3, sw3 );
Hotboards 0:8e569e04a0fb 45 }
Hotboards 0:8e569e04a0fb 46
Hotboards 0:8e569e04a0fb 47 Hotboards_switches::Hotboards_switches( PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 48 {
Hotboards 0:8e569e04a0fb 49 _sws = 5;
Hotboards 0:8e569e04a0fb 50 _close = close;
Hotboards 0:8e569e04a0fb 51 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 52 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 53 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 54 begin( 3, sw3 );
Hotboards 0:8e569e04a0fb 55 begin( 4, sw4 );
Hotboards 0:8e569e04a0fb 56 }
Hotboards 0:8e569e04a0fb 57
Hotboards 0:8e569e04a0fb 58 Hotboards_switches::Hotboards_switches( PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 59 {
Hotboards 0:8e569e04a0fb 60 _sws = 6;
Hotboards 0:8e569e04a0fb 61 _close = close;
Hotboards 0:8e569e04a0fb 62 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 63 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 64 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 65 begin( 3, sw3 );
Hotboards 0:8e569e04a0fb 66 begin( 4, sw4 );
Hotboards 0:8e569e04a0fb 67 begin( 5, sw5 );
Hotboards 0:8e569e04a0fb 68 }
Hotboards 0:8e569e04a0fb 69
Hotboards 0:8e569e04a0fb 70 Hotboards_switches::Hotboards_switches( PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 71 {
Hotboards 0:8e569e04a0fb 72 _sws = 7;
Hotboards 0:8e569e04a0fb 73 _close = close;
Hotboards 0:8e569e04a0fb 74 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 75 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 76 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 77 begin( 3, sw3 );
Hotboards 0:8e569e04a0fb 78 begin( 4, sw4 );
Hotboards 0:8e569e04a0fb 79 begin( 5, sw5 );
Hotboards 0:8e569e04a0fb 80 begin( 6, sw6 );
Hotboards 0:8e569e04a0fb 81 }
Hotboards 0:8e569e04a0fb 82
Hotboards 0:8e569e04a0fb 83 Hotboards_switches::Hotboards_switches( PinName sw7, PinName sw6, PinName sw5, PinName sw4, PinName sw3, PinName sw2, PinName sw1, PinName sw0, bool close )
Hotboards 0:8e569e04a0fb 84 {
Hotboards 0:8e569e04a0fb 85 _sws = 8;
Hotboards 0:8e569e04a0fb 86 _close = close;
Hotboards 0:8e569e04a0fb 87 begin( 0, sw0 );
Hotboards 0:8e569e04a0fb 88 begin( 1, sw1 );
Hotboards 0:8e569e04a0fb 89 begin( 2, sw2 );
Hotboards 0:8e569e04a0fb 90 begin( 3, sw3 );
Hotboards 0:8e569e04a0fb 91 begin( 4, sw4 );
Hotboards 0:8e569e04a0fb 92 begin( 5, sw5 );
Hotboards 0:8e569e04a0fb 93 begin( 6, sw6 );
Hotboards 0:8e569e04a0fb 94 begin( 7, sw7 );
Hotboards 0:8e569e04a0fb 95 }
Hotboards 0:8e569e04a0fb 96
Hotboards 0:8e569e04a0fb 97 uint8_t Hotboards_switches::read( uint8_t sw )
Hotboards 0:8e569e04a0fb 98 {
Hotboards 0:8e569e04a0fb 99 uint8_t i;
Hotboards 0:8e569e04a0fb 100 uint8_t val = 0;
Hotboards 0:8e569e04a0fb 101
Hotboards 0:8e569e04a0fb 102 if( sw == 0xff )
Hotboards 0:8e569e04a0fb 103 {
Hotboards 0:8e569e04a0fb 104 for( i=0 ; i<_sws ; i++ )
Hotboards 0:8e569e04a0fb 105 {
Hotboards 0:8e569e04a0fb 106 bitWrite( val, i, readSw( i ) );
Hotboards 0:8e569e04a0fb 107 }
Hotboards 0:8e569e04a0fb 108 }
Hotboards 0:8e569e04a0fb 109 else
Hotboards 0:8e569e04a0fb 110 {
Hotboards 0:8e569e04a0fb 111 val = readSw( sw );
Hotboards 0:8e569e04a0fb 112 }
Hotboards 0:8e569e04a0fb 113 return val;
Hotboards 0:8e569e04a0fb 114 }
Hotboards 0:8e569e04a0fb 115
Hotboards 0:8e569e04a0fb 116 bool Hotboards_switches::hasItChange( uint8_t sw )
Hotboards 0:8e569e04a0fb 117 {
Hotboards 0:8e569e04a0fb 118 uint8_t i;
Hotboards 0:8e569e04a0fb 119 bool isChanged = 0;
Hotboards 0:8e569e04a0fb 120
Hotboards 0:8e569e04a0fb 121 if( sw == 0xff )
Hotboards 0:8e569e04a0fb 122 {
Hotboards 0:8e569e04a0fb 123 for( i=0 ; i<_sws ; i++ )
Hotboards 0:8e569e04a0fb 124 {
Hotboards 0:8e569e04a0fb 125 isChanged |= hasItChangeSw( i );
Hotboards 0:8e569e04a0fb 126 }
Hotboards 0:8e569e04a0fb 127 }
Hotboards 0:8e569e04a0fb 128 else
Hotboards 0:8e569e04a0fb 129 {
Hotboards 0:8e569e04a0fb 130 isChanged = hasItChangeSw( sw );
Hotboards 0:8e569e04a0fb 131 }
Hotboards 0:8e569e04a0fb 132
Hotboards 0:8e569e04a0fb 133 return isChanged;
Hotboards 0:8e569e04a0fb 134 }
Hotboards 0:8e569e04a0fb 135
Hotboards 0:8e569e04a0fb 136 void Hotboards_switches::begin( uint8_t sw, PinName pin )
Hotboards 0:8e569e04a0fb 137 {
Hotboards 1:dfb1302f847d 138 _sw[ sw ] = new DigitalIn( pin );
Hotboards 0:8e569e04a0fb 139 }
Hotboards 0:8e569e04a0fb 140
Hotboards 0:8e569e04a0fb 141 bool Hotboards_switches::readSw( uint8_t sw )
Hotboards 0:8e569e04a0fb 142 {
Hotboards 1:dfb1302f847d 143 bool val = _sw[ sw ]->read();
Hotboards 0:8e569e04a0fb 144 if( _close == 0 ) val = !val;
Hotboards 0:8e569e04a0fb 145 return val;
Hotboards 0:8e569e04a0fb 146 }
Hotboards 0:8e569e04a0fb 147
Hotboards 0:8e569e04a0fb 148 bool Hotboards_switches::hasItChangeSw( uint8_t sw )
Hotboards 0:8e569e04a0fb 149 {
Hotboards 0:8e569e04a0fb 150 bool swState;
Hotboards 0:8e569e04a0fb 151 bool isChanged = 0;
Hotboards 0:8e569e04a0fb 152 // leemos el boton de la tarjeta
Hotboards 0:8e569e04a0fb 153 swState = read( sw );
Hotboards 0:8e569e04a0fb 154
Hotboards 0:8e569e04a0fb 155 // comparemos el estado actual con la anterior lectura
Hotboards 0:8e569e04a0fb 156 if( swState != bitRead( _lastSwState, sw ) )
Hotboards 0:8e569e04a0fb 157 {
Hotboards 0:8e569e04a0fb 158 // si el estado cambio esque recien se presiono
Hotboards 0:8e569e04a0fb 159 isChanged = 1;
Hotboards 0:8e569e04a0fb 160 }
Hotboards 0:8e569e04a0fb 161 // respaldamos el esatdo actual para compararlo la siguiente vez que preguntemos
Hotboards 0:8e569e04a0fb 162 bitWrite( _lastSwState, sw, swState );
Hotboards 0:8e569e04a0fb 163 return isChanged;
Hotboards 0:8e569e04a0fb 164 }