Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 2.13/mono/spi/pet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/pixpaper-213-m-test-frdm-imx93-pet
/pixpaper-213-m-test-rpi5-pet
34 changes: 34 additions & 0 deletions 2.13/mono/spi/pet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Local build for the Open-EP Pet examples.
#
# Flags mirror ci/build-one.sh; CI (ci/discover-targets.py + GitHub Actions)
# remains the source of truth for the cross-arch board builds.
#
# This x86 dev host can natively build only the libgpiod v1 target (imx93).
# The rpi5 target needs libgpiod v2 headers/lib (build it on an rpi5 or in CI).
#
# make build every target
# make imx93 build the frdm-imx93 (libgpiod v1) target
# make rpi5 build the rpi5 (libgpiod v2) target
# make clean remove built binaries

CC ?= cc
CFLAGS ?= -std=gnu11 -Wall -Wextra \
-Wno-error=incompatible-pointer-types \
-Werror=implicit-function-declaration
LDLIBS ?= -lgpiod

COMMON := pet_app.h petsprite.h jpfont.h

IMX93 := pixpaper-213-m-test-frdm-imx93-pet
RPI5 := pixpaper-213-m-test-rpi5-pet

.PHONY: all imx93 rpi5 clean
all: $(IMX93) $(RPI5)
imx93: $(IMX93)
rpi5: $(RPI5)

%: %.c $(COMMON)
$(CC) $(CFLAGS) -I. $< -o $@ $(LDLIBS)

clean:
$(RM) $(IMX93) $(RPI5)
106 changes: 106 additions & 0 deletions 2.13/mono/spi/pet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Open-EP Pet

A Tamagotchi-style virtual pet running on the 2.13" mono PixPaper e-paper
panel over SPI, animated with partial
refresh so the puppy blinks, walks, eats and jumps without full-screen
flashing.

The UI shows the pet sprite, three stat bars (空腹 hunger / 元気 energy /
気分 mood) and three action buttons (エサ feed / あそぶ play / そうじ clean),
in either landscape or portrait orientation.

## Layout

| File | Description |
|---|---|
| `pet_app.h` | Board-independent core: SPI transport, EPD init/partial-refresh sequence, custom partial LUT, framebuffer drawing, Latin + Japanese fonts, pet state machine and main loop |
| `pixpaper-213-m-test-frdm-imx93-pet.c` | GPIO backend for FRDM-i.MX93 (libgpiod **v1**) |
| `pixpaper-213-m-test-rpi5-pet.c` | GPIO backend for Raspberry Pi 5 (libgpiod **v2**) |
| `petsprite.h` | Generated 1-bit sprite frames (do not edit) |
| `jpfont.h` | Generated 1-bit Japanese glyph bitmaps (do not edit) |
| `sprite_gen.py` | Regenerates `petsprite.h` from the `dog*.png` artwork |
| `jpfont_gen.py` | Regenerates `jpfont.h` from a system Noto CJK font |
| `dog*.png` | Source artwork for the 14 sprite frames |
| `pixpaper-pet.service` | systemd unit to run the pet as an auto demo at boot |
| `Makefile` | Local build for both targets |

Each per-board `.c` file only provides the GPIO backend
(`epd_dc_set` / `epd_rst_set` / `epd_busy_get` / `epd_gpio_init` /
`epd_gpio_release`) and then includes `pet_app.h`, which contains
everything else including `main()`.

## Hardware / wiring

SPI device: `/dev/spidev0.0` at 5 MHz, mode 0.

| Signal | FRDM-i.MX93 (`gpiochip0`) | Raspberry Pi 5 (`gpiochip15`) |
|---|---|---|
| DC | line 0 | line 5 |
| RST | line 5 | line 6 |
| BUSY | line 26 | line 26 |

To port to another board, copy one of the `.c` files and adapt the chip
name / line offsets (use the libgpiod v1 file for libgpiod < 2.0, the
rpi5 file for v2).

## Build

Requires libgpiod development headers (v1 for the imx93 target, v2 for
the rpi5 target).

```sh
make # build every target
make imx93 # FRDM-i.MX93 (libgpiod v1)
make rpi5 # Raspberry Pi 5 (libgpiod v2)
make clean
```

## Run

```sh
sudo ./pixpaper-213-m-test-rpi5-pet [auto|interactive] [landscape|portrait]
```

