Ciao. Sto cercando di fare un progetto che richiede l'implementazione di molti moduli. Per avere un codice più ordinato, ho deciso di dividere il tutto in librerie. Sembrerebbe andare tutto bene tranne per la libreria che dovrebbe gestire la comunicazione con il display Nextion. Quando compilo il tutto ottengo l'errore
NextionMngr.cpp: 15:36: error: invalid use of non-static member function
page0.attachPush(page0PushCallback)
Error compiling project sources
Di seguito riporto il codice delle mie librerie chiamate "NextionMngr":
NextionMngr.h
#ifndef NextionMngr_h
#define NextionMngr_h
#include <Arduino.h>
#include <Nextion.h>
class NextionMngr {
public:
byte page;
//Initialize the comunication with the display.
void begin(int baudRate);
//Send the specified command to the Nextion display.
void sendCommand(String cmd);
//Check if there are new Touch Events and if so call the appropriate functions.
//Necessary also to let the Arduino know the current page on the display.
void checkTouchEvents();
String getWrittenCode();
private:
NexPage page0 = NexPage(0, 0, "p0");
NexPage page1 = NexPage(1, 0, "p1");
NexPage page2 = NexPage(2, 0, "p2");
NexPage page3 = NexPage(3, 0, "p3");
NexPage page4 = NexPage(3, 0, "p4");
NexButton b1 = NexButton(0, 4, "b1"); //page, id, name
NexText pswt = NexText(2, 1, "pswt");
NexTouch *nex_listen_list[6] = {
&page0,
&page1,
&page2,
&page3,
&page4,
&b1
};
void page0PushCallback(void *ptr);
void page1PushCallback(void *ptr);
void page2PushCallback(void *ptr);
void page3PushCallback(void *ptr);
void page4PushCallback(void *ptr);
void b1PushCallback(void *ptr);
void b1PopCallback(void *ptr);
};
#endif
NextionMngr.cpp
#include "NextionMngr.h"
void NextionMngr::begin(int baudRate) {
page = 0;
Serial.begin(9600);
if (baudRate != 9600) {
delay(500);
sendCommand("baud=" + baudRate);
Serial.end();
Serial.begin(baudRate);
}
//Register the event callback of the functions
page0.attachPush(page0PushCallback);
page1.attachPush(page1PushCallback);
page2.attachPush(page2PushCallback);
page3.attachPush(page3PushCallback);
b1.attachPush(b1PushCallback);
b1.attachPop(b1PopCallback);
}
void NextionMngr::sendCommand(String cmd) {
Serial.print(cmd);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
void NextionMngr::checkTouchEvents() {
nexLoop(nex_listen_list);
}
String NextionMngr::getWrittenCode() {
char buffer[8];
pswt.getText(buffer, sizeof(buffer));
return buffer;
}
/////////////////////////////////////////////////
// Nextion display's objects functions //
/////////////////////////////////////////////////
void NextionMngr::page0PushCallback(void *ptr) {
page = 0;
}
void NextionMngr::page1PushCallback(void *ptr) {
page = 1;
}
void NextionMngr::page2PushCallback(void *ptr) {
page = 2;
}
void NextionMngr::page3PushCallback(void *ptr) {
page = 3;
}
void NextionMngr::page4PushCallback(void *ptr) {
page = 4;
}
void NextionMngr::b1PushCallback(void *ptr) {
digitalWrite(13, HIGH);
}
void NextionMngr::b1PopCallback(void *ptr) {
digitalWrite(13, LOW);
}