Ocky Kristanto / NextionSerial
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NextionSerial.cpp Source File

NextionSerial.cpp

00001 #include "NextionSerial.h"
00002 #include "NextionUI.h"
00003 
00004 #include <cstdio>
00005 #include <iterator>
00006 #include <string>
00007 
00008 NextionSerial::NextionSerial(PinName aTxPin, PinName aRxPin, int aBaud) :
00009   iNextionSerial(aTxPin, aRxPin, aBaud)
00010 {
00011   iNextionSerial.set_blocking(true);
00012   serialThread.start([this]() { SerialLoop(); });
00013 }
00014 
00015 void NextionSerial::AddNextionUI(NextionUI* aNextionUI)
00016 {
00017     iNextionUiElements.emplace_back(aNextionUI);
00018 }
00019 
00020 void NextionSerial::SerialLoop() {
00021   while (true) {
00022     char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00023                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00024     iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
00025 
00026     // 0x65 is UI Input Event
00027     if (receivedBytes[0] == 0x65) {
00028       auto nextionUI = std::find_if(iNextionUiElements.begin(), iNextionUiElements.end(), [&receivedBytes](NextionUI* aNextionUI)
00029       {
00030           return (aNextionUI->GetPage() == static_cast<int>(receivedBytes[1]) &&
00031                  (aNextionUI->GetId() == static_cast<int>(receivedBytes[2])));
00032       });
00033 
00034       if (nextionUI != iNextionUiElements.end())
00035       {
00036           if (static_cast<int>(receivedBytes[3] == 1))
00037           {
00038               (*nextionUI)->PressReleaseTriggered(TEventType::EPress);
00039           }
00040           else if (static_cast<int>(receivedBytes[3] == 0))
00041           {
00042               (*nextionUI)->PressReleaseTriggered(TEventType::ERelease);
00043           }
00044       }
00045     }
00046   }
00047 }
00048 
00049 bool NextionSerial::GetCurrentDualStateButton(const std::string& aObjectName)
00050 {
00051     std::string command = "get ";
00052     command.append(aObjectName);
00053     command.append(".val");
00054     command.append(3, 0xff);
00055     iNextionSerial.write(command.c_str(), command.size());
00056 
00057     char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00058                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00059     iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
00060 
00061       if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x00 &&
00062           receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
00063           receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
00064           receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
00065         return false;
00066       } else if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x01 &&
00067                  receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
00068                  receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
00069                  receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
00070         return true;
00071       } else {
00072         printf("Error!! Current state of DualStateButton is invalid @ %s : %d \n", __PRETTY_FUNCTION__, __LINE__ );
00073         return false;
00074       }
00075 }