- **Orientation**: `landscape` (default) or `portrait`. If portrait comes
out upside-down on your unit, see the note at the top of `pet_app.h`.
- **Interactive mode** (default when stdin is a TTY) — keys:
- `f` feed (エサ): +hunger
- `p` play (あそぶ): +mood, -energy
- `c` clean (そうじ): +mood
- `q` quit
- **Auto mode** (`auto`, or automatic when stdin is not a TTY, e.g. under
systemd): cycles feed → play → clean on its own as a demo.

Left alone, the pet idles with random liveliness gestures (blinks,
looking around, a short walk cycle, a floating heart), falls asleep after
~60 frames without interaction, and its stats slowly decay; at zero
hunger it sulks until fed.

## Regenerating the assets

Both generated headers are committed, so this is only needed after
changing the artwork or UI strings. Requires Python 3 with Pillow and
NumPy.

```sh
python3 sprite_gen.py --emit # dog*.png -> petsprite.h (preview: /tmp/sprite_frames.png)
python3 jpfont_gen.py # Noto Sans CJK Bold -> jpfont.h
```

`sprite_gen.py` traces every frame at one fixed scale (anchored to the
sitting frame) and bottom-aligns them on a common floor, so crouch/jump
poses keep their real height differences. `jpfont_gen.py` embeds only
the glyphs actually used by the UI (edit its `JOBS` list when adding
strings).

## Display technique notes

- **Partial refresh** uses a custom LUT (`WF_PARTIAL` in `pet_app.h`);
animation frames are written to RAM 0x24 only, keeping the panel in a
`0x0C` "keep on" display mode between frames (~35 ms/frame pacing).
- **Ghosting control**: every 200 frames the panel is hardware-reset,
re-initialised and given a full refresh of the current frame as a new
base map.
- **Blink masking**: when switching between sprite frames with very
different silhouettes (sad/eat/walk), a one-frame blink pose is
inserted in between to hide partial-refresh residue.
Binary file added 2.13/mono/spi/pet/dog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_blink.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_crouch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_eat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_eat_closed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_happy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_jump.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_look.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_look_half.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_sad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_walk1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_walk2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_walk3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2.13/mono/spi/pet/dog_walk4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions 2.13/mono/spi/pet/jpfont.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* generated by jpfont_gen.py - do not edit by hand */
#ifndef JPFONT_H
#define JPFONT_H
#include <stdint.h>

struct jpglyph { uint32_t cp; uint8_t size, w, h; uint16_t off; };

static const struct jpglyph jpglyphs[] = {
{0x3042, 14, 14, 14, 0},
{0x3046, 14, 14, 14, 28},
{0x3058, 14, 14, 14, 56},
{0x305D, 14, 14, 14, 84},
{0x3076, 14, 14, 14, 112},
{0x30A8, 14, 14, 14, 140},
{0x30B5, 14, 14, 14, 168},
{0x304A, 16, 16, 16, 196},
{0x304B, 16, 16, 16, 228},
{0x306A, 16, 16, 16, 260},
{0x30C3, 16, 16, 16, 292},
{0x30C8, 16, 16, 16, 324},
{0x30DA, 16, 16, 16, 356},
{0x5143, 20, 20, 20, 388},
{0x5206, 20, 20, 20, 448},
{0x6C17, 20, 20, 20, 508},
{0x7A7A, 20, 20, 20, 568},
{0x8179, 20, 20, 20, 628},
};
#define JPGLYPH_COUNT (sizeof(jpglyphs)/sizeof(jpglyphs[0]))

