Skip to content

Instantly share code, notes, and snippets.

@dglaude
Created February 8, 2023 20:53
Show Gist options
  • Save dglaude/ef325471aa3a7ae22cab766aaa62fefb to your computer and use it in GitHub Desktop.
Save dglaude/ef325471aa3a7ae22cab766aaa62fefb to your computer and use it in GitHub Desktop.
Mouse jiggler with speed control of the rotation, and only visible as mouse if boot.py is selected.
import storage, usb_cdc
import board, digitalio
import usb_hid, usb_midi
# On the Macropad, pressing a key grounds it. You need to set a pull-up.
# If not pressed, the key will be at +V (due to the pull-up).
button = digitalio.DigitalInOut(board.KEY12)
button.pull = digitalio.Pull.UP
# Disable devices only if button is not pressed.
if button.value:
storage.disable_usb_drive()
print("storage.disable_usb_drive()")
usb_cdc.disable()
print("usb_cdc.disable()")
usb_midi.disable()
print("usb_midi.disable()")
# usb_hid.enable()
# print("usb_hid.enable()")
usb_hid.enable((usb_hid.Device.MOUSE,)) # Enable just MOUSE.
print("usb_hid.enable((usb_hid.Device.MOUSE,))")
print("End of boot.py")
# Adafruit Macropad, rotating mouse
# From: https://github.com/todbot/circuitpython-tricks
# Detect if USB is connected or not
import supervisor
import time
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse
import rotaryio
from digitalio import DigitalInOut,Pull
led = DigitalInOut(board.LED)
led.switch_to_output()
led.value = False
# In practice need 0.8 s before connection
for i in range(10):
if supervisor.runtime.usb_connected:
led.value = True # USB
print("Connected")
else:
led.value = False # no USB
print("NOT Connected")
time.sleep(0.2) # Debounce delay
print("---------")
for i in range(10):
led.value = not led.value
time.sleep(1)
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""CircuitPython Essentials HID Mouse example"""
mouse = Mouse(usb_hid.devices)
select = digitalio.DigitalInOut(board.BUTTON)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP
encoder = rotaryio.IncrementalEncoder(board.ENCODER_A, board.ENCODER_B) # must be consecutive on Pico
# Method #7: Using the iter function and the next function
# From: https://www.geeksforgeeks.org/iterate-over-a-list-in-python/
list = [(0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1)]
# Use the next function to retrieve the elements of the iterator
while True:
# Create an iterator object using the iter function
iterator = iter(list)
try:
while True:
element = next(iterator)
led.value = not select.value
# print(encoder.position) # starts at zero, goes neg or pos
mouse.move(x=encoder.position*element[0],y=encoder.position*element[1])
if select.value is False:
mouse.click(Mouse.LEFT_BUTTON)
time.sleep(0.2) # Debounce delay
except StopIteration:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment