BrendanSimon / micropython_experiments

A place to share my micropython experimental code
24 stars 4 forks source link

Key index incorrect #1

Open hacksterous opened 5 years ago

hacksterous commented 5 years ago

https://github.com/BrendanSimon/micropython_experiments/blob/204b28311856ce54387e0e43b841aa9dc25b6950/keypad/keypad_timer.py#L174

Shouldn't the index be key_code + col

self.key_char = self.keys[key_code + col]

BrendanSimon-SEPL commented 5 years ago

I haven't touched or looked at this code in forever - but it is on my "one day I might get around to it" list ;-)

I believe the code is correct as I'm sure it worked.

You might be confusing the local list variable key with the class attribute self.keys.

The local attribute keys will get deleted (garbage collected) when the __init__() function terminates, but self.keys will stick around for the life of the instantiated object.

        keys = [
                '1', '2', '3', 'A',
                '4', '5', '6', 'B',
                '7', '8', '9', 'C',
                '*', '0', '#', 'D',
               ]

        ## Initialise all keys to the UP state.
        self.keys = [ { 'char' : key, 'state' : self.KEY_UP } for key in keys ]

self.keys is a list of dictionaries (with two keys char and state).

Equivalent of:

self.key[ 0 ] = { 'char' : '1' , 'state' : self.KEY_UP }
self.key[ 1 ] = { 'char' : '2' , 'state' : self.KEY_UP }
self.key[ 2 ] = { 'char' : '3' , 'state' : self.KEY_UP }
self.key[ 3 ] = { 'char' : 'A' , 'state' : self.KEY_UP }
...
self.key[ 12 ] = { 'char' : '*' , 'state' : self.KEY_UP }
self.key[ 13 ] = { 'char' : '0' , 'state' : self.KEY_UP }
self.key[ 14 ] = { 'char' : '#' , 'state' : self.KEY_UP }
self.key[ 15 ] = { 'char' : 'D' , 'state' : self.KEY_UP }

So the code:

key_code = self.scan_row * len(self.cols)
self.key_char = self.keys[key_code]['char']

is equivalent to:

#! key_code is the index to the key lookup table
key_code = self.scan_row * len(self.cols)

#! get the dict for this key code, which contains the ascii char and the current state of the key
d = self.keys[key_code]

#! get the ascii char for the key (key code)
self.key_char = d['char']

Does that make sense?

hacksterous commented 5 years ago

key_code = self.scan_row * len(self.cols)

key_code gets values 0, 4, 8, 12, 0, ... for a 4x4 keypad, so self.keys[i] for i = 1,2,3,5,6,7 ... cannot be sampled. See my fork here: https://github.com/hacksterous/Keypad/blob/master/Keypad.py

BrendanSimon-SEPL commented 5 years ago

Oh, yes, now I see what you mean now. I believe you are correct !!

I probably intended to write something like this (or how I would rewrite it so that key_code is always the code for the current key being processed).

        #! get key_code/index for start of row
        key_code = self.scan_row * len( self.cols )

        for col in range( len( self.cols ) ) :
            #! process pin state.
            key_event = self.key_process( key_code, self.col_pins[ col ] )

            #! process key event.
            if key_event == self.KEY_DOWN :
                self.key_code = key_code
                self.key_char = self.keys[ key_code ][ 'char' ]

            #! next key code in the row (i.e. for next column)
            key_code += 1