Author |
Message |
14/08/2021 15:06:33
|
#1
|
Frank79
Joined: Aug 14, 2021
Messages: 5
Offline
|
Hi there,
i'm trying to add an analog Item at Runtime. My Menu Structure was created by the Designer; i use an ESP32.
I added in a Function in the Sketch:
const PROGMEM AnalogMenuInfo minfoTest = { "test", 90, 0xffff, 100, changespeed, 0, 1, "%" };
AnalogMenuItem menuTest(&minfoTest, 0, NULL);
menuMgr.addMenuAfter(&menuRecv2, &menuTest, false);
It should be the last Item in my Root Structure. When i call the Fuction, the Item does not appear. After that, i selected another Item and the Display freeze and than the ESP resets.
Is something missing here ?
Frank
|
|
|
14/08/2021 18:04:55
|
#2
|
davetcc
Joined: Jan 19, 2019
Messages: 686
Offline
|
Quick question. Is minfoTest defined globally. IE outside of any functions? And also the menu object.
If you don’t want to define them globally, you could use the new operator instead.
|
|
|
14/08/2021 20:50:23
|
#3
|
Frank79
Joined: Aug 14, 2021
Messages: 5
Offline
|
Hello and thanks for your answer,
minfoTest is defined local in a Function. But now i did a test and paste the three Lines in the setup; before and after setupMenu();
In both Cases i got two strange bars with a strange Text on the first one and the Display resets every 2-3 Seconds.
I also read a bit more in the Documentation and here https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menumanager-and-iteration/ i could read, that a creation at Runtime is (only ?) possible for an item that extends from RuntimeMenuItem.
https://www.thecoderscorner.com/ref-docs/tcmenu/html/class_menu_item.html
Is this up to date; no Creation of an AnalogItem at Runtime ?
What is the new operator ? If a Creation is not possible at Runtime, is there at least a possibilty to set/change the Text of the Item at Runtime ?
Frank
|
|
|
15/08/2021 05:07:06
|
#4
|
davetcc
Joined: Jan 19, 2019
Messages: 686
Offline
|
It is possible to create an Analog item at runtime. But you must locate it globally, or create it using new (needs careful evaluation to ensure heap usage is safe). In C and C++ local variables are undefined when control exits the block.
To create at runtime you remove the progmem definition from the info definition and ensure the extra AnalogMenuItem constructor parameter indicating not to use program memory is set. See https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menu-item-types/analog-menu-item/#creating-analog-items-manually
|
|
|
15/08/2021 10:59:51
|
#5
|
Frank79
Joined: Aug 14, 2021
Messages: 5
Offline
|
O.k.,
i added global :
AnalogMenuItem* menuTest;
and then i added to my Function:
AnalogMenuInfo minfoTest = { "test", 90, -1, 100, changespeed, 0, 1, "%" };
menuTest = new AnalogMenuItem (&minfoTest, 0, NULL, false);
menuMgr.addMenuAfter(&menuRecv2, menuTest, false);
I get an Item with many hieroglyphs on it and it only appears, when i enter a submenu and then go back ( i tried also menuMgr.notifyStructureChanged ()). Also i cannot change the item value with my encoder.
What am i doing wrong here ?
Frank
|
|
|
15/08/2021 12:19:45
|
#6
|
davetcc
Joined: Jan 19, 2019
Messages: 686
Offline
|
As per my previous comment the memory for ALL local variables go out of scope after the block finishes execution.
So your info block will be invalid as it is local. Define it in RAM - non const not progmem using the way described in the above link.
|
|
|
16/08/2021 07:56:34
|
#7
|
davetcc
Joined: Jan 19, 2019
Messages: 686
Offline
|
The best thing is to take a look at the dynamicMenuItems example as this shows many different ways of dealing with menu items and dialogs at runtime.
https://github.com/davetcc/tcMenuLib/blob/master/examples/dynamicMenuItems/dynamicMenuItems.ino
You can define the info structure globally, and change it before adding it.
|
|
|
16/08/2021 11:44:45
|
#8
|
Frank79
Joined: Aug 14, 2021
Messages: 5
Offline
|
O.k., thank you. A last question : For the AnalogMenuInfo minfoTest i can now set the integer values at Runtime, but i'm struggling with the minfoTest.name . How is the right format for setting this at Runtime ?
Frank
|
|
|
16/08/2021 15:00:11
|
#9
|
davetcc
Joined: Jan 19, 2019
Messages: 686
Offline
|
It's just an array of characters, so you'd use string copy normally:
strcpy(myInfo.name, "some value");
|
|
|
16/08/2021 15:19:43
|
#10
|
Frank79
Joined: Aug 14, 2021
Messages: 5
Offline
|
O.k., thank you very much !
Frank
|
|
|
|