Bitmap Fonts

This is a modified version of the SSD1306 library provided by Adafruit that is used with this lesson and future lessons. This is a very stripped down version that is tested to work with i2c 128x64 OLED displays. It provides functionality to plot pixels, blit sprites and draw text using bitmap fonts generated using the technique explained in this course.
This document contains a lot of detail about how we can generate images we can render on an OLED screen using a scripting language (PHP) and some images we gathered from the internet. Bitmap Fonts are just a special name for sprites that are used for representing text on the screen. The information contained in this document is also applicable for converting sprite sheets into code you can then use in your Arduino game projects. Old Game Boy games had sprites with only 4 colors and were generally small in size. They lend themselves very well to use in game projects on the Arduino if you do not want to design your own graphics. Also, using the information contained in this document, you will learn how you can use any paint program to design sprites so you can stop making them using Graphing paper and hand calculations.
Code Page 437

This is the image you will want to download if you wish to generate the header file yourself with the code provided.
<?php

$im = imagecreatefrompng('Codepage-437.png');

$image_width = imagesx($im);
$image_height = imagesy($im);

$tile_x_offset = 0;
$tile_y_offset = 0;

$tile_boundary_width = 9;
$tile_boundary_height = 16;

$tile_columns = $image_width / $tile_boundary_width;
$tile_rows = $image_height / $tile_boundary_height;

$tile_width = 8;
$tile_height = 16;

$binary = [];

for ($row = 0; $row < $tile_rows; $row++) {
    for ($column = 0; $column < $tile_columns; $column++) {

        for ($tile_y = 0; $tile_y < $tile_height; $tile_y++) {
            for ($tile_x = 0; $tile_x < $tile_width; $tile_x++) {

                $image_x = $tile_x_offset + $column * $tile_boundary_width + $tile_x;
                $image_y = $tile_y_offset + $row * $tile_boundary_height + $tile_y;

                if ($image_x >= $image_width) {
                    exit('Invalid Tile Width Measurements');
                }

                if ($image_y >= $image_height) {
                    exit('Invalid Tile Height Measurements');
                }

                $pixel_color = imagecolorat($im, $image_x, $image_y);

                $tile_number = $row * $tile_columns + $column;

                if (!isset($binary[$tile_number][$tile_y])) {
                    $binary[$tile_number][$tile_y] = '0b';
                }

                $binary[$tile_number][$tile_y] .= $pixel_color ? '1' : '0';
            }
        }
    }
}

print_r($binary);

foreach ($binary as $n => $vals) {
    $binary[$n] = implode(", //" . ($n + 1) . "\r\n  ", $vals);
}

$code = '
PROGMEM const byte CODEPAGE437[] = {
  ' . implode(",\r\n  ", $binary) . '
};
';
$fp = fopen('Codepage437.h', 'w');
fwrite($fp, $code);
fclose($fp);


This is the header file which contains the definitions for the Code Page 437 character set.
1602 LCD Character Display

This is the image you will want to download if you wish to generate the header file yourself with the code provided.
<?php

$im = imagecreatefrompng('1602.png');

$image_width = imagesx($im);
$image_height = imagesy($im);

$tile_x_offset = 103;
$tile_y_offset = 46;

$tile_boundary_width = 35;
$tile_boundary_height = 50;

$tile_columns = 16;
$tile_rows = 16;

$tile_width = 5;
$tile_height = 8;

$pixel_width = 5;
$pixel_height = 5;

$pixel_width_midpoint = floor($pixel_width / 2);
$pixel_height_midpoint = floor($pixel_height / 2);

$binary = [];

$positions = [
    0 // 0
    , 0 // 1
    , 1 // 2
    , 2 // 3
    , 3 // 4
    , 4 // 5
    , 5 // 6
    , 6 // 7
    , 0 // 8
    , 0 // 9
    , 7 // 10
    , 8 // 11
    , 9 // 12
    , 10 // 13
    , 11 // 14
    , 12 // 15
];

for ($column = 0; $column < $tile_columns; $column++) {
    for ($row = 0; $row < $tile_rows; $row++) {

        for ($tile_x = 0; $tile_x < $tile_width; $tile_x++) {
            for ($tile_y = 0; $tile_y < $tile_height; $tile_y++) {

                $image_x = $tile_x_offset +  $positions[$column] * $tile_boundary_width + ($tile_x + 1) * $pixel_width + $pixel_width_midpoint;
                $image_y = $tile_y_offset + $row * $tile_boundary_height + (7 - $tile_y) * $pixel_height + $pixel_height_midpoint;

                if ($image_x >= $image_width) {
                    exit('Invalid Tile Width Measurements');
                }

                if ($image_y >= $image_height) {
                    exit('Invalid Tile Height Measurements');
                }

                $pixel_color = imagecolorat($im, $image_x, $image_y);
                $tile_number = $tile_rows * $column + $row;

                if (!isset($binary[$tile_number][$tile_x])) {
                    $binary[$tile_number][$tile_x] = '0b';
                }
                $binary[$tile_number][$tile_x] .= $pixel_color ? '0' : '1';
            }
        }
    }
}

print_r($binary);

foreach ($binary as $n => $vals) {
    $binary[$n] = implode(", //" . ($n + 1) . "\r\n  ", $vals);
}

$code = '
PROGMEM const byte LCD1602[] = {
  ' . implode(",\r\n  ", $binary) . '
};
';
$fp = fopen('LCD1602.h', 'w');
fwrite($fp, $code);
fclose($fp);

    
    
    
This is the header file which contains the definitions for the character set found on commonly used LCD Character displays.