https://github.com/sonjoonho/flask-wii
After stumbling across a neat little project called web-riimote by konaraddio I thought it would be fun to recreate the features of this app from scratch, and without peeking at konaraddio’s code. I had been using Flask a lot recently, so that was my tool of choice.
Device orientation data is collected from the controller (a mobile device) using the Device Orientation API which makes it extrordinarily simple to extract.
Now all we have to do is send this data to the server.
To support the sending of realtime device orientation data from the client to the server, we need to use WebSockets. We will implement this using the JavaScript library SocketIO, which uses WebSockets where available, but also has the advantages of providing failovers in the event that WebSockets are not supported. SocketIO also allows the use of namespaces and rooms to separate communication channels.
Receiving socket events from the Flask server is also made easy with the Flask-SocketIO extension. We can receive, handle, and emit socket events from the server.
The controller, the center of the main display, and the edge of the main display form a right angle triangle. Then we can the tangent trigonometric ratio to calculate the position of the cursor on the screen. I feel like this could be proved, but it works fine for the demo. The calculate_pos
function performs this calculation and returns a dictionary of the form {"x": x_pos, "y": y_pos}
.
The server then emits the position data to the server, which then handles the cursor position.
And that’s pretty much it! Obviously, this is very much a skeleton, and doesn’t include any of the backend routing and features such as steering and controller button presses, nor the hours of troubleshooting networking bugs that occur for no discernable reason; but this encompasses much of the basic architecture. For a more in-depth look, visit the GitHub repo (linked at the top of this article).
Built With
- Flask - Python micro web framework
- SocketIO - provides realtime. bi-directional communication
- Flask-SocketIO - Flask integration with SocketIO
Tools
- Vim
- Pipenv - dependency management
- Black - Python code formatter, to keep style consistent
- Coffee - fuel for the brain
Acknowledgements
- Pointing cursor and grabbing cursor are from the Nintendo Wiki.
- Wii wheel is from the Mario Kart Wiki.
Future Work
- This application has not been tested on any platforms other than Chrome and Android. In particular, apparently iPhones use game-based calibration for DeviceOrientationEvent.alpha, which means additional conversion would have to be implemented in the app.
- Latency issues
- Project structure
- Canvas demo