static const uint8_t jpbits[] = {
0x00,0x00,0x06,0x00,0x3F,0xE0,0x3F,0xE0,0x04,0x40,0x07,0xE0,0x0F,0xF0,0x1C,0x98,
0x35,0x98,0x67,0x18,0x66,0x18,0x7E,0x70,0x30,0xE0,0x00,0x00,0x00,0x00,0x0F,0xC0,
0x07,0xC0,0x00,0x00,0x03,0x80,0x3F,0xE0,0x38,0x60,0x00,0x20,0x00,0x60,0x00,0x60,
0x00,0xE0,0x07,0xC0,0x0F,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x18,0xA0,0x18,0xB0,
0x18,0xC0,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x10,0x18,0x30,0x18,0x70,
0x1F,0xE0,0x07,0x80,0x0F,0xC0,0x1F,0xE0,0x01,0x80,0x03,0x00,0x0E,0x30,0x7F,0xF0,
0x7F,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x03,0xE0,0x01,0xC0,0x00,0x00,
0x00,0x00,0x06,0x00,0x07,0x88,0x01,0xA8,0x01,0x90,0x00,0x00,0x06,0x00,0x07,0x20,
0x03,0x30,0x19,0x90,0x71,0x98,0xE1,0x98,0x4F,0x80,0x07,0x00,0x00,0x00,0x3F,0xF0,
0x3F,0xF0,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x03,0x00,0x7F,0xF8,
0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x60,0x08,0x40,0x08,0x40,
0x7F,0xF8,0x7F,0xF8,0x08,0x40,0x08,0x40,0x18,0xC0,0x00,0xC0,0x01,0xC0,0x03,0x80,
0x07,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x06,0xD8,0x3F,0xDC,0x3F,0x0E,
0x06,0x02,0x06,0xE0,0x0F,0xF8,0x1E,0x1C,0x3E,0x0C,0x76,0x0C,0x76,0x1C,0x3E,0xF8,
0x1C,0xE0,0x00,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x06,0x18,0x3F,0xCC,0x7F,0xEC,
0x0C,0x66,0x0C,0x66,0x0C,0x67,0x18,0x60,0x18,0x60,0x38,0x60,0x30,0xC0,0x77,0xC0,
0x23,0x80,0x00,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x7F,0x98,0x7F,0x1E,0x0C,0x0E,
0x0C,0x60,0x18,0x60,0x18,0x60,0x38,0x60,0x33,0xF0,0x77,0xF8,0x06,0x7C,0x06,0x6C,
0x07,0xE0,0x01,0xC0,0x00,0x00,0x00,0x00,0x01,0x00,0x19,0x9C,0x19,0x9C,0x19,0x98,
0x0C,0x38,0x00,0x30,0x00,0x70,0x00,0xE0,0x03,0xC0,0x07,0x80,0x02,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0x00,
0x07,0x80,0x07,0xF0,0x07,0xFC,0x06,0x18,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0x00,
0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x16,0x07,0x16,0x0F,0x9C,
0x0F,0xC0,0x1C,0xE0,0x38,0x70,0x70,0x38,0x60,0x38,0x00,0x1C,0x00,0x0E,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x80,0x1F,0xFF,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFF,0xE0,0x7F,0xFF,0xE0,0x03,
0x1C,0x00,0x07,0x1C,0x00,0x07,0x1C,0x00,0x07,0x1C,0x00,0x0E,0x1C,0x60,0x0E,0x1C,
0x60,0x1C,0x1C,0x60,0x78,0x1F,0xE0,0x70,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
0x03,0x0C,0x00,0x07,0x0E,0x00,0x07,0x07,0x00,0x0E,0x07,0x00,0x1C,0x03,0x80,0x3C,
0x01,0xC0,0x78,0x01,0xE0,0x7F,0xFF,0xE0,0x6F,0xFF,0xC0,0x01,0xC3,0x00,0x01,0xC3,
0x00,0x01,0x83,0x00,0x03,0x83,0x00,0x03,0x83,0x00,0x07,0x03,0x00,0x0F,0x07,0x00,
0x1E,0x07,0x00,0x3C,0x3E,0x00,0x30,0x3E,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x0F,
0xFF,0xE0,0x1F,0xFF,0xE0,0x1C,0x00,0x00,0x3F,0xFF,0x80,0x77,0xFF,0x80,0x70,0x00,
0x00,0x3F,0xFF,0x00,0x1F,0xFF,0x00,0x00,0x63,0x00,0x0C,0x73,0x00,0x1E,0xE3,0x00,
0x0F,0xC3,0x00,0x03,0xC3,0x00,0x07,0xE3,0x20,0x0F,0xF3,0xA0,0x3E,0x7B,0xE0,0x3C,
0x11,0xE0,0x30,0x00,0xC0,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x60,0x00,0x7F,0xFF,
0xE0,0x7F,0xFF,0xE0,0x73,0x98,0xE0,0x73,0x98,0xE0,0x23,0x98,0x00,0x07,0x18,0xC0,
0x3E,0x1F,0xC0,0x3C,0x0F,0xC0,0x30,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0x80,0x1F,
0xFF,0x80,0x00,0x60,0x00,0x00,0x60,0x00,0x00,0x60,0x00,0x7F,0xFF,0xE0,0x7F,0xFF,
0xE0,0x00,0x00,0x00,0x3F,0x30,0x00,0x3F,0x7F,0xE0,0x37,0x7F,0xE0,0x37,0xE0,0x00,
0x37,0xFF,0xC0,0x3F,0xF0,0xC0,0x3F,0x70,0xC0,0x37,0x7F,0xC0,0x37,0x60,0xC0,0x37,
0x7F,0xC0,0x3F,0x3C,0x00,0x3F,0x3F,0xC0,0x37,0x7F,0xC0,0x77,0xF1,0xC0,0x67,0xFF,
0x80,0x67,0x1F,0x00,0x67,0x7F,0x80,0x6F,0xFF,0xE0,0x6E,0xE0,0xE0,0x00,0x00,0x00,
};

