Working from home means that my laptop can’t sleep. So I needed something that appears as a Human Interface Device (HID) and randomly sends a mouse input every three minutes or so.
You’ll need to install CircuitPython version 7.0.0 or higher first, otherwise you won’t be able to make the device identify as USB HID without rebuilding CircuitPython.
I used an Adafruit Neo Trinkey and a Raspberry Pico. Neither are expensive, I paid £6.30 and £3.60 respectively. Trinkey is a bit flashier, doesn’t need a cable, fits on a keyring and has the ability to bypass
boot.py by pressing both capacitive keys.
No point in having a version control system and barking oneself, so the code is on Github. Copy both
code.py to the device.
I’ll go over the Pico code here but the Neo is similar.
Make the mouse move
import usb_hid import board import digitalio from time import sleep from adafruit_hid.mouse import Mouse from random import randint HOW_LONG = 180 # Pause between jiggles in seconds led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT mouse = Mouse(usb_hid.devices) def blink(times): for x in range(times): led.value = False sleep(0.5) led.value = True print(x) def jiggle(): for each in range(randint(1, 4)): x = randint(1, 50) y = randint(1, 50) mouse.move(x, y) mouse.move(-x, -y) sleep(HOW_LONG) while True: led.value = True blink(3) jiggle()
Once we know that code is working and more importantly that it works the way you need it to then we can move on to the next step.
Make the device appear as just HID
I was able to avoid this with the Neo Trinkey by checking at boot time for both capacitive keys being active. Had to put in a delay too, otherwise it is difficult to time. Quite common working with embedded systems that you need to add delays.
import storage import usb_cdc storage.disable_usb_drive() # Disable USB Drive usb_cdc.disable() # Disable REP
If it all goes wrong…
If it all goes wrong and you can’t access either device, then you can reset the flash memory.
Disconnect the Pico, hold the BOOTSEL button and keep it depressed while reinserting. When RPI-RP2 drive appears, copy the bootloader file from Adafruit. This will completely reset the Pico.
Follow the process here.