/ GemmaM0_Pinball / code.py
code.py
 1  # SPDX-FileCopyrightText: 2022 john park & tod kurt for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  # Gemma IO demo - Keyboard emu
 4  # iCade Pinball Edition
 5  
 6  import board
 7  from digitalio import DigitalInOut, Pull
 8  from adafruit_debouncer import Debouncer
 9  import usb_hid
10  from adafruit_hid.keyboard import Keyboard
11  from adafruit_hid.keycode import Keycode
12  
13  # Allows three buttons on a Gemma M0 to control iCade standard Pinball Arcade
14  # game on iOS using USB to Lightning "camera connector"
15  
16  # iCade keyboard mappings
17  # See developer doc at: http://www.ionaudio.com/products/details/icade
18  
19  #    WE     YT UF IM OG
20  # AQ< -->DC
21  #    XZ     HR JN KP LV
22  
23  #control key is triggered by a press, doesn't repeat, second control key is
24  #triggered by a release
25  
26  # define buttons
27  num_keys = 3
28  pins = (
29      board.D0, # D0
30      board.D1, # D1
31      board.D2 # D2
32  )
33  
34  keys = []
35  
36  # The keycode pair sent for each button:
37  # D0 is left flipper -  iCade key sequence (hold, release) is "hr"
38  # D1 is right flipper - iCade key sequence (hold, release) is "lv"
39  # D2 is plunger -       iCade key sequence (hold, release) is "xz"
40  
41  for pin in pins:
42      tmp_pin = DigitalInOut(pin)
43      tmp_pin.pull = Pull.UP
44      keys.append(Debouncer(tmp_pin))
45  
46  keymap_pressed = {
47      (0): ("Left Paddle", [Keycode.H]),
48      (1): ("Right Paddle", [Keycode.L]),
49      (2): ("Plunger", [Keycode.X])
50  }
51  keymap_released = {
52      (0): ("Left Paddle", [Keycode.R]),
53      (1): ("Right Paddle", [Keycode.V]),
54      (2): ("Plunger", [Keycode.Z])
55  }
56  
57  # the keyboard object
58  kbd = Keyboard(usb_hid.devices)
59  
60  print("\nWelcome to keypad")
61  print("keymap:")
62  for k in range(num_keys):
63      print("\t", (keymap_pressed[k][0]))
64  print("Waiting for button presses")
65  
66  
67  while True:
68      for i in range(num_keys):
69          keys[i].update()
70          if keys[i].fell:
71              print(keymap_pressed[i][0])
72              kbd.send(*keymap_pressed[i][1])
73          if keys[i].rose:
74              print(keymap_released[i][0])
75              kbd.send(*keymap_released[i][1])