[Logo] TCC discussion forum
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 


This forum is read only and new users cannot register, please ask all new questions either using GitHub discussions, or in Arduino forum tagging @davetcc.

Adding Analog Item at Runtime Problem RSS feed
Forum Index » tcMenu Arduinio library
Author Message
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
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.
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
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
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
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.
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.
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
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");
Frank79


Joined: Aug 14, 2021
Messages: 5
Offline
O.k., thank you very much !

Frank
 
Forum Index » tcMenu Arduinio library
Go to:   
Mobile view
Powered by JForum 2.7.0 © 2020 JForum Team • Maintained by Andowson Chang and Ulf Dittmer

This site uses cookies to analyse traffic, serve ads by Google AdSense (non-personalized in EEA/UK), and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.