#endif /* JPFONT_H */
80 changes: 80 additions & 0 deletions 2.13/mono/spi/pet/jpfont_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""
Generate jpfont.h: a tiny 1-bit bitmap font holding ONLY the Japanese (and any
other) glyphs used by the Open-EP Pet UI, rasterised from a system CJK font.

Each glyph is rendered into a square size*size cell (fullwidth), thresholded to
pure black/white (no anti-alias, for crisp e-paper output). Re-run after
changing any UI string; the build embeds only what is listed in JOBS.

python3 jpfont_gen.py # writes jpfont.h next to this script
"""
import os
from PIL import Image, ImageFont, ImageDraw

FONT = "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc"
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "jpfont.h")

# (pixel size, text whose unique chars must be available at that size)
JOBS = [
(20, "空腹元気気分"), # stat labels
(16, "おなかペット"), # title
(14, "エサあそぶそうじ"), # buttons: Feed / Play / Clean
]

def render_glyph(ch, size):
"""Return (w, h, rows[]) 1-bit bitmap centred in a size*size cell."""
font = ImageFont.truetype(FONT, size)
img = Image.new("L", (size, size), 0)
d = ImageDraw.Draw(img)
x0, y0, x1, y1 = font.getbbox(ch)
gw, gh = x1 - x0, y1 - y0
pos = ((size - gw) // 2 - x0, (size - gh) // 2 - y0)
d.text(pos, ch, font=font, fill=255)
px = img.load()
stride = (size + 7) // 8
rows = []
for y in range(size):
row = bytearray(stride)
for x in range(size):
if px[x, y] >= 128:
row[x >> 3] |= 0x80 >> (x & 7)
rows.append(row)
return size, size, rows

def main():
glyphs = [] # (cp, size, w, h, rows)
seen = set()
for size, text in JOBS:
for ch in text:
key = (ord(ch), size)
if key in seen:
continue
seen.add(key)
w, h, rows = render_glyph(ch, size)
glyphs.append((ord(ch), size, w, h, rows))
glyphs.sort(key=lambda g: (g[1], g[0])) # by (size, codepoint)

blob, offs = bytearray(), []
for cp, size, w, h, rows in glyphs:
offs.append(len(blob))
for row in rows:
blob += row

with open(OUT, "w") as f:
f.write("/* generated by jpfont_gen.py - do not edit by hand */\n")
f.write("#ifndef JPFONT_H\n#define JPFONT_H\n#include <stdint.h>\n\n")
f.write("struct jpglyph { uint32_t cp; uint8_t size, w, h; uint16_t off; };\n\n")
f.write("static const struct jpglyph jpglyphs[] = {\n")
for (cp, size, w, h, _), off in zip(glyphs, offs):
f.write(f"\t{{0x{cp:04X}, {size}, {w}, {h}, {off}}},\n")
f.write("};\n#define JPGLYPH_COUNT (sizeof(jpglyphs)/sizeof(jpglyphs[0]))\n\n")
f.write("static const uint8_t jpbits[] = {\n")
for i in range(0, len(blob), 16):
f.write("\t" + ",".join(f"0x{b:02X}" for b in blob[i:i+16]) + ",\n")
f.write("};\n\n#endif /* JPFONT_H */\n")

print(f"{len(glyphs)} glyphs, {len(blob)} bytes -> {OUT}")

if __name__ == "__main__":
main()
Loading
Loading