简体   繁体   English

使用树莓派3从4x4键盘读取多个数字

[英]Read multiple digits from 4x4 keypad using raspberry pi 3

I am doing some attendance related project. 我正在做一些出勤相关的项目。 Where I am using 4x4 keypad and a LCD display. 我在哪里使用4x4键盘和LCD显示器。 So the question is, How can I read more than one number from a 4x4 matrix keypad?. 因此,问题是,如何从4x4矩阵键盘读取多个数字? I am using pad4pi library. 我正在使用pad4pi库。 Where I am allowed to read only single number or digit at a time. 允许我一次只读取一个数字或数字。 Now I want to read a number like 1234 or 12345. Could you please help me. 现在,我想读取一个数字,例如1234或12345。请您帮助我。

Storing a sequence of keys could be facilitated by using a data structure to store the pressed values. 通过使用数据结构存储按下的值,可以方便地存储键序列。 And then clearing it when you need to reset. 然后在需要重置时清除它。

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)

I did this but still I am stucked 我做到了,但仍然被困

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()

But its not doing anything. 但是它什么也没做。

Thanks for showing me. 谢谢你给我看 That code isn't going to do anything. 该代码不会做任何事情。 You have to actually do something with the stored data structure after registering keys. 注册密钥后,您实际上必须对存储的数据结构做一些事情。

For example: 例如:

#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)

That code will print the internal data structure on # being pressed. 该代码将在按#时打印内部数据结构。 Of course I'm assuming the key passed on is a string, but I don't really know I'm not familiar with the library. 当然,我假设传递的键是一个字符串,但是我真的不知道我对库不熟悉。

here is my code according to changes. 这是我根据更改的代码。

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 clear string
    def clear_keys(self):
        self.pressed_keys = self.pressed_keys.replace(self.pressed_keys,'')

    def store_key(self,key):
        if key=='#':
            #printing the sequence of keys.
            print(self.pressed_keys)
            self.clear_keys()
        else:
            self.pressed_keys += key

keys = KeyStore()

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM