Home Archives About
moefh.github.io
The Magic Smoke Has Left the Building
Porting Loser Corps to the ESP32 - Part 6

More progress in porting Loser Corps to the ESP32 (see all posts about the port).

The game now runs with networking enabled while using the full 320x240 resolution:

Two ESP32s talking to each other via ESP-NOW, each one controlling a character (note also the Wii Classic Controller)
Two ESP32s talking to each other via ESP-NOW, each one controlling a character (note also the Wii Classic Controller)

To fit WiFi and the two 320x240 framebuffers in memory, I had to make some changes: first, don't use Bluetooth -- so the Wiimote is out. Luckily I now can use the Wii Classic Controller (as shown in the picture) or Wii Nunchuk, so that's not a big deal.

Second, I had to decrease the number of transmit and receive buffers used by the WiFi library. This can't be changed in with the Arduino Core for the ESP32, so I can't use the Arduino IDE for this. Using the ESP-IDF framework, this (and a lot more) can be changed with idf.py menuconfig:

Adjusting the number of WiFi receive buffers
Adjusting the number of WiFi receive buffers

Thankfully it's not too much of a hassle to use the same code with the two APIs (which is not that surprising, since the Arduino Core is just an added layer on top of the ESP-IDF). The largest hurdle was to learn how to set the CPU clock using the ESP-IDF (it's a lot more involved than I thought, I ended up lifting code from the Arduino Core) and to support the Wiimote library, which uses the Bluetooth API from the Arduino Core (so once more I lifted some code from there).

In the end, it was fun and I learned a little bit about FreeRTOS (which the Arduino Core completely hides) in order to run my code on one of the CPUs of the ESP32 and let the WiFi and other stuff use the other one.

The other change in the game since the last update post was the addition of Wii Nunchuk and Classic Controller support via I2C with my esp32-wii-nunchuk library.

After plugging the library to the game code, I noticed that sometimes the I2C read from the controller gets stuck for a little bit. Usually that's not a big problem, but for this game any hiccup is really noticeable and annoying (the engine really wants to run at 60fps). So I had to add some functions to the library to run a loop that reads the controller state in a different CPU core than the game engine, and it ended up working really well.

The source code for the version of the game that uses the Arduino API is here: https://github.com/moefh/esp32-loser. I haven't published the version using the ESP-IDF yet, but it just adds some stuff to compile under ESP-IDF.

Using Wii I2C Controllers on the ESP32

I wrote a very simple library to make the ESP32 talk via I2C to Wii controllers like the Wii Nunchuk and the Wii Classic Controller.

There are a few libraries around that do this for the Arduino using the Wire library, but for some reason that doesn't work consistently with the ESP32 and the Wii controllers. So I wrote the I2C communication using the ESP-IDF API, and it works really well, except that for some reason I wasn't able to make it work at 400KHz (which is supposedly what the Wii controllers accept), only at 100KHz. That's a little disappointing, but good enough for my purposes, which is using the controllers in the ESP32 port of Loser Corps.

While playing with it, I wrote a little test program to display the state of the connected controller on a small OLED screen (SSD1306), which is controlled using the Adafruit SSD1306 library also via I2C, but using the Wire library:

Testing the Wii Classic Controler
Testing the Wii Classic Controler

When using the Wii Classic Controller, the screen shows two triangles that point on the direction where you move the sticks, growing in size as you move the stick farther along. Pressing each button lights a corresponding character on the screen, and pulling the analog triggers increases the size of two bars that appear on the left side.

When using the Wii Nunchuk, the screen shows a single triangle with the status of the nunchuk stick and three bars to the left that show the status of the X, Y and Z accelerometer readings. Pressing C or Z displays the corresponding letter on the screen.

I wrote this small test program mostly for fun, but it tests something important: that my library, which uses I2C via ESP-IDF, can coexist with Wire library. When using them together, you just have to be careful not to use the same ESP32 I2C port with the two libraries at the same time. In this program, my library uses I2C port 0 and the Wire library uses I2C port 1 (by using the Wire1 object).

The source code for the library is here: https://github.com/moefh/esp32-wii-nunchuk

Porting Loser Corps to the ESP32 - Part 5

More progress in porting Loser Corps to the ESP32 (see all posts about the port).

This is just a quick update: I have added shooting to the game. You shoot by pressing ② in the Wiimote (or D in the Arduino joystick). There can be at most 14 shots active at once, and they do nothing but fly in a straight line until they hit a wall or the edge of the map.

Scheenshot of projectile (the camera doesn't like moving objects, in  reality it's less blurry than shown  here)
Scheenshot of projectile (the camera doesn't like moving objects, in reality it's less blurry than shown here)

I also ported the game to use the ESP-IDF framework (as opposed to the Arduino framework, which originally used). I'm trying to see if I can decrease the memory used by the WiFi system so the game can use full 320x240 resolution while in networked mode, and the EDP-IDF framework has lots of configuration options. I haven't made much progress with it yet: I was just recently able to make the game run at full speed, after finding that I have to set not ony the processor clock to 240MHz but also the flash SPI clock to 80MHz (apparently the flash speed is a bottleneck, which is not surprising since we copy images directly from flash to the framebuffer).

As usual, the source is on Github (I realize I haven't written this before, so from now on I'll start adding this link to every post).