【问题标题】:Read multiple digits from 4x4 keypad using raspberry pi 3使用树莓派 3 从 4x4 键盘读取多个数字
【发布时间】:2018-06-06 14:07:10
【问题描述】:

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

【问题讨论】:

    标签: python raspberry-pi3 lcd keypad


    【解决方案1】:

    感谢您向我展示。该代码不会做任何事情。在注册键之后,您必须对存储的数据结构进行实际操作。

    例如:

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

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

    【讨论】:

    • 嘿 csevier,非常感谢您的回复。我认为 python 中没有用于 list 的 clear() 函数。而且我还需要一个数字或字符串,而不是列表。所以像下面这样更改代码。现在它的工作就像我需要的那样。谢谢你的好意和理解我的小脑袋。 :)
    • 嗨 csevier,我还有一个问题,我如何使用“keypad.registerKeyPressHandler(keys.store_key)”调用其他函数。这是pad4pi库的链接,如果需要参考:github.com/brettmclean/pad4pi
    • 嘿 Mohan,我真的很喜欢帮助你,但其他问题可能应该通过单独的帖子来解决。这也有助于其他人找到并获得帮助。如果您不介意为此选择一个答案,您可以将另一个堆栈溢出帖子与您的新问题链接起来。
    • 好的。我现在得到了。下次我会发布新问题。谢谢。
    【解决方案2】:

    我这样做了,但我还是卡住了

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

    但它什么也没做。

    【讨论】:

      【解决方案3】:

      这是我根据更改的代码。

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

      【讨论】:

        【解决方案4】:

        可以通过使用数据结构来存储按下的值来促进存储键序列。然后在需要重置时清除它。

        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)
        

        【讨论】:

        • 您好 csevier,感谢您的回复。但是当我尝试上面的代码时,代码什么也不做。在我在终端中运行代码后,程序立即终止。
        • 你能告诉我你的 repl 交互吗?还是您通过 .py 文件运行它?
        • 我想我可能会理解这种困惑。该印刷品是如何使用它的一个例子。它将打印一个空列表,您只需运行它即可。您将不得不在其他事件中按键并对其进行操作。
        • 是的,我正在通过 .py 文件运行它。如果我使用下面的 while 循环,我会无限循环打印第一个按下的按钮。 while(1): for key in keys.pressed_keys: print(key)
        • 嘿,csevier,我刚刚发布了我的代码,请您检查一下。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-10
        • 2021-07-01
        • 2018-02-07
        • 2017-03-30
        • 2023-01-30
        • 1970-01-01
        • 2016-04-24
        相关资源
        最近更新 更多