From a04edf24c141dffaab8530d098881442a17ef906 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Thu, 9 Jul 2020 13:18:12 -0400 Subject: [PATCH] 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. --- src/Arduboy2Core.cpp | 17 +++++++++++------ src/Arduboy2Core.h | 25 +++++++++++++++---------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Arduboy2Core.cpp b/src/Arduboy2Core.cpp index 7ccbaae..cef7f21 100644 --- a/src/Arduboy2Core.cpp +++ b/src/Arduboy2Core.cpp @@ -73,6 +73,10 @@ const PROGMEM uint8_t lcdBootProgram[] = { }; +//======================================== +//========== class Arduboy2Core ========== +//======================================== + Arduboy2Core::Arduboy2Core() { } void Arduboy2Core::boot() @@ -572,11 +576,12 @@ void Arduboy2Core::exitToBootloader() 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 UDCON = _BV(DETACH); @@ -598,11 +603,11 @@ void Arduboy2Core::mainNoUSB() bitClear(DOWN_BUTTON_DDR, DOWN_BUTTON_BIT); // 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 (bitRead(DOWN_BUTTON_PORTIN, DOWN_BUTTON_BIT) == 0) { - exitToBootloader(); + Arduboy2Core::exitToBootloader(); } // The remainder is a copy of the Arduino main() function with the diff --git a/src/Arduboy2Core.h b/src/Arduboy2Core.h index 13cc73d..d1d4763 100644 --- a/src/Arduboy2Core.h +++ b/src/Arduboy2Core.h @@ -248,9 +248,9 @@ /** \brief * Eliminate the USB stack to free up code space. * - * \note - * **WARNING:** Removing the USB code will make it impossible for sketch - * uploader programs to automatically force a reset into the bootloader! + * \warning + * Removing the USB code will make it impossible for sketch uploader + * programs to automatically force a reset into the bootloader! * 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. * 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)); \ int main() { \ - Arduboy2Core::mainNoUSB(); \ + Arduboy2NoUSB::mainNoUSB(); \ 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 * 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 * library, in order to extend the functionality, in most circumstances. */ -class Arduboy2Core +class Arduboy2Core : public Arduboy2NoUSB { friend class Arduboy2Ex; @@ -828,11 +838,6 @@ class Arduboy2Core */ 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: // internals static void setCPUSpeed8MHz();