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)
relY = y - centerY m.move(int(scaleX * relX), int(scaleY * relY), 0) buttons = nc.buttons c = buttons.C z = buttons.Z if z and not zDown: stillDown = True for n in range(CHECK_COUNT): if nc.button_Z: stillDown = False break if stillDown: m.press(Mouse.LEFT_BUTTON) zDown = True elif not z and zDown: stillDown = True for n in range(CHECK_COUNT): if not nc.button_Z: stillDown = False break if stillDown: m.release(Mouse.LEFT_BUTTON) zDown = False if c and not cDown: m.press(Mouse.RIGHT_BUTTON) cDown = True elif not c and cDown: m.release(Mouse.RIGHT_BUTTON)
# Countdown to start - give time for the OS to see the device dot[0] = (15, 0, 0) sleep(2) dot[0] = (0, 0, 127) sleep(2) 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
buttons = 0 buttons_released = 0 press_flg = False release_flg = False if not btn1.value: #Press if not prev_btn1: buttons = buttons | Mouse.LEFT_BUTTON press_flg = True prev_btn1 = True else: if prev_btn1: buttons_released = buttons_released | Mouse.LEFT_BUTTON release_flg = True prev_btn1 = False if not btn2.value: #Press if not prev_btn2: buttons = buttons | Mouse.RIGHT_BUTTON press_flg = True prev_btn2 = True else: if prev_btn1: buttons_released = buttons_released | Mouse.RIGHT_BUTTON release_flg = True prev_btn2 = False if press_flg: mouse.press(buttons) if release_flg: mouse.release(buttons_released) time.sleep(0.01)
# Fonction permettant de déplacer le curseur sur de nouvelles coordonnées en cliquant gauche ou pas def trace(Depl_X,Depl_Y,BOUTON) : # Si le 3ème paramètre est 'C' alors on émule un clic sur le bouton gauche de la souris # sinon on relâche l'ensemble des boutons if (BOUTON == 'C') : souris.press(Mouse.LEFT_BUTTON) else : souris.release_all() # On déplace le curseur via les déplacements relatifs Depl_X et Depl_Y souris.move(x=Depl_X, y=Depl_Y) # On trace le dessin en prenant chaque coordonnée dans le tuple for coordonnee in dessin : trace(coordonnee[0],coordonnee[1],coordonnee[2]) # pause pour donner l'impression d'un dessin fait à la main sleep(0.1) # Relâche le bouton de la souris et ramène le curseur à gauche sous le dessin précédent souris.release_all() souris.move(x=-((6*L)+(5*E)),y=2*H) # Emule l'appui sur le bouton gauche de la souris puis trace un sinus pour souligner souris.press(Mouse.LEFT_BUTTON) for i in range (2*180) : souris.move(x=1, y=int(10*sin(i))) # Emule le relâchement de tous les boutons de la souris souris.release_all() print("fin")
if data_string[:5]=="MCLIC": try: mouse.click(int(data_string[6:])) uart.write(b'OK') except: print("MCLIC ERROR") uart.write(b'NO') # MMOVE:x,y,wheel ex. MMOVE:x=-10,y=2 if data_string[:5]=="MMOVE": try: mouse.move(data_string[6:]) uart.write(b'OK') except: print("MMOVE ERROR") uart.write(b'NO') # MPRES:button (1,2 or 4) ex. MPRES:2 if data_string[:5]=="MPRES": try: mouse.press(int(data_string[6:])) uart.write(b'OK') except: print("MPRES ERROR") uart.write(b'NO') # MRLEA:button (1,2 or 4) ex. MRLEA:2 if data_string[:5]=="MRLEA": try: mouse.release(int(data_string[6:])) uart.write(b'OK') except: print("MRLEA ERROR") uart.write(b'NO')
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, 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) # 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 def process(self): # Call the debouncing library frequently self.joy_z.update() self.dpad.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.mouse.release(Mouse.LEFT_BUTTON) self.mouse.release(Mouse.RIGHT_BUTTON) self.state = _USERCODE elif self.state == _USERCODE: self.dotstar_led[0] = (0, 0, 0) self.dotstar_led.deinit() self.joystick_gnd.deinit() self.x_axis.deinit() self.y_axis.deinit() self.dpad.deinit() self.joy_z.deinit() try: # Load usercode.py __import__("usercode") except ImportError: print("Missing usercode.py file") # If we get here due to an exception or the user code exiting # then restart as it's probably going to by the most stable # strategy # supervisor.reload() # 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)