Dot Matrix Display Custom Font Method

Hi guys,

I am looking for a bit of general direction on how to streamline my program.

I have a 11x7 matrix LED display that I want to display speed and other metrics from my ebike over canbus. I have got the canbus part sorted.

The LED matrix works by sending a co-ordinate for the LED to be lit, and can also be used with the Adafruit GFX lib.

Example -

ledmatrix.drawPixel(6, 0, brightness);

I have designed the numbers that I want to use, using excel and created a table with the corresponding co-ordinates.


My question is how can I elegantly use these without writing lots of lines of code to light up the individual LEDs that make up the number?

Would creating a library for this be a good approach? Make a function for each number? How is this handled usually?

Thanks in advance!

The LEDs can be represented by bits in an array of bytes

byte numbers[10][5] =
{
  {
    0b011100000,  //0
    0b010100000,
    0b010100000,
    0b010100000,
    0b011100000
  },
  {
    0b001000000,  //1
    0b001000000,
    0b001000000,
    0b001000000,
    0b001000000
  }
//etc
};

Given a digit value, it can be used as the index to the array then the required LED states for the LEDs read out of the array and applied to the LEDs

As an extension, the data for 2 digits can be put into the spare bits of each byte thus saving memory

1 Like

You could fit each character in 3 bytes by using one byte per column and one bit for each of the 5 rows.

byte font[3][10] =
{
{0x1F, 0x11, 0x1F}, // 0
{0x00, 0x1F, 0x00}, // 1
.
.
.
}

Then use nested loops to go through the columns and rows:

 void showDigit(int digit, int startRow, int startCol)
{
  for (int col = 0; col < 3; col++)
  {
    byte columnBits = font[col][digit];
    for (int row = 0; row < 5; row++)
    {
      bool rowBit = columnBits & (0x10 >> row);
      ledmatrix.drawPixel(startRow + row, startCol + col, brightness * rowBit);
    }
  }
}

An example

byte numbers[10][5] =
{
  {
    0b011100000,  //0
    0b010100000,
    0b010100000,
    0b010100000,
    0b011100000
  },
  {
    0b001000000,  //1
    0b001000000,
    0b001000000,
    0b001000000,
    0b001000000
  }
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  outputDigit(0);
  outputDigit(1);
}

void loop()
{
}

void outputDigit(byte digit)
{
  for (int row = 0; row < 5; row++)
  {
    for (int bit = 7; bit >= 0; bit--)
    {
      if (bitRead(numbers[digit][row], bit) == 1)
      {
        Serial.print("X");
      }
      else
      {
        Serial.print(" ");
      }
    }
    Serial.println();
  }
}