/ Visualizer_Touch_Control / code.py
code.py
1 # SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import busio 8 from adafruit_hid.keyboard import Keyboard 9 from adafruit_hid.keycode import Keycode 10 import usb_hid 11 import adafruit_mpr121 12 13 # Create I2C bus. 14 i2c = busio.I2C(board.SCL, board.SDA) 15 # Create MPR121 object. 16 mpr121 = adafruit_mpr121.MPR121(i2c) 17 # Note you can optionally change the address of the device: 18 # mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91) 19 kbd = Keyboard(usb_hid.devices) 20 keylist = [ 21 Keycode.RIGHT_ARROW, 22 Keycode.ONE, 23 Keycode.TWO, 24 Keycode.THREE, 25 Keycode.FOUR, 26 Keycode.W, 27 Keycode.A, 28 Keycode.S, 29 Keycode.D, 30 Keycode.B, 31 Keycode.I, 32 Keycode.H, 33 ] 34 35 # Loop forever testing each input and sending keystrokes when they're touched. 36 while True: 37 # Loop through all 12 inputs (0-11). 38 for i in range(12): 39 # Call is_touched and pass it then number of the input. If it's touched 40 # it will return True, otherwise it will return False. 41 if mpr121[i].value: 42 # print("Input {} touched!".format(i)) 43 kbd.send(keylist[i]) 44 time.sleep(0.15) # Small delay to keep from spamming output messages.