Monday, September 21, 2009

Look-up tables from one-hot encoding inputs

In regards to manually scanning a matrix keypad with  M rows and N columns. 


Say we output on the columns and read in the rows which are pulled up high. We will pull one column low and see if one of the rows is also pulled low (we are only worried about one key).


So our output O bit string with (N-1) 1's and one 0 corresponding to the low column [n will be the value of the low bit].


Our input I also going to be bitstring with either all 1's or (M-1) 1's and the 0 corresponds to the low row [m will be the value of the low bit].


To make this manageable we make a look-up table that matches our keyboard layout:
key_lut .byte 'q','w','e','r',...'|'
            .byte 'a','s'
            ...
For convenience we can write each new row on a different line, but essentially this is a one dimensional array which can be indexed.

key_lut[row*N+column]

 We don't have a row and column, but instead a position of n and m. If we negate I and O then we have a one-hot (one high) bitstring which equals n or m. But we need the position of this bit, not its value. So we can take the log_2 of this value and get the position.

key_lut[log_2(m)*N+log_2(n)]

To take the log_2 of a bit string, we simple need to count the number division by 2 until the value is 1. We simply set up a loop to increment a counter and shift the register right until its 1. Remember log(0) is undefined!



No comments:

Post a Comment