SHARE
TWEET

GPIO Control Module

hbrown0579 Aug 4th, 2015 (edited) 233 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import os
  3. import RPi.GPIO as gpio
  4. from time import sleep
  5. from evdev import uinput, ecodes as e
  6.  
  7. gpio.setmode(gpio.BCM)
  8. gpio.setup(2, gpio.IN, pull_up_down = gpio.PUD_UP)
  9. gpio.setup(3, gpio.IN, pull_up_down = gpio.PUD_UP)
  10. gpio.setup(4, gpio.IN, pull_up_down = gpio.PUD_UP)
  11. gpio.setup(5, gpio.IN, pull_up_down = gpio.PUD_UP)
  12. gpio.setup(6, gpio.IN, pull_up_down = gpio.PUD_UP)
  13. gpio.setup(7, gpio.IN, pull_up_down = gpio.PUD_UP)
  14. gpio.setup(8, gpio.IN, pull_up_down = gpio.PUD_UP)
  15. gpio.setup(9, gpio.IN, pull_up_down = gpio.PUD_UP)
  16. gpio.setup(10, gpio.IN, pull_up_down = gpio.PUD_UP)
  17. gpio.setup(11, gpio.IN, pull_up_down = gpio.PUD_UP)
  18. gpio.setup(12, gpio.IN, pull_up_down = gpio.PUD_UP)
  19. gpio.setup(13, gpio.IN, pull_up_down = gpio.PUD_UP)
  20. gpio.setup(14, gpio.IN, pull_up_down = gpio.PUD_UP)
  21. gpio.setup(15, gpio.IN, pull_up_down = gpio.PUD_UP)
  22. gpio.setup(16, gpio.IN, pull_up_down = gpio.PUD_UP)
  23. gpio.setup(17, gpio.IN, pull_up_down = gpio.PUD_UP)
  24. gpio.setup(18, gpio.IN, pull_up_down = gpio.PUD_UP)
  25. gpio.setup(19, gpio.IN, pull_up_down = gpio.PUD_UP)
  26. gpio.setup(20, gpio.IN, pull_up_down = gpio.PUD_UP)
  27. gpio.setup(21, gpio.IN, pull_up_down = gpio.PUD_UP)
  28. gpio.setup(22, gpio.IN, pull_up_down = gpio.PUD_UP)
  29. gpio.setup(23, gpio.IN, pull_up_down = gpio.PUD_UP)
  30. gpio.setup(24, gpio.IN, pull_up_down = gpio.PUD_UP)
  31. gpio.setup(25, gpio.IN, pull_up_down = gpio.PUD_UP)
  32. gpio.setup(26, gpio.IN, pull_up_down = gpio.PUD_UP)
  33. gpio.setup(27, gpio.IN, pull_up_down = gpio.PUD_UP)
  34.  
  35. p1up = 2
  36. p1down = 3
  37. p1left = 4
  38. p1right = 5
  39. p1b1 = 6
  40. p1b2 = 7
  41. btstart = 8
  42. btselect = 9
  43. p2up = 10
  44. p2down = 11
  45. p2left = 12
  46. p2right = 13
  47. p2b1 = 14
  48. p2b2 = 15
  49. btexit = 16
  50. p1coin = 17
  51. p2coin = 18
  52. p1start = 19
  53. p2start = 20
  54. l = 21
  55. r = 22
  56. x = 23
  57. y = 24
  58. p1b3 = 25
  59. p2b3 = 26
  60.  
  61. def p1uppress(channel):
  62.     with uinput.UInput() as ui:
  63.         ui.write(e.EV_KEY, e.KEY_UP, 1)
  64.         ui.write(e.EV_KEY, e.KEY_UP, 0)
  65.         ui.syn()
  66.     while gpio.input(p1up) == False:
  67.         with uinput.UInput() as ui:
  68.             ui.write(e.EV_KEY, e.KEY_UP, 1)
  69.             ui.write(e.EV_KEY, e.KEY_UP, 0)
  70.             ui.syn()
  71.  
  72. def p1downpress(channel):
  73.     with uinput.UInput() as ui:
  74.         ui.write(e.EV_KEY, e.KEY_DOWN, 1)
  75.         ui.write(e.EV_KEY, e.KEY_DOWN, 0)
  76.         ui.syn()
  77.     while gpio.input(p1down) == False:
  78.         ui.write(e.EV_KEY, e.KEY_DOWN, 1)
  79.         ui.write(e.EV_KEY, e.KEY_DOWN, 0)
  80.         ui.syn()
  81.  
  82. def p1leftpress(channel):
  83.     with uinput.UInput() as ui:
  84.         ui.write(e.EV_KEY, e.KEY_LEFT, 1)
  85.         ui.write(e.EV_KEY, e.KEY_LEFT, 0)
  86.         ui.syn()
  87.     while gpio.input(p1left) == False:
  88.         ui.write(e.EV_KEY, e.KEY_LEFT, 1)
  89.         ui.write(e.EV_KEY, e.KEY_LEFT, 0)
  90.         ui.syn()
  91.  
  92. def p1rightpress(channel):
  93.     with uinput.UInput() as ui:
  94.         ui.write(e.EV_KEY, e.KEY_RIGHT, 1)
  95.         ui.write(e.EV_KEY, e.KEY_RIGHT, 0)
  96.         ui.syn()
  97.     while gpio.input(p1right) == False:
  98.         ui.write(e.EV_KEY, e.KEY_RIGHT, 1)
  99.         ui.write(e.EV_KEY, e.KEY_RIGHT, 0)
  100.         ui.syn()
  101.  
  102. def p1b1press(channel):
  103.     with uinput.UInput() as ui:
  104.         ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 1)
  105.         ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 0)
  106.         ui.syn()
  107.  
  108. def p1b2press(channel):
  109.     with uinput.UInput() as ui:
  110.         ui.write(e.EV_KEY, e.KEY_LEFTALT, 1)
  111.         ui.write(e.EV_KEY, e.KEY_LEFTALT, 0)
  112.         ui.syn()
  113.  
  114. def p1b3press(channel):
  115.     with uinput.UInput() as ui:
  116.         ui.write(e.EV_KEY, e.KEY_SPACE, 1)
  117.         ui.write(e.EV_KEY, e.KEY_SPACE, 0)
  118.         ui.syn()
  119.  
  120. def btstartpress(channel):
  121.     with uinput.UInput() as ui:
  122.         ui.write(e.EV_KEY, e.KEY_ENTER, 1)
  123.         ui.write(e.EV_KEY, e.KEY_ENTER, 0)
  124.         ui.syn()
  125.  
  126. def btselectpress(channel):
  127.     with uinput.UInput() as ui:
  128.         ui.write(e.EV_KEY, e.KEY_RIGHTSHIFT, 1)
  129.         ui.write(e.EV_KEY, e.KEY_RIGHTSHIFT, 0)
  130.         ui.syn()
  131.  
  132. def btexitpress(channel):
  133.     with uinput.UInput() as ui:
  134.         ui.write(e.EV_KEY, e.KEY_ESC, 1)
  135.         ui.syn()
  136.         sleep(0.125)
  137.         ui.write(e.EV_KEY, e.KEY_ESC, 0)
  138.         ui.syn()
  139.         sleep(0.125)
  140.         ui.write(e.EV_KEY, e.KEY_F12, 1)
  141.         ui.write(e.EV_KEY, e.KEY_ENTER, 1)
  142.         ui.syn()
  143.         sleep(0.25)
  144.         ui.write(e.EV_KEY, e.KEY_F12, 0)
  145.         ui.write(e.EV_KEY, e.KEY_ENTER, 0)
  146.         ui.syn()
  147.  
  148. def lpress(channel):
  149.     with uinput.UInput() as ui:
  150.         ui.write(e.EV_KEY, e.KEY_Q, 1)
  151.         ui.write(e.EV_KEY, e.KEY_Q, 0)
  152.         ui.syn()
  153.  
  154. def rpress(channel):
  155.     with uinput.UInput() as ui:
  156.         ui.write(e.EV_KEY, e.KEY_W, 1)
  157.         ui.write(e.EV_KEY, e.KEY_W, 0)
  158.         ui.syn()
  159.  
  160. def xpress(channel):
  161.     with uinput.UInput() as ui:
  162.         ui.write(e.EV_KEY, e.KEY_I, 1)
  163.         ui.write(e.EV_KEY, e.KEY_I, 0)
  164.         ui.syn()
  165.  
  166. def ypress(channel):
  167.     with uinput.UInput() as ui:
  168.         ui.write(e.EV_KEY, e.KEY_O, 1)
  169.         ui.write(e.EV_KEY, e.KEY_O, 0)
  170.         ui.syn()
  171.  
  172. def p1coinpress(channel):
  173.     with uinput.UInput() as ui:
  174.         ui.write(e.EV_KEY, e.KEY_5, 1)
  175.         ui.write(e.EV_KEY, e.KEY_5, 0)
  176.         ui.syn()
  177.  
  178. def p2coinpress(channel):
  179.     with uinput.UInput() as ui:
  180.         ui.write(e.EV_KEY, e.KEY_6, 1)
  181.         ui.write(e.EV_KEY, e.KEY_6, 0)
  182.         ui.syn()
  183.  
  184. def p1startpress(channel):
  185.     with uinput.UInput() as ui:
  186.         ui.write(e.EV_KEY, e.KEY_1, 1)
  187.         ui.write(e.EV_KEY, e.KEY_1, 0)
  188.         ui.syn()
  189.  
  190. def p2startpress(channel):
  191.     with uinput.UInput() as ui:
  192.         ui.write(e.EV_KEY, e.KEY_2, 1)
  193.         ui.write(e.EV_KEY, e.KEY_2, 0)
  194.         ui.syn()
  195.  
  196. def p2uppress(channel):
  197.     with uinput.UInput() as ui:
  198.         ui.write(e.EV_KEY, e.KEY_R, 1)
  199.         ui.write(e.EV_KEY, e.KEY_R, 0)
  200.         ui.syn()
  201.  
  202. def p2downpress(channel):
  203.     with uinput.UInput() as ui:
  204.         ui.write(e.EV_KEY, e.KEY_F, 1)
  205.         ui.write(e.EV_KEY, e.KEY_F, 0)
  206.         ui.syn()
  207.  
  208. def p2leftpress(channel):
  209.     with uinput.UInput() as ui:
  210.         ui.write(e.EV_KEY, e.KEY_D, 1)
  211.         ui.write(e.EV_KEY, e.KEY_D, 0)
  212.         ui.syn()
  213.  
  214. def p2rightpress(channel):
  215.     with uinput.UInput() as ui:
  216.         ui.write(e.EV_KEY, e.KEY_G, 1)
  217.         ui.write(e.EV_KEY, e.KEY_G, 0)
  218.         ui.syn()
  219.  
  220. def p2b1press(channel):
  221.     with uinput.UInput() as ui:
  222.         ui.write(e.EV_KEY, e.KEY_A, 1)
  223.         ui.write(e.EV_KEY, e.KEY_A, 0)
  224.         ui.syn()
  225.  
  226. def p2b2press(channel):
  227.     with uinput.UInput() as ui:
  228.         ui.write(e.EV_KEY, e.KEY_S, 1)
  229.         ui.write(e.EV_KEY, e.KEY_S, 0)
  230.         ui.syn()
  231.  
  232. def p2b3press(channel):
  233.     with uinput.UInput() as ui:
  234.         ui.write(e.EV_KEY, e.KEY_Q, 1)
  235.         ui.write(e.EV_KEY, e.KEY_Q, 0)
  236.         ui.syn()
  237.  
  238. gpio.add_event_detect(p1up, gpio.RISING, callback=p1uppress)
  239. gpio.add_event_detect(p1down, gpio.RISING, callback=p1downpress)
  240. gpio.add_event_detect(p1left, gpio.RISING, callback=p1leftpress)
  241. gpio.add_event_detect(p1right, gpio.RISING, callback=p1rightpress)
  242. gpio.add_event_detect(p1b1, gpio.RISING, callback=p1b1press)
  243. gpio.add_event_detect(p1b2, gpio.RISING, callback=p1b2press)
  244. gpio.add_event_detect(p1b3, gpio.RISING, callback=p1b3press)
  245. gpio.add_event_detect(btstart, gpio.RISING, callback=btstartpress)
  246. gpio.add_event_detect(btselect, gpio.RISING, callback=btselectpress)
  247. gpio.add_event_detect(btexit, gpio.RISING, callback=btexitpress)
  248. gpio.add_event_detect(l, gpio.RISING, callback=lpress)
  249. gpio.add_event_detect(r, gpio.RISING, callback=rpress)
  250. gpio.add_event_detect(x, gpio.RISING, callback=xpress)
  251. gpio.add_event_detect(y, gpio.RISING, callback=ypress)
  252. gpio.add_event_detect(p1coin, gpio.RISING, callback=p1coinpress)
  253. gpio.add_event_detect(p2coin, gpio.RISING, callback=p2coinpress)
  254. gpio.add_event_detect(p1start, gpio.RISING, callback=p1startpress)
  255. gpio.add_event_detect(p2start, gpio.RISING, callback=p2startpress)
  256. gpio.add_event_detect(p2up, gpio.RISING, callback=p2uppress)
  257. gpio.add_event_detect(p2down, gpio.RISING, callback=p2downpress)
  258. gpio.add_event_detect(p2left, gpio.RISING, callback=p2leftpress)
  259. gpio.add_event_detect(p2right, gpio.RISING, callback=p2rightpress)
  260. gpio.add_event_detect(p2b1, gpio.RISING, callback=p2b1press)
  261. gpio.add_event_detect(p2b3, gpio.RISING, callback=p2b2press)
  262. gpio.add_event_detect(p2b3, gpio.RISING, callback=p2b3press)
  263.  
  264. while True:
  265.     sleep(0.01)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top