Drawing to the Screen¶
After this page you can draw text, shapes and a small picture on the screen.
The Cyber Fidget has a 128 x 64 pixel monochrome OLED display. You draw to it by clearing a buffer, drawing primitives (text, shapes, bitmaps), and then pushing the buffer to the screen. The buffer keeps your drawing until you clear or change it. The usual app pattern redraws the whole frame.
What is this?¶
The display works like an Etch A Sketch: you clear it, draw everything fresh, then show it. Calling clear() erases the drawing buffer, ready for a full redraw. Your update() loop is responsible for drawing the full picture every frame.
Like an Etch A Sketch
Clear it, draw everything fresh, then show it. Each frame is a complete redraw. Your code chooses when to clear the previous drawing.
Display basics¶
- Resolution: 128 x 64 pixels
- Type: Monochrome OLED (black and white)
- Coordinate system: (0, 0) is top-left. X increases right, Y increases down.
Inside the studio starter, display is ready to use in begin(), update() and end(). Add fragments where their comments say; the starter shows the drawing with its final display.display() call.
DisplayProxy is the app interface, the set of drawing functions your app can use. In a helper at the top of the file, use HAL::displayProxy() to get the screen instead of the class member display.
// Insert inside update(), just above display.display();
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "Hello!");
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
In the starter, nullptr means a pointer has no object. * in a declaration marks a pointer (an address), and before a pointer reads its object. & in a type refers to an existing object; before a value it gives its address. const means the code cannot change the value through that declaration.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "Hello!");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Keep important pixels out of the corners¶
The case has a 45-degree corner cut (a chamfer) at each corner of the screen opening. Those cuts hide about 8 pixels at each corner of the 128 x 64 display: the hidden area is roughly a right triangle with 8-pixel sides, so a pixel drawn at (0, 0) is not visible on the device, and anything within about 8 pixels of a corner along both edges may be clipped.
The four marked triangles are hidden by the case. The Studio emulator draws the real case, so it hides them the same way.
- Keep text, score counters and key sprites out of the four 8 x 8 pixel corner areas.
- Backgrounds, borders and anything that is fine to lose can run into the corners.
The figure comes from the case's corner cuts: about 6% of the screen width (7.7 px of 128) and 12% of the screen height (7.9 px of 64), rounded to 8. It is an estimate taken from the case drawing, not a measured tolerance, so leave a pixel or two of margin.
The clear -> draw -> display cycle¶
Every frame, follow this pattern:
- clear() -- Erase the display buffer.
- draw -- Add text, shapes, bitmaps.
- display() -- Send the buffer to the physical screen.
Until you call display(), nothing appears on the OLED. All drawing goes to an internal buffer.
Drawing primitives¶
Text¶
// Insert inside update(), just above display.display();
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(10, 10, "Hello!");
display.drawStringMaxWidth(10, 30, 108, "Short text");
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(10, 10, "Hello!");
display.drawStringMaxWidth(10, 30, 108, "Short text");
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
drawStringMaxWidth() accepts a width, but studio apps draw one unwrapped line on the device and in the browser emulator. Keep lines short or draw separate lines yourself.
Shapes¶
// Insert inside update(), just above display.display();
display.clear();
display.drawRect(10, 10, 50, 20);
display.fillRect(10, 40, 50, 16);
display.drawCircle(84, 32, 15);
display.fillCircle(84, 32, 10);
display.drawLine(10, 10, 117, 53);
display.drawHorizontalLine(10, 32, 108);
display.drawVerticalLine(64, 8, 48);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
display.drawRect(10, 10, 50, 20);
display.fillRect(10, 40, 50, 16);
display.drawCircle(84, 32, 15);
display.fillCircle(84, 32, 10);
display.drawLine(10, 10, 117, 53);
display.drawHorizontalLine(10, 32, 108);
display.drawVerticalLine(64, 8, 48);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Bitmaps (drawXbm)¶
const keeps the image data unchanged. Each uint8_t is a whole number from 0 to 255, stored in one byte.
// Insert at the top of the file, below the #include lines
static const uint8_t smile[8] = {
0x3c, 0x42, 0xa5, 0x81, 0xa5, 0x99, 0x42, 0x3c
};
// Insert inside update(), just above display.display();
display.clear();
// drawXbm(x, y, width, height, data)
display.drawXbm(60, 28, 8, 8, smile);
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
static const uint8_t smile[8] = {
0x3c, 0x42, 0xa5, 0x81, 0xa5, 0x99, 0x42, 0x3c
};
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
display.clear();
// drawXbm(x, y, width, height, data)
display.drawXbm(60, 28, 8, 8, smile);
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
A bitmap is an array of pixels. XBM stores one on/off bit per pixel, with the leftmost pixel in the lowest bit of each byte. This 8 x 8 image needs eight bytes. Larger pictures need more bytes; do not increase the dimensions without supplying enough data.
Code example: text and shapes¶
The fixed score value below demonstrates drawing text. For a score that changes when you press a button, use the state and callback in Buttons & Events. Change ready to false to draw only the circle outline.
// Insert at the top of the file, below the #include lines
#include <stdio.h>
// Insert inside update(), just above display.display();
int score = 42; // Replace with your game's score.
bool ready = true;
display.clear();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 4, "My App");
display.drawRect(10, 24, 108, 20);
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
char text[24];
snprintf(text, sizeof(text), "Score: %d", score);
display.drawString(14, 28, text);
display.drawCircle(64, 52, 6);
if (ready) {
display.fillCircle(64, 52, 4);
}
Full code for this step
This full code is for code name MyApp (create it with App Name "My App"); if your app has another code name, change MyApp and myApp to yours.
#include "MyApp.h"
#include "globals.h"
#include "HAL.h"
#include "MenuManager.h"
#include <stdio.h>
MyApp myApp(HAL::buttonManager());
MyApp* MyApp::instance = nullptr;
MyApp::MyApp(ButtonManager& btnMgr) :
buttonManager(btnMgr), display(HAL::displayProxy()), audio(HAL::audioManager()) { instance = this; }
// begin() runs once when your app opens from the menu.
// Set things up here: hook up the buttons and start with the lights off.
void MyApp::begin() {
registerButtonCallbacks();
setColorsOff();
}
// update() runs about 50 times a second while your app is open.
// Clear the screen, draw what you want, then show it.
// The case hides about 8 pixels at each screen corner, so keep text and
// important pixels out of those corners.
void MyApp::update() {
display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setFont(ArialMT_Plain_16);
display.drawString(64, 24, "MyApp");
int score = 42; // Replace with your game's score.
bool ready = true;
display.clear();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 4, "My App");
display.drawRect(10, 24, 108, 20);
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
char text[24];
snprintf(text, sizeof(text), "Score: %d", score);
display.drawString(14, 28, text);
display.drawCircle(64, 52, 6);
if (ready) {
display.fillCircle(64, 52, 4);
}
display.display();
}
// end() runs when your app closes. Let go of the buttons and turn the
// lights off (if your app plays sounds, call audio.stopTone() here too).
void MyApp::end() {
unregisterButtonCallbacks();
setColorsOff();
}
// The bottom-left button closes your app and goes back to the menu.
void MyApp::onButtonBackPressed(const ButtonEvent& event) {
if (event.eventType == ButtonEvent_Released) {
instance->end();
MenuManager::instance().returnToMenu();
}
}
void MyApp::registerButtonCallbacks() {
buttonManager.registerCallback(button_BottomLeftIndex, onButtonBackPressed);
}
void MyApp::unregisterButtonCallbacks() {
buttonManager.unregisterCallback(button_BottomLeftIndex);
}
Functions on this page¶
| Call | What it does |
|---|---|
DisplayProxy& HAL::displayProxy()Example: HAL::displayProxy() |
The 128 x 64 screen, for code outside your app's functions. Inside begin(), update() and end(), the starter already calls it display. |
void display.clear()Example: display.clear(); |
Erase the drawing buffer before drawing a frame. |
void display.display()Example: display.display(); |
Show the drawing buffer on screen. |
void display.setColor(OLEDDISPLAY_COLOR color)Example: display.setColor(WHITE); |
Choose white, black, or inverted pixels for drawing. |
void display.setPixel(int16_t x, int16_t y)Example: display.setPixel(64, 32); |
Draw one pixel at x, y. |
void display.drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1)Example: display.drawLine(0, 0, 40, 20); |
Draw a line between two pixel positions. |
void display.drawRect(int16_t x, int16_t y, int16_t w, int16_t h)Example: display.drawRect(0, 0, 30, 20); |
Draw a rectangle outline; width and height are pixels. |
void display.fillRect(int16_t x, int16_t y, int16_t w, int16_t h)Example: display.fillRect(0, 0, 30, 20); |
Draw a solid rectangle; width and height are pixels. |
void display.drawCircle(int16_t x, int16_t y, int16_t r)Example: display.drawCircle(64, 32, 10); |
Draw a circle outline; radius is pixels. |
void display.fillCircle(int16_t x, int16_t y, int16_t r)Example: display.fillCircle(64, 32, 10); |
Draw a solid circle; radius is pixels. |
void display.drawHorizontalLine(int16_t x, int16_t y, int16_t len)Example: display.drawHorizontalLine(0, 0, 20); |
Draw a horizontal line of the given pixel length. |
void display.drawVerticalLine(int16_t x, int16_t y, int16_t len)Example: display.drawVerticalLine(0, 0, 20); |
Draw a vertical line of the given pixel length. |
void display.drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2)Example: display.drawTriangle(0, 0, 20, 0, 10, 20); |
Join three pixel positions with an outline. |
void display.fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2)Example: display.fillTriangle(0, 0, 20, 0, 10, 20); |
Fill the triangle between three pixel positions. |
void display.drawXbm(int16_t x, int16_t y, int16_t w, int16_t h, const unsigned char* data)Example: display.drawXbm(0, 0, 8, 8, bitmap); |
Draw a 1-bit bitmap: one on/off bit per pixel, lowest bit first in each byte. |
void display.drawProgressBar(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t progress)Example: display.drawProgressBar(0, 0, 60, 8, 50); |
Draw a progress bar; final argument is 0-100 %. |
void HAL::clearDisplay()Example: HAL::clearDisplay(); |
Erase the screen drawing buffer. |
void HAL::updateDisplay()Example: HAL::updateDisplay(); |
Show the drawing buffer on screen. |
uint16_t display.drawString(int16_t x, int16_t y, const String& text)Example: display.drawString(0, 0, "Hi"); |
Draw text using the current font and alignment. Studio apps return 0 on the device and in the browser emulator. |
uint16_t display.drawStringMaxWidth(int16_t x, int16_t y, uint16_t maxLineWidth, const String& text)Example: display.drawStringMaxWidth(0, 0, 80, "Hi"); |
Draw one unwrapped line in studio apps, on the device and in the browser emulator; the width argument is ignored. |
uint16_t display.getStringWidth(const String& text)uint16_t display.getStringWidth(const char* text, uint16_t length, bool = false)Example: display.getStringWidth("Hi"); |
Measure text width in pixels; also accepts text and length. |
void display.setFont(const uint8_t* fontData)Example: display.setFont(ArialMT_Plain_16); |
Select a 10, 16, or 24 pixel font handle. |
void display.setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT alignment)Example: display.setTextAlignment(TEXT_ALIGN_LEFT); |
Place text left, right, or centered on x; CENTER_BOTH also centers on y. |
void HAL::drawString(int16_t x, int16_t y, const String& text)Example: HAL::drawString(0, 0, "Hi"); |
Draw text at x, y with the current font and alignment. |
OLEDDISPLAY_COLOR BLACK = 0Example: BLACK |
Draw pixels off. |
OLEDDISPLAY_COLOR WHITE = 1Example: WHITE |
Draw pixels on. |
OLEDDISPLAY_COLOR INVERSE = 2Example: INVERSE |
Toggle drawn pixels on/off. |
const uint8_t ArialMT_Plain_10[]Example: ArialMT_Plain_10 |
10 pixel font; pass to setFont(). |
const uint8_t ArialMT_Plain_16[]Example: ArialMT_Plain_16 |
16 pixel font; pass to setFont(). |
const uint8_t ArialMT_Plain_24[]Example: ArialMT_Plain_24 |
24 pixel font; pass to setFont(). |
OLEDDISPLAY_TEXT_ALIGNMENT TEXT_ALIGN_LEFT = 0Example: TEXT_ALIGN_LEFT |
Text starts at x. |
OLEDDISPLAY_TEXT_ALIGNMENT TEXT_ALIGN_RIGHT = 1Example: TEXT_ALIGN_RIGHT |
Text ends at x. |
OLEDDISPLAY_TEXT_ALIGNMENT TEXT_ALIGN_CENTER = 2Example: TEXT_ALIGN_CENTER |
Text is centered on x. |
OLEDDISPLAY_TEXT_ALIGNMENT TEXT_ALIGN_CENTER_BOTH = 3Example: TEXT_ALIGN_CENTER_BOTH |
Text is centered on x and y. |
Framework detail: DisplayProxy wrapping SSD1306Wire¶
Advanced: how the firmware drives the display
You do not need these details to build apps in the studio. They describe the firmware internals for readers writing an app by hand and building it into the firmware.
Inside the firmware, the HAL creates a SSD1306Wire display driver and wraps it in a DisplayProxy. A driver is code that talks to a hardware part. This firmware-source excerpt is not for a studio app:
static SSD1306Wire s_realDisplay(0x3C, SDA, SCL);
static DisplayProxy s_displayProxy(s_realDisplay);
The firmware's DisplayProxy also manages overlays, drawings such as status icons over an app. Studio apps have a smaller set of functions: they do not control display contrast or orientation, and their overlay setters have no effect. Use the app function reference for the calls available to your app; methods from the driver's documentation are not necessarily available.