Decoding DipSwitch
How to convert mask the dipswitch value to identify individual switches
Concept
When using the Make Controller, often it is convenient to use the dipswitch on the application board. These 8 switches are an excellent way to integrate switching into a controller project without requiring extra hardware.
The difficulty arises in determining which individual switches are thrown. When we poll the dipswitch, it returns a value between 0 and 255 depending on the binary representation of the switches. For example:
/dipswitch/value 156
means that the switches thrown are as follows
BINARY REP: 10011100
SWITCH # 7 6 5 4 3 2 1 0
THROWN? ON OFF OFF ON ON ON OFF OFF
It can be a hassle to creat 'if-then' coding to convert all of the 256 possilbilites into their related switch possibilities. It would be far easier to use a bit mask to convert 156 into it's indivitual components. We would like to create 8 variables that will be TRUE or FALSE, each of which represent an individual switch. These variables should be updated each time we receive a message from the dipswitch. These variables can be used elsewhere in the code after they have been updated.
The following code is in C# but should require only minor changes to work inside firmware. chkDip1-8 represent the individual variables representing the switches. As shown below, we can compress hundreds of 'if-then' lines into 16 ternary lines.
int value; //this is the value received from the dipswitch between 0 and 255
long result; //this will be a variable to hold our masked valueresult = 1 & value; //mask the value against 00000001
chkDip1 = result == 1 ? true : false; //ternary operation to assign 'true' or 'false' to the chkDip1 variable
result = 2 & value; //mask the value against 00000010
chkDip2 = result == 2 ? true : false;
result = 4 & value; //mask the value against 00000100
chkDip3 = result == 4 ? true : false;
result = 8 & value; //mask the value against 00001000
chkDip4 = result == 8 ? true : false;
result = 16 & value; //mask the value against 00010000
chkDip5 = result == 16 ? true : false;
result = 32 & value; //mask the value against 00100000
chkDip6 = result == 32 ? true : false;
result = 64 & value; //mask the value against 01000000
chkDip7 = result == 64 ? true : false;
result = 128 & value; //mask the value against 10000000
chkDip8 = result == 128 ? true : false;
How it Works
Bit masking is a technique in which binary operations are used to extract data. It uses simple boolean algebra to determine if a specific value is contained within a set of bits. the "result = X & value" is the bit masking procedure. The 'value' is the unmasked data, 'X' is the mask, and the masked data is 'result'. Masking bits is the process of analyzing related bits and gathering a result. This is done using the boolean AND operator. If the masked data is the same as the mask, then the mask was present as a subset in the data.
Example:
Analyze 156 to see if switch 7 is active
10011100 value - 156 in binary as seen by microcontroller
01000000 mask - 64 in binary, this is the mask associated with bit 7
-------------
00000000 result - 0 is the result, meaning that switch 7 is off (result does not equal mask)
Analyze 199 to see if switch 3 is active
11000111 value - 199 in binary as seen by microcontroller
00000100 mask - 4 in binary, this is the mask associated with bit 3
-------------
00000100 result - 4 is the result, meaning that switch 3 is on (resultequals mask)

