PocketWand
About this project
PocketWand turns any phone into a motion controller for a Python program. The phone streams its rotation through its web browser, so there is no app to install, and it works on iPhone and Android, on any network (campus Wi-Fi included).
What it does
- Starts a Room from one line of Python and prints a QR code. Scan it, tap Enable motion, and the phone is a controller.
- Gives you the phone’s orientation as a quaternion, a 3x3 rotation matrix or yaw, pitch and roll in degrees, plus an on-screen button.
- Supports several phones at once. Each one gets its own Slot, and a phone that reconnects gets its old Slot back.
- Recentres on demand, so “pointing at the screen” counts as straight ahead again.
Try the demo
pip install "pocketwand[demo]"
pocketwand demo
A window opens with a QR code. Scan it with your phone’s camera, open the link, and allow motion access. Move the phone and the phone on screen copies it. pocketwand print does the same without a window and prints the Controller state to the terminal.
Use it in your own game
import pocketwand
room = pocketwand.Room() # starts the server and tunnel, prints a QR code
room.on_join = lambda c: print("joined slot", c.slot)
while True:
c = room.controller(0) # never None; an empty Slot reads as disconnected
if c.connected:
c.orientation # Quaternion(w, x, y, z)
c.orientation.matrix() # 3x3 rotation matrix
yaw, pitch, roll = c.euler # degrees
c.button # True while the on-screen button is held
State is updated on a background thread, so reading it from a game loop every frame is safe and cheap.
How it works
- Server.
Room()runs a small web server on your computer. - Tunnel. It opens a free Cloudflare quick tunnel to that server. The
cloudflaredprogram is downloaded automatically on first run. - HTTPS. The phone opens the page over HTTPS, which iOS requires before a page can read motion sensors.
- Stream. The phone sends its orientation back over a WebSocket. Both sides only make outgoing connections, so firewalls and campus client isolation don’t get in the way.
- Frames. Orientation rotates the phone’s frame into the world frame (+X right, +Y towards your screen, +Z up). Recentring only changes heading, so tilt always matches gravity.