Comments (3)
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?
from micropython_experiments.
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
from micropython_experiments.
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
from micropython_experiments.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micropython_experiments.