From 24b72b00612c2a8c471a415ec766ac77e278fbab Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Fri, 25 Mar 2022 10:56:18 +0100
Subject: [PATCH] Begin developing CyanBus

---
 CLC-qthing/SiliconTorch/CyanBus.cpp | 63 +++++++++++++++++++++++++++++
 CLC-qthing/SiliconTorch/CyanBus.hpp | 37 +++++++++++++++++
 CLC-qthing/SiliconTorch/RS485.cpp   | 12 +++++-
 CLC-qthing/SiliconTorch/RS485.hpp   | 10 ++---
 CLC-qthing/device_config.h          |  2 +-
 CLC-qthing/device_main.cpp          | 10 +++--
 6 files changed, 121 insertions(+), 13 deletions(-)
 create mode 100644 CLC-qthing/SiliconTorch/CyanBus.cpp
 create mode 100644 CLC-qthing/SiliconTorch/CyanBus.hpp

diff --git a/CLC-qthing/SiliconTorch/CyanBus.cpp b/CLC-qthing/SiliconTorch/CyanBus.cpp
new file mode 100644
index 0000000..73f446d
--- /dev/null
+++ b/CLC-qthing/SiliconTorch/CyanBus.cpp
@@ -0,0 +1,63 @@
+#include "CyanBus.hpp"
+
+// C++ system level
+#include <cinttypes>
+#include <functional>
+
+// ESP32 specific
+#include "esp_log.h"
+#include "driver/gpio.h"
+#include "driver/uart.h"
+
+// project specific
+#include "LambdaTask.hpp"
+
+// qthing stuff
+// #include ""
+
+
+
+static const char* TAG = "CyanBus";
+
+
+// This leads to ~54 P/s @ 2 MBaud at 100% load
+// TODO: Make configurable?!
+const uint32_t CyanBusMTU = 4096;
+
+
+namespace SiliconTorch {
+
+  namespace CyanBus {
+
+
+    CyanBus::CyanBus(Reader reader) : reader(reader) {
+
+      readerTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ readerTask(); });
+
+    }
+
+    void CyanBus::readerTask() {
+      const uint8_t* header = (const uint8_t*)"fxCyan";
+
+      while (true) {
+
+        ESP_LOGW(TAG, "Hello, ReaderTask()!  My header is: %s", header);
+
+        vTaskDelay(420);
+
+      }
+    }
+
+    void CyanBus::setBaudRate(uint32_t baudRate) {
+      ESP_LOGE(TAG, "Operation unsupported: setBaudRate(…)");
+    }
+
+    uint32_t CyanBus::getBaudRate() {
+      ESP_LOGE(TAG, "Operation unsupported: getBaudRate()");
+      return 1337;
+    }
+  }
+}
+
+
+
diff --git a/CLC-qthing/SiliconTorch/CyanBus.hpp b/CLC-qthing/SiliconTorch/CyanBus.hpp
new file mode 100644
index 0000000..25f0340
--- /dev/null
+++ b/CLC-qthing/SiliconTorch/CyanBus.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+// C++ system level
+#include <string>
+#include <cinttypes>
+#include <functional>
+
+// project specific
+#include "LambdaTask.hpp"
+
+
+namespace SiliconTorch {
+
+  namespace CyanBus {
+
+    typedef std::function<uint8_t*(uint32_t)> Reader;
+
+    class CyanBus {
+      public:
+        CyanBus(Reader reader);
+
+        // TODO: How do we wanna set baudRates when we only have a reader?!
+        // TODO: Elaborate on this topic!
+        uint32_t getBaudRate();
+        void setBaudRate(uint32_t baudRate);
+
+      private:
+
+        Reader reader;
+
+        void readerTask();
+
+        SiliconTorch::Util::LambdaTask* readerTaskHandle = NULL;
+
+    };
+  }
+}
diff --git a/CLC-qthing/SiliconTorch/RS485.cpp b/CLC-qthing/SiliconTorch/RS485.cpp
index ad8862b..c4fd6e9 100644
--- a/CLC-qthing/SiliconTorch/RS485.cpp
+++ b/CLC-qthing/SiliconTorch/RS485.cpp
@@ -9,7 +9,7 @@
 #include "driver/uart.h"
 
 // project specific
