def main(): hid = HIDService() advertisement = ProvideServicesAdvertisement(hid) advertisement.appearance = 961 ble = adafruit_ble.BLERadio() if ble.connected: for c in ble.connections: c.disconnect() ble.start_advertising(advertisement) advertising = True ble_keyboard = Keyboard(hid.devices) matrix = Matrix() usb_keyboard = Keyboard(usb_hid.devices) while True: pressed_keys, released_keys, new_keys = matrix.scan() if released_keys: released_keycodes = list(map(lambda i: KEYMAP[i], released_keys)) print('released keys {}'.format(released_keycodes)) usb_keyboard.release(*released_keycodes) if ble.connected: advertising = False ble_keyboard.release(*released_keycodes) if new_keys: new_keycodes = list(map(lambda i: KEYMAP[i], new_keys)) print('new keys {}'.format(new_keycodes)) usb_keyboard.press(*new_keycodes) if ble.connected: advertising = False ble_keyboard.press(*new_keycodes) if not ble.connected and not advertising: ble.start_advertising(advertisement) advertising = True time.sleep(0.001)
btn2.direction = digitalio.Direction.INPUT keyboard = Keyboard(usb_hid.devices) while True: print(btn1.value, btn2.value) keys = [ (btn1, Keycode.A), (btn2, Keycode.B) ] key_press_flags = [ False, False, ] for i, (btn, keycode) in enumerate(keys): if not btn.value: #Press keyboard.press(keycode) key_press_flags[i] = True led.value = True time.sleep(0.5) for i, (btn, keycode) in enumerate(keys): if key_press_flags[i]: keyboard.release(keycode) led.value = False time.sleep(0.1)
from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode button_1 = DigitalInOut(board.D0) button_2 = DigitalInOut(board.D1) button_1.direction = Direction.INPUT button_2.direction = Direction.INPUT button_1.pull = Pull.UP button_2.pull = Pull.UP keyboard = Keyboard(usb_hid.devices) print("waiting for a pin press") while True: # pull up means we have to check for False if not button_1.value: print("REC pressed") keyboard.press(Keycode.CONTROL, Keycode.ALT, Keycode.R) time.sleep(0.05) keyboard.release(Keycode.CONTROL, Keycode.ALT, Keycode.R) time.sleep(1) if not button_2.value: print("Pause pressed") keyboard.press(Keycode.CONTROL, Keycode.ALT, Keycode.P) time.sleep(0.05) keyboard.release(Keycode.CONTROL, Keycode.ALT, Keycode.P) time.sleep(1)
# Compare old and new state oldState = state newState = [] newBtn = None for button in buttonMap: r = pins[button["row"]] r.value = True if pins[button["col"]].value: newState += [button["id"]] if not button["id"] in oldState: newBtn = button["id"] r.value = False # Press & release keys for oldID in oldState: if not oldID in newState: kbd.release(buttonIDtoKeycode[oldID]) dot[0] = (0, 0, 0) if newBtn: kbd.press(buttonIDtoKeycode[newBtn]) dot[0] = (255, 0, 0) state = newState #new stuff counter_array = ['mode 1','mode 2'] keys_pressed = { 'mode 1': ['A', 'B', 'C', 'D', 'E'], 'mode 2': ['F', 'G', 'H', 'I', 'J']} print(keys_pressed[counter_array[0]][0]) # CircuitPython demo - Keyboard emulator
# get actions for this icon from config object _cur_actions = touch_deck_config["layers"][current_layer]["shortcuts"][index][ "actions"] # tuple means it's a single action if isinstance(_cur_actions, tuple): # put it in a list by itself _cur_actions = [_cur_actions] # loop over the actions for _action in _cur_actions: # HID keyboard keys if _action[0] == KEY: kbd.press(*_action[1]) kbd.release(*_action[1]) # String to write from layout elif _action[0] == STRING: kbd_layout.write(_action[1]) # Consumer control code elif _action[0] == MEDIA: cc.send(_action[1]) # Key press elif _action[0] == KEY_PRESS: kbd.press(*_action[1]) # Key release elif _action[0] == KEY_RELEASE:
boton3.pull = digitalio.Pull.DOWN boton4 = digitalio.DigitalInOut(boton4_pin) boton4.direction = digitalio.Direction.INPUT boton4.pull = digitalio.Pull.DOWN boton5 = digitalio.DigitalInOut(boton5_pin) boton5.direction = digitalio.Direction.INPUT boton5.pull = digitalio.Pull.DOWN while True: if boton1.value: print("Botón 1 Guardar") teclado.press(Keycode.CONTROL, Keycode.S) time.sleep(0.1) teclado.release(Keycode.CONTROL, Keycode.S) if boton2.value: print("Botón 2 Exportar un fotograma") teclado.press(Keycode.CONTROL, Keycode.SHIFT, Keycode.E) time.sleep(0.1) teclado.release(Keycode.CONTROL, Keycode.SHIFT, Keycode.E) if boton3.value: print("Botón 3 Exportar Medios") teclado.press(Keycode.CONTROL, Keycode.M) time.sleep(0.1) teclado.release(Keycode.CONTROL, Keycode.M) if boton4.value: print("Botón 4 Deshacer") teclado.press(Keycode.CONTROL, Keycode.Z) time.sleep(0.1) teclado.release(Keycode.CONTROL, Keycode.Z)
sleep(delaytime) clock.value = False sleep(delaytime) button_status[buttons[x + 1]] = data.value if button_status['select'] == False and button_status[ 'start'] == False and button_status['up'] == False: if mode == 2: mode = 1 else: mode = 2 keyboard.release_all() print(mode) sleep(0.3) press_buttons = [] release_buttons = [] for x in buttons: if mode == 2: if button_status[buttons[x]] == True: gamepad.release_buttons(x + 1) else: gamepad.press_buttons(x + 1) else: if button_status[buttons[x]] == True: release_buttons.append(keyboard_buttons[buttons[x]]) else: press_buttons.append(keyboard_buttons[buttons[x]]) if mode == 1: keyboard.press(*press_buttons) keyboard.release(*release_buttons)
import time import board import digitalio import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode usb_keyboard = Keyboard(usb_hid.devices) button = digitalio.DigitalInOut(board.USR_BTN) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP last_value = button.value while True: if last_value != button.value: last_value = button.value if not button.value: print('Button is pressed') usb_keyboard.press(Keycode.A) else: print('Button is released') usb_keyboard.release(Keycode.A)
switch = [] for x in range(13): temp = DigitalInOut(getattr(board,f"GP{x}")) temp.switch_to_input(Pull.UP) switch.append(temp) continue while True: # We could also do "led.value = not switch.value"! if switch[0].value: ledB.value = False else: ledB.value = True kbd.send(Keycode.F13) time.sleep(0.1) kbd.release(Keycode.F13) if switch[1].value: ledB.value = False else: ledB.value = True kbd.send(Keycode.F14) time.sleep(0.1) kbd.release(Keycode.F14) if switch[2].value: ledB.value = False else: ledB.value = True kbd.send(Keycode.F15) time.sleep(0.1)
dot[0] = (0, 15, 0) sleep(2) # The main loop - hold the right mouse button for 2 minutes, then scan left and right while True: dot[0] = (15, 0, 0) mouse.press(Mouse.RIGHT_BUTTON) sleep(120) mouse.release(Mouse.RIGHT_BUTTON) sleep(1) dot[0] = (0, 15, 0) key = Keycode.A keyboard.press(key) sleep(0.1) keyboard.release(key) sleep(0.3) dot[0] = (0, 0, 127) key = Keycode.D keyboard.press(key) sleep(0.1) keyboard.release(key) sleep(1)
# Tell the device to act like a keyboard. keyboard = Keyboard(usb_hid.devices) # Send a keypress of ESCAPE keyboard.send(Keycode.ESCAPE) # Send CTRL-A (select all in most text editors) keyboard.send(Keycode.CONTROL, Keycode.A) # You can also control key press and release manually: keyboard.press(Keycode.CONTROL, Keycode.A) keyboard.release_all() keyboard.press(Keycode.ESCAPE) keyboard.release(Keycode.ESCAPE) def type_keys(characters): character = { 'a': Keycode.A, 'b': Keycode.B, 'c': Keycode.C, 'd': Keycode.D, 'e': Keycode.E, 'f': Keycode.F, 'g': Keycode.G, 'h': Keycode.H, 'i': Keycode.I, 'j': Keycode.J, 'k': Keycode.K,
import time import digitalio import board import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode boton1_pin = board.GP15 teclado = Keyboard(usb_hid.devices) boton1 = digitalio.DigitalInOut(boton1_pin) boton1.direction = digitalio.Direction.INPUT boton1.pull = digitalio.Pull.DOWN while True: if boton1.value: print("Botón 1 Seleccionar Todo") teclado.press(Keycode.CONTROL, Keycode.A) time.sleep(0.1) teclado.release(Keycode.CONTROL, Keycode.A) time.sleep(0.1)
[Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.TWO], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.THREE], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.FOUR], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.FIVE], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.SIX], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.SEVEN], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.EIGHT], [Keycode.RIGHT_CONTROL, Keycode.RIGHT_ALT, Keycode.NINE]] keyboard = Keyboard(usb_hid.devices) buttons = [digitalio.DigitalInOut(bp) for bp in button_pins] for btn in buttons: btn.direction = digitalio.Direction.INPUT btn.pull = digitalio.Pull.UP while True: last_pressed = [False for _ in button_pins] for ix, btn in enumerate(buttons): if not btn.value: last_pressed[ix] = True for ix, pressed in enumerate(last_pressed): if pressed: keyboard.press(*button_mapping[ix]) time.sleep(0.01) keyboard.release(*button_mapping[ix]) time.sleep(0.25) time.sleep(0.001)
layout = KeyboardLayoutUS(kbd) for pin in buttonpins: button = DigitalInOut(pin) button.direction = Direction.INPUT button.pull = Pull.DOWN buttons.append(button) led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT print("Waiting for button presses") while True: for button in buttons: if button.value: print("Button #%d Pressed" % i) i = buttons.index(button) led.value = True k = buttonkeys[i] if type(k) is str: layout.write(k) else: kbd.press(controlkey, k) led.value = False else: i = buttons.index(button) k = buttonkeys[i] kbd.release(controlkey, k)
led4.value = False led5.value = False led6.value = False led7.value = False led8.value = False led9.value = False led10.value = False led11.value = False led12.value = True else: pass if not btn1.value: keyboard.press(key_Ctrl, key_Keypad1) time.sleep(0.1) keyboard.release(key_Ctrl, key_Keypad1) selectedInput = 1 elif not btn2.value: keyboard.press(key_Ctrl, key_Keypad2) time.sleep(0.1) keyboard.release(key_Ctrl, key_Keypad2) selectedInput = 2 elif not btn3.value: keyboard.press(key_Ctrl, key_Keypad3) time.sleep(0.1) keyboard.release(key_Ctrl, key_Keypad3) selectedInput = 3 elif not btn4.value: keyboard.press(key_Ctrl, key_Keypad4) time.sleep(0.1) keyboard.release(key_Ctrl, key_Keypad4)
kbd.press(keymap[keycode_LUT.index(key_event.key_number)][1]) print(keymap[keycode_LUT.index(key_event.key_number)][1]) key_pixels[pixel_LUT.index(key_event.key_number)] = RED elif shift_mod is True and ctrl_mod is False: kbd.press(Keycode.SHIFT, keymap[keycode_LUT.index(key_event.key_number)][1]) print(keymap[keycode_LUT.index(key_event.key_number)][1]) key_pixels[pixel_LUT.index(key_event.key_number)] = RED elif shift_mod is False and ctrl_mod is True: kbd.press(Keycode.CONTROL, keymap[keycode_LUT.index(key_event.key_number)][1]) print(keymap[keycode_LUT.index(key_event.key_number)][1]) key_pixels[pixel_LUT.index(key_event.key_number)] = RED elif shift_mod is True and ctrl_mod is True: kbd.press( Keycode.SHIFT, Keycode.CONTROL, keymap[keycode_LUT.index(key_event.key_number)][1] ) print(keymap[keycode_LUT.index(key_event.key_number)][1]) key_pixels[pixel_LUT.index(key_event.key_number)] = RED board_pix[0] = WHITE if key_event.released: if keymap[keycode_LUT.index(key_event.key_number)][0] == 1: # un-shift shift_mod = False elif keymap[keycode_LUT.index(key_event.key_number)][0] == 2: # un-ctrl ctrl_mod = False kbd.release(keymap[keycode_LUT.index(key_event.key_number)][1]) key_pixels[pixel_LUT.index(key_event.key_number)] = WHITE board_pix[0] = BLUE
class CPKFKeyboard: def __init__(self, keymap, scan_method): self.scan_method = scan_method self.keymap = keymap self.currentLayer = 0 self.layerHistory = [self.currentLayer] self.press = [] for i in range(len(keymap[0])): self.press.append(None) usb_status = len(usb_hid.devices) try: if (usb_status): self.adakbd = Keyboard(usb_hid.devices) self.mouse = Mouse(usb_hid.devices) else: self.adakbd = None self.mouse = None except OSError: self.adakbd = None self.mouse = None def updateHIDdevice(self, hid_device=None): if self.adakbd: try: self.release_all() except OSError: pass if hid_device: self.hid_device = hid_device elif supervisor.runtime.serial_connected: self.hid_device = usb_hid.devices print(self.hid_device) if self.hid_device: self.adakbd = Keyboard(self.hid_device) self.mouse = Mouse(self.hid_device) def start(self): print(TAPPING_TERM_FORCE_HOLD if "TTFH is True" else "TTFH is False") self.scan_method(self) def physicalKeypress(self, i): if not self.press[i]: if TAPPING_TERM_FORCE_HOLD: for k in self.press: if isinstance(k, KeyObject): k.force_tick(self) k = self.keymap[self.layerHistory[-1]][i] if k: print("Button #%d Pressed" % i) self.press[i] = k self.keyPress(k) def keyPress(self, k): if (self.adakbd is None) and supervisor.runtime.serial_connected: self.adakbd = Keyboard(usb_hid.devices) self.mouse = Mouse(usb_hid.devices) if type(k) is int: modifier_bit = k >> 8 if modifier_bit: self.adakbd.report_modifier[0] |= modifier_bit self.adakbd.press(k & 0xFF) elif isinstance(k, KeyObject): k.press(self, time.monotonic()) def physicalKeyrelease(self, i): if self.press[i]: print("Button #%d released" % i) k = self.press[i] if k: self.keyRelease(k) self.press[i] = None def keyRelease(self, k): if type(k) is int: modifier_bit = k >> 8 if modifier_bit: self.adakbd.report_modifier[0] &= ~modifier_bit self.adakbd.release(k & 0xFF) elif isinstance(k, KeyObject): k.release(self, time.monotonic()) def send(self, kc): # kbd.send()だとmodifierの情報が消える self.adakbd.press(kc) self.adakbd.release(kc) def release_all(self): self.adakbd.release_all() def tick(self, t): for k in self.press: if isinstance(k, KeyObject): k.tick(self, t) def changeLayer(self, to): self.layerHistory.append(to) def backLayer(self, current): self.layerHistory.remove(current)
cap_touches[1] = touches[1].raw_value > 3000 cap_touches[2] = touches[2].raw_value > 3000 cap_touches[3] = touches[3].raw_value > 3000 return cap_touches while True: caps = read_caps() print(caps) # light up the matching LED for i,c in enumerate(caps): leds[i].value = c if caps[0]: if ENABLE_KEYBOARD: # Zoom kbd.press(Keycode.ALT, Keycode.V) kbd.release(Keycode.V) time.sleep(0.25) kbd.press(Keycode.A) kbd.release_all() if caps[1]: if ENABLE_KEYBOARD: # Teams # Note that video toggle doesn't work in the web app kbd.press(Keycode.CONTROL, Keycode.SHIFT, Keycode.M) kbd.release(Keycode.M) time.sleep(0.5) kbd.press(Keycode.O) kbd.release_all() if caps[2]: if ENABLE_KEYBOARD: # Skype
# update state of the buttons inputs[i].update() # if you press the center button for a long press if inputs[0].long_press: # sends space key # used in Doom for use/open kbd.send(Keycode.SPACE) # if a press is detected... if inputs[i].pressed: # if in streaming mode if chill: # send the streaming keycodes kbd.press(CHILL_CODES[i]) # if in doom mode if doom: # send the doom keycodes kbd.press(DOOM_CODES[i]) # if a button is released... if inputs[i].released: # if in streaming mode if chill: # release the streaming keycodes kbd.release(CHILL_CODES[i]) # if in doom mode if doom: # release the doom keycodes kbd.release(DOOM_CODES[i]) # if BLE disconnects, begin advertising again ble.start_advertising(advertisement)
timer = 0 cpx.pixels.brightness = 0.01 cpx.pixels.fill((255, 0, 0)) #print(lines) while True: if cpx.button_a: for line in lines: line = line.split() print(line) if line[0] == "STRING": layout.write(' '.join(line[1:])) setter = 1 if line[0] == "DELAY": sleep(int(line[1])) timer = 1 if not setter and not timer: for l in line: kbd.press(command_dispatcher[l]) print("Done") for l in line: kbd.release(command_dispatcher[l]) print("------") setter = 0 timer = 0 #sleep(0.8) cpx.pixels.fill((0, 255, 0))
if cleanStartup: if buttons[0].value: # if key was up last time, this is a press. if buttonState[0] == False: kbd.press(controlKey, shiftKey) buttonState[0] = True # if not, was it down last frame? else: # if key was down last time, this is a release if buttonState[0] == True: kbd.release(controlKey, shiftKey) buttonState[0] = False # if hardware key is down, if buttons[1].value: # if key was up last time, this is a press. if buttonState[1] == False: kbd.press(controlKey, altKey) buttonState[1] = True # if not, was it down last frame? else:
BLACK = 0x000000 pixel_pin = board.D9 pixels = neopixel.NeoPixel(pixel_pin, 2, brightness=1.0) pixels.fill(BLACK) time.sleep(0.3) pixels.fill(WHITE) time.sleep(0.3) pixels.fill(BLACK) time.sleep(0.3) pixels[0] = MAGENTA pixels[1] = CYAN while True: switch_a.update() # Debouncer checks for changes in switch state switch_b.update() if switch_a.fell: keyboard.press(switch_a_output) pixels[0] = WHITE if switch_a.rose: keyboard.release(switch_a_output) pixels[0] = MAGENTA if switch_b.fell: keyboard.press(switch_b_output) pixels[1] = WHITE if switch_b.rose: keyboard.release(switch_b_output) pixels[1] = CYAN
user_button_pressed = False reset_button_pressed = False last_position = encoder.position while True: if mute_button_pressed: if mute_button.value: mute_button_pressed = False else: if not mute_button.value: cc.send(ConsumerControlCode.MUTE) mute_button_pressed = True if user_button_pressed: if user_button.value: kbd.release(Keycode.LEFT_CONTROL, Keycode.LEFT_ALT, Keycode.RIGHT_ALT) user_button_pressed = False else: if not user_button.value: kbd.press(Keycode.LEFT_CONTROL, Keycode.LEFT_ALT, Keycode.RIGHT_ALT) user_button_pressed = True if reset_button_pressed: if reset_button.value: kbd.release(Keycode.LEFT_SHIFT, Keycode.LEFT_CONTROL, Keycode.LEFT_ALT, Keycode.RIGHT_ALT) reset_button_pressed = False else: if not reset_button.value: kbd.press(Keycode.LEFT_SHIFT, Keycode.LEFT_CONTROL, Keycode.LEFT_ALT, Keycode.RIGHT_ALT) reset_button_pressed = True
buttonpins = [board.BUTTON_A, board.BUTTON_B] buttons = [] buttonkeys = [Keycode.LEFT_ARROW, Keycode.RIGHT_ARROW] controlkey = Keycode.SHIFT time.sleep(1) kbd = Keyboard() layout = KeyboardLayoutUS(kbd) for pin in buttonpins: button = DigitalInOut(pin) button.direction = Direction.INPUT button.pull = Pull.DOWN buttons.append(button) led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT print('Waiting for button presses') while True: for button in buttons: if button.value: print('Button #%d Pressed' % i) i = buttons.index(button) led.value = True k = buttonkeys[i] layout.write(k) if type(k) is str else kbd.press(controlkey, k) led.value = False _hy_anon_var_1 = None else: i = buttons.index(button) k = buttonkeys[i] _hy_anon_var_1 = kbd.release(controlkey, k)
if time.monotonic() < last_read: last_read = time.monotonic() for i, btn_action in enumerate(btn_actions): # check if button is pressed but make sure it is not held down if not btn_action["button"].value and not btn_action["held"]: # print the name of the command for debug purposes print(btn_action["name"]) if btn_action["type"] != "hold": # send the keyboard commands keyboard.press(*btn_action["keycode"]) time.sleep(0.001) keyboard.release(*btn_action["keycode"]) # rotate led on active scene for scene type buttons if btn_action["type"] == "scene": # light up the associated LED btn_action["led"].value = True # turn off other LEDs that may be on for j, alt_action in enumerate(btn_actions): if j != i: if alt_action["type"] == "scene": alt_action["led"].value = False # toggle led for toggle type buttons if btn_action["type"] == "toggle":
class KeyboardImpl: def __init__(self, name="Generic Keyboard", gpio_row=None, gpio_col=None, layout=None, layers=None, macro_layer=None): self.name = name self.kbd = Keyboard(usb_hid.devices) # Setup keyboard HID device self.kbd_layout = KeyboardLayoutUS(self.kbd) self.keymap = KeyboardHwLayout(layout) # map layout to hw layout self.key_row = [] # write self.key_col = [] # read self.last_scan_matrix = { } # used to determine diff between scans (if a release needs to be signalled) # Layers 0, 1, 2, 3 where 0 is the default layer 1 & 2 is the extended layers and 3 is the macro layer self.current_layer = 0 # Setup write pins for pin in gpio_row: key_pin = digitalio.DigitalInOut(pin) key_pin.direction = digitalio.Direction.OUTPUT self.key_row.append(key_pin) # Setup read pins for pin in gpio_col: key_pin = digitalio.DigitalInOut(pin) key_pin.direction = digitalio.Direction.INPUT key_pin.pull = digitalio.Pull.UP self.key_col.append(key_pin) # Scan keyboard matrixfor keystrokes. # A registered keystroke will be send directly to the host USB. def keyboard_scan(self): scan_matrix = [] for col in self.key_col: for row in self.key_row: row.value = False # 0V if not col.value: # Check if grounded by row key # Ignore keybounce if it occurs (this is highly unlikely to get caught here) if col.value: continue # Add scanned keyposition to scan matrix. # This will be used to map keycode or macro depending on current layer scan_matrix.append( Key(self.key_row.index(row), self.key_col.index(col))) row.value = True # 5V return scan_matrix # The released keys are the difference between last scan and current scan def get_diff_scan(self, scan_matrix): diff = [] for key in self.last_scan_matrix: if key not in scan_matrix: diff.append(key) return diff def determine_layer(self, diff): # Check if layer button is pressed i, increase layer variable # TODO shift layer inc key to decrease variable if KC_LAYER_INC_KEY_POS in diff: #and KC_LAYER_INC_KEY_POS in diff: print("INCREMENT LAYER") self.current_layer = (self.current_layer + 1) % KC_MACRO_LAYER # Either release or press keys def check_keyevent(self, scan_matrix, diff): # Check if the release previously pressed key if diff: mapped_diff = self.keymap.get_keymap(diff, self.current_layer) if mapped_diff: self.kbd.release(*mapped_diff) # If list contains scanned keys de if scan_matrix: mapped_keys = self.keymap.get_keymap(scan_matrix, self.current_layer) if mapped_keys: try: self.kbd.press(*mapped_keys) except ValueError: print( "No more than six regular keys may be pressed simultaneously." ) self.last_scan_matrix = scan_matrix def update(self): scan_matrix = self.keyboard_scan( ) # scan keyboard and create matrix with pressed keys diff = self.get_diff_scan(scan_matrix) self.determine_layer(diff) #increase or decrease layer (wrap-around) # No need to release keys or create key reports when in macro layer # since "write()" are sent directly and is self-releasing if self.current_layer == KC_MACRO_LAYER: print("IN MACRO LAYER WAHOOO") for diff_key in diff: # TODO Get macro layers to work print("Trying to write: ", self.keymap.get_mapped_key(diff_key)) print("kmap", self.keymap[1][1][0]) #self.kbd_layout.write(self.keymap.get_mapped_key(diff_key)) #self.kbd_layout.write("HELLO THERE!") self.check_keyevent(scan_matrix, diff)
dot_on(key_dots[k], keymap[k][0]) # use individual key colors from set time.sleep(0.05) while True: for button in range(12): if switch_state[button] == 0: if not switches[button].value: try: if keymap[button][1] == KEY: kbd.press(*keymap[button][2]) else: cc.send(keymap[button][2]) dot_on(key_dots[button], RED) except ValueError: # deals w six key limit pass print("pressed key{}".format(button)) switch_state[button] = 1 if switch_state[button] == 1: if switches[button].value: try: if keymap[button][1] == KEY: kbd.release(*keymap[button][2]) dot_on(key_dots[button], keymap[button][0]) except ValueError: pass print("released key{}".format(button)) switch_state[button] = 0 time.sleep(0.01) # debounce
print("down", down) # Lower the brightness so that we don't draw too much current when we turn all of # the LEDs on. trellis.pixels.brightness = 0.2 trellis.pixels.fill(keymap[down][0]) if keymap[down][1] == KEY: kbd.press(*keymap[down][2]) else: cc.send(keymap[down][2]) # else if the entry starts with 'l' for layout.write last_press = now for up in current_press - pressed: if up in keymap: print("up", up) if keymap[up][1] == KEY: kbd.release(*keymap[up][2]) # Reset the LEDs when there was something previously pressed (current_press) but nothing now # (pressed). if not pressed and current_press: trellis.pixels.brightness = 1 trellis.pixels.fill((0, 0, 0)) for button in keymap: trellis.pixels[button] = keymap[button][0] if not sleeping: snore_count = -1 else: sleep_time -= TIMEOUT # Fade all out if sleep_time < FADE_TIME:
class PiperCommandCenter: def __init__(self, joy_x_pin=board.A4, joy_y_pin=board.A3, joy_z_pin=board.D2, joy_gnd_pin=board.A5, dpad_l_pin=board.D3, dpad_r_pin=board.D4, dpad_u_pin=board.D1, dpad_d_pin=board.D0, mc_top_pin=board.SCK, mc_middle_pin=board.MOSI, mc_bottom_pin=board.MISO, outputScale=20.0, deadbandCutoff=0.1, weight=0.2): self.x_axis = PiperJoystickAxis(joy_x_pin, outputScale=outputScale, deadbandCutoff=deadbandCutoff, weight=weight) self.y_axis = PiperJoystickAxis(joy_y_pin, outputScale=outputScale, deadbandCutoff=deadbandCutoff, weight=weight) self.joy_z = PiperJoystickZ(joy_z_pin) self.dpad = PiperDpad(dpad_l_pin, dpad_r_pin, dpad_u_pin, dpad_d_pin) self.minecraftbuttons = PiperMineCraftButtons(mc_top_pin, mc_middle_pin, mc_bottom_pin) # Drive pin low if requested for easier joystick wiring if joy_gnd_pin is not None: # Provide a ground for the joystick - this is to facilitate # easier wiring self.joystick_gnd = DigitalInOut(joy_gnd_pin) self.joystick_gnd.direction = Direction.OUTPUT self.joystick_gnd.value = 0 self.keyboard = Keyboard(usb_hid.devices) self.keyboard_layout = KeyboardLayoutUS( self.keyboard) # Change for non-US self.mouse = Mouse(usb_hid.devices) # State # self.state = _UNWIRED self.timer = time.monotonic() self.last_mouse_wheel = time.monotonic() self.last_mouse = time.monotonic() self.dotstar_led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) self.dotstar_led.brightness = 0.2 self.up_pressed = False self.down_pressed = False self.left_pressed = False self.right_pressed = False self.mc_mode = _MC_DEFAULT self.mc_flyingdown_req = False self.mc_sprinting_req = False self.mc_crouching_req = False self.mc_utility_req = False # def process_repl_cmds(self): # # Assume that the command will be pasted, because input() # # will block until end of line # # # if supervisor.runtime.serial_bytes_available: # cmd = input() # exec(cmd) def releaseJoystickHID(self): self.mouse.release(Mouse.LEFT_BUTTON) self.mouse.release(Mouse.RIGHT_BUTTON) def releaseKeyboardHID(self): self.keyboard.release(Keycode.UP_ARROW) self.keyboard.release(Keycode.DOWN_ARROW) self.keyboard.release(Keycode.LEFT_ARROW) self.keyboard.release(Keycode.RIGHT_ARROW) self.keyboard.release(Keycode.SPACE) self.keyboard.release(Keycode.X) self.keyboard.release(Keycode.Z) self.keyboard.release(Keycode.C) def releaseMinecraftHID(self): self.mouse.release(Mouse.LEFT_BUTTON) self.mouse.release(Mouse.MIDDLE_BUTTON) self.mouse.release(Mouse.RIGHT_BUTTON) self.keyboard.release(Keycode.A) self.keyboard.release(Keycode.CONTROL) self.keyboard.release(Keycode.D) self.keyboard.release(Keycode.E) self.keyboard.release(Keycode.ESCAPE) self.keyboard.release(Keycode.F5) self.keyboard.release(Keycode.LEFT_SHIFT) self.keyboard.release(Keycode.Q) self.keyboard.release(Keycode.S) self.keyboard.release(Keycode.SPACE) self.keyboard.release(Keycode.W) def process(self): #self.process_repl_cmds() # Call the debouncing library frequently self.joy_z.update() self.dpad.update() self.minecraftbuttons.update() dx = self.x_axis.readJoystickAxis() dy = self.y_axis.readJoystickAxis() # Command Center State Machine # if self.state == _UNWIRED: self.dotstar_led[0] = ((time.monotonic_ns() >> 23) % 256, 0, 0) if dx == 0 and dy == 0: self.state = _WAITING self.timer = time.monotonic() elif self.state == _WAITING: self.dotstar_led[0] = ((time.monotonic_ns() >> 23) % 256, 0, 0) if dx != 0 or dy != 0: self.state = _UNWIRED else: if time.monotonic() - self.timer > 0.5: self.state = _JOYSTICK elif self.state == _JOYSTICK: self.dotstar_led[0] = (0, 255, 0) if self.joy_z.zPressed(): self.timer = time.monotonic() self.state = _JWAITING elif self.state == _JWAITING: if not self.joy_z.zPressed(): self.state = _JOYSTICK else: if time.monotonic() - self.timer > 1.0: self.state = _KEYBOARD self.releaseJoystickHID() elif self.state == _KEYBOARD: self.dotstar_led[0] = (0, 0, 255) if self.joy_z.zPressed( ) and not self.minecraftbuttons.bottomPressed(): self.timer = time.monotonic() self.state = _KWAITING_TO_J elif self.joy_z.zPressed() and self.minecraftbuttons.bottomPressed( ): self.timer = time.monotonic() self.state = _KWAITING_TO_MC elif self.state == _KWAITING_TO_J: if not self.joy_z.zPressed( ) or self.minecraftbuttons.bottomPressed(): self.state = _KEYBOARD self.up_pressed = False self.down_pressed = False self.left_pressed = False self.right_pressed = False else: if time.monotonic() - self.timer > 1.0: self.state = _JOYSTICK self.releaseKeyboardHID() elif self.state == _KWAITING_TO_MC: if not self.joy_z.zPressed( ) or not self.minecraftbuttons.bottomPressed(): self.state = _KEYBOARD self.up_pressed = False self.down_pressed = False self.left_pressed = False self.right_pressed = False else: if time.monotonic() - self.timer > 1.0: self.state = _MINECRAFT self.releaseKeyboardHID() elif self.state == _MINECRAFT: if self.mc_mode == _MC_DEFAULT: self.dotstar_led[0] = (0, 255, 255) # cyan elif self.mc_mode == _MC_FLYINGDOWN: self.dotstar_led[0] = (255, 0, 255) # magenta elif self.mc_mode == _MC_SPRINTING: self.dotstar_led[0] = (255, 128, 128) # pink elif self.mc_mode == _MC_CROUCHING: self.dotstar_led[0] = (255, 165, 0) # orange elif self.mc_mode == _MC_UTILITY: self.dotstar_led[0] = (255, 255, 0) # yellow if self.joy_z.zPressed() and self.dpad.upPressed( ) and self.dpad.downPressed() and self.dpad.leftPressed( ) and self.dpad.rightPressed(): self.timer = time.monotonic() self.state = _MWAITING elif self.state == _MWAITING: if not self.joy_z.zPressed() or not self.dpad.upPressed( ) or not self.dpad.downPressed() or not self.dpad.leftPressed( ) or not self.dpad.rightPressed(): self.state = _MINECRAFT else: if time.monotonic() - self.timer > 1.0: self.state = _JOYSTICK self.releaseMinecraftHID() # Command Center Joystick Handling # if self.state == _JOYSTICK or self.state == _JWAITING: # Determine mouse wheel direction # dwheel = 0 if self.dpad.upPressed(): dwheel = -1 elif self.dpad.downPressed(): dwheel = 1 # Initial quick and dirty mouse movement pacing # if time.monotonic() - self.last_mouse > 0.005: self.last_mouse = time.monotonic() self.mouse.move(x=dx, y=dy) # Initial quick and dirty mouse scroll wheel pacing # if time.monotonic() - self.last_mouse_wheel > 0.1: self.last_mouse_wheel = time.monotonic() self.mouse.move(wheel=dwheel) if self.dpad.leftPressedEvent(): self.mouse.press(Mouse.LEFT_BUTTON) elif self.dpad.leftReleasedEvent(): self.mouse.release(Mouse.LEFT_BUTTON) if self.dpad.rightPressedEvent(): self.mouse.press(Mouse.RIGHT_BUTTON) elif self.dpad.rightReleasedEvent(): self.mouse.release(Mouse.RIGHT_BUTTON) # Command Center Keyboard Handling # if self.state == _KEYBOARD or self.state == _KWAITING_TO_J or self.state == _KWAITING_TO_MC: if self.dpad.upPressedEvent(): self.keyboard.press(Keycode.SPACE) elif self.dpad.upReleasedEvent(): self.keyboard.release(Keycode.SPACE) if self.dpad.downPressedEvent(): self.keyboard.press(Keycode.X) elif self.dpad.downReleasedEvent(): self.keyboard.release(Keycode.X) if self.dpad.leftPressedEvent(): self.keyboard.press(Keycode.Z) elif self.dpad.leftReleasedEvent(): self.keyboard.release(Keycode.Z) if self.dpad.rightPressedEvent(): self.keyboard.press(Keycode.C) elif self.dpad.rightReleasedEvent(): self.keyboard.release(Keycode.C) if dx == 0: if self.left_pressed: self.left_pressed = False self.keyboard.release(Keycode.LEFT_ARROW) if self.right_pressed: self.right_pressed = False self.keyboard.release(Keycode.RIGHT_ARROW) elif dx > 0: if self.left_pressed: self.left_pressed = False self.keyboard.release(Keycode.LEFT_ARROW) if not self.right_pressed: self.right_pressed = True self.keyboard.press(Keycode.RIGHT_ARROW) elif dx < 0: if not self.left_pressed: self.left_pressed = True self.keyboard.press(Keycode.LEFT_ARROW) if self.right_pressed: self.right_pressed = False self.keyboard.release(Keycode.RIGHT_ARROW) if dy == 0: if self.up_pressed: self.up_pressed = False self.keyboard.release(Keycode.UP_ARROW) if self.down_pressed: self.down_pressed = False self.keyboard.release(Keycode.DOWN_ARROW) elif dy < 0: if not self.up_pressed: self.up_pressed = True self.keyboard.press(Keycode.UP_ARROW) if self.down_pressed: self.down_pressed = False self.keyboard.release(Keycode.DOWN_ARROW) elif dy > 0: if self.up_pressed: self.up_pressed = False self.keyboard.release(Keycode.UP_ARROW) if not self.down_pressed: self.down_pressed = True self.keyboard.press(Keycode.DOWN_ARROW) # Command Center Minecraft Handling # if self.state == _MINECRAFT: # Modifier button # if self.minecraftbuttons.bottomPressed(): if self.joy_z.zPressedEvent(): self.mc_flyingdown_req = True self.mc_sprinting_req = False self.mc_crouching_req = False self.mc_utility_req = False elif self.dpad.upPressedEvent(): self.mc_flyingdown_req = False self.mc_sprinting_req = True self.mc_crouching_req = False self.mc_utility_req = False elif self.dpad.downPressedEvent(): self.mc_flyingdown_req = False self.mc_sprinting_req = False self.mc_crouching_req = True self.mc_utility_req = False elif self.dpad.leftPressedEvent(): self.mc_flyingdown_req = False self.mc_sprinting_req = False self.mc_crouching_req = False self.mc_utility_req = True if self.minecraftbuttons.bottomReleasedEvent(): self.releaseMinecraftHID() if self.mc_flyingdown_req: self.mc_mode = _MC_FLYINGDOWN self.mc_flyingdown_req = False elif self.mc_sprinting_req: self.mc_mode = _MC_SPRINTING self.mc_sprinting_req = False self.keyboard.press(Keycode.CONTROL) elif self.mc_crouching_req: self.mc_mode = _MC_CROUCHING self.mc_crouching_req = False self.keyboard.press(Keycode.LEFT_SHIFT) elif self.mc_utility_req: self.mc_mode = _MC_UTILITY self.mc_utility_req = False else: self.mc_mode = _MC_DEFAULT # Joystick functionality for mouse movement is always active # # Mouse movement is paced - may need to adjust # if time.monotonic() - self.last_mouse > 0.005: self.last_mouse = time.monotonic() self.mouse.move(x=dx, y=dy) # Top and bottom buttons changed by mod key in default mode # if self.mc_mode == _MC_DEFAULT and self.minecraftbuttons.bottomPressed( ): if self.minecraftbuttons.topPressedEvent(): self.keyboard.press(Keycode.Q) elif self.minecraftbuttons.topReleasedEvent(): self.keyboard.release(Keycode.Q) if self.minecraftbuttons.middlePressedEvent(): self.mouse.press(Mouse.MIDDLE_BUTTON) elif self.minecraftbuttons.middleReleasedEvent(): self.mouse.release(Mouse.MIDDLE_BUTTON) else: if self.minecraftbuttons.topPressedEvent(): self.mouse.press(Mouse.LEFT_BUTTON) elif self.minecraftbuttons.topReleasedEvent(): self.mouse.release(Mouse.LEFT_BUTTON) if self.minecraftbuttons.middlePressedEvent(): self.mouse.press(Mouse.RIGHT_BUTTON) elif self.minecraftbuttons.middleReleasedEvent(): self.mouse.release(Mouse.RIGHT_BUTTON) # Don't generate key presses for buttons if modifier key is pressed # if not self.minecraftbuttons.bottomPressed(): # Joystick button changes based on minecraft mode # if self.joy_z.zPressedEvent(): self.keyboard.press(_MC_JOYSTICK_Z[self.mc_mode]) elif self.joy_z.zReleasedEvent(): self.keyboard.release(_MC_JOYSTICK_Z[self.mc_mode]) # DPAD buttons special in utility mode # if self.mc_mode == _MC_UTILITY: if self.dpad.upPressedEvent(): self.mouse.move(wheel=-1) if self.dpad.downPressedEvent(): self.mouse.move(wheel=1) if self.dpad.leftPressedEvent(): self.keyboard.press(Keycode.E) elif self.dpad.leftReleasedEvent(): self.keyboard.release(Keycode.E) if self.dpad.rightPressedEvent(): self.keyboard.press(Keycode.ESCAPE) elif self.dpad.rightReleasedEvent(): self.keyboard.release(Keycode.ESCAPE) else: if self.dpad.upPressedEvent(): self.keyboard.press(Keycode.W) elif self.dpad.upReleasedEvent(): self.keyboard.release(Keycode.W) if self.dpad.downPressedEvent(): self.keyboard.press(Keycode.S) elif self.dpad.downReleasedEvent(): self.keyboard.release(Keycode.S) if self.dpad.leftPressedEvent(): self.keyboard.press(Keycode.A) elif self.dpad.leftReleasedEvent(): self.keyboard.release(Keycode.A) if self.dpad.rightPressedEvent(): self.keyboard.press(Keycode.D) elif self.dpad.rightReleasedEvent(): self.keyboard.release(Keycode.D)
# #print("Button 2 pressed!") # # channel.write(["", "bproc", "./test.sh"]) # channel.write(["dbput", "dbput", "TESTING", "FROMPORTAL"]) # channel.write(["dbput", "dbput", "TESTING", "TIME:" + str(time.time())]) # button2_pressed = True if mcp is not None: for i in range(pin_count): value = pins[i].value if value != pin_states[i]: # print("Pin " + str(i) + " is " + str(value)) pin_states[i] = value if not value: kbd.press(*pin_keys[i]) else: kbd.release(*pin_keys[i]) point = touch.touch_point if point: # Touch point needs to be rotated, flipped vertically, and halved point = (point[1] // 2), ((480 - point[0]) // 2) for button in buttons: if button.contains(point): button.selected = True elif button.selected: button.selected = False had_point = True elif had_point: for button in buttons: if button.selected: date_label.text = "button " + button.id + " was pressed"