Driving A Bicolor LED Matrix From Adafruit

I recently had driven a single-color LED matrix using a Raspberry Pi, which was more difficult than it needed to be because I decided to send the bytes myself. However, I had a bi-color matrix and wanted to get it to work, just with an easier method.

Luckily Adafruit has a library that we can use for this. Now, what to display? It is 8x8, so that sounds perfect for some retro game sprites.

ImageMagick has some neat tricks. I used one of them to force 8x8 images down to four colors and then to remap the colors to the Gameboy palette. I chose a specific palette because I could later always refer to the same colors in my code, and I chose this palette because I like it.

## You need a 4 pixel PNG of the gameboy palette for this

# One file
convert tile_0021.png -colors 4 -remap gb.png out.png

# As a loop
for i in $(ls *.png); do convert $i -colors 4 -remap gb.png gbcolor/gb-$i; done

After getting the images in a set palette, the next step was extracting the pixel colors and transforming it into a map we could paste into our Arduino code. This was easily done using the pillow image manipulation library for Python3. We could get the RGB values as an array and convert them into a string, such as “255255255”, and use this string as a key in a map.

COLOR_MAP = {
    "155615": "0",
    "489848": "1",
    "13917215": "2",
    "15518815": "3"
}

The image can now be represented like this:

PhilipJFryVII :: code/py/scripts ยป python3.12 get_8x8_pixel_data.py
00000000
00033000
02222311
23311211
23311211
02222311
00033000
00000000

Now we just needed to save this as a format easily importable in the Arduino’s C++ code. After some fiddling, I decided that a string with spaces separating the rows was the way to go:

  char img[] = "21000000 20300020 20030100 02003000 02010300 00200030 30022003 13000222";

  for (int i=0; i<72; i++) {
    if (isSpace(img[i])) {
      Serial.println("Space!");
    } else {
      Serial.println("Not Space!");
    }
  }

Since we would be putting this into an array, we modified the python script to be able to output that format for us:

String imgs[] = {
"00000000 02000000 00210010 02000000 00002000 01000210 00002000 00000000",
"00000111 00001330 30013231 31333233 31333223 30013231 00001331 00111111",
"00000000 00200020 02002300 00033200 00233000 00320020 02000300 00000000",
"00100000 01010000 00100000 00000010 01000000 00000100 00001010 00000100",

[....]

}

Now we just iterate over each string, and matching each number to a color we want to display.

Here is a screenshot of the display in action

If you want to see the code for this project, it is located here: https://github.com/LetsEatLabs/Adafruit-8x8-BiColor-Pixel