Base library for drawing on small dislpays
$ dotnet add package OledSharp
A .NET 8.0 library for drawing text and graphics on small displays. This library provides a simple abstraction for pixel-based displays with built-in text rendering capabilities using fonts defined through the IFont interface. A default font called "Font5x5" is also included.

[!TIP] The picture above shows an example of what the included font looks like on a 128x64 px display (the image is scaled up 4x for better visibility).
This base library provides two interfaces:
IOledDisplay for controlling individual pixels on a display.IFont for getting character data from a provided character.This library also provides some classes:
TextRenderer uses an IOledDisplay and an IFont to draw text to a display.CharacterData represents the data for a single character in a font.Font5x5 a 5 px implementation of IFont.To install the OledSharp base library via NuGet, run the following command in your project directory:
dotnet add package OledSharp
For implementations of the interfaces in this base library there are two other libraries:
IOledDisplay InterfaceThe main display abstraction interface that all display implementations must implement. Implements IDisposable which means it should be disposed of correctly.
Properties:
int Width { get; } - Display width in pixelsint Height { get; } - Display height in pixelsMethods:
void Initialize() - Initialize the display hardwarevoid SetBufferPixel(int x, int y, bool isOn) - Set a pixel in the display bufferbool GetBufferPixel(int x, int y) - Get a pixel value from the display buffervoid ClearBuffer() - Clear the entire display buffervoid PushBuffer() - Push the buffer contents to the actual displayvoid Dispose() - Clean up resources (inherited from IDisposable)TextRenderer ClassHandles text rendering on displays. Uses an instance of IOledDisplay to draw pixels for a font. Uses an instance of IFont to get the CharacterData for each character to draw in a text.
[!IMPORTANT] By default, if no
IFontis provided, theTextRendereruses theFont5x5.
Properties:
int CharacterSpacing { get; set; } - Spacing between characters in pixels (default: 1)int LineSpacing { get; set; } - Spacing between lines in pixels (default: 2)Constructors:
TextRenderer(IOledDisplay display) - Create with default Font5x5TextRenderer(IOledDisplay display, IFont font) - Create with custom fontMethods:
void DrawText(string text, int x, int y) - Draw text at specified position with line break supportint DrawCharacter(int x, int y, char character, bool isOn = true) - Draw single character, returns actual widthint DrawString(int x, int y, string text, bool isOn = true) - Draw string on single line, returns total widthint DrawMultiLineString(int x, int y, string text, bool isOn = true) - Draw multi-line text, returns total heightint DrawWrappedString(int x, int y, string text, int maxWidth, bool isOn = true) - Draw text with word wrapping, returns total heightint CalculateStringWidth(string text) - Calculate pixel width of textint CalculateMultiLineStringHeight(string text) - Calculate pixel height of multi-line textRepresents a character with its bitmap data and dimensions.
Properties:
byte[] Bitmap { get; } - Bitmap data (width × height bytes, 0 = off, 1 = on)int Width { get; } - Character width in pixelsint Height { get; } - Character height in pixelsint VerticalOffset { get; } - Vertical offset from baseline (positive = down, negative = up)Constructor:
CharacterData(byte[] bitmap, int width, int height, int verticalOffset = 0)Methods:
bool GetPixel(int x, int y) - Get pixel value at specified coordinatesInterface for bitmap fonts used by text renderers.
Properties:
int LineHeight { get; } - Recommended line height in pixelsint BaselineOffset { get; } - Baseline offset from bottom of line heightCharacterData DefaultUnsupportedCharacter { get; } - Default character for unsupported charactersMethods:
CharacterData GetCharacter(char character) - Get character data for specific characterbool IsCharacterSupported(char character) - Check if character is supportedDefault IFont implementation that supports the characters:
0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
åäöÅÄÖ
.,:;!'?-"%_/\()*[]{}+=<>|
[INFO] All characters are drawn as upper case letters.
// Assuming you have an IOledDisplay implementation
using (IOledDisplay display = new YourDisplayImplementation())
{
display.Initialize();
// Create text renderer
TextRenderer renderer = new TextRenderer(display);
// Draw with word wrapping
renderer.DrawWrappedString(
x: 2, // 2 px in from the left edge
y: 2, // 2 px in from the left edge
text: "This is a long text that will wrap",
maxWidth: display.Width - 4); // 2 px from the right edge too
// Push to display
display.PushBuffer();
}
MIT License - see project file for details.