Ocky Kristanto / NextionSerial

Files at this revision

API Documentation at this revision

Comitter:
Ocky Kristanto
Date:
Wed Mar 03 09:01:07 2021 +0100
Commit message:
feat: initial version

Changed in this revision

Button.cpp Show annotated file Show diff for this revision Revisions of this file
Button.h Show annotated file Show diff for this revision Revisions of this file
DualStateButton.cpp Show annotated file Show diff for this revision Revisions of this file
DualStateButton.h Show annotated file Show diff for this revision Revisions of this file
NextionSerial.cpp Show annotated file Show diff for this revision Revisions of this file
NextionSerial.h Show annotated file Show diff for this revision Revisions of this file
NextionUI.cpp Show annotated file Show diff for this revision Revisions of this file
NextionUI.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.cpp	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,35 @@
+#include "Button.h"
+#include "NextionSerial.h"
+
+Button::Button(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aNextionSerial) : 
+  NextionUI(aPage, aId, aObjectName, aNextionSerial)
+{
+}
+
+void Button::SetReleaseCallback(std::function<void()> aCallback)
+{
+    iReleaseCallback = std::move(aCallback);
+}
+
+void Button::SetPushCallback(std::function<void ()> aPushCallback)
+{
+    iPushCallback = std::move(aPushCallback);
+}
+
+void Button::PressReleaseTriggered(TEventType aEventType)
+{
+    switch (aEventType) {
+        case TEventType::EPress:
+        {
+            if (iPushCallback != nullptr) { iPushCallback(); }
+            break;
+        }    
+        case TEventType::ERelease:
+        {
+            if (iReleaseCallback != nullptr) { iReleaseCallback(); }
+            break;
+        }
+        default:
+            break;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Button.h	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "NextionUI.h"
+
+#include <cstdint>
+#include <functional>
+#include <string>
+
+class NextionSerial;
+
+class Button : public NextionUI
+{
+public:
+    explicit Button(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aNextionSerial);
+
+    void SetPushCallback(std::function<void()> aPushCallback);
+    void SetReleaseCallback(std::function<void()> aReleaseCallback);
+    
+    void PressReleaseTriggered(TEventType aEventType) override;
+
+private:
+    std::function<void()> iPushCallback {nullptr};
+    std::function<void()> iReleaseCallback {nullptr};
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DualStateButton.cpp	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,12 @@
+#include "DualStateButton.h"
+#include "NextionSerial.h"
+
+DualStateButton::DualStateButton(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aNextionSerial) : 
+  Button(aPage, aId, aObjectName, aNextionSerial)
+{
+}
+
+bool DualStateButton::GetState() const
+{
+    return iNextionSerial.GetCurrentDualStateButton(iObjectName);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DualStateButton.h	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "Button.h"
+
+#include <cstdint>
+#include <string>
+
+class NextionSerial;
+
+class DualStateButton : public Button
+{
+public:
+    explicit DualStateButton(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aNextionSerial);
+    bool GetState() const;
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NextionSerial.cpp	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,75 @@
+#include "NextionSerial.h"
+#include "NextionUI.h"
+
+#include <cstdio>
+#include <iterator>
+#include <string>
+
+NextionSerial::NextionSerial(PinName aTxPin, PinName aRxPin, int aBaud) :
+  iNextionSerial(aTxPin, aRxPin, aBaud)
+{
+  iNextionSerial.set_blocking(true);
+  serialThread.start([this]() { SerialLoop(); });
+}
+
+void NextionSerial::AddNextionUI(NextionUI* aNextionUI)
+{
+    iNextionUiElements.emplace_back(aNextionUI);
+}
+
+void NextionSerial::SerialLoop() {
+  while (true) {
+    char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
+
+    // 0x65 is UI Input Event
+    if (receivedBytes[0] == 0x65) {
+      auto nextionUI = std::find_if(iNextionUiElements.begin(), iNextionUiElements.end(), [&receivedBytes](NextionUI* aNextionUI)
+      {
+          return (aNextionUI->GetPage() == static_cast<int>(receivedBytes[1]) &&
+                 (aNextionUI->GetId() == static_cast<int>(receivedBytes[2])));
+      });
+
+      if (nextionUI != iNextionUiElements.end())
+      {
+          if (static_cast<int>(receivedBytes[3] == 1))
+          {
+              (*nextionUI)->PressReleaseTriggered(TEventType::EPress);
+          }
+          else if (static_cast<int>(receivedBytes[3] == 0))
+          {
+              (*nextionUI)->PressReleaseTriggered(TEventType::ERelease);
+          }
+      }
+    }
+  }
+}
+
+bool NextionSerial::GetCurrentDualStateButton(const std::string& aObjectName)
+{
+    std::string command = "get ";
+    command.append(aObjectName);
+    command.append(".val");
+    command.append(3, 0xff);
+    iNextionSerial.write(command.c_str(), command.size());
+
+    char receivedBytes[16]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+    iNextionSerial.read(&receivedBytes, sizeof(receivedBytes));
+
+      if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x00 &&
+          receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
+          receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
+          receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
+        return false;
+      } else if (receivedBytes[0] == 0x71 && receivedBytes[1] == 0x01 &&
+                 receivedBytes[2] == 0x00 && receivedBytes[3] == 0x00 &&
+                 receivedBytes[4] == 0x00 && receivedBytes[5] == 0xFF &&
+                 receivedBytes[6] == 0xFF && receivedBytes[7] == 0xFF) {
+        return true;
+      } else {
+        printf("Error!! Current state of DualStateButton is invalid @ %s : %d \n", __PRETTY_FUNCTION__, __LINE__ );
+        return false;
+      }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NextionSerial.h	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "mbed.h"
+
+#include <vector>
+
+class NextionUI;
+
+class NextionSerial 
+{
+public:
+    explicit NextionSerial(PinName aTxPin, PinName aRxPin, int aBaud);
+
+    bool GetCurrentDualStateButton(const std::string& aObjectName);
+    void AddNextionUI(NextionUI* aNextionUI);
+
+private:
+    void SerialLoop();
+
+private:
+    BufferedSerial iNextionSerial;
+    std::vector<NextionUI*> iNextionUiElements;
+    Thread serialThread;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NextionUI.cpp	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,36 @@
+#include "NextionUI.h"
+#include "NextionSerial.h"
+
+NextionUI::NextionUI(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aNextionSerial) :
+  iPage(aPage),
+  iId(aId),
+  iObjectName(aObjectName),
+  iNextionSerial(aNextionSerial)
+{
+    aNextionSerial.AddNextionUI(this);
+}
+
+uint32_t NextionUI::GetPage() const
+{
+    return iPage;
+}
+
+uint32_t NextionUI::GetId() const
+{
+    return iId;
+}
+
+void NextionUI::PressReleaseTriggered(TEventType aEventType)
+{
+    #ifdef DEBUG
+        switch (aEventType)
+        {
+            case TEventType::EPress:
+                printf("Received Press Event\n");
+                break;
+            case TEventType::ERelease:
+                printf("Received Release Event\n");
+                break;
+        }
+    #endif
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NextionUI.h	Wed Mar 03 09:01:07 2021 +0100
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+class NextionSerial;
+
+enum class TEventType 
+{
+    EPress,
+    ERelease
+};
+
+class NextionUI
+{
+public:
+  explicit NextionUI(uint32_t aPage, uint32_t aId, const std::string& aObjectName, NextionSerial& aSerialHandler);
+  virtual ~NextionUI() = default;
+
+  uint32_t GetPage() const;
+  uint32_t GetId() const;
+
+  virtual void PressReleaseTriggered(TEventType aEventType);
+
+protected:
+  uint32_t iPage;
+  uint32_t iId;
+  std::string iObjectName;
+  NextionSerial& iNextionSerial;
+};
\ No newline at end of file