/ PyRuler_Video_Panic / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 John Thurmond for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import os 6 import board 7 from digitalio import DigitalInOut, Direction 8 import time 9 import touchio 10 11 # Set this to True to turn the touchpads into a keyboard 12 ENABLE_KEYBOARD = True 13 14 # Used if we do HID output, see below 15 if ENABLE_KEYBOARD: 16 from adafruit_hid.keyboard import Keyboard 17 from adafruit_hid.keycode import Keycode 18 from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS 19 import usb_hid 20 kbd = Keyboard(usb_hid.devices) 21 layout = KeyboardLayoutUS(kbd) 22 23 #print(dir(board), os.uname()) # Print a little about ourselves 24 25 led = DigitalInOut(board.D13) 26 led.direction = Direction.OUTPUT 27 28 touches = [DigitalInOut(board.CAP0)] 29 for p in (board.CAP1, board.CAP2, board.CAP3): 30 touches.append(touchio.TouchIn(p)) 31 32 leds = [] 33 for p in (board.LED4, board.LED5, board.LED6, board.LED7): 34 led = DigitalInOut(p) 35 led.direction = Direction.OUTPUT 36 led.value = True 37 time.sleep(0.25) 38 leds.append(led) 39 for led in leds: 40 led.value = False 41 42 43 cap_touches = [False, False, False, False] 44 45 def read_caps(): 46 t0_count = 0 47 t0 = touches[0] 48 t0.direction = Direction.OUTPUT 49 t0.value = True 50 t0.direction = Direction.INPUT 51 # funky idea but we can 'diy' the one non-hardware captouch device by hand 52 # by reading the drooping voltage on a tri-state pin. 53 t0_count = t0.value + t0.value + t0.value + t0.value + t0.value + \ 54 t0.value + t0.value + t0.value + t0.value + t0.value + \ 55 t0.value + t0.value + t0.value + t0.value + t0.value 56 cap_touches[0] = t0_count > 2 57 cap_touches[1] = touches[1].raw_value > 3000 58 cap_touches[2] = touches[2].raw_value > 3000 59 cap_touches[3] = touches[3].raw_value > 3000 60 return cap_touches 61 62 while True: 63 caps = read_caps() 64 print(caps) 65 # light up the matching LED 66 for i,c in enumerate(caps): 67 leds[i].value = c 68 if caps[0]: 69 if ENABLE_KEYBOARD: 70 # Zoom 71 kbd.press(Keycode.ALT, Keycode.V) 72 kbd.release(Keycode.V) 73 time.sleep(0.25) 74 kbd.press(Keycode.A) 75 kbd.release_all() 76 if caps[1]: 77 if ENABLE_KEYBOARD: 78 # Teams 79 # Note that video toggle doesn't work in the web app 80 kbd.press(Keycode.CONTROL, Keycode.SHIFT, Keycode.M) 81 kbd.release(Keycode.M) 82 time.sleep(0.5) 83 kbd.press(Keycode.O) 84 kbd.release_all() 85 if caps[2]: 86 if ENABLE_KEYBOARD: 87 # Skype 88 kbd.press(Keycode.CONTROL, Keycode.M) 89 kbd.release(Keycode.M) 90 time.sleep(0.5) 91 kbd.press(Keycode.SHIFT, Keycode.K) 92 kbd.release_all() 93 if caps[3]: 94 if ENABLE_KEYBOARD: 95 # Jitsi 96 kbd.press(Keycode.M) 97 kbd.release(Keycode.M) 98 time.sleep(0.5) 99 kbd.press(Keycode.V) 100 kbd.release_all() 101 time.sleep(.2)