Files
basic1/UPLOAD_TOOL.md
Adolfo Reyna 84b009c33e Add serial upload tool for rapid Lua game iteration
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>
2026-02-12 22:52:57 -05:00

152 lines
3.6 KiB
Markdown

# Lua Game Upload Tool
Rapidly upload and execute Lua games on your RP2350 via USB serial for quick iteration during development!
## Features
- Upload Lua game files directly to the SD card via USB serial
- Automatically launches the uploaded game immediately
- No need to manually swap SD cards or restart the device
- Perfect for rapid game development and testing
## Requirements
### Computer Side
- Python 3.x
- pyserial library: `pip install pyserial`
### RP2350 Side
- Firmware must be built with serial uploader support (already included)
- SD card inserted and formatted (FAT32)
- USB connection to computer
## Usage
### 1. Connect Your Device
Connect your RP2350 to your computer via USB. The device will appear as a serial port:
- **Linux/Mac**: Usually `/dev/ttyACM0` or `/dev/ttyUSB0`
- **Windows**: Usually `COM3`, `COM4`, etc.
### 2. Upload a Lua Game
Basic usage:
```bash
python upload_game.py my_game.lua
```
Specify a custom serial port:
```bash
python upload_game.py my_game.lua /dev/ttyACM0 # Linux/Mac
python upload_game.py my_game.lua COM3 # Windows
```
The script will:
1. Read your Lua file
2. Upload it to the RP2350 via serial
3. Save it to `/games/<filename>.lua` on the SD card
4. Automatically launch the game!
### 3. View Available Serial Ports
Run the script without arguments to see available ports:
```bash
python upload_game.py
```
## Example Workflow
```bash
# Edit your game
vim snake.lua
# Upload and test (the game starts immediately!)
python upload_game.py snake.lua
# Make changes
vim snake.lua
# Upload again (instantly replaces and restarts)
python upload_game.py snake.lua
```
## Protocol Details
The tool uses a simple text-based protocol over USB serial:
```
UPLOAD <filename> <size_in_bytes>
<base64_encoded_file_content>
END
```
**Response:**
```
OK <bytes_written>
LAUNCHED <game_name>
```
## Troubleshooting
### "No serial port found"
- Make sure your device is connected via USB
- Check if the device appears in your system (use `ls /dev/tty*` on Linux/Mac)
- On Linux, you may need to add your user to the `dialout` group: `sudo usermod -a -G dialout $USER`
### "Permission denied"
On Linux/Mac, you may need permissions to access the serial port:
```bash
sudo chmod 666 /dev/ttyACM0 # Quick fix
# OR
sudo usermod -a -G dialout $USER && newgrp dialout # Permanent fix
```
### "Upload failed" or "ERROR" messages
- Make sure the SD card is properly inserted and formatted (FAT32)
- Check that there's enough space on the SD card
- Verify the Lua file is valid (syntax errors will appear when game launches)
### Game doesn't appear or launch
- Check the serial output from the RP2350 for error messages
- Ensure the Lua file has proper metadata comments at the top:
```lua
-- NAME: My Game
-- DESCRIPTION: A fun game
```
## Tips for Rapid Development
1. **Use a serial terminal** alongside uploads to see debug output:
```bash
screen /dev/ttyACM0 115200
```
2. **Create a watch script** to auto-upload on file changes:
```bash
while inotifywait -e modify my_game.lua; do
python upload_game.py my_game.lua
done
```
3. **Use printf() in Lua** for debugging:
```lua
function update(event)
print("Event type: " .. event.type)
-- your code here
end
```
## File Size Limits
- Maximum file size: 64 KB
- This should be plenty for most Lua games
- If you need more, consider splitting assets or code
## Related Files
- `lib/serial_uploader.h` - C++ header for serial upload handler
- `lib/serial_uploader.cpp` - C++ implementation
- `upload_game.py` - Python upload script
Happy game development! 🎮