Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. # GPIOkbd.py
  2. # written by Roger Woollett
  3. # modify by pondahai 2019.10.3
  4.  
  5. # This is a python equivalent to the Adafruit Retrogame program.
  6. # It translates GPIO button presses into keyboard presses.
  7. # It assumes that buttons will gound their GPIO when pressed.
  8. # All testing has been done using python3
  9. # This program must have root priviledge. This is fine if run from rc.local
  10. # but if you are testing use sudo python3 GPIOkbd.py
  11.  
  12. # You must install uinport for this to work
  13. # sudo pip3 install python-uinport
  14. # Help(ui) shows a list of the symbols that represent keyboard keys.
  15.  
  16. # You can also define a button that causes system shutdown when pressed
  17. # for more than 2 seconds.
  18.  
  19. import uinput as ui
  20. import RPi.GPIO as gp
  21. from os import system
  22. from time import sleep
  23.  
  24. # create bindings between keys and gpio
  25. # TODO - change this to suit your needs
  26. bindings = ((ui.KEY_ENTER,20),(ui.KEY_SPACE,21),(ui.KEY_UP,16),(ui.KEY_RIGHT,19),(ui.KEY_LEFT,13),(ui.KEY_DOWN,26))
  27.  
  28. # define gpio for shutdown (exit) button
  29. # comment out the last line of this program
  30. # if you do not want to cause a system shutdown
  31. # TODO change this to suit your needs
  32. # if you do not want a shutdown key set to 0
  33. SHUTDOWN_GPIO = 20
  34.  
  35. # make sure kernal module is loaded
  36. system("modprobe uinput")
  37.  
  38. # always use Broadcom numbers
  39. gp.setmode(gp.BCM)
  40.  
  41. oldkey = [1]*27
  42.  
  43. class KeyBtn:
  44. # class to associate a GPIO button with a keyboard press
  45. def __init__(self,device,key,gpio):
  46. self.device = device
  47. self.key = key
  48. gp.setup(gpio,gp.IN,pull_up_down = gp.PUD_UP)
  49. gp.add_event_detect(gpio,gp.BOTH,callback = self.callback)
  50. def key_process(self, channel):
  51. if gp.input(channel) == 0:
  52. self.device.emit(self.key, 1)
  53. else:
  54. self.device.emit(self.key, 0)
  55. # global oldkey
  56. # sleep(0.04)
  57. # gpinput = gp.input(channel)
  58. # if gpinput != oldkey[channel]:
  59. # sleep(0.1)
  60. # if gpinput != oldkey[channel]:
  61. # oldkey[channel] = gpinput
  62. # if gpinput == 0:
  63. # self.device.emit(self.key, 1)
  64. # else:
  65. # self.device.emit(self.key, 0)
  66. def callback(self,channel):
  67. # because of key bounce check button is really down
  68. #print(oldkey)
  69. #sleep(0.05)
  70. self.key_process(channel)
  71.  
  72. class ExitBtn:
  73. # class to implement a button that causes program to terminate
  74. def __init__(self,gpio):
  75. if gpio != 0:
  76. gp.setup(gpio,gp.IN,pull_up_down = gp.PUD_UP)
  77. self.gpio = gpio
  78.  
  79. def check_continue(self):
  80. if self.gpio == 0:
  81. return True
  82.  
  83. # return False if button pressed for longer than 2 seconds
  84. count = 20
  85. while count:
  86. if gp.input(self.gpio):
  87. # button is not pressed
  88. return True
  89. sleep(0.1)
  90. count -= 1
  91. # if we get here it is time to exit
  92. return False
  93.  
  94. # create uinput device
  95. events = []
  96. for(key,gpio) in bindings:
  97. events.append(key)
  98. device = ui.Device(events)
  99.  
  100. # create KeyBtn objects
  101. for(key,gpio) in bindings:
  102. KeyBtn(device,key,gpio)
  103.  
  104. # This button causes program to exit
  105. exit_button = ExitBtn(SHUTDOWN_GPIO)
  106.  
  107. # check if we should exit every half second
  108. while exit_button.check_continue():
  109. for(key,gpio) in bindings:
  110. if gp.input(gpio) == 1:
  111. device.emit(key,0)
  112. sleep(0.5)
  113.  
  114. # All done so exit
  115. device.destroy()
  116. gp.cleanup()
  117.  
  118. #system("poweroff")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement