Serial uploader searches for games by filename (e.g., "tetris") but
games are registered by their metadata NAME field (e.g., "Tetris").
This caused launch failures when case didn't match.
Changed select_game_by_name() to use case-insensitive matching:
- strcasecmp() for exact match
- tolower() both strings before strstr() for partial match
Now uploading "tetris.lua" will successfully match and launch "Tetris".
Co-Authored-By: Claude <noreply@anthropic.com>
Serial uploader was crashing the Pico when launching games because
it accessed SD card (SPI) while Core 1 was refreshing display (also SPI).
Display and SD card share the same SPI bus and cannot be accessed
simultaneously.
Split game launch into prepare and execute phases:
- prepare: Re-scan games directory (safe, SD access done immediately)
- execute: Load Lua script from SD (deferred until display is idle)
Main loop now checks !is_refresh_in_progress() before completing
launch, preventing SPI conflicts.
Also updated SD card best practices skill to document SPI bus
contention as the #1 most critical issue to avoid.
Co-Authored-By: Claude <noreply@anthropic.com>
Implements a complete serial upload workflow that allows uploading and
immediately testing Lua games via USB serial connection.
New Components:
- SerialUploader: Receives files via serial, writes to SD card
- upload_game.py: Python tool for sending files from host computer
- Protocol: Text-based with base64 encoding for reliability
Key Features:
- Uploads file to /games folder on SD card
- Overwrites existing files (FA_CREATE_ALWAYS)
- Auto-launches uploaded game immediately
- Proper memory cleanup (prevents Lua state conflicts)
SD Card Fixes:
- Fixed SPI speed management (12.5MHz for SD, 32MHz for display)
- Fixed SD write protocol (poll for data response token)
- Added speed switching wrappers around all FatFS operations
- Cleaned up excessive debug output
Game Launcher Improvements:
- Added clear_games() to prevent duplicate registrations
- Added cleanup in select_game_by_name() to delete old instances
- Added exact match priority in game selection
- LuaGameLoader now has clear_factory_data() for memory cleanup
Integration:
- Added serial_uploader to CMakeLists.txt
- Integrated into main loop in basic1.cpp
- Re-scans games after upload to pick up new files
Documentation:
- UPLOAD_TOOL.md: Usage instructions
- sd_card_best_practices.md: Critical lessons learned
Known Issues:
- Game launch after upload occasionally causes freeze (needs investigation)
- Display may not refresh properly after upload
Usage:
python upload_game.py games/lua_examples/2048.lua /dev/tty.usbmodem101
Co-Authored-By: Claude <noreply@anthropic.com>
Updated games:
- pong.lua: All text now uses text_scaled with scale=2
- air_hockey.lua: All text now uses text_scaled with scale=2
- asteroids.lua: All text now uses text_scaled with scale=2
- ball.lua: All text now uses text_scaled with scale=2
- breakout.lua: All text now uses text_scaled with scale=2
- counter.lua: All text now uses text_scaled with scale=2
- flappy_bird.lua: All text now uses text_scaled with scale=2
- lunar_lander.lua: All text now uses text_scaled with scale=2
- snake.lua: All text now uses text_scaled with scale=2
- tetris.lua: All text now uses text_scaled with scale=2
All 14 games now have consistent 2x text scaling for better readability.
lua_bindings.cpp:
- Added new lua_renderer_text_scaled() function
- Wraps renderer->draw_string_scaled() with proper scale support
- Registered as renderer.text_scaled(x, y, text, on, scale)
- Scale parameter defaults to 1 if omitted
Updated all 4 games to use text_scaled():
- simon_says.lua: All text now uses text_scaled with scale=2
- tic_tac_toe.lua: All text now uses text_scaled with scale=2
- memory_match.lua: All text now uses text_scaled with scale=2
- 2048.lua: All text now uses text_scaled with scale=2
This properly uses the C++ renderer's scaled text rendering instead of
workarounds in Lua, providing better performance and consistency.
All 4 games (simon_says, tic_tac_toe, memory_match, 2048):
- Changed sizing to use min(width, height) instead of just width
- Grids now scale proportionally on both axes
- All UI text now uses text_scale=2 for better visibility
- Games fill more of the screen properly on portrait mode
Fixes:
- Simon Says buttons now square grid on any orientation
- Tic-Tac-Toe grid centered vertically using full screen height
- Memory Match cards use smallest dimension for square sizing
- 2048 tiles scale and center on both axes
- All text (scores, menus, game over) now 2x larger
snake.lua:
- Changed wall collision to wrap around (toroidal grid)
- Snake now travels through edges instead of crashing
- Doubled move_speed from 10 to 20 frames (half speed)
- Updated speed increase cap from 3 to 5 frames (slower at max)
- Snake can now traverse indefinitely without losing to walls
2048.lua:
- Removed hardcoded TILE_SIZE and TILE_SPACING constants
- Added get_tile_size() function that calculates tile dimensions based on screen width
- Added get_grid_start_x/y() to center grid
- Updated draw() to use dynamic sizing for all tiles
- 4x4 grid now scales proportionally on any screen size
memory_match.lua:
- Removed hardcoded CARD_SIZE and CARD_SPACING
- Added get_card_size() function that calculates card dimensions based on available screen space
- Grid now fills screen width (minus padding) and uses available height
- Added get_grid_start_x/y() to center grid
- All card positions and hit detection updated to use dynamic sizing
- Cards scale proportionally on any screen size while maintaining 4x4 layout
simon_says.lua:
- Removed hardcoded button positions
- Added get_buttons() function that calculates button size and position based on screen dimensions
- Buttons now use 10% screen padding and scale to fill available width
- 2x2 grid automatically scales with screen size
tic_tac_toe.lua:
- Replaced hardcoded CELL_SIZE with dynamic calculation
- Added get_cell_size() to compute size based on screen width
- Added get_grid_start_x/y() to center grid both horizontally and vertically
- Grid now scales to fill available space while maintaining 3x3 layout
- All cell positions updated to use dynamic functions
Both games now work on any screen size and scale proportionally.
All games had the same issue: renderer.circle() requires integer arguments,
but float calculations produced non-integer coordinates.
Fixed in all games using math.floor(x + 0.5) for proper rounding:
- pong.lua: Ball position
- air_hockey.lua: Puck position
- asteroids.lua: Asteroid positions
- ball.lua: Ball and trail positions, velocity line
- breakout.lua: Ball position
- flappy_bird.lua: Bird Y position
- counter.lua: Last touch marker position
- snake.lua: Food position (center calculation)
- tic_tac_toe.lua: O circle center position
Also fixed floating-point coordinate calculations in ball and line
drawing to ensure all coordinates are integers.
- Display < PREV and NEXT > buttons at bottom of screen when multiple pages exist
- Buttons are touchable and respond to taps
- Button dimensions: 150x40 at y=235
- PREV button: x=30, NEXT button: x=200
- Updated instructions to show 'Touch buttons or KEY0/KEY1'
- Both KEY0/KEY1 and touch button presses navigate pages
- Updated lib/ and emulator/ versions
- GameLauncher now displays only 4 games per page to keep menu in bounds
- Added page navigation with page indicator (Page X/Y)
- KEY0 navigates between pages and within page
- KEY1 selects the highlighted game
- Touch selection works on current page only
- Helper methods: get_total_pages(), get_page_start_index(), get_page_end_index()
- Updated both lib/ and emulator/ versions for consistency
Add intelligent partial screen update system using bitwise XOR change detection
and 4-quadrant tracking (top-left, top-right, bottom-left, bottom-right). Each
changed pixel is routed to its quadrant, with sophisticated merge logic that
combines adjacent rectangles when beneficial (<40% overhead). This dramatically
reduces SPI bandwidth for UIs with scattered updates (e.g., corners, sidebars).
Key changes:
- 4-quadrant dirty rectangle tracking with automatic merging
- XOR-based change detection for fast byte-level comparison
- Expose st7796_set_window() for partial region updates
- 30 FPS frame rate limiter (33ms per frame) to prevent excessive refreshes
- Smart sleep timing when frame rate limit is active
Performance: Up to 99% reduction in SPI traffic for corner-based UIs
(e.g., 4 small regions vs full 480x320 screen updates).
Co-Authored-By: Claude <noreply@anthropic.com>
- Added PWM brightness control to ST7796 driver (0-100%)
- Implemented hardware sleep mode for ST7796 (saves ~150mA)
- Added sleep/wake functions preserving framebuffer and settings
- Implemented timer-based inactivity detection (checks every 10s)
- Auto-sleep after 5 minutes of no user input
- Touch controller remains active during TFT sleep for instant wake
- E-ink display also sleeps after timeout, requires re-init on wake
- Added hardware_pwm library dependency to CMakeLists.txt
- Brightness and sleep/wake methods exposed through display abstraction layer
- Added INPUT_FRAME_TICK event type to input_event.h
- Added wants_frame_updates() virtual method to Game base class
- Implemented frame tick logic in main loop (basic1.cpp and emulator/main.cpp)
- Added Lua bindings: game.set_frame_updates(bool) and INPUT.FRAME_TICK
- Updated LuaGame to support frame updates via registry flag
- Updated ball.lua to use continuous frame updates for smooth animation
- Both hardware and emulator now support continuous animation for physics/games
Store game names and descriptions in persistent LuaGameFactoryData
structure instead of local stack variables to prevent dangling pointers.
Same fix as emulator version.
- Created emulator-specific lua_game_emulator.cpp using filesystem instead of FatFS
- Created lua_game_loader_emulator.cpp to scan games/lua_examples directory
- Updated CMakeLists.txt to include Lua 5.4 engine and bindings
- Updated to SFML 3.0 API compatibility (event handling, sprite initialization)
- Updated Game class to use public members for Lua bindings
- Updated GameLauncher to use std::function for lambda captures
- Added continuous 60 FPS rendering for smooth display
- Emulator now loads and runs all three example Lua games
- Integrated Lua 5.4 engine (32-bit mode for embedded ARM)
- Created LuaGame wrapper class implementing Game interface
- Added C++ bindings exposing renderer, game state, and input to Lua
- Implemented SD card loader for automatic .lua game discovery
- Updated GameLauncher to support std::function for lambda captures
- Made Game class members public for Lua bindings access
- Added example Lua games: counter, snake, bouncing ball
- Included comprehensive API documentation
Games can now be written as .lua text files on SD card and loaded
without recompilation. Build size: 747KB UF2, Lua VM uses ~50-80KB RAM.