Arduboy2 Library
repository·master·Indexed 43 days ago
https://github.com/mlxxxp/arduboy2A maintained fork of the original Arduboy library providing a standard API for controlling the display, buttons, and hardware of the Arduboy miniature game system. Includes tools like Cabi for RLE-encoded image compression and support for memory optimization via Arduboy2Base and SpritesB.
What's inside Arduboy2
- ArduBreakout is a brick-breaking game inspired by Atari's Breakout. Players control a paddle using the directional keys to bounce a ball against a wall of bricks. The game features high score persistence, where scores are saved to the Arduboy's EEPROM and remain available even after the device is restarted.
Choose between Sprites and SpritesB for memory optimization
masterThe library provides two sprite classes:
Sprites: Optimized for execution speed.SpritesB: Optimized for small code size.
If execution speed is not a critical factor for your sketch, using
SpritesBcan help reduce the overall memory footprint.Sprites sprites; // Optimized for speed SpritesB sprites; // Optimized for code sizeFree up code space using Arduboy2Base
masterTheArduboy2class includes text rendering support (fonts,setCursor,print, etc.), which consumes significant flash memory. If your sketch does not require text output, you can inherit from or use theArduboy2Baseclass instead to eliminate the font tables and text support code from your binary.Access EEPROM safely in a sketch
masterThe Arduboy2 library reserves the beginning of the EEPROM for system information (e.g., audio mute state, Unit Name, and Unit ID).
Warning: Do not use the reserved area for your own data. To ensure you are using safe memory, use the defined constant
EEPROM_STORAGE_SPACE_STARTas the starting address for your sketch's EEPROM operations.Configure boot logo and unit name via EEPROM
masterThe Arduboy2 library uses system EEPROM to manage boot-up behavior and personalization. You can use the included
SetSystemEEPROMexample sketch to manage the following:- Disable Boot Logo: Set a flag to prevent the logo sequence from playing on boot.
- Set Unit Name: Save a custom name that appears briefly at the bottom of the boot logo. Note: This requires using the
Arduboy2class (notArduboy2Base) because it relies on text display functions. - Toggle Unit Name Display: Set a flag to control whether the saved unit name is shown during the logo sequence, regardless of whether a name is set.
Prepare PNG images for Cabi conversion
masterTo ensure correct conversion, follow these requirements for your input PNG:
- Dimensions: The image height must be a multiple of 8 pixels (e.g., 8, 16, 24, 32...). Width can be any size.
- Color Logic: Cabi uses RGBA values to determine pixel state:
- Image Output (Drawn vs Non-drawn):
- Pixels with Alpha <= 127 are always non-drawn (0).
- Pixels with Alpha > 127 and Red > 127 are drawn (1).
- All other combinations result in non-drawn (0).
- Mask Output (Transparency):
- Only the Alpha channel is used.
- Alpha > 127 results in a mask value of 1.
- Alpha <= 127 results in a mask value of 0.
- Image Output (Drawn vs Non-drawn):
Recommendation: For best results, use fully white pixels for drawn areas, fully black for non-drawn areas, and fully transparent pixels for masked areas.
Build the Cabi program
masterCabi is a command-line tool written in C that converts PNG images into RLE-encoded C/C++ arrays for use with the Arduboy2
drawCompressed()function. Since pre-built binaries are not provided, you must compile it using an ANSI C99 compatible compiler likegccorclang.To build the program from the
cabidirectory, run:gcc cabi.c lodepng/lodepng.c -o cabi # or for Windows gcc cabi.c lodepng/lodepng.c -o CABI.EXEInstall the Arduboy2 library via PlatformIO
masterFor developers using the PlatformIO IDE, the library is available through the PlatformIO Library Manager. The library includes alibrary.jsonmanifest to facilitate discovery and installation.Play musical compositions with ArduboyPlaytune
masterTo play music in the background while your main sketch code runs in the foreground, you must install the
ArduboyPlayTunelibrary.- Install the library: Download it from https://github.com/Arduboy/ArduboyPlayTune.
- Define a score: Store your musical composition in a
PROGMEMbyte array. - Play the score: Call
playScore(score)within your sketch loop to begin playback.
// Define the composition in PROGMEM byte PROGMEM score[] = { /* ... musical data ... */ }; void loop() { // Start playing the score in the background playScore(score); // Your 'real' sketch code runs here in the foreground }Bypass the boot logo sequence
masterThe
begin()function triggers a boot logo sequence where the ARDUBOY logo scrolls down the screen and the RGB LED flashes. To skip this sequence for faster testing or gameplay:- Method 1: Hold the
RIGHTbutton while powering up the device, then release it. - Method 2: Press the
RIGHTbutton while the logo is currently scrolling down the screen.
- Method 1: Hold the
Use the Arduboy2 library in a sketch
masterTo use the Arduboy2 library, include the header file, instantiate an
Arduboy2object, and callbegin()within yoursetup()function. If you need sprite functionality, you must also instantiate aSpritesobject.To load built-in examples in the Arduino IDE, navigate to:
File > Examples > Arduboy2.#include <Arduboy2.h> Arduboy2 arduboy; Sprites sprites; void setup() { arduboy.begin(); // more setup code follows, if required }Optimize code space by using boot() instead of begin()
masterThe
begin()function includes several startup features (like the boot logo and system button checks). To save memory, you can callboot()instead, which only initializes the hardware.If you use
boot(), you must manually call other features if needed. Crucially, you should always include eitherflashlight()orsafeMode()to ensure you can still upload new sketches if the bootloader becomes inaccessible.void setup() { // Initializes hardware but skips extra boot features arduboy.boot(); // Manually add back necessary features arduboy.display(); arduboy.flashlight(); // Safeguard for uploading arduboy.audio.begin(); arduboy.waitNoButtons(); // Additional setup code... }