Interfaces

Assignment

Write an application that interfaces a user with an input or output device that you made.

For the machine design week, I wrote the code that glues the UI interface to the RP2040 controlling the electronics, so this was a more familiar assignment.

Given that I had an accelerometer on deck, I wanted to make a video-game like interface where movement of the accelerometer leads to movement on the interface screen! I was inspired by Mario Kart, and wanted to make an interface that responded to turning of a steering wheel.

Using PyGame, I created a simple platformer following their tutorials, that moves left and right.

Interfaces

The player and platform code creates sprites, with functions initializing position and how the platform looks.
Interfaces

The actual game logic occurs in this while loop, which calls the player’s move function, and updates the screen at each timestep.

Interfaces

As of now, the player is controlled by L + R arrow keys, but upon integrating the accelorometer, the player will move based on physical movement.

During machine week, I had an RP2040 communicating serially with the computer interface, so I’m reusing that communication protocol here.

The Xiao RP2040 will be running code that prints out movement decisions based on what it senses. The Python interface will then serially read this information, and call the appropriate move functions accordingly.

Integration

Using my ADXL board module for my final project, I updated the code to simply poll the accelerometer and write back “LEFT” or “RIGHT” based on whether the y acceleration is > 2.5 or < -2.5
Interfaces

For the game code, I read from the serial port until I receive a “LEFT” or “RIGHT” command, and move accordingly.
Interfaces
Interfaces

It works! Upon steering right, we go right. Upon steering left, we go left!

Unfortunately, the pygame interface crashes after some time. My suspicions is that I’m printing too often to the serial port.

Files

Game Code