LED matrix games python

i spent the night tring to play the games on the led matrix and mangaed to find and map controls for 2 games on my units. i could not find tetris.

but i wanted to share my code that i wrote in python to play the games. so perhaps you might be able to play as well.

controls are WASD and arrow keys for snake and A-D for top pong and arrow keys for lower player.

i still have work to do but right now it’s playable. my code uses the serial commands to control the stock matrix. you will need pip - keyboard and pip - serial installed to play.

linux users you may need to modify the com ports to play as i am using windows.

happy gaming :smiley:

known issues. exit is not fully working as intended (ctrl - c to quit fully)
i have yet to add any logic on probing the game status besides printing it out. (uncoment the function near the bottom to see that status)
having the status printout on and the key refresh rate higher than 10hz can lead to serial conflicts. i tried to write some code to go around that but it’s honistly not great.

im not the best coder but ill slowly figure it out.

2 Likes

neat!

by the way, you may want to add the appropriate serial port names for linux since they will be different to on windows (there are multiple ways to detect if the host os is linux or windows form puthon), or use a command line argument to override the serial ports used.

also the reason exit may not be working is because it falls into that infinite loop at the end. you could trigger a break from here, raise and then catch a (possibly custom) exception, or call sys.exit(0) (make sure to do all of this only after the rest of your exit handler is done cleaning up!)

sadly, i dont have led matrix modules, so i cant actually test your code.

edit to add: afaik the keyboard module does global key listeners. on linux, it fails to load if the program is not run as root and there is no x11 server (so even if you fix the serial port thing it can have issues). on a fw16 running linux, the user is most likely using wayland (since it actually works on amd drivers) so although it will probably work with xwayland input wont work unless the user is focusing an app running in xwayland (which may not be the terminal emulator). to do key listeners in the terminal only (that dont require root or x11) you can use the curses library to change the terminal mode to not buffer input until enter is pressed and then use curses functions to get input (annoyingly i dont think curses works on windows and i cant think of any other python library that provides easy key listeners without starting a gui, unlike in c#/dotnet or java where there are built-in language functions for this type of console input).

edit to add (the second): you can use argparse (like curses, its part of the python standard library so you dont need to install anything) to implement command line arguments with relative ease

Thanks for the tips! I will look into adding some Linux support as I go!

Definitely sounds like getting it to fully work on Linux might be a slight challenge with permissions. Eventually, I might try to add a UI as I will need that for the real project I have in mind for the LEDs for my use case (mapping I/O bits to LEDs over different protocols on a network).

1 Like

on the “serial conflicts” with high poll rate, do you know if the led matrix modules support hardware flow control?

if they do, you can use it to detect if the module is ready and then dynamically decrease the poll rate if required. this optimisation will only really help if the rate of data sent to the led matrices is somewhat variable.

i dont think there is a hardware flow control as the serial is software emulated over usb. i havent even looked at rust yet to start looking at the micro controler side of the code to figure more out. i have been mostly using this → inputmodule-rs/commands.md at main · FrameworkComputer/inputmodule-rs · GitHub

1 Like