A Classic Snake Game in Rust

The goal of this project was to experiment with Rust bare-metal programming by implementing a simple game. The chosen hardware platform was STM32F4DISCOVERY. It is similar to F3 DISCOVERY from the official embedded Rust tutorial however, it has some additional features like analog-digital converter required for the snake to be controlled with a joystick. The game is displayed on a 96×64 OLED screen using the SSD1331 controller and SPI interface. In the process of development and debugging two additional versions of the game were created: one with text-based UI for running the game in a terminal and the other one using a game engine called Quicksilver. Quicksilver is capable of targeting WebAssembly, which means the same game can be played both on the microcontroller and in a web browser. Implementation of the game. Embedded HAL  hides a lot of details and allows us to interact with the hardware on a relatively high level of abstraction.
Some remarkable issues are:

* Development without OS and allocator requires #![no_std] flag but Rust’s native test framework depends on standard library facilities. To keep unit tests in the same file as the source code (which is Rust’s convention), a conditional compilation was required.

* Lack of dynamic memory allocation makes it difficult to parameterize application code with the size of “things” – everything has to be set at compile time. It is a problem when unit testing or targeting different screen sizes. The easy solution for this would be to use a feature named const generics which, unfortunately, is not yet fully implemented in Rust. Luckily there is a workaround in the form of crate generic_array, but overall Rust still has to catch up with C++ in this subject.

* Important thing one has to remember when developing with Rust is that the difference in performance between debug and release versions is huge. In the case of this game and microcontroller, the debug version was unusable.


For more information follow the link below: