This sample draws colour bars across the display, DISPLAY, and sends the
whole frame with pbl_display_update_sync(). Three seconds later it draws a
checkerboard over the lower third and sends only those rows with
pbl_display_update(), which runs in the background and calls back from the
display’s interrupt when done. The frame buffer is allocated to the size of
the display, in its pixel format: one byte per pixel in colour, one bit in
mono, where anything but black shows as white.
Watch the screen, on a watch or in the emulator’s window.
pbl samples samples/drivers/display
every board
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <pbl/drivers/display.h>
#include <pbl/kernel/delay.h>
#include <pbl/kernel/malloc.h>
#include <pbl/kernel/sem.h>
#define BARS 8
#define SQUARE 16
#define SHOW_MS 3000
/* ARGB2222, opaque: red, green and blue from 0 to 3. */
#define RGB(r, g, b) (0xC0U | ((r) << 4) | ((g) << 2) | (b))
static const uint8_t s_bar_colors[BARS] = {
RGB(3, 3, 3), RGB(3, 3, 0), RGB(0, 3, 3), RGB(0, 3, 0),
RGB(3, 0, 3), RGB(3, 0, 0), RGB(0, 0, 3), RGB(0, 0, 0),
};
static PBL_SEM_DEFINE(s_done, 0, 1);
static int s_status;
static void prv_set_pixel(const struct pbl_display_props *props,
const struct pbl_display_buffer *fb, uint16_t x, uint16_t y,
uint8_t color) {
uint8_t *row = &fb->data[y * fb->pitch];
if (props->format == PBL_DISPLAY_FORMAT_ARGB2222) {
row[x] = color;
} else if ((color & 0x3FU) != 0U) {
/* Mono: anything but black is white, the leftmost pixel in the lowest bit. */
row[x / 8U] |= (uint8_t)(1U << (x % 8U));
} else {
row[x / 8U] &= (uint8_t)~(1U << (x % 8U));
}
}
static void prv_draw_bars(const struct pbl_display_props *props,
const struct pbl_display_buffer *fb) {
for (uint16_t y = 0U; y < props->height; y++) {
for (uint16_t x = 0U; x < props->width; x++) {
prv_set_pixel(props, fb, x, y, s_bar_colors[x * BARS / props->width]);
}
}
}
static void prv_draw_checkerboard(const struct pbl_display_props *props,
const struct pbl_display_buffer *fb, uint16_t y0, uint16_t y1) {
for (uint16_t y = y0; y <= y1; y++) {
for (uint16_t x = 0U; x < props->width; x++) {
prv_set_pixel(props, fb, x, y,
((x / SQUARE + y / SQUARE) % 2U) != 0U ? RGB(3, 3, 3) : RGB(0, 0, 0));
}
}
}
/* Runs from the display's interrupt: only hand the result over. */
static void prv_update_done(int status, void *arg) {
s_status = status;
pbl_sem_give(&s_done);
}
int main(void) {
/* Size and pixel format: what a framebuffer for this display looks like. */
const struct pbl_display_props *props = pbl_display_get_props(DISPLAY);
bool mono = props->format == PBL_DISPLAY_FORMAT_MONO;
uint16_t y0 = props->height * 2U / 3U;
uint16_t y1 = props->height - 1U;
struct pbl_display_buffer fb;
int ret;
printf("display: %dx%d, %s\n", props->width, props->height, mono ? "mono" : "argb2222");
/* The buffer is the whole frame: updates send rows of it, by their position on the display. */
fb.pitch = mono ? (props->width + 7U) / 8U : props->width;
fb.data = pbl_zalloc(fb.pitch * props->height);
if (fb.data == nullptr) {
fprintf(stderr, "display: no memory for a frame\n");
return -ENOMEM;
}
prv_draw_bars(props, &fb);
ret = pbl_display_update_sync(DISPLAY, &fb, 0U, props->height - 1U);
if (ret != 0) {
fprintf(stderr, "display: update failed (%d)\n", ret);
goto out;
}
printf("display: bars shown\n");
pbl_sleep_ms(SHOW_MS);
/* Only the rows that changed, in the background: the callback says when they are out. */
prv_draw_checkerboard(props, &fb, y0, y1);
ret = pbl_display_update(DISPLAY, &fb, y0, y1, prv_update_done, nullptr);
if (ret == 0) {
(void)pbl_sem_take(&s_done, PBL_FOREVER);
ret = s_status;
}
if (ret != 0) {
fprintf(stderr, "display: update failed (%d)\n", ret);
goto out;
}
printf("display: checkerboard shown\n");
pbl_sleep_ms(SHOW_MS);
printf("Sample done\n");
out:
pbl_free(fb.data);
return ret;
}
display: \d+x\d+, (mono|argb2222)
display: bars shown
display: checkerboard shown
Sample done
Generated from PebbleOS 9e782207f. This page is maintained in the pebbleos repository: docs/samples/drivers/display.md.