C++

Page principale

Répondre à ce message
Auteur: Frédéric
Date:  
À: Guilde
Sujet: C++
Hello,

Je développe une petite appli Arduino qui utilise cette librairie :

https://github.com/jonblack/arduino-menusystem

J'essaye d'implémenter cet exemple :

https://github.com/jonblack/arduino-menusystem/blob/master/examples/led_matrix/led_matrix.ino

de manière plus objet. Je voudrais que les callbacks on_xxx_selected()
deviennent les membres d'une classe où, en gros, je rapatrie tout ce qui
est après la ligne 88.

Mais ça ne marche pas, j'obtiens l'erreur :

--------------

/tmp/arduino_build_965784/sketch/myMenu.cpp: In constructor 'MyMenu::MyMenu(MyRenderer*)':
myMenu.cpp:12:39: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&MyMenu::on_time_selected' [-fpermissive]
     m_mi_time = new MenuItem("TIME", &on_time_selected);
                                       ^
myMenu.cpp:12:55: error: no matching function for call to 'MenuItem::MenuItem(const char [5], void (MyMenu::*)(MenuComponent*))'
     m_mi_time = new MenuItem("TIME", &on_time_selected);
                                                       ^
In file included from /tmp/arduino_build_965784/sketch/myMenu.h:4:0,
                 from /tmp/arduino_build_965784/sketch/myMenu.cpp:4:
/home/fma/Arduino/libraries/arduino-menusystem/MenuSystem.h:195:5: note: candidate: MenuItem::MenuItem(const char*, MenuComponent::SelectFnPtr)
     MenuItem(const char* name, SelectFnPtr select_fn);
     ^
/home/fma/Arduino/libraries/arduino-menusystem/MenuSystem.h:195:5: note:   no known conversion for argument 2 from 'void (MyMenu::*)(MenuComponent*)' to 'MenuComponent::SelectFnPtr {aka void (*)(MenuComponent*)}'


--------------

Du coup, j'ai essayé de déclarer ces callback static, mais là, j'obtiens :

--------------

myMenu.cpp:65:69: error: cannot declare member function 'static void MyMenu::on_time_selected(MenuComponent*)' to have static linkage [-fpermissive]
static void MyMenu::on_time_selected(MenuComponent* p_menu_component)

--------------

Une idée comment corriger ça ? J'ai joint la classe en question...

Merci d'avance pour vos lumières.

-- 
    Frédéric

/*
*/

#include "myMenu.h"


MyMenu::MyMenu(MyRenderer* myRenderer)
{
    m_myRenderer = myRenderer;


    m_ms = new MenuSystem((MenuComponentRenderer const&)m_myRenderer);
    m_mi_time = new MenuItem("TIME", &on_time_selected);
    m_mi_date = new MenuItem("DATE", &on_date_selected);
    m_mu_disp = new Menu("DISP*");
    m_mi_brightness = new MenuItem("BRTNS", &on_brightness_selected);
    m_mi_color = new MenuItem("COLOR", &on_color_selected);
}


void MyMenu::begin()
{
//     ledMatrix.clear();
//     ledMatrix.setfont(FONT_5x7);


    m_ms->get_root_menu().add_item(m_mi_time);
    m_ms->get_root_menu().add_item(m_mi_date);
    m_ms->get_root_menu().add_menu(m_mu_disp);
    m_mu_disp->add_item(m_mi_brightness);
    m_mu_disp->add_item(m_mi_color);
    m_ms->display();
}


void MyMenu::loop()
{
    char inChar;


    if ((inChar = Serial.read()) > 0) {
        switch (inChar) {


            case 'w':  // Previus item
                m_ms->prev();
                m_ms->display();
                break;


            case 's':  // Next item
                m_ms->next();
                m_ms->display();
                break;


            case 'a':  // Back presed
                m_ms->back();
                m_ms->display();
                break;


            case 'd':  // Select presed
                m_ms->select();
                m_ms->display();
                break;


            default:
                break;
        }
    }
}


static void MyMenu::on_time_selected(MenuComponent* p_menu_component)
{
    Serial.println("Time selected");
}


static void MyMenu::on_date_selected(MenuComponent* p_menu_component)
{
    Serial.println("Date selected");
}


static void MyMenu::on_brightness_selected(MenuComponent* p_menu_component)
{
    Serial.println("Brightness selected");
}


static void MyMenu::on_color_selected(MenuComponent* p_menu_component)
{
    Serial.println("Color selected");
}

#ifndef MYMENU_H
#define MYMENU_H

#include <MenuSystem.h>
// #include <i2cNavKey.h>

// // navKey pin
// #define I2CNAVKEY_INT_PIN 22;

class MyRenderer;


class MyMenu {
    public:
        MyMenu(MyRenderer* myRenderer);


        void begin();
        void loop();


        void render(Menu const& menu) const;


        void render_menu_item(MenuItem const& menu_item);
        void render_back_menu_item(BackMenuItem const& menu_item);
        void render_numeric_menu_item(NumericMenuItem const& menu_item);
        void render_menu(Menu const& menu) const;


    private:
        MyRenderer* m_myRenderer;


//         i2cNavKey* m_navKey;


        // Menu variables
        MenuSystem* m_ms;
        MenuItem* m_mi_time;
        MenuItem* m_mi_date;
        Menu* m_mu_disp;
        MenuItem* m_mi_brightness;
        MenuItem* m_mi_color;


        // Menu callback functions
        static void on_time_selected(MenuComponent* p_menu_component);
        static void on_date_selected(MenuComponent* p_menu_component);
        static void on_brightness_selected(MenuComponent* p_menu_component);
        static void on_color_selected(MenuComponent* p_menu_component);


        // Functions
        void serial_handler();
};


#endif