TL;DR:
I want to use the "new" functionality to create an instance of a class, but a lot of extra libraries are loaded so it won't fit my target. Is there a way to reduce the used memory?
Long story:
I have a question about memory usage on SAMD21 devices. I have two targets; a development board and a custom designed PCB with almost the same microcontroller:
Development board (SAMD21 Xplained Pro)
Microcontroller: ATSAMD21J18
Program memory: 256 KB
RAM: 32 KB
Custom designed PCB
Microcontroller: ATSAMD21G16
Program memory: 64 KB
RAM: 8 KB
Basically the microcontroller on the boards is the same, but the development board has 4 times as much program memory and RAM. The code I am using is working on the development board, but the custom designed PCB with the other microcontroller does not have enough memory...
The code works like this; I have two classes, ClassOne and ClassTwo, which are derived from CommonClass. Depending on whether a digital pin is high, I want either to load an instance of ClassOne or ClassTwo. So directly after some system initialization in my main() loop, I do this:
CommonClass *Object; if (port_pin_get_input_level(PIN_PA15) == true) { // If switch is pressed Object = new ClassOne(); } else { Object = new ClassTwo(); }
I never actually delete the object, since it is only created once and lives until power drops or when a reset is done.
Before using this approach, I just had two separate *.hex files that made either a ClassOne object or ClassTwo object globally. So it was impossible to change from One to Two dynamically. Choosing the right class dynamically on startup has the advantage of just having one *.hex file that can be uploaded to all my targets.
Using MapViewer, I was able to see the difference between the two approaches:
1. New approach with the two dynamic objects:
2. Old approach, creating an instance of just ClassOne globally:
Is there any hope to reduce the project's size?