def __init__(self,display): self._display=display #read json button defs f=open("/buttons.json") # load buttons pages self.buttonPages=json.load(f) f.close() for buttonPage in self.buttonPages["buttons"]: print("Button Page : %s"%(buttonPage["name"])) parseButtons(buttonPage) self.currButtonPageIndex=0 #setup keyboard device self.keyboard=Keyboard(usb_hid.devices) self.cc=ConsumerControl(usb_hid.devices) #init button ui self.ui=ButtonPageUI() #init list ui self.uilist=ButtonListUI(self.buttonPages["buttons"]) self.listVisible=False #Initialize first button page self.LoadButtonPage(0) self._display.show(self.ui.group)
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)
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 __init__(self, rows_, cols_, keymaps_): self.LED_ON = False self.LED_OFF = True self.kbd = Keyboard(usb_hid.devices) self.rows = self.set_rows(rows_) self.cols = self.set_cols(cols_) self.keymaps = keymaps_ self.led = DigitalInOut(board.D13) self.led.direction = Direction.OUTPUT
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())
class ButtonModel: def __init__(self): #read json button defs f = open("/buttons.json") # load buttons pages self.buttonPages = json.load(f) f.close() for buttonPage in self.buttonPages["buttons"]: print("Button Page : %s" % (buttonPage["name"])) parseButtons(buttonPage) self.LoadButtonPage(0) self.currButtonPageIndex = 0 #setup keyboard device self.keyboard = Keyboard(usb_hid.devices) self.cc = ConsumerControl(usb_hid.devices) # Loads the button page specified by the index def LoadButtonPage(self, newButtonPageIndex): self.currButtonDef = self.buttonPages["buttons"][newButtonPageIndex] #print("Changeing BGImg To " + buttonDef['img'] ) #imgbg=Image.open(buttonDef['img']) print("Loaded Button Page - %s" % (self.currButtonDef["name"])) #Flip the button page index def FlipButtonPage(self, changeIndex): self.currButtonPageIndex += changeIndex if self.currButtonPageIndex < 0: self.currButtonPageIndex = len(self.buttonPages['buttons']) - 1 elif self.currButtonPageIndex >= len(self.buttonPages['buttons']): self.currButtonPageIndex = 0 self.LoadButtonPage(self.currButtonPageIndex) # Button presed on current page def ButtonPressed(self, keyIndex): button = self.currButtonDef.get(keyIndex, None) if button != None and button['keycodes'] != None: print(keyIndex + "pressed") if (button["isCC"]) == False: self.keyboard.send(*button['keycodes']) time.sleep(150 / 1000) else: self.cc.send(button["keycodes"][0]) #Current Page Dictionary @property def CurrentButtons(self): return self.currButtonDef
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 __init__(self): #read json button defs f = open("/buttons.json") # load buttons pages self.buttonPages = json.load(f) f.close() for buttonPage in self.buttonPages["buttons"]: print("Button Page : %s" % (buttonPage["name"])) parseButtons(buttonPage) self.LoadButtonPage(0) self.currButtonPageIndex = 0 #setup keyboard device self.keyboard = Keyboard(usb_hid.devices) self.cc = ConsumerControl(usb_hid.devices)
def __init__(self, filename="shortcuts.txt"): self.display = LCD(I2CPCF8574Interface(0x27)) self.display.clear() self.keyboard = Keyboard() self.mouse = Mouse() self.consumer_control = ConsumerControl() self.display.print( "Ready! Choose a\nshortcut and press\nthe do-it switch.") self.switch_ins = tuple( digitalio.DigitalInOut(pin) for pin in SWITCH_PINS) for switch_in in self.switch_ins: switch_in.switch_to_input(pull=digitalio.Pull.UP) try: # Shortcuts is a dict mapping a switch number to a list of Shortcuts. self.shortcuts = Shortcut.read_shortcuts(filename) except BadShortcut as ex: self.fatal_error(*ex.args) if not self.shortcuts: self.fatal_error("No shortcuts defined!") self.current_switch = None # Keep track of the current shortcut for each switch. Start at the zero-th ones. # Skip any switches with no shortcuts. self.current_shortcut_index = {} for switch in self.shortcuts: self.current_shortcut_index[switch] = 0
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 paste(self, text): if self.keyboard is None: if usb_hid: self.keyboard = Keyboard(usb_hid.devices) self.keyboard_layout = KeyboardLayoutUS(self.keyboard) else: return if self.keyboard_layout is None: raise ValueError("USB HID not available") text = str(text) self.keyboard_layout.write(text) raise RuntimeError("Pasted")
def __init__(self, config): """ Init and binds the H/W """ # Pimoroni's RGB Keypad - Default wiring self.KEYPAD = RgbKeypad() self.KEYS = self.KEYPAD.keys # DS3231 module, i2c1, SCL=GP11 and SDA=GP10 i2c = busio.I2C(board.GP11, board.GP10) self.DS = DS3231(i2c) print(self.DS.datetime) # Just to check time at boot when dev self.CONFIG = config # USB HID keyboard = Keyboard(usb_hid.devices) if self.CONFIG.get("layout", "us") == "fr": # More to come from adafruit_hid.keyboard_layout_fr import KeyboardLayoutFR self.LAYOUT = KeyboardLayoutFR(keyboard) else: # Default US layout from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS self.LAYOUT = KeyboardLayoutUS(keyboard) # Pico display self.DISPLAY = pico_dio.get_display() self.SCREENS = dict() self.SCREENS["splash"] = pico_dio.get_splash() self.DISPLAY.show(self.SCREENS["splash"]) self.UPDATE_INDEX = 0 self.LOCKED = False self.LAST_CODE = "" self.OTP = None self.MODE = 0 self.PAGE = 0 self.INDEX = None self.LAST_COUNTER = 0 # time // 30, OTP counter self.SCREENS["OTP"] = pico_dio.get_otp_group() self.SCREENS["PAGE"] = pico_dio.get_page_group() self.display_page(self.PAGE) for key in self.KEYS: @self.KEYPAD.on_press(key) def press_handler(a_key): self.handle_numpad(button_to_numpad(a_key.number))
def paste(self, text): if self.keyboard is None: if usb_hid: self.keyboard = Keyboard(usb_hid.devices) self.keyboard_layout = KeyboardLayoutUS(self.keyboard) else: return if self.keyboard_layout is None: self.add_trail("No USB") else: text = str(text) self.keyboard_layout.write(text) self.add_trail(f"Pasted {text}")
def setup_hid_devices(self): self.kbd = [ Keyboard(usb_hid.devices), Keyboard(usb_hid.devices), Keyboard(usb_hid.devices), Keyboard(usb_hid.devices), Keyboard(usb_hid.devices), Keyboard(usb_hid.devices) ] self.cc = ConsumerControl(usb_hid.devices)
def spamKey(code): knobkeys = [Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.SPACE, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET] spamRate = [0.01, 0.05, 0.125, 0.25, 0.5, 0.5, 0.5, 0.25, 0.125, 0.05, 0.01] kbd = Keyboard() kbd.press(knobkeys[code]) # which keycode is entered kbd.release_all() time.sleep(spamRate[code]) # how fast the key is spammed
def main(): # Set up a input button on GPIO Pin 15 btn_pin = board.GP15 btn = digitalio.DigitalInOut(btn_pin) btn.direction = digitalio.Direction.INPUT btn.pull = digitalio.Pull.DOWN # Set up the Pico onboard LED led = digitalio.DigitalInOut(board.GP25) led.direction = digitalio.Direction.OUTPUT # Create Mouse and Keybord devices mouse = Mouse(usb_hid.devices) keyboard = Keyboard(usb_hid.devices) # List the keystrokes we want to initiate on button press keystrokes = [[Keycode.CONTROL, Keycode.SHIFT, Keycode.S], Keycode.KEYPAD_PLUS, Keycode.ENTER] # define the target screen size screen = {"width":1920, "height":1080} button_loop(led, btn, keystrokes, keyboard, mouse, screen)
# Use default HID descriptor hid = HIDService() device_info = DeviceInfoService( software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries" ) advertisement = ProvideServicesAdvertisement(hid) advertisement.appearance = 961 scan_response = Advertisement() ble = adafruit_ble.BLERadio() if ble.connected: for c in ble.connections: c.disconnect() print("advertising") ble.start_advertising(advertisement, scan_response) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) while True: while not ble.connected: pass print("Start typing:") while ble.connected: c = sys.stdin.read(1) sys.stdout.write(c) kl.write(c) # print("sleeping") time.sleep(0.1) ble.start_advertising(advertisement)
import board from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS import adafruit_thermistor # Switch to quickly enable/disable # light level # temperature # Set the keyboard object! # Sleep for a bit to avoid a race condition on some systems time.sleep(1) kbd = Keyboard() layout = KeyboardLayoutUS(kbd) # US is only current option... print("Time\tLight\tTemperature\tX\tY\tZ") # Print column headers def slow_write(string): # Typing should not be too fast for for c in string: # the computer to be able to accept layout.write(c) time.sleep(0.2) # use 1/5 second pause between characters while True: if cpx.switch: # If the slide switch is on, don't log continue
import usb_hid from digitalio import DigitalInOut, Direction, Pull from adafruit_hid.keyboard import Keyboard from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS from adafruit_hid.keycode import Keycode switch = DigitalInOut(board.GP20) switch.direction = Direction.INPUT switch.pull = Pull.UP switch2 = DigitalInOut(board.GP21) switch2.direction = Direction.INPUT switch2.pull = Pull.UP keyboard = Keyboard(usb_hid.devices) keyboard_layout = KeyboardLayoutUS(keyboard) while True: if not switch.value: print('y') keyboard.press(Keycode.LEFT_ALT, Keycode.F1) keyboard.release_all() if not switch2.value: print('y') keyboard.press(Keycode.LEFT_ALT, Keycode.F2) keyboard.release_all() print('---') time.sleep(0.2)
import gc from adafruit_hid.keyboard import Keyboard gc.collect() from adafruit_hid.keycode import Keycode gc.collect() from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS gc.collect() import board import pulseio import adafruit_dotstar import adafruit_irremote import time # The keyboard object! time.sleep(1) # Sleep for a bit to avoid a race condition on some systems keyboard = Keyboard() keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) decoder = adafruit_irremote.GenericDecode() pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=100, idle_state=True) # Expected pulse, pasted in from previous recording REPL session: key1_pulses = [0] key2_pulses = [1] print('IR listener') # Fuzzy pulse comparison function:
import time import board import digitalio from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard() # define buttons. these can be any physical switches/buttons, but the values # here work out-of-the-box with a CircuitPlayground Express' A and B buttons. swap = digitalio.DigitalInOut(board.D4) swap.direction = digitalio.Direction.INPUT swap.pull = digitalio.Pull.DOWN search = digitalio.DigitalInOut(board.D5) search.direction = digitalio.Direction.INPUT search.pull = digitalio.Pull.DOWN while True: # press ALT+TAB to swap windows if swap.value: kbd.send(Keycode.ALT, Keycode.TAB) # press CTRL+K, which in a web browser will open the search dialog elif search.value: kbd.send(Keycode.CONTROL, Keycode.K) time.sleep(0.1)
import time import digitalio import board import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode # The button pins we'll use, each will have an internal pullup buttonpins = [board.D0] # The keycode sent for each button, will be paired with a control key buttonkeys = [ Keycode.SPACE # Space ] keyboard = Keyboard(usb_hid.devices) # our array of button objects buttons = [] # make all pin objects, make them inputs w/pullups for pin in buttonpins: button = digitalio.DigitalInOut(pin) button.direction = digitalio.Direction.INPUT button.pull = digitalio.Pull.UP buttons.append(button) print("Waiting for button presses") while True: # check each button
from adafruit_hid.keyboard import Keyboard from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS from adafruit_hid.keycode import Keycode from adafruit_hid.consumer_control import ConsumerControl from adafruit_hid.consumer_control_code import ConsumerControlCode from digitalio import DigitalInOut, Direction, Pull cs = DigitalInOut(board.GP17) cs.direction = Direction.OUTPUT cs.value = 0 num_pixels = 16 pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, num_pixels, brightness=0.1, auto_write=True) i2c = busio.I2C(board.GP5, board.GP4) device = I2CDevice(i2c, 0x20) kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) def colourwheel(pos): if pos < 0 or pos > 255: return (0, 0, 0) if pos < 85: return (255 - pos * 3, pos * 3, 0) if pos < 170: pos -= 85 return (0, 255 - pos * 3, pos * 3) pos -= 170 return (pos * 3, 0, 255 - pos * 3) def read_button_states(x, y): pressed = [0] * 16 with device: device.write(bytes([0x0]))
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)
# Encoder button is a digital input with pullup on D9 button = DigitalInOut(board.D9) button.direction = Direction.INPUT button.pull = Pull.UP # Rotary encoder inputs with pullup on D10 & D11 rot_a = DigitalInOut(board.D10) rot_a.direction = Direction.INPUT rot_a.pull = Pull.UP rot_b = DigitalInOut(board.D11) rot_b.direction = Direction.INPUT rot_b.pull = Pull.UP # Used to do HID output, see below kbd = Keyboard(usb_hid.devices) consumer_control = ConsumerControl(usb_hid.devices) # time keeper, so we know when to turn off the LED timestamp = time.monotonic() ######################### MAIN LOOP ############################## # the counter counts up and down, it can roll over! 16-bit value encoder_counter = 0 # direction tells you the last tick which way it went encoder_direction = 0 # constants to help us track what edge is what A_POSITION = 0 B_POSITION = 1
runScript(line[7:]) elif (line[0:13] == "DEFAULT_DELAY"): defaultDelay = int(line[14:]) * 10 elif (line[0:12] == "DEFAULTDELAY"): defaultDelay = int(line[13:]) * 10 elif (line[0:3] == "LED"): if (led.value == True): led.value = False else: led.value = True else: newScriptLine = convertLine(line) runScriptLine(newScriptLine) kbd = Keyboard(usb_hid.devices) layout = KeyboardLayout(kbd) # turn off automatically reloading when files are written to the pico supervisor.disable_autoreload() # sleep at the start to allow the device to be recognized by the host computer time.sleep(.5) led_pwm_up(led) def getProgrammingStatus(): # check GP0 for setup mode # see setup mode for instructions progStatusPin = digitalio.DigitalInOut(GP0)
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS from adafruit_hid.keycode import Keycode # A simple neat keyboard demo in CircuitPython # The pins we'll use, each will have an internal pullup keypress_pins = [board.A1, board.A2] # Our array of key objects key_pin_array = [] # The Keycode sent for each button, will be paired with a control key keys_pressed = [Keycode.A, "Hello World!\n"] control_key = Keycode.SHIFT # The keyboard object! time.sleep(1) # Sleep for a bit to avoid a race condition on some systems keyboard = Keyboard(usb_hid.devices) keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) # Make all pin objects inputs with pullups for pin in keypress_pins: key_pin = digitalio.DigitalInOut(pin) key_pin.direction = digitalio.Direction.INPUT key_pin.pull = digitalio.Pull.UP key_pin_array.append(key_pin) # For most CircuitPython boards: led = digitalio.DigitalInOut(board.D13) # For QT Py M0: # led = digitalio.DigitalInOut(board.SCK) led.direction = digitalio.Direction.OUTPUT
from digitalio import DigitalInOut, Direction, Pull 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)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import digitalio import usb_hid from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode kbd = Keyboard(usb_hid.devices) # define buttons. these can be any physical switches/buttons, but the values # here work out-of-the-box with a CircuitPlayground Express' A and B buttons. swap = digitalio.DigitalInOut(board.D4) swap.direction = digitalio.Direction.INPUT swap.pull = digitalio.Pull.DOWN search = digitalio.DigitalInOut(board.D5) search.direction = digitalio.Direction.INPUT search.pull = digitalio.Pull.DOWN while True: # press ALT+TAB to swap windows if swap.value: kbd.send(Keycode.ALT, Keycode.TAB) # press CTRL+K, which in a web browser will open the search dialog elif search.value: kbd.send(Keycode.CONTROL, Keycode.K)
button = DigitalInOut(p) button.direction = Direction.INPUT button.pull = Pull.UP buttons.append(button) # Servo on D5 # create a PWMOut object on Pin D5 pwm = pwmio.PWMOut(board.D5, duty_cycle=2 ** 15, frequency=50) servo = servo.Servo(pwm) # NeoPixel strip (of 16 LEDs) connected on D6 NUMPIXELS = 16 neopixels = neopixel.NeoPixel(board.D6, NUMPIXELS, brightness=0.2, auto_write=False) # Used if we do HID output, see below kbd = Keyboard() ######################### HELPERS ############################## # Helper to convert analog input to voltage def getVoltage(pin): return (pin.value * 3.3) / 65536 # Helper to give us a nice color swirl def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if (pos < 0): return [0, 0, 0] if (pos > 255): return [0, 0, 0]
from adafruit_hid.keyboard import Keyboard from adafruit_hid.keycode import Keycode from adafruit_hid.consumer_control import ConsumerControl from adafruit_hid.consumer_control_code import ConsumerControlCode import adafruit_dotstar as dotstar dots = dotstar.DotStar(board.SCK, board.MOSI, 12, brightness=0.4) RED = 0xFF0000 AMBER = 0xAA9900 BLUE = 0x0066FF MAGENTA = 0xFF00FF PURPLE = 0x3B0F85 BLACK = 0x000000 kbd = Keyboard(usb_hid.devices) cc = ConsumerControl(usb_hid.devices) orientation = 1 # 0 = portrait/vertical, 1 = landscape/horizontal if orientation == 0: key_dots = [0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11] # 0 #4 #8 # 1 #5 #9 # 2 #6 #10 # 3 #7 #11 if orientation == 1: key_dots = [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8] # 3 #2 #1 #0 # 7 #6 #5 #4 # 11 #10 #9 #8