Here's an attempted explanation of the CheckIfShapeRemoved sub. If I screw it up someone will correct me.
The Flags variable is made up of one byte, within this byte there are 8 bits.
Each bit has a value, if all bits are set to 0 then Flags would equal 0, if all bit are set to 1 Flags would equal 255. The values for each bit are:
bit0 - 1
bit1 - 2
bit3 - 4
bit4 - 8
bit5 - 16
bit6 - 32
bit7 - 64
bit8 - 128
We are using 4 of these bits to enable (send message) or disable (don't send message) to the LCD.
Each of the 4 bits used is assigned to a shape
<code><pre><font size=2 face='Courier'>
Triangle - bit0 (1)
Circle - bit1 (2)
Square - bit2 (4)
Rectangle - bit5 (32)
</font></pre></code>
At the start of the progam all bits we are using (0,1,2,5) are set to 1 via the Flags variable (39), as the program cycles and each shape is inserted the bit for that shape is cleared so the value of the Flags variable decreases, with all shapes in the holes Flags = 0, all out Flags = 39.
When the CheckIfShapeRemoved sub is called the variable PinState is set to equal the state of the input pins, the mask (%0100111) is used to filter out unwanted input pins by logially ANDing all input pins with the mask eg:
the line "PinState = pins & %00100111" gets the state of inputs we want and filters out the unwanted inputs, see below
<code><pre><font size=2 face='Courier'>
Bit/pin No 76543210
-------------------
pins = 10000110 <- all input pins
mask = 00100111 <- we only want pins 0,1,2,5 so mask out the others
-------------------
PinState= xx0xx110 <- result of pins with unwanted inputs filtered out.
</font></pre></code>
The bits in PinState that are shown as "x" will always be 0 as these have been filtered out by the mask.
You can see when pins is ANDed with the mask and a 1 in pins aligns with a 1 in the mask that a 1 is put into PinState, so because PinState = 00000110 this tells us that
Pin0 = 0 - Triangle in the hole
Pin1 = 1 - nothing in hole (Circle)
Pin2 = 1 - nothing in hole (Square)
Pin5 = 0 - Rectangle in the hole
With a triangle and rectangle in the hole Flags will equal 6 as the starting value of Flags was 39, then a triangle was put in, this subtracted 1 from Flags by clearing bit0 then a rectangle was put in, this cleared bit5 of Flags so 32 was subtracted from Flags so 39 - 32 - 1 = 6.
The line "if PinState > flags then" checks if any shapes have been removed. with the example above Flags = 6 and PinState = 6 so 2 shapes are in and nothing has been removed. if a shape is removed the PinState value will increase as the input pin goes high which adds to the PinState value, when PinState is greater than Flags then the line "flags = PinState" is executed, what this does is set the bits in Flags the same as the inputs, in the example above if the rectangle was removed then PinState will change from 00000110 to 00100110 as pin5 has gone high so now Flags = 00100110 which means bit5 is now 1. What this has done isre-enabled the rectangle so next time it's put in the condition"elseif pin5 = 0 and bit5 = 1 then" will be true so the message will be sent to the LCD. This goes on and on setting and clearing the bits in the Flags variable as shapes are put in and pulled out.
Hope that wasn't too painful.
Edited by - Phil75 on 06/02/2007 06:36:38