Virtual keypad decoding
skylight
Posts: 1,915
I have created a virtual keypad using a QMP touchscreen, I need advice as to the concept of reading the keypresses and storing in a variable.
The keypad has numbered buttons 0-9 and an enter button
Each button has a unique keycode returned after pressing and then a repeat loop uses if statements to see which button was pressed
The keypad user will enter up to a three digit number which is in the form of hundreds,tens & units
I'm thinking that as the user types the number each digit drops into a variable array of three bytes byte[0], byte[1] and byte[2]
after entering the number be it a 1,2 or 3 digit number then byte[0] is multiplied by 1 or stays the same, byte[1] is then multiplied by 10 and byte[2] multiplied by 100. (see problem below)
If only one digit is entered then it is multiplied by 1 or stays the same, bytes [1] and [2] are zero values(nothing entered) so multiplying by 10 and 100 =0, respectively
same for two digits, digit 1*1 and digit 2*10 digit 3 =0*100 , and it goes on for three digits entered, you get the idea
thing is a one digit number will be a unit but the third digit entered will also be a unit (ie 123 where 3 is the unit) so does the unit digit go in array[0] or [2]?
I suppose a way around is to force the user to use leading zeros but I would prefer if they didn't have to
Is this the best method to use or is there an easier way? Does spin have a way of making this easier?.
The keypad has numbered buttons 0-9 and an enter button
Each button has a unique keycode returned after pressing and then a repeat loop uses if statements to see which button was pressed
The keypad user will enter up to a three digit number which is in the form of hundreds,tens & units
I'm thinking that as the user types the number each digit drops into a variable array of three bytes byte[0], byte[1] and byte[2]
after entering the number be it a 1,2 or 3 digit number then byte[0] is multiplied by 1 or stays the same, byte[1] is then multiplied by 10 and byte[2] multiplied by 100. (see problem below)
If only one digit is entered then it is multiplied by 1 or stays the same, bytes [1] and [2] are zero values(nothing entered) so multiplying by 10 and 100 =0, respectively
same for two digits, digit 1*1 and digit 2*10 digit 3 =0*100 , and it goes on for three digits entered, you get the idea
thing is a one digit number will be a unit but the third digit entered will also be a unit (ie 123 where 3 is the unit) so does the unit digit go in array[0] or [2]?
I suppose a way around is to force the user to use leading zeros but I would prefer if they didn't have to
Is this the best method to use or is there an easier way? Does spin have a way of making this easier?.
Comments
How would comparisons be made?
so that 2 and 5 = 25 entered
would I do a Dec (long) to get the decimal value?
1) Initialize long variable to 0 at the beginning of the program
2) Read the key (0-9; Enter; or other special character) <- as long as total number of unique characters can be mapped to a number less than 16
3) Shift long variable left by 4
4) Add Key value from #2 to long variable
5) Check for a match against a hard coded long or one soft set by the user
6) Repeat from step 2 only.
store number 3 (0011) to long x
x:=x<<4
after shift
x = 11110
next number is 1 so user has entered 31 on keypad
x+=1
x now equals 11111 = 31
%00000011 << 4 becomes %00110000 + 1 becomes %00110001 which is the same as $31
shifting Binary(3) left by 4 only equals decimal 25 (011000) not 30 as expected so 5 short (000101)
adding 011001 + 000101 = 30
I'm missing something here but can't see where i'm going wrong
btw the editor is stripping the % and some leading zeros from the above values
I'm getting really confused at the moment
Say you have your standard keys 0-9 already mapped to values 0-9 ... lets also say that...
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
* = 10
# = 11
[enter] = 12
You have room for 3 more as long as the mapped value does not exceed 15
For example lets say that the code is 7542 and that password starts out as Zero
First Key Press "7"
Second Key Press "5"
Third Key Press "4"
Fourth Key Press "2"
Now when you see a key press value of 12 (Enter) then you know to compare the Password value to a (predefined by you) fixed value, or one set by the user.
If your looking at the 'stored' Password value as a decimal number it would be 30018 for this example, but if you look at it as a Hexadecimal value then the number is 7542.
In software all you need to do is place a $ in front of the value if you want to hard-code it in Hexadecimal like this ....
ValidCode := $7542
If you leave off the $ and write it like this ...
ValidCode := 7542
...it won't work ... you would have to enter 1D76 on the keypad with D equaling a mapped value of 13 for 7542 to work without the $
The problem is it's not a password keypad, more like a calculators keypad so in the example above the final result would have to be in decimal
"seven thousand five hundred and forty two" although it will never be more than three digits. I'm trying to create a DMX keypad so the max value entered would be 512
I'm not creating a table to look up for comparison I just need the keypad to output to a variable the 1,2 and 3 digits as units or tens and units or hundreds and tens and units.
does it mean I will have to do a comparison for all 512 possible entered values?
I was originally thinking it was just a matter of adding the binary values together after they had been shifted.
Sorry if i'm a bit slow to grasp this, i'm waiting for that eureka moment
In that case it might be easier to multiply by 10 rather than shift left by 4. The same algorithm still applies. Instead of shifting by 4, just multiply by 10 (as you had originally indicated) prior to adding the new key press value. Sorry to add complexity to the original question.
thanks also to Mike for helping
There is still the problem of entering three digits and entering only one digit
in entering three digits the last one entered is the units, with one digit it is the units, this makes it hard to know which value goes into which array ie I can't say that array[0] is the one to multiply by 1 and [2] is the one to multiply by 100 as that only applies to three digits having been entered.
The only way I can see is to see first how many digits were entered then use a CASE to decide what array element is multiplied by what.
EDIT: Thinking again if the array elements are loaded right to left as the digits would appear on screen I just need to shift the value from element to element as another digit is entered and when enter is pressed then do the multiplication.
this will work with any number of digits as long as result variable has enough bits
Dave