-1

I'm trying to create the menu in a display linked to arduino by inserting the elements inside an array like the one below in pseudo-code (javascript).

var menu = {
    title: 'main menu',
    item: [{
            txt: 'item1',
            action: function() { // function that will be performed in case such an element is activated
                // my code
            }
        },
        {
            txt: 'item2',
            item: [{
                    txt: 'item4',
                    action: function() {
                        // my code
                    }
                },
                {
                    txt: 'item5',
                    action: function() {
                        // my code
                    }
                }
            ],
            action: function() {
                // my code
            }
        },
        {
            txt: 'item3',
            action: function() {
                // my code
            }
        }
    ]
};

Later this array will be read by a recursive function that will print the menu on the liquid crystal display.

How can i do this to arduino? Using javascript seems like an operation at anyone's reach, but can you do the same in C / C ++?

Thanks to everyone in advance for the help!

Pinguto
  • 416
  • 3
  • 17
  • 1
    i wrote this for myself, but it might help you get going with the "strange" "object-literal" system in arduino: http://pagedemos.com/json2arduino/ – dandavis May 15 '17 at 14:31

2 Answers2

4

Create a struct with an string, pointer to a function and pointers to the next and previous struct

the string is the text that will be displayed for the option, the function is the function called if the user click the item, and the pointers give you the previous and next itens if the user go up or down

example:

in the header file:

const struct item
{
    const char name[16];                
    void (*func)(void);                     // Pointer to the item function
    const struct item * prev;           // Pointer to the previous
    const struct item * next;           // Pointer to the next
};

in the c file:

const struct item item_ON = 
{
    " 1 On",
    fcn_item_turn_on,
    &item_OFF,
    &item_PARTIAL 
};

const struct item item_PARTIAL = 
{
    " 2 Partial",
    fcn_item_partial,
    &item_ON,
    &item_OFF 
};

const struct item item_OFF =
{
    " 3 Off",
    fcn_item_turn_off,
    &item_PARTIAL,
    &item_ON
}; 

then:

void menu_show() 
{
    putrsXLCD((rom char *)(*ptr_item).name); // or the LCD print function in your code
}

void menu_inc() {
    ptr_item = (*ptr_item).prev;
    menu_show();
}

void menu_dec() {
    ptr_item = (*ptr_item).next;
    menu_show();
}

void menu_fwd() {
    (*ptr_item).func(); // execute item function
}

don't forget to initialize the ptr_item with the first item:

ptr_item = &item_ON;
Gustavo Laureano
  • 556
  • 2
  • 10
1

From the looks of it you are trying to create a hierarchical menu system. (As the JSON Object is not an array, but more akin to a tree.)

C++ would probably be easier to implement in because of the STL, I'm not sure on your experience but I'll give a general layout. Design-wise anyways.

#include <functional>
#include <vector>

class MenuTreeNode {
    std::string title;
    std::vector<MenuTreeNode> children;
    std::function<void(int)> action;
public:
    MenuTreeNode(const std::string& title, std::function<void(int)> action = {});
    // ^ Construct the node, with the action item being optional.
    // {} is just an empty function block.

    /*
    ** You can construct with a lambda function, which are pretty useful.
    */

    void addChild(MenuTreeNode&& childNode); // append a node to the child array.
    void displayStuff() {

        // However you display stuff to Arduino...

        for (auto& child : this->children) {
            child.displayStuff();
        }
        this->action(); // Call the action.
    }
};

I hope that gives you some guidance. The older answer in C is good however doesn't allow for child items that you have in your JSON struct. This might be easier to work with IMO.

TinfoilPancakes
  • 238
  • 1
  • 13