Ocky Kristanto / NextionSerial
Committer:
Ocky Kristanto
Date:
Wed Mar 03 09:01:07 2021 +0100
Revision:
0:87b7b2ae63c3
feat: initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ocky Kristanto 0:87b7b2ae63c3 1 #include "NextionSerial.h"
Ocky Kristanto 0:87b7b2ae63c3 2 #include "NextionUI.h"
Ocky Kristanto 0:87b7b2ae63c3 3
Ocky Kristanto 0:87b7b2ae63c3 4 #include <cstdio>
Ocky Kristanto 0:87b7b2ae63c3 5 #include <iterator>
Ocky Kristanto 0:87b7b2ae63c3 6 #include <string>
Ocky Kristanto 0:87b7b2ae63c3 7
Ocky Kristanto 0:87b7b2ae63c3 8 NextionSerial::NextionSerial(PinName aTxPin, PinName aRxPin, int aBaud) :
Ocky Kristanto 0:87b7b2ae63c3 9 iNextionSerial(aTxPin, aRxPin, aBaud)
Ocky Kristanto 0:87b7b2ae63c3 10 {
Ocky Kristanto 0:87b7b2ae63c3 11 iNextionSerial.set_blocking(true);
Ocky Kristanto 0:87b7b2ae63c3 12 serialThread.start([this]() { SerialLoop(); });
Ocky Kristanto 0:87b7b2ae63c3 13 }
Ocky Kristanto 0:87b7b2ae63c3 14
Ocky Kristanto 0:87b7b2ae63c3 15 void NextionSerial::AddNextionUI(NextionUI* aNextionUI)
Ocky Kristanto 0:87b7b2ae63c3 16 {
Ocky Kristanto 0:87b7b2ae63c3 17 iNextionUiElements.emplace_back(aNextionUI);
Ocky Kristanto 0:87b7b2ae63c3 18 }
Ocky Kristanto 0:87b7b2ae63c3 19
Ocky Kristanto 0:87b7b2ae63c3 20 void NextionSerial::SerialLoop() {
Ocky Kristanto 0:87b7b2ae63c3 21 while (true) {
Ocky Kristanto 0:87b7b2ae63c3 22 char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Ocky Kristanto 0:87b7b2ae63c3 23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Ocky Kristanto 0:87b7b2ae63c3 24 iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
Ocky Kristanto 0:87b7b2ae63c3 25
Ocky Kristanto 0:87b7b2ae63c3 26 // 0x65 is UI Input Event
Ocky Kristanto 0:87b7b2ae63c3 27 if (receivedBytes[0] == 0x65) {
Ocky Kristanto 0:87b7b2ae63c3 28 auto nextionUI = std::find_if(iNextionUiElements.begin(), iNextionUiElements.end(), [&receivedBytes](NextionUI* aNextionUI)
Ocky Kristanto 0:87b7b2ae63c3 29 {
Ocky Kristanto 0:87b7b2ae63c3 30 return (aNextionUI->GetPage() == static_cast<int>(receivedBytes[1]) &&
Ocky Kristanto 0:87b7b2ae63c3 31 (aNextionUI->GetId() == static_cast<int>(receivedBytes[2])));
Ocky Kristanto 0:87b7b2ae63c3 32 });
Ocky Kristanto 0:87b7b2ae63c3 33
Ocky Kristanto 0:87b7b2ae63c3 34 if (nextionUI != iNextionUiElements.end())
Ocky Kristanto 0:87b7b2ae63c3 35 {
Ocky Kristanto 0:87b7b2ae63c3 36 if (static_cast<int>(receivedBytes[3] == 1))
Ocky Kristanto 0:87b7b2ae63c3 37 {
Ocky Kristanto 0:87b7b2ae63c3 38 (*nextionUI)->PressReleaseTriggered(TEventType::EPress);
Ocky Kristanto 0:87b7b2ae63c3 39 }
Ocky Kristanto 0:87b7b2ae63c3 40 else if (static_cast<int>(receivedBytes[3] == 0))
Ocky Kristanto 0:87b7b2ae63c3 41 {
Ocky Kristanto 0:87b7b2ae63c3 42 (*nextionUI)->PressReleaseTriggered(TEventType::ERelease);
Ocky Kristanto 0:87b7b2ae63c3 43 }
Ocky Kristanto 0:87b7b2ae63c3 44 }
Ocky Kristanto 0:87b7b2ae63c3 45 }
Ocky Kristanto 0:87b7b2ae63c3 46 }
Ocky Kristanto 0:87b7b2ae63c3 47 }
Ocky Kristanto 0:87b7b2ae63c3 48
Ocky Kristanto 0:87b7b2ae63c3 49 bool NextionSerial::GetCurrentDualStateButton(const std::string& aObjectName)
Ocky Kristanto 0:87b7b2ae63c3 50 {
Ocky Kristanto 0:87b7b2ae63c3 51 std::string command = "get ";
Ocky Kristanto 0:87b7b2ae63c3 52 command.append(aObjectName);
Ocky Kristanto 0:87b7b2ae63c3 53 command.append(".val");
Ocky Kristanto 0:87b7b2ae63c3 54 command.append(3, 0xff);
Ocky Kristanto 0:87b7b2ae63c3 55 iNextionSerial.write(command.c_str(), command.size());
Ocky Kristanto 0:87b7b2ae63c3 56
Ocky Kristanto 0:87b7b2ae63c3 57 char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Ocky Kristanto 0:87b7b2ae63c3 58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Ocky Kristanto 0:87b7b2ae63c3 59 iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
Ocky Kristanto 0:87b7b2ae63c3 60
Ocky Kristanto 0:87b7b2ae63c3 61 if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x00 &&
Ocky Kristanto 0:87b7b2ae63c3 62 receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
Ocky Kristanto 0:87b7b2ae63c3 63 receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
Ocky Kristanto 0:87b7b2ae63c3 64 receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
Ocky Kristanto 0:87b7b2ae63c3 65 return false;
Ocky Kristanto 0:87b7b2ae63c3 66 } else if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x01 &&
Ocky Kristanto 0:87b7b2ae63c3 67 receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
Ocky Kristanto 0:87b7b2ae63c3 68 receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
Ocky Kristanto 0:87b7b2ae63c3 69 receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
Ocky Kristanto 0:87b7b2ae63c3 70 return true;
Ocky Kristanto 0:87b7b2ae63c3 71 } else {
Ocky Kristanto 0:87b7b2ae63c3 72 printf("Error!! Current state of DualStateButton is invalid @ %s : %d \n", __PRETTY_FUNCTION__, __LINE__ );
Ocky Kristanto 0:87b7b2ae63c3 73 return false;
Ocky Kristanto 0:87b7b2ae63c3 74 }
Ocky Kristanto 0:87b7b2ae63c3 75 }