diff --git a/INextionWidget.h b/INextionWidget.h index 0ad709f..c7b8c40 100644 --- a/INextionWidget.h +++ b/INextionWidget.h @@ -26,14 +26,15 @@ class INextionWidget bool setStringProperty(char *propertyName, char *value); size_t getStringProperty(char *propertyName, char *value, size_t len); -protected: + + protected: bool sendCommand(char *commandStr, bool checkComplete = true); protected: Nextion &m_nextion; //!< Reference to the Nextion driver - uint8_t m_pageID; //!< ID of page this widget is on - uint8_t m_componentID; //!< Component ID of this widget const char *m_name; //!< Name of this widget + uint8_t m_componentID; //!< Component ID of this widget + uint8_t m_pageID; //!< ID of page this widget is on }; #endif diff --git a/Nextion.cpp b/Nextion.cpp index a5e3f29..d015e9a 100644 --- a/Nextion.cpp +++ b/Nextion.cpp @@ -23,6 +23,7 @@ Nextion::Nextion(Stream &stream, bool flushSerialBeforeTx) */ bool Nextion::init() { + WakeEvent=false; sendCommand(""); sendCommand("bkcmd=1"); @@ -43,7 +44,9 @@ void Nextion::poll() { char c = m_serialPort.read(); - if (c == NEX_RET_EVENT_TOUCH_HEAD) + //Serial.print("Ret:"); Serial.println((byte)c); + + if (c == NEX_RET_EVENT_TOUCH_HEAD ) { delay(10); @@ -56,7 +59,7 @@ void Nextion::poll() for (i = 1; i < 7; i++) buffer[i] = m_serialPort.read(); buffer[i] = 0x00; - + //Serial.println(String(buffer[0])+" "+String(buffer[1])+" "+String(buffer[2])+" "+String(buffer[3])+" "+String(buffer[4])+" "+String(buffer[5])+" "+String(buffer[6])); if (buffer[4] == 0xFF && buffer[5] == 0xFF && buffer[6] == 0xFF) { ITouchableListItem *item = m_touchableList; @@ -68,9 +71,43 @@ void Nextion::poll() } } } + + if(WakeEvent) + if(c == NEX_RET_EVENT_SLEEP_POSITION_HEAD) + { + delay(10); + ITouchableListItem *item = m_touchableList; + while (item != NULL) + { + // Serial.println(item->item->getComponentID()); + if(item->item->getComponentID()==WakeComponentID&&item->item->getPageID()==WakePageID) {item->item->processEvent(WakePageID, WakeComponentID, NEX_EVENT_POP); break;} + else item = item->next; + } + } + } } +/*! + * \brief Do not handle Wake event. + */ +void Nextion::DeActivateWakeEvent() +{ + WakeEvent=false; +} + +/*! + * \brief Set the callback to call when Wake event occours (the component needs a callback attached). + * \param page_id In which page the component is + * \param component_id The component ID + */ +void Nextion::ActivateWakeEvent(uint8_t page_id, uint8_t component_id) +{ + WakeEvent=true; + WakePageID=page_id; + WakeComponentID=component_id; +} + /*! * \brief Refreshes the entire page. * \return True if successful @@ -239,9 +276,10 @@ bool Nextion::drawStr(uint16_t x, uint16_t y, uint16_t w, uint16_t h, { size_t commandLen = 65 + strlen(str); char commandBuffer[commandLen]; - snprintf(commandBuffer, commandLen, "xstr %d,%d,%d,%d,%d,%ld,%ld,%d,%d,%d,%s", + snprintf(commandBuffer, commandLen, "xstr %d,%d,%d,%d,%d,%ld,%ld,%d,%d,%d,\"%s\"", x, y, w, h, fontID, fgColour, bgColour, xCentre, yCentre, bgType, str); + //Serial.println(commandBuffer); sendCommand(commandBuffer); return checkCommandComplete(); } diff --git a/Nextion.h b/Nextion.h index 3fde709..cd61795 100644 --- a/Nextion.h +++ b/Nextion.h @@ -67,12 +67,17 @@ class Nextion bool checkCommandComplete(); bool receiveNumber(uint32_t *number); size_t receiveString(char *buffer, size_t len); + + void ActivateWakeEvent(uint8_t page_id, uint8_t component_id); + void DeActivateWakeEvent(); private: Stream &m_serialPort; //!< Serial port device is attached to uint32_t m_timeout; //!< Serial communication timeout in ms bool m_flushSerialBeforeTx; //!< Flush serial port before transmission ITouchableListItem *m_touchableList; //!< LInked list of INextionTouchable + bool WakeEvent; + uint8_t WakePageID, WakeComponentID; }; #endif diff --git a/NextionTypes.h b/NextionTypes.h index d6f8462..507438a 100644 --- a/NextionTypes.h +++ b/NextionTypes.h @@ -3,6 +3,8 @@ #ifndef __NEONEXTION_NEXTIONTYPES #define __NEONEXTION_NEXTIONTYPES + + /*! * \enum NextionValue * \brief Values used in messages. diff --git a/README.md b/README.md index 9ae0744..e363ca8 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,19 @@ Avaliable through the library manager of the Arduio IDE as `NeoNextion`. ## New features -I don't use Nextion displays in my projects anymore so this library may not -allow all the features in the latest display firmware to be used. However -I still own a few of them and am happy to work on adding new functionality -if it is desired. +Added by Viktor1970: +1. Removed a check in sendMessage method, that prevents to control objects in other pages. Now the page's show() method it's working. -If you find a feature that is missing that you want implemented let me know -and I'll look at adding it for you. +2. Added 2 methods to Nextion.h/.cpp: + +`ActivateWakeEvent(uint8_t page_id, uint8_t component_id);` +This method permits to execute a callback handler attached to **component_id** in **page_id** when the display wakes up after sleep. + +`DeActivateWakeEvent();` +This method stops to execute callback described above (so wake-up after sleep send no event to code). ## Links -- Repository: https://github.com/DanNixon/NeoNextion +- Original Repository: https://github.com/DanNixon/NeoNextion - Travis CI: https://travis-ci.org/DanNixon/NeoNextion - Documentation: https://dannixon.github.io/NeoNextion