Hello @sheng, I have been thinking about implementing this in my code and you got me motivated… just a bit. I am not an expert, but I have had to dig into this library to work around some differences for the BluzDK. I have not tested my suggestion, so I hope it works.
I can think of three options: modify the library to handle the events; adapt the library by creating a fake button press; or handle them in your loop()
function. The last one is the easiest, but it corrupts the structure of the library by moving code into your main loop.
Modifying the library is beyond my present skills. Too many pointers to follow without a debugger, so I am going to punt this one… perhaps @ScruffR is interested.
Adapting the library would make it intercept the 68 and 86 events in the nexLoop
function in the file NexHardware.cpp
. You would add an else if
statement that changes the buffer to the cid and pid of the hidden buttons and pretend they actually occurred.
These events would execute the callback functions in your main code. The hidden buttons should not generate any data, but first check what codes they would send for a button press.
This is more work, but I feel it is worth a shot because it enhances the library as opposed to having lose code in your main file. Note that you have to add #define
for the 86 opcode… or just type it in.
For example, the sleep event would look like this:
void nexLoop(NexTouch *nex_listen_list[])
{
static uint8_t __buffer[20];
uint16_t i;
uint8_t c;
delay(100); //need delay or it will not see data on serial port
while (nexSerial.available())
{
c = nexSerial.read();
.
.
.
//new else if statement to be added after the last else if
else if (NEX_RET_EVENT_SLEEP_POSITION_HEAD == c)
{
if (nexSerial.available() >= 6)
{
__buffer[0] = NEX_RET_EVENT_TOUCH_HEAD ;
for (i = 1; i < 7; i++)
{
__buffer[i] = nexSerial.read();
}
__buffer[i] = 0x00;
//change values based on hidden button
__buffer[1] = 0x00; //insert code of hidden button
__buffer[2] = 0x01; //insert code of hidden button
__buffer[3] = 0x02; //insert code of hidden button
if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6])
{
NexTouch::iterate(nex_listen_list, __buffer[1], __buffer[2], (int32_t)__buffer[3], NULL);
}
}
}
Then at the top of your .ino file you declare two button objects, you add them to the *nex_listen_list[]
and you create the callback functions.
Finally, you “attach” the buttons to the callback functions in the setup()
function of your code.
This is no different than adding a new button press even to your code.
The less intense method would be to use Serial.peek();
In your loop()
function you check what’s in the serial port buffer. If the header code is 68 or 86, you first flush the serial buffer and then call a function in your .ino file. Otherwise, you let the Nextion library handle it.
You also have to remove the while (nexSerial.available())
line from the nexLoop
function and add it to your loop()
function, otherwise the event might happen right after “peeking” and the Nextion code will throw it away. This is the part I don’t like.
I am curious how you will use the sleep/wakeup events. Let me know.
EDIT: Forgot some curly brackets.