首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用raspberry pi 3从4x4键盘读取多个数字

使用raspberry pi 3从4x4键盘读取多个数字
EN

Stack Overflow用户
提问于 2018-06-06 22:07:10
回答 4查看 2K关注 0票数 1

我正在做一些与出勤相关的项目。我使用的是4x4键盘和LCD显示屏。所以问题是,我如何从4x4矩阵键盘上读取多个数字?我正在使用pad4pi库。允许我一次只读一个数字或数字。现在我想读一个像1234或12345这样的数字。你能帮帮我吗?

EN

回答 4

Stack Overflow用户

发布于 2018-06-07 04:49:19

谢谢你带我去看。这段代码不会做任何事情。在注册密钥之后,您必须实际对存储的数据结构执行一些操作。

例如:

代码语言:javascript
复制
#change store key function to do something on submission of a certain key that indicated send, will use pound for example.
def store_key(self,key):
     If(key==‘#’):
          #im printing but you should do whatever it is you intend to do with the sequence of keys. 
          print(self.pressed_keys)
     else:
          self.pressed_keys.append(key)

该代码将在按下#时打印内部数据结构。当然,我假设传递的键是一个字符串,但我真的不知道我对这个库不熟悉。

票数 1
EN

Stack Overflow用户

发布于 2018-06-06 22:24:19

通过使用数据结构来存储按下的值,可以方便地存储键序列。然后在需要重置时将其清除。

代码语言:javascript
复制
class KeyStore:
    def __init__(self):
        #list to store them
        self.pressed_keys = []

    #function to add to list
   def store_key(self,key):
       if(key==‘#’):
      #im printing but you should do whatever it is you intend to do with the sequence of keys. 
      print(self.pressed_keys)
 else:
      self.pressed_keys.append(key)

    #function to clear list
    def clear_keys(self):
        self.pressed_keys.clear()

keys = KeyStore()

# store_key will be called each time a keypad button is pressed
keypad.registerKeyPressHandler(keys.store_key)
票数 0
EN

Stack Overflow用户

发布于 2018-06-07 00:11:00

我做了这件事,但我还是被困住了

代码语言:javascript
复制
from pad4pi import rpi_gpio
import time


# Setup Keypad
KEYPAD = [
        ["1","2","3","A"],
        ["4","5","6","B"],
        ["7","8","9","C"],
        ["*","0","#","D"]
]

# same as calling: factory.create_4_by_4_keypad, still we put here fyi:
ROW_PINS = [16, 20, 21, 5] # BCM numbering; Board numbering is: 7,8,10,11 (see pinout.xyz/)
COL_PINS = [6, 13, 19, 26] # BCM numbering; Board numbering is: 12,13,15,16 (see pinout.xyz/)

factory = rpi_gpio.KeypadFactory()

# Try keypad = factory.create_4_by_3_keypad() or 
# Try keypad = factory.create_4_by_4_keypad() #for reasonable defaults
# or define your own:
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)


class KeyStore:
    def __init__(self):
        #list to store them
        self.pressed_keys =[]

    #function to add to list
    def store_key(self, key):
        self.pressed_keys.append(key)

    #function to clear list
    def clear_keys(self):
        self.pressed_keys.clear()

keys = KeyStore()

# store_key will be called each time a keypad button is pressed
keypad.registerKeyPressHandler(keys.store_key)
for key in keys.pressed_keys:
    print(key)

try:
    while(True):
        time.sleep(0.2)
except:
    keypad.cleanup()

但它什么也没做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50722486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档