Introduction: What Are We Building?
Have you ever wanted to add lively, interactive 2D animations to your embedded projects, but weren't sure how to handle multiple moving objects, transparent sprites, and character walk-cycles without bogging down your microcontroller?
In this comprehensive tutorial, we will build a complete, synchronized outdoor animated scene from scratch on an STM32F7 microcontroller running LVGL (Light and Versatile Graphics Library) version 9.5 and FreeRTOS.
By the end of this guide, you will be able to:
- Design a clean UI layout using LVGL Pro (declarative XML).
- Handle transparent PNG images properly on embedded displays without ugly black background boxes.
- Master the LVGL 9.5 C Animation API (lv_anim_t) to create continuous forward and reverse movement.
- Synchronize multiple independent moving objects along a unified 12-second timeline.
- Build a direction-aware animated character (a walking dog) that steps naturally and turns around seamlessly.
The Animation Scene & Goals
Before writing any code, let's look at the visual breakdown of what we want to achieve on our 800x480 display:
Our scene consists of a beautiful outdoor landscape with three independently animated elements:
- Drifting Cloud (img_cloud)
- What it does: Glides horizontally across the sky from left (x = 0) to right (x = 670 px) over 6 seconds, then glides back from right to left over 6 seconds.
- Cycle: 12 seconds (12000 ms) in a continuous, smooth loop.
- 2. Hot Air Balloon (img_balloon)
- What it does: Performs a 2D "box-like" flight trajectory with altitude changes:
- 0s to 5s: Glides from Right to Left (x: 736 to 0 px) at normal height (y = 150 px).
- 5s to 6s: Holds at the left edge and descends 40 px (y: 150 to 190 px).
- 6s to 11s: Glides from Left to Right (x: 0 to 736 px) at lower height (y = 190 px).
- 11s to 12s: Holds at the right edge and ascends 40 px (y: 190 to 150 px) back to normal altitude.
- Cycle: 12 seconds (12000 ms) in a continuous loop.
- Walking Dog Sprite (img_dog)
- What it does: A character that walks across the grass:
- 0s to 6s (Walking Right): Moves from x = 0 to x = 632 px. As it moves, it alternates between two right-facing leg frames (dog_walk1 and dog_walk2) every 20 px so the dog appears to be walking.
- 6s to 12s (Walking Left): Reaches the right edge, turns around to face left, moves from x = 632 back to x = 0 px, and alternates between two left-facing leg frames (dog_walk1_r and dog_walk2_r) every 20 px.
- Cycle: 12 seconds (12000 ms) in a continuous loop.
Hardware & Software Requirements
- Microcontroller Board: STM32F769I-DISCO (ARM Cortex-M7 @ 216 MHz, with hardware Chrom-ART DMA2D graphics accelerator).
- Display: 800x480 capacitive touch screen (DSI interface).
- Graphics Library: LVGL v9.5.
- UI Design Tool: LVGL Pro (XML-based editor).
- Operating System: FreeRTOS.
- Toolchain / IDE: STM32CubeIDE / GCC / CMake with Ninja.
- background.png (800x480 background illustration).
- cloud.png (Transparent cloud image).
- balloon.png (Transparent hot air balloon).
- dog1_walk1.png (Right-facing dog, Step Frame 1).
- dog1_walk2.png (Right-facing dog, Step Frame 2).
- dog1_walk1_r.png (Left-facing dog, Step Frame 1 - horizontally mirrored).
- dog1_walk2_r.png (Left-facing dog, Step Frame 2 - horizontally mirrored).
- RGB565 (16-bit color): 5 bits Red, 6 bits Green, 5 bits Blue. It has no Alpha channel. If you convert a transparent PNG to RGB565, all transparent areas become solid black (0x0000), leaving an ugly black box around your sprite!
- ARGB8888 (32-bit color): 8 bits Alpha, 8 bits Red, 8 bits Green, 8 bits Blue. It preserves smooth, per-pixel transparency and anti-aliased edges.
Avoiding the Variable Shadowing Trap
- Direction Detection: Knowing whether the character is moving left or right so we display the correct mirrored sprite.
- Stepping Stride: Swapping leg frames without a separate timer so the feet never slide across the ground.
- 0 to 19 px: (x / 20) is 0 (Even) -> Show Step Frame 1 (r1 / l1).
- 20 to 39 px: (x / 20) is 1 (Odd) -> Show Step Frame 2 (r2 / l2).
- 40 to 59 px: (x / 20) is 2 (Even) -> Show Step Frame 1 (r1 / l1).
Testing & Verification on Hardware
- The Cloud seamlessly drifting back and forth across the mountain peaks.
- The Balloon flying from right to left, dipping down by 40 px at the left edge, flying back across the sky, and ascending back to its starting height on every cycle.
- The Dog walking across the foreground with natural stepping legs, smoothly turning around at the right edge, and walking back in reverse.
- All three elements operate in phase-locked harmony on an infinite 12-second cycle with 0 memory leaks and fluid performance!

No comments:
Post a Comment