Make all possible functions and variables static

This is mainly to reduce code size.

The write() function in class Arduboy2 has to remain virtual,
so functions that result in it eventually being called could
not be made static.

The members of the Point and Rect structures could not be made static
due to their intended use as having multiple instances.

Also to reduce code size, bootLogoExtra() was made non-virtual.
This meant that the begin() and bootLogo...() functions in the
Arduboy2Base class had to have duplicate or equivalent functions
added to the Arduboy2 class.

Related documentation was updated and some minor changes were made to
non-related documentation.
This commit is contained in:
Scott Allen 2020-09-24 17:31:27 -04:00
parent e78e7c9d55
commit 4698dd276e
7 changed files with 1552 additions and 1307 deletions

133
README.md
View File

@ -4,7 +4,7 @@ The Arduboy2 library is maintained in a git repository hosted on [GitHub](https:
https://github.com/MLXXXp/Arduboy2
The **Arduboy2** library is a fork of the [Arduboy library](https://github.com/Arduboy/Arduboy), which provides a standard *application programming interface* (API) to the display, buttons and other hardware of the Arduino based [Arduboy miniature game system](https://www.arduboy.com/).
The **Arduboy2** library is a fork of the [Arduboy library](https://github.com/Arduboy/Arduboy), which provides a standard *application programming interface* (API) to the display, buttons and other hardware of the Arduino based [Arduboy miniature game system](https://www.arduboy.com/). The original *Arduboy* library is no longer being maintained.
The name *Arduboy2* doesn't indicate that it's for a new "next generation" of the Arduboy hardware. The name was changed so it can coexist in the Arduino IDE with the current *Arduboy* library, without conflict. This way, existing sketches can continue to use the *Arduboy* library and class, without changes, while new sketches can be written (or old ones modified) to use and take advantage of the capabilities of the *Arduboy2* class and library.
@ -47,9 +47,10 @@ A user settable *unit name* can be saved in system EEPROM memory. If set, this n
Once the logo display sequence completes, the sketch continues.
For developers who wish to quickly begin testing, or impatient users who want to go strait to playing their game, the boot logo sequence can be bypassed by holding the *RIGHT* button while powering up, and then releasing it. Alternatively, the *RIGHT* button can be pressed while the logo is scrolling down.
**Note:**
For users who wish to always disable the displaying of the boot logo sequence on boot up, a flag in system EEPROM is available for this. The included *SetSystemEEPROM* example sketch can be used to set this flag.
- For developers who wish to quickly begin testing, or impatient users who want to go strait to playing their game, the boot logo sequence can be bypassed by holding the *RIGHT* button while powering up, and then releasing it. Alternatively, the *RIGHT* button can be pressed while the logo is scrolling down.
- For users who wish to always disable the displaying of the boot logo sequence on boot up, a flag in system EEPROM is available for this. The included *SetSystemEEPROM* example sketch can be used to set this flag.
### "Flashlight" mode
@ -165,63 +166,99 @@ with
Arduboy2Base arduboy;
```
#### Remove boot up features
#### Substitute or remove boot up features
As previously described, the *begin()* function includes features that are intended to be available to all sketches during boot up. However, if you're looking to gain some code space, you can call *boot()* instead of *begin()*. This will initialize the system but not include any of the extra boot up features. If desired, you can then add back in any of these features by calling the functions that perform them. You will have to trade off between the desirability of having a feature and how much memory you can recover by not including it.
As previously described in the _Start up features_ section, the *begin()* function includes features that are intended to be available to all sketches during boot up. However, if you're looking to gain some code space, you can call *boot()* instead of *begin()*. This will initialize the system but not include any of the extra boot up features. You can then add back in any of these features by calling the functions that perform them. You will have to trade off between the desirability of having a feature and how much memory you can recover by not including it.
A good way to use *boot()* instead of *begin()* is to copy the code from the body of the *begin()* function, in file *Arduboy2.cpp*, into your sketch and then edit it to retain the *boot()* call and any feature calls desired.
You should at least call either *flashlight()* or *safeMode()* as a safeguard to allow uploading a new sketch when the bootloader "magic key" problem is an issue.
As of this writing, the begin function is:
Here is a template that provides the equivalent of *begin()*
```cpp
void Arduboy2Base::begin()
void setup()
{
boot(); // raw hardware
// Required to initialize the hardware.
arduboy.boot();
display(); // blank the display (sBuffer is global, so cleared automatically)
// This clears the display. (The screen buffer will be all zeros)
// It may not be needed if something clears the display later on but
// "garbage" will be displayed if systemButtons() is used without it.
arduboy.display();
flashlight(); // light the RGB LED and screen if UP button is being held.
// flashlight() or safeMode() should always be included to provide
// a method of recovering from the bootloader "magic key" problem.
arduboy.flashlight();
// arduboy.safeMode();
// check for and handle buttons held during start up for system control
systemButtons();
// This allows sound to be turned on or muted. If the sketch provides
// its own way of toggling sound, or doesn't produce any sound, this
// function may not be required.
arduboy.systemButtons();
audio.begin();
// This is required to initialize the speaker. It's not needed if
// the sketch doesn't produce any sounds.
arduboy.audio.begin();
bootLogo();
// This displays the boot logo sequence but note that the logo can
// be suppressed by the user, by pressing the RIGHT button or using
// a system EEPROM setting. If not removed entirely, an alternative
// bootLogo...() function may save some memory.
arduboy.bootLogo();
// arduboy.bootLogoCompressed();
// arduboy.bootLogoSpritesSelfMasked();
// arduboy.bootLogoSpritesOverwrite();
// arduboy.bootLogoSpritesBSelfMasked();
// arduboy.bootLogoSpritesBOverwrite();
// arduboy.bootLogoText();
// Wait for all buttons to be released, in case a pressed one might
// cause problems by being acted upon when the actual sketch code
// starts. If neither systemButtons() nor bootLogo() is kept, this
// function isn't required.
arduboy.waitNoButtons();
// Additional setup code...
waitNoButtons(); // wait for all buttons to be released
}
```
To incorporate it into your sketch just keep *boot()* and whatever feature calls are desired, if any. Comment out or delete the rest. Remember to add the class object name in front of each function call, since they're now being called from outside the class itself. If your sketch uses sound, it's a good idea to keep the call to *audio.begin()*.
For example: Let's say a sketch has its own code to enable, disable and save the *audio on/off* setting, and wants to keep the *flashlight* function. In *setup()* it could replace *begin()* with:
```cpp
arduboy.boot(); // raw hardware
// *** This particular sketch clears the display soon, so it doesn't need this:
// display(); // blank the display (sBuffer is global, so cleared automatically)
arduboy.flashlight(); // light the RGB LED and screen if UP button is being held.
// check for and handle buttons held during start up for system control
// systemButtons();
arduboy.audio.begin();
// bootLogo();
// waitNoButtons(); // wait for all buttons to be released
```
This saves whatever code *display()*, *systemButtons()*, *bootLogo()* and *waitNoButtons()* would use.
There are a few functions provided that are roughly equivalent to the standard functions used by *begin()* but which use less code space.
- *bootLogoCompressed()*, *bootLogoSpritesSelfMasked()*, *bootLogoSpritesOverwrite()*, *bootLogoSpritesBSelfMasked()* and *bootLogoSpritesBOverwrite()* will do the same as *bootLogo()* but will use *drawCompressed()*, or *Sprites* / *SpritesB* class *drawSelfMasked()* or *drawOverwrite()* functions respectively, instead of *drawBitmask()*, to render the logo. If the sketch uses one of these functions, then using the boot logo function that also uses it may reduce code size. It's best to try each of them to see which one produces the smallest size.
- *bootLogoCompressed()*, *bootLogoSpritesSelfMasked()*, *bootLogoSpritesOverwrite()*, *bootLogoSpritesBSelfMasked()* and *bootLogoSpritesBOverwrite()* will do the same as *bootLogo()* but will use *drawCompressed()*, or *Sprites* / *SpritesB* class *drawSelfMasked()* or *drawOverwrite()* functions respectively, instead of *drawBitmap()*, to render the logo. If the sketch uses one of these functions, then using the boot logo function that also uses it may reduce code size. It's best to try each of them to see which one produces the smallest size.
- *bootLogoText()* can be used in place *bootLogo()* in the case where the sketch uses text functions. It renders the logo as text instead of as a bitmap (so doesn't look as good).
- *safeMode()* can be used in place of *flashlight()* for cases where it's needed to allow uploading a new sketch when the bootloader "magic key" problem is an issue. It only lights the red RGB LED, so you don't get the bright light that is the primary purpose of *flashlight()*.
- *safeMode()* can be used in place of *flashlight()* as a safeguard to allow uploading a new sketch when the bootloader "magic key" problem is an issue. It only lights the red RGB LED, so you don't get the bright light that is the primary purpose of *flashlight()*.
It is also possible to replace the boot logo drawing function with one that uses a different bitmap rendering function used elsewhere in your sketch. This may save memory by using this bitmap function for the logo, instead of the logo using a separate function that only ends up being used once. For example, if you use the *ArdBitmap* library's *drawCompressed()* function in your sketch, you could convert the **ARDUBOY** logo to *Ardbitmap* compressed format, and create *drawLogoArdCompressed()* and *bootLogoArdCompressed()* functions:
```cpp
void drawLogoArdCompressed(int16_t y)
{
ardbitmap.drawCompressed(20, y, arduboy_logo_ardbitmap,
WHITE, ALIGN_CENTER, MIRROR_NONE);
}
void bootLogoArdCompressed()
{
if (arduboy.bootLogoShell(drawLogoArdCompressed))
{
arduboy.bootLogoExtra();
}
}
void setup()
{
arduboy.beginDoFirst();
bootLogoArdCompressed();
arduboy.waitNoButtons();
// Additional setup code...
}
```
The **ARDUBOY** logo, in PNG format, is included in the library repository as file:
`extras/assets/arduboy_logo.png`
#### Use the SpritesB class instead of Sprites
@ -246,6 +283,8 @@ The *ARDUBOY_NO_USB* macro is used to eliminate the USB code. The *exitToBootloa
## What's different from Arduboy library V1.1
(These notes apply to when the *Arduboy2* library was first released. There will have been many additional changes, enhancements and features added to *Arduboy2* since then.)
A main goal of Arduboy2 is to provide ways in which more code space can be freed for use by large sketches. Another goal is to allow methods other than the *tunes* functions to be used to produce sounds. Arduboy2 remains substantially compatible with [Arduboy library V1.1](https://github.com/Arduboy/Arduboy/releases/tag/v1.1), which was the latest stable release at the time of the fork. Arduboy2 is based on the code targeted for Arduboy library V1.2, which was still in development and unreleased at the time it was forked.
Main differences between Arduboy2 and Arduboy V1.1 are:
@ -424,13 +463,13 @@ The benefit of using *ArduboyTones* would be reduced code size and possibly easi
### Sketch uses the beginNoLogo() function instead of begin()
The *beginNoLogo()* function has been removed. Instead, *boot()* can be used with additional functions following it to add back in desired boot functionality. See the information above, under the heading *Remove boot up features*, for more details. Assuming the object is named *arduboy*, a direct replacement for *beginNoLogo()* would be:
The *beginNoLogo()* function has been removed. *beginNoLogo()* can be replaced with *begin()*, since users can choose to suppress the logo sequence using the *RIGHT* button or by setting a flag in system EEPROM.
If using *begin()* results in the sketch program memory size being too large, *beginDoFirst()* or *boot()* can be used with additional functions following it to add back in desired boot functionality. See the information above, under the heading *Substitute or remove boot up features*, for more details. Assuming the object is named *arduboy*, an equivalent replacement for *beginNoLogo()* would be:
```cpp
arduboy.boot();
arduboy.display();
arduboy.flashlight();
arduboy.audio.begin();
arduboy.beginDoFirst();
arduboy.waitNoButtons();
```
----------

View File

@ -26,6 +26,7 @@ blank KEYWORD2
boot KEYWORD2
bootLogo KEYWORD2
bootLogoCompressed KEYWORD2
bootLogoExtra KEYWORD2
bootLogoShell KEYWORD2
bootLogoSpritesBOverwrite KEYWORD2
bootLogoSpritesBSelfMasked KEYWORD2

View File

@ -12,20 +12,40 @@
uint8_t Arduboy2Base::sBuffer[];
Arduboy2Base::Arduboy2Base()
{
currentButtonState = 0;
previousButtonState = 0;
// frame management
setFrameDuration(16);
frameCount = 0;
justRendered = false;
}
uint16_t Arduboy2Base::frameCount = 0;
uint8_t Arduboy2Base::eachFrameMillis = 16;
uint8_t Arduboy2Base::thisFrameStart;
uint8_t Arduboy2Base::lastFrameDurationMs;
bool Arduboy2Base::justRendered = false;
uint8_t Arduboy2Base::currentButtonState = 0;
uint8_t Arduboy2Base::previousButtonState = 0;
// functions called here should be public so users can create their
// own init functions if they need different behavior than `begin`
// provides by default
// provides by default.
//
// This code and it's documentation should be kept in sync with
// Aruduboy2::begin()
void Arduboy2Base::begin()
{
beginDoFirst();
bootLogo();
// alternative logo functions. Work the same as bootLogo() but may reduce
// memory size if the sketch uses the same bitmap drawing function or
// `Sprites`/`SpritesB` class
// bootLogoCompressed();
// bootLogoSpritesSelfMasked();
// bootLogoSpritesOverwrite();
// bootLogoSpritesBSelfMasked();
// bootLogoSpritesBOverwrite();
waitNoButtons(); // wait for all buttons to be released
}
void Arduboy2Base::beginDoFirst()
{
boot(); // raw hardware
@ -37,17 +57,6 @@ void Arduboy2Base::begin()
systemButtons();
audio.begin();
bootLogo();
// alternative logo functions. Work the same as bootLogo() but may reduce
// memory size if the sketch uses the same bitmap drawing function
// bootLogoCompressed();
// bootLogoSpritesSelfMasked();
// bootLogoSpritesOverwrite();
// bootLogoSpritesBSelfMasked();
// bootLogoSpritesBOverwrite();
waitNoButtons(); // wait for all buttons to be released
}
void Arduboy2Base::flashlight()
@ -159,12 +168,12 @@ void Arduboy2Base::drawLogoSpritesBOverwrite(int16_t y)
// bootLogoText() should be kept in sync with bootLogoShell()
// if changes are made to one, equivalent changes should be made to the other
void Arduboy2Base::bootLogoShell(void (*drawLogo)(int16_t))
bool Arduboy2Base::bootLogoShell(void (&drawLogo)(int16_t))
{
bool showLEDs = readShowBootLogoLEDsFlag();
if (!readShowBootLogoFlag()) {
return;
return false;
}
if (showLEDs) {
@ -174,7 +183,7 @@ void Arduboy2Base::bootLogoShell(void (*drawLogo)(int16_t))
for (int16_t y = -15; y <= 24; y++) {
if (pressed(RIGHT_BUTTON)) {
digitalWriteRGB(RGB_OFF, RGB_OFF, RGB_OFF); // all LEDs off
return;
return false;
}
if (showLEDs && y == 4) {
@ -197,14 +206,12 @@ void Arduboy2Base::bootLogoShell(void (*drawLogo)(int16_t))
delayShort(400);
digitalWriteRGB(BLUE_LED, RGB_OFF);
bootLogoExtra();
return true;
}
// Virtual function overridden by derived class
void Arduboy2Base::bootLogoExtra() { }
// wait for all buttons to be released
void Arduboy2Base::waitNoButtons() {
void Arduboy2Base::waitNoButtons()
{
do {
delayShort(50); // simple button debounce
} while (buttonsState());
@ -1146,15 +1153,83 @@ void Arduboy2Base::swapInt16(int16_t& a, int16_t& b)
//========== class Arduboy2 ==========
//====================================
Arduboy2::Arduboy2()
int16_t Arduboy2::cursor_x = 0;
int16_t Arduboy2::cursor_y = 0;
uint8_t Arduboy2::textColor = WHITE;
uint8_t Arduboy2::textBackground = BLACK;
uint8_t Arduboy2::textSize = 1;
bool Arduboy2::textWrap = false;
bool Arduboy2::textRaw = false;
// functions called here should be public so users can create their
// own init functions if they need different behavior than `begin`
// provides by default.
//
// This code and it's documentation should be kept in sync with
// Aruduboy2Base::begin()
void Arduboy2::begin()
{
cursor_x = 0;
cursor_y = 0;
textColor = 1;
textBackground = 0;
textSize = 1;
textWrap = 0;
textRaw = 0;
beginDoFirst();
bootLogo();
// alternative logo functions. Work the same as bootLogo() but may reduce
// memory size if the sketch uses the same bitmap drawing function or
// `Sprites`/`SpritesB` class
// bootLogoCompressed();
// bootLogoSpritesSelfMasked();
// bootLogoSpritesOverwrite();
// bootLogoSpritesBSelfMasked();
// bootLogoSpritesBOverwrite();
waitNoButtons();
}
void Arduboy2::bootLogo()
{
if (bootLogoShell(drawLogoBitmap))
{
bootLogoExtra();
}
}
void Arduboy2::bootLogoCompressed()
{
if (bootLogoShell(drawLogoCompressed))
{
bootLogoExtra();
}
}
void Arduboy2::bootLogoSpritesSelfMasked()
{
if (bootLogoShell(drawLogoSpritesSelfMasked))
{
bootLogoExtra();
}
}
void Arduboy2::bootLogoSpritesOverwrite()
{
if (bootLogoShell(drawLogoSpritesOverwrite))
{
bootLogoExtra();
}
}
void Arduboy2::bootLogoSpritesBSelfMasked()
{
if (bootLogoShell(drawLogoSpritesBSelfMasked))
{
bootLogoExtra();
}
}
void Arduboy2::bootLogoSpritesBOverwrite()
{
if (bootLogoShell(drawLogoSpritesBOverwrite))
{
bootLogoExtra();
}
}
// bootLogoText() should be kept in sync with bootLogoShell()
@ -1171,7 +1246,7 @@ void Arduboy2::bootLogoText()
digitalWriteRGB(RED_LED, RGB_ON);
}
for (int16_t y = -16; y <= 24; y++) {
for (int16_t y = -15; y <= 24; y++) {
if (pressed(RIGHT_BUTTON)) {
digitalWriteRGB(RGB_OFF, RGB_OFF, RGB_OFF); // all LEDs off
return;

File diff suppressed because it is too large Load Diff

View File

@ -80,11 +80,8 @@ class Arduboy2Audio
*
* \details
* The speaker is initialized based on the current mute setting saved in
* system EEPROM. This function is called by `Arduboy2Base::begin()` so it
* isn't normally required to call it within a sketch. However, if
* `Arduboy2Core::boot()` is used instead of `Arduboy2Base::begin()` and the
* sketch includes sound, then this function should be called after `boot()`.
*/
* system EEPROM.
*/
static void begin();
/** \brief

View File

@ -13,8 +13,6 @@
//========== class Arduboy2Core ==========
//========================================
Arduboy2Core::Arduboy2Core() { }
// Commands sent to the OLED display to initialize it
const PROGMEM uint8_t Arduboy2Core::lcdBootProgram[] = {
// boot defaults are commented out but left here in case they

View File

@ -337,7 +337,6 @@ class Arduboy2Core : public Arduboy2NoUSB
friend class Arduboy2Ex;
public:
Arduboy2Core();
/** \brief
* Idle the CPU to save power.
@ -797,14 +796,21 @@ class Arduboy2Core : public Arduboy2NoUSB
* \details
* This function initializes the display, buttons, etc.
*
* This function is called by begin() so isn't normally called within a
* This function is called by `begin()` so isn't normally called within a
* sketch. However, in order to free up some code space, by eliminating
* some of the start up features, it can be called in place of begin().
* The functions that begin() would call after boot() can then be called
* to add back in some of the start up features, if desired.
* See the README file or documentation on the main page for more details.
* The functions that `begin()` would call after `boot()` can then be
* called to add back in some of the start up features as space permits.
*
* \see Arduboy2Base::begin()
* See the README file or main page, in section
* _Substitute or remove boot up features_, for more details.
*
* \warning
* If this function is used, it is recommended that at least `flashlight()`
* or `safeMode()` be called after it to provide a means to upload a new
* sketch if the bootloader "magic number" problem is encountered.
*
* \see Arduboy2::begin() Arduboy2Base::flashlight() safeMode()
*/
static void boot();
@ -818,8 +824,8 @@ class Arduboy2Core : public Arduboy2NoUSB
* sketch, for sketches that interfere with the bootloader "magic number".
* The problem occurs with certain sketches that use large amounts of RAM.
*
* This function should be called after `boot()` in sketches that
* potentially could cause the problem.
* This function should be called after `boot()` in sketches that don't
* call `flashlight()`.
*
* It is intended to replace the `flashlight()` function when more
* program space is required. If possible, it is more desirable to use