-#include "LambdaTask.hpp"
+// #include "LambdaTask.hpp"
 
 // qthing stuff
 // #include ""
@@ -75,18 +75,28 @@ namespace SiliconTorch {
       uart_set_pin(_ch, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
       uart_set_mode(_ch, UART_MODE_UART);
       uart_set_rx_timeout(_ch, 0);  // TODO: timeout…??
+
+      // TOOD: receive only ATM!
+      txEN(false);
+      rxEN(true);
     }
 
 
     void RS485::write(const uint8_t* data, uint32_t length) {
       uint32_t send = uart_write_bytes((uart_port_t)uartChannel, reinterpret_cast<const char*>(data), length);
 
+      // TODO: remove after change!
+      ESP_LOGE(TAG, "This class is receive-only atm! TX won't work!");
+
       if (send != length) ESP_LOGE(TAG, "Sending bytes#[ %d ] failed: Only bytes#[ %d ] were send!", length, send);
     }
 
     void RS485::write(std::string& data) {
       uint32_t send = uart_write_bytes((uart_port_t)uartChannel, data.c_str(), data.length());
 
+      // TODO: remove after change!
+      ESP_LOGE(TAG, "This class is receive-only atm! TX won't work!");
+
       if (send != data.length()) ESP_LOGE(TAG, "Sending bytes#[ %d ] failed: Only bytes#[ %d ] were send!", data.length(), send);
     }
 
diff --git a/CLC-qthing/SiliconTorch/RS485.hpp b/CLC-qthing/SiliconTorch/RS485.hpp
index 1e8ccec..7a02cd2 100644
--- a/CLC-qthing/SiliconTorch/RS485.hpp
+++ b/CLC-qthing/SiliconTorch/RS485.hpp
@@ -18,25 +18,21 @@ namespace SiliconTorch {
         uint32_t getBaudRate();
         void setBaudRate(uint32_t baudRate);
 
-        void write(std::string& data);
-        void write(const uint8_t* data, uint32_t length);
-
         uint32_t available();
         uint32_t read(uint8_t* data, uint32_t length);
 
       private:
 
         uint8_t uartChannel;
-
-
-
-
         uint8_t tx, rx, de, re;
 
         void txEN(bool state);
         void rxEN(bool state);
         void txrxEN(bool state);
 
+        // Receive only ATM!
+        void write(std::string& data);
+        void write(const uint8_t* data, uint32_t length);
 
     };
   }
diff --git a/CLC-qthing/device_config.h b/CLC-qthing/device_config.h
index 41978b1..b9bf347 100644
--- a/CLC-qthing/device_config.h
+++ b/CLC-qthing/device_config.h
@@ -1,5 +1,5 @@
 // hostname and device namespace
-#define DEVICE_NAME "CLC"
+#define DEVICE_NAME "CyanLight"
 
 #define NTP_SERVER "pool.ntp.org"
 
diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp
index 7ff3f2e..47dfe72 100644
--- a/CLC-qthing/device_main.cpp
+++ b/CLC-qthing/device_main.cpp
@@ -16,12 +16,14 @@ qthing::Config cfg;
 CyanLight::CyanLightControl* ctrl;
 
 
-
 void device_main() {
 
   cfg.apply();
 
-  ctrl = new CyanLight::CyanLightControl(3);
+  ctrl = new CyanLight::CyanLightControl(1);
+  ctrl->addChannel(26);
+  ctrl->addChannel(25);
+  
   ctrl->setFrqRes(100, 19);
 
 
@@ -36,8 +38,8 @@ void device_main() {
   }
 
 
-  ctrl->setPWM(0, 0.0f);
-  ctrl->setPWM(1, 0.0f);
+  ctrl->setPWM(0, 1.0f);
+  ctrl->setPWM(1, 1.0f);
   ctrl->setPWM(2, 1.0f);
 
 
-- 
GitLab