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, 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 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 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 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)
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
import time import usb_hid from adafruit_hid.mouse import Mouse import board import digitalio as digitalIO mouse = Mouse(usb_hid.devices) switch = False # set the button btn = digitalIO.DigitalInOut(board.GP15) btn.direction = digitalIO.Direction.INPUT btn.pull = digitalIO.Pull.DOWN led = digitalIO.DigitalInOut(board.GP25) led.direction = digitalIO.Direction.OUTPUT pixels_to_move = 30 #initialize led value as false led.value = False #time.sleep(3) def move(is_left=False): global switch counter = 0 multiplier = 1 if btn.value: switch = not switch print(switch) if switch:
import digitalio import busio import adafruit_lis3dh import usb_hid from adafruit_hid.mouse import Mouse killswitch = digitalio.DigitalInOut(board.SLIDE_SWITCH) killswitch.direction = digitalio.Direction.INPUT killswitch.pull = digitalio.Pull.UP lmb = digitalio.DigitalInOut(board.BUTTON_A) lmb.direction = digitalio.Direction.INPUT lmb.pull = digitalio.Pull.DOWN lmb_pre = lmb.value rmb = digitalio.DigitalInOut(board.BUTTON_B) rmb.direction = digitalio.Direction.INPUT rmb.pull = digitalio.Pull.DOWN rmb_pre = rmb.value mouse = Mouse(usb_hid.devices) while True: if killswitch.value: if rmb.value: mouse.move(0, 0, 1) if lmb.value: mouse.move(0, 0, -1)
import usb_hid import time from adafruit_circuitplayground.express import cpx from adafruit_hid.mouse import Mouse from adafruit_circuitplayground import cp m = Mouse(usb_hid.devices) cpx.adjust_touch_threshold(200) pix = cpx.pixels pix.brightness = 0.4 while True: x, y, z = cpx.acceleration for i in range(10): print((abs(int(x)) * 10, abs(int(y)) * 10, abs(int(z)) * 10)) pix[i] = (abs(int(x)) * 10 % 255, abs(int(y)) * 10 % 255, abs(int(z)) * 10 % 255) if cpx.switch: continue cpx.red_led = True m.move(5 * int(-x), 5 * int(y), 0) if cp.button_a: m.click(Mouse.LEFT_BUTTON) if cp.button_b: m.click(Mouse.RIGHT_BUTTON) print(y)
def mouse_config_move_y(y_distance): mouse = Mouse(usb_hid.devices) mouse.move(x = 0, y = y_distance)
def mouse_config(key_to_be_pressed): mouse = Mouse(usb_hid.devices) mouse.click(key_to_be_pressed)
"""CircuitPython Essentials HID Mouse example""" import time import analogio import board import digitalio import usb_hid from adafruit_hid.mouse import Mouse mouse = Mouse(usb_hid.devices) x_axis = analogio.AnalogIn(board.A0) y_axis = analogio.AnalogIn(board.A1) select = digitalio.DigitalInOut(board.A2) select.direction = digitalio.Direction.INPUT select.pull = digitalio.Pull.UP pot_min = 0.00 pot_max = 3.29 step = (pot_max - pot_min) / 20.0 def get_voltage(pin): return (pin.value * 3.3) / 65536 def steps(axis): """ Maps the potentiometer voltage range to 0-20 """ return round((axis - pot_min) / step) while True:
import usb_hid from adafruit_circuitplayground.express import cpx from adafruit_hid.mouse import Mouse m = Mouse(usb_hid.devices) cpx.adjust_touch_threshold(200) while True: if cpx.touch_A4: m.move(-1, 0, 0) if cpx.touch_A3: m.move(1, 0, 0) if cpx.touch_A7: m.move(0, -1, 0) if cpx.touch_A1: m.move(0, 1, 0)
import board import time import rotaryio #from adafruit_hid.keycode import Keyboard from adafruit_hid.keycode import Keycode from adafruit_hid.mouse import Mouse from adafruit_hid.keyboard import Keyboard # For Feather M0 Express, Metro M0 Express, Metro M4 Express, and Circuit Playground Express import neopixel led = neopixel.NeoPixel(board.NEOPIXEL, 1) led.brightness = 0.3 m = Mouse() kbd = Keyboard() #red wire, (Alt-F4 button) button1 = digitalio.DigitalInOut(board.D9) button1.direction = digitalio.Direction.INPUT button1.pull = digitalio.Pull.UP button1_state = None #grey wire, Twitter button button2 = digitalio.DigitalInOut(board.D6) button2.direction = digitalio.Direction.INPUT button2.pull = digitalio.Pull.UP button2_state = None #blue wire, (Instagram button)
import time import board import digitalio from adafruit_hid.mouse import Mouse mouse = Mouse() # 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. up = digitalio.DigitalInOut(board.D4) up.direction = digitalio.Direction.INPUT up.pull = digitalio.Pull.DOWN down = digitalio.DigitalInOut(board.D5) down.direction = digitalio.Direction.INPUT down.pull = digitalio.Pull.DOWN while True: # scroll up one unit (varies with host/OS) if up.value: mouse.move(wheel=1) # scroll down one unit (varies with host/OS) elif down.value: mouse.move(wheel=-1) time.sleep(0.1)
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT import usb_hid from adafruit_circuitplayground.express import cpx from adafruit_hid.mouse import Mouse m = Mouse(usb_hid.devices) while True: if cpx.button_a: m.press(Mouse.LEFT_BUTTON) while cpx.button_a: # Wait for button A to be released pass m.release(Mouse.LEFT_BUTTON) if cpx.button_b: m.press(Mouse.RIGHT_BUTTON) while cpx.button_b: # Wait for button B to be released pass m.release(Mouse.RIGHT_BUTTON)
"""programme 6-8-3 : Emulation de la souris""" # importation des modules natifs utiles from board import * from digitalio import * from analogio import * # importation de modules supplémentaires import usb_hid from adafruit_hid.mouse import Mouse from simpleio import * # Instanciation de l'objet 'souris' souris = Mouse(usb_hid.devices) # Mise en place des broches des axes du joystick analogique axe_x = AnalogIn(A4) axe_y = AnalogIn(A3) # Mise en place de la broche émulant le bouton gauche de la souris bouton_gauche = DigitalInOut(A5) bouton_gauche.direction = Direction.INPUT bouton_gauche.pull = Pull.UP # --------------------------------------- # ------- BOUCLE PRINCIPALE ----------- # --------------------------------------- while True: # Mesure la valeur numérique des deux axes + mise à l'échelle x=map_range(axe_x.value, 0, 65535, 0, 20) y=map_range(axe_y.value, 0, 65535, 0, 20) # Si le bouton gauche est appuyé alors on émule le clic gauche de la souris if bouton_gauche.value is False: souris.click(Mouse.LEFT_BUTTON) # Si le joystick est à droite, on déplace le curseur d'une unité à droite
touch_a1.threshold = 2000 touch_a2 = touchio.TouchIn(board.A2) touch_a2.threshold = 2000 touch_a3 = touchio.TouchIn(board.A3) touch_a3.threshold = 2000 touch_a4 = touchio.TouchIn(board.A4) touch_a4.threshold = 2000 # Keyboard & Mouse Setup # The keyboard object! kbd = Keyboard() # we're americans :) layout = KeyboardLayoutUS(kbd) # The mouse object! mouse = Mouse() # Accelerometer Setup # Initialize Accelerometer i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=25) # Set range of accelerometer # (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G). lis3dh.range = adafruit_lis3dh.RANGE_8_G # Controller Functions # A helper to 'remap' an input range to an output range def Map(x, in_min, in_max, out_min, out_max):
import board import adafruit_nunchuk from adafruit_hid.mouse import Mouse m = Mouse() nc = adafruit_nunchuk.Nunchuk(board.I2C()) centerX = 128 centerY = 128 scaleX = 0.3 scaleY = 0.3 cDown = False zDown = False #This is to allow double checking (only on left click - and it doesn't really work) CHECK_COUNT = 0 #This is just to show that we're getting back data - uncomment it and hold down the buttons #while True: # print((0 if nc.button_C else 1, 0 if nc.button_Z else 1)) while True: x, y = nc.joystick #Eliminate spurious reads if (x == 255 or y == 255): continue relX = x - centerX relY = centerY - y
def mouse_config_move_x(x_distance): mouse = Mouse(usb_hid.devices) mouse.move(x = x_distance, y = 0)
led = DigitalInOut(board.D13) led.direction = Direction.OUTPUT ## flash the LED at boot for x in range(0, 9): led.value = False time.sleep(0.05) led.value = True time.sleep(0.05) ## configure device as keyboard kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) ## configure mouse for scroll function m = Mouse(usb_hid.devices) ## define buttons encClick = DigitalInOut(board.D9) encClick.direction = Direction.INPUT encClick.pull = Pull.UP b1 = DigitalInOut(board.D0) b1.direction = Direction.INPUT b1.pull = Pull.UP b2 = DigitalInOut(board.D3) b2.direction = Direction.INPUT b2.pull = Pull.UP b3 = DigitalInOut(board.D6)
# Configure buttons on Maker Pi Pico btn1 = digitalio.DigitalInOut(board.GP20) btn2 = digitalio.DigitalInOut(board.GP21) btn3 = digitalio.DigitalInOut(board.GP22) btn1.direction = digitalio.Direction.INPUT btn2.direction = digitalio.Direction.INPUT btn3.direction = digitalio.Direction.INPUT btn1.pull = digitalio.Pull.UP btn2.pull = digitalio.Pull.UP btn3.pull = digitalio.Pull.UP # Set up keyboard and mouse. kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) mouse = Mouse(usb_hid.devices) direction = True while True: # Test buttons on Maker Pi Pico if not btn1.value: # Debounce time.sleep(0.3) while not btn1.value: pass # Type 'abc' followed by newline. layout.write('abc\n') # Type numbers followed by newline. layout.write('789\n') # Type symbols followed by tab. layout.write('!?_*\t')
import time import analogio import board import digitalio from adafruit_hid.mouse import Mouse mouse = Mouse() x_axis = analogio.AnalogIn(board.A0) y_axis = analogio.AnalogIn(board.A1) select = digitalio.DigitalInOut(board.A2) select.direction = digitalio.Direction.INPUT select.pull = digitalio.Pull.UP pot_min = 0.00 pot_max = 3.29 step = (pot_max - pot_min) / 20.0 def get_voltage(pin): return (pin.value * 3.3) / 65536 def steps( axis): # Maps the potentiometer voltage range to 0-20 in whole numbers return round((axis - pot_min) / step) while True: x = get_voltage(x_axis) y = get_voltage(y_axis)
from adafruit_circuitplayground.express import cpx from adafruit_hid.mouse import Mouse m = Mouse() cpx.adjust_touch_threshold(200) while True: if cpx.touch_A4: m.move(-1, 0, 0) if cpx.touch_A3: m.move(1, 0, 0) if cpx.touch_A7: m.move(0, -1, 0) if cpx.touch_A1: m.move(0, 1, 0)
import board import adafruit_nunchuk from adafruit_hid.mouse import Mouse THRESHOLD = 10 m = Mouse() nc = adafruit_nunchuk.Nunchuk(board.I2C()) while True: x, y = nc.joystick x = (x - 128) // 2 y = (128 - y) // 2 if abs(x) > THRESHOLD: m.move(x, 0, 0) if abs(y) > THRESHOLD: m.move(0, y, 0) if nc.button_Z: m.click(Mouse.LEFT_BUTTON) if nc.button_C: m.click(Mouse.RIGHT_BUTTON)
advertisement = ProvideServicesAdvertisement(hid) advertisement.appearance = 961 scan_response = Advertisement() scan_response.complete_name = "CircuitPython HID" ble = adafruit_ble.BLERadio() if not ble.connected: print("advertising") ble.start_advertising(advertisement, scan_response) else: print("already connected") print(ble.connections) # setup for mouse mouse = Mouse(hid.devices) while True: while not ble.connected: pass while ble.connected: # sets x and y values for accelerometer x and y values # x and y are swapped for orientation of feather y, x, z = lsm6ds33.acceleration # map range of horizontal movement to mouse x movement horizontal_mov = simpleio.map_range(steps(x), 1.0, 20.0, -15.0, 15.0) # map range of vertical movement to mouse y movement vertical_mov = simpleio.map_range(steps(y), 20.0, 1.0, -15.0, 15.0) # map range of mouse y movement to scrolling scroll_dir = simpleio.map_range(vertical_mov, -15.0, 15.0, 3.0, -3.0)
import digitalio import busio import adafruit_lis3dh import usb_hid from adafruit_hid.mouse import Mouse killswitch = digitalio.DigitalInOut(board.SLIDE_SWITCH) killswitch.direction = digitalio.Direction.INPUT killswitch.pull = digitalio.Pull.UP smb = digitalio.DigitalInOut(board.BUTTON_A) smb.direction = digitalio.Direction.INPUT smb.pull = digitalio.Pull.DOWN smb_pre = smb.value mouse = Mouse(usb_hid.devices) smb_time = 0 RMB_DELAY = 0.5 while True: if killswitch.value: if smb.value is not smb_pre: smb_pre = smb.value if smb.value: print("button clicked...") smb_time = time.monotonic() print("press time is", smb_time) if not smb.value: print("release time is", time.monotonic())
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)
D3.direction = Direction.INPUT D3.pull = Pull.UP # fourth button D4 = DigitalInOut(board.D4) D4.direction = Direction.INPUT D4.pull = Pull.UP # loop forever while True: if not D1.value: # configure device as keyboard kbd = Keyboard(usb_hid.devices) layout = KeyboardLayoutUS(kbd) kbd.send(Keycode.SPACE) # press the SPACEBAR time.sleep(debounce_time) # debounce delay if not D2.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.click(Mouse.LEFT_BUTTON) # press the Left MOUSE Button time.sleep(debounce_time) # debounce delay if not D3.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.move(-10) # move the mouse right 10 if not D4.value: # configure device as mouse mouse = Mouse(usb_hid.devices) mouse.move(10) # move the mouse left 10
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import board import usb_hid from adafruit_hid.mouse import Mouse import adafruit_nunchuk m = Mouse(usb_hid.devices) nc = adafruit_nunchuk.Nunchuk(board.I2C()) centerX = 120 centerY = 110 scaleX = 0.4 scaleY = 0.5 cDown = False zDown = False # This is to allow double checking (only on left click - and it doesn't really work) CHECK_COUNT = 0 # This is just to show that we're getting back data - uncomment it and hold down the buttons # while True: # print((0 if nc.button_C else 1, 0 if nc.button_Z else 1)) while True: accel = nc.acceleration # print(accel)
import time import usb_hid from adafruit_hid.mouse import Mouse mouse = Mouse(usb_hid.devices) while True: mouse.move(x=20) time.sleep(2) mouse.move(x=-20) time.sleep(5) print("jiggle")