Move mainNoUSB() to its own class

Suggested and developed by @Pharap

The mainNoUSB() function was never intended to be used directly in
sketches. To help prevent this, it has been made a private member of
a new class, Arduboy2NoUSB, which declares main() as a friend.

The ARDUBOY_NO_USB macro has been modified appropriately.

The Arduboy2Core class now inherits Arduboy2NoUSB, for sketches that
improperly have their own main() which calls the old version of
mainNoUSB() instead of using the ARDUBOY_NO_USB macro.
This commit is contained in:
Scott Allen 2020-07-09 13:18:12 -04:00
parent b668114475
commit a04edf24c1
2 changed files with 26 additions and 16 deletions

View File

@ -73,6 +73,10 @@ const PROGMEM uint8_t lcdBootProgram[] = {
}; };
//========================================
//========== class Arduboy2Core ==========
//========================================
Arduboy2Core::Arduboy2Core() { } Arduboy2Core::Arduboy2Core() { }
void Arduboy2Core::boot() void Arduboy2Core::boot()
@ -572,11 +576,12 @@ void Arduboy2Core::exitToBootloader()
while (true) { } while (true) { }
} }
// Replacement main() that eliminates the USB stack code.
// Used by the ARDUBOY_NO_USB macro. This should not be called
// directly from a sketch.
void Arduboy2Core::mainNoUSB() //=========================================
//========== class Arduboy2NoUSB ==========
//=========================================
void Arduboy2NoUSB::mainNoUSB()
{ {
// disable USB // disable USB
UDCON = _BV(DETACH); UDCON = _BV(DETACH);
@ -598,11 +603,11 @@ void Arduboy2Core::mainNoUSB()
bitClear(DOWN_BUTTON_DDR, DOWN_BUTTON_BIT); bitClear(DOWN_BUTTON_DDR, DOWN_BUTTON_BIT);
// Delay to give time for the pin to be pulled high if it was floating // Delay to give time for the pin to be pulled high if it was floating
delayShort(10); Arduboy2Core::delayShort(10);
// if the DOWN button is pressed // if the DOWN button is pressed
if (bitRead(DOWN_BUTTON_PORTIN, DOWN_BUTTON_BIT) == 0) { if (bitRead(DOWN_BUTTON_PORTIN, DOWN_BUTTON_BIT) == 0) {
exitToBootloader(); Arduboy2Core::exitToBootloader();
} }
// The remainder is a copy of the Arduino main() function with the // The remainder is a copy of the Arduino main() function with the

View File

@ -248,9 +248,9 @@
/** \brief /** \brief
* Eliminate the USB stack to free up code space. * Eliminate the USB stack to free up code space.
* *
* \note * \warning
* **WARNING:** Removing the USB code will make it impossible for sketch * Removing the USB code will make it impossible for sketch uploader
* uploader programs to automatically force a reset into the bootloader! * programs to automatically force a reset into the bootloader!
* This means that a user will manually have to invoke a reset in order to * This means that a user will manually have to invoke a reset in order to
* upload a new sketch, after one without USB has be been installed. * upload a new sketch, after one without USB has be been installed.
* Be aware that the timing for the point that a reset must be initiated can * Be aware that the timing for the point that a reset must be initiated can
@ -303,10 +303,20 @@
*/ */
#define ARDUBOY_NO_USB int main() __attribute__ ((OS_main)); \ #define ARDUBOY_NO_USB int main() __attribute__ ((OS_main)); \
int main() { \ int main() { \
Arduboy2Core::mainNoUSB(); \ Arduboy2NoUSB::mainNoUSB(); \
return 0; \ return 0; \
} }
// A replacement for the Arduino main() function that eliminates the USB code.
// Used by the ARDUBOY_NO_USB macro.
class Arduboy2NoUSB
{
friend int main();
private:
static void mainNoUSB();
};
/** \brief /** \brief
* Lower level functions generally dealing directly with the hardware. * Lower level functions generally dealing directly with the hardware.
@ -322,7 +332,7 @@ int main() { \
* that this may eliminate the need to create an entire local copy of the * that this may eliminate the need to create an entire local copy of the
* library, in order to extend the functionality, in most circumstances. * library, in order to extend the functionality, in most circumstances.
*/ */
class Arduboy2Core class Arduboy2Core : public Arduboy2NoUSB
{ {
friend class Arduboy2Ex; friend class Arduboy2Ex;
@ -828,11 +838,6 @@ class Arduboy2Core
*/ */
static void exitToBootloader(); static void exitToBootloader();
// Replacement main() that eliminates the USB stack code.
// Used by the ARDUBOY_NO_USB macro. This should not be called
// directly from a sketch.
static void mainNoUSB();
protected: protected:
// internals // internals
static void setCPUSpeed8MHz(); static void setCPUSpeed8MHz();