From 67f557fb38e79b04846cdc85a944f945ef8453a8 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Fri, 22 Apr 2022 02:31:17 +0200 Subject: [PATCH] =?UTF-8?q?Moving=20the=20DevEnv=C2=AE=20to=20the=20other?= =?UTF-8?q?=20Schlepptop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLC-qthing/SiliconTorch/CyanBus.cpp | 35 ++++++++++++++++++++---- CLC-qthing/SiliconTorch/CyanBus.hpp | 18 ++++++++++++ CLC-qthing/SiliconTorch/VSyncManager.cpp | 22 +++++++++++++++ CLC-qthing/SiliconTorch/VSyncManager.hpp | 22 +++++++++++++++ CLC-qthing/SpiderLib/Callback.hpp | 8 +++--- CLC-qthing/device_main.cpp | 13 +++++++-- 6 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 CLC-qthing/SiliconTorch/VSyncManager.cpp create mode 100644 CLC-qthing/SiliconTorch/VSyncManager.hpp diff --git a/CLC-qthing/SiliconTorch/CyanBus.cpp b/CLC-qthing/SiliconTorch/CyanBus.cpp index c6eeb1b..dd8162a 100644 --- a/CLC-qthing/SiliconTorch/CyanBus.cpp +++ b/CLC-qthing/SiliconTorch/CyanBus.cpp @@ -137,7 +137,7 @@ namespace SiliconTorch { size_t available; uart_get_buffered_data_len(_ch, &available); - ESP_LOGI(TAG, "Got a new UART event! 🤩 QByte[ %d ] QMsg#[ %d ] event.size[ %d ] uart.available[ %d ]", space, msgs, event.size, available); + ESP_LOGI(TAG, "Got a new UART event! 🤩 QByte[ %d ] |QMsg|[ %d ] event.size[ %d ] uart.available[ %d ]", space, msgs, event.size, available); switch (event.type) { @@ -145,7 +145,7 @@ namespace SiliconTorch { case UART_DATA: { if ((bufPTR - buffer) + event.size > bufLEN) { - ESP_LOGE(TAG, "Buffer#[ %d ] Overflow!", bufLEN); + ESP_LOGE(TAG, "|buffer|[ %d ] Overflow!", bufLEN); bufPTR = buffer; std::memset(buffer, 0x00, bufLEN); // TODO: needed…? @@ -162,7 +162,7 @@ namespace SiliconTorch { if (strncmp(reinterpret_cast<const char*>(buffer), HEADER, headerLEN) == 0) { payloadLEN = getLength(buffer + headerLEN); - ESP_LOGW(TAG, "Found header: payload#[ %d ]", payloadLEN); + ESP_LOGW(TAG, "Found header: |payload|[ %d ]", payloadLEN); if (payloadLEN > MTU) { ESP_LOGE(TAG, "payload#[ %d ] exceeds MTU[ %d ]", payloadLEN, MTU); @@ -191,7 +191,9 @@ namespace SiliconTorch { else ESP_LOGE(TAG, "Packet CRC check failed!"); bufPTR = buffer; - std::memset(buffer, 0x00, bufLEN); // TODO: needed…? + std::memset(buffer, 0x00, bufLEN); // TODO: needed…? (if not: move to --> if not crcOK………) + + if (!crcOK) xQueueSendToBack(Q2, (void*)buffer, portMAX_DELAY); // TODO: do we really need this after successful packet reception? I think not! // uart_flush_input(_ch); @@ -267,16 +269,37 @@ namespace SiliconTorch { if (xQueueReceive(Q, (void*)buffer, (TickType_t)portMAX_DELAY)) { payloadLEN = getLength(buffer); + bool crcOK = payloadLEN > 0; + + if (crcOK) ESP_LOGE("PacketTask", "PACKET DEBUG: |payload|[ %d ] starts with data[ %s ]", payloadLEN, buffer+2); + else ESP_LOGE("PacketTask", "PACKET DEBUG: |payload|[ %d ] has incorrect CRC", payloadLEN); + + const PacketData packet = { + .buffer = buffer+2, + .length = payloadLEN + }; - ESP_LOGE("PacketTask", "PACKET DEBUG: payload#[ %d ] starts with data[ %s ]", payloadLEN, buffer+2); + const PacketMetrics metrics = { + .crcOK = crcOK, + .payloadLEN = payloadLEN + }; - // TODO: really handle packet! + if (crcOK) packetCallback(packet); + metricsCallback(metrics); } } delete buffer; } + + // void CyanBus::registerPacketCallback(std::string& magicString, PacketCallback callback) { + // } + + // void CyanBus::registerMetricsCallback(MetricsCallback callback) { + // } + + void CyanBus::setBaudRate(uint32_t baudRate) { uart_set_baudrate((uart_port_t)uartChannel, baudRate); } diff --git a/CLC-qthing/SiliconTorch/CyanBus.hpp b/CLC-qthing/SiliconTorch/CyanBus.hpp index 47f267b..ad1e9f8 100644 --- a/CLC-qthing/SiliconTorch/CyanBus.hpp +++ b/CLC-qthing/SiliconTorch/CyanBus.hpp @@ -10,6 +10,7 @@ #include "freertos/queue.h" // project specific +#include "SpiderLib/Callback.hpp" #include "LambdaTask.hpp" @@ -25,6 +26,17 @@ namespace SiliconTorch { extern const char* const HEADER; + typedef struct { + const uint8_t* buffer; + const uint16_t length; + } PacketData; + + typedef struct { + const bool crcOK; + const uint16_t payloadLEN; + } PacketMetrics; + + class CyanBus { public: CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate = 115200, uint8_t uartChannel = 1); @@ -32,6 +44,12 @@ namespace SiliconTorch { uint32_t getBaudRate(); void setBaudRate(uint32_t baudRate); + const SpiderLib::Callback<void(const PacketData&)> packetCallback; + const SpiderLib::Callback<void(const PacketMetrics&)> metricsCallback; + + // void registerMetricsCallback(MetricsCallback callback); + // void registerPacketCallback(std::string& magicString, PacketCallback callback); + private: QueueHandle_t uartEvQ; diff --git a/CLC-qthing/SiliconTorch/VSyncManager.cpp b/CLC-qthing/SiliconTorch/VSyncManager.cpp new file mode 100644 index 0000000..9bc9fc9 --- /dev/null +++ b/CLC-qthing/SiliconTorch/VSyncManager.cpp @@ -0,0 +1,22 @@ +#include "VSyncManager.hpp" + +// C++ system level +// #include <cstring> // memset, strncmp +// #include <cstdlib> // TODO: is this for memcpy? +// #include <cinttypes> +// #include <functional> + +// ESP32 specific +#include "esp_log.h" +// #include "driver/gpio.h" +// #include "driver/uart.h" +// #include "freertos/FreeRTOS.h" +// #include "freertos/queue.h" + +// project specific +// #include "CyanBusCRC.hpp" + +// qthing stuff +// #include "" + + diff --git a/CLC-qthing/SiliconTorch/VSyncManager.hpp b/CLC-qthing/SiliconTorch/VSyncManager.hpp new file mode 100644 index 0000000..69ca8c7 --- /dev/null +++ b/CLC-qthing/SiliconTorch/VSyncManager.hpp @@ -0,0 +1,22 @@ +#pragma once + +// C++ system level +// #include <cstring> // memset, strncmp +// #include <cstdlib> // TODO: is this for memcpy? +// #include <cinttypes> +// #include <functional> + +// ESP32 specific +#include "esp_log.h" +// #include "driver/gpio.h" +// #include "driver/uart.h" +// #include "freertos/FreeRTOS.h" +// #include "freertos/queue.h" + +// project specific +// #include "CyanBusCRC.hpp" + +// qthing stuff +// #include "" + + diff --git a/CLC-qthing/SpiderLib/Callback.hpp b/CLC-qthing/SpiderLib/Callback.hpp index 658b183..d637e17 100644 --- a/CLC-qthing/SpiderLib/Callback.hpp +++ b/CLC-qthing/SpiderLib/Callback.hpp @@ -31,16 +31,16 @@ namespace SpiderLib { Callback() : callback(NULL) {}; - void operator()(Args&&... args) { + void operator()(Args&&... args) const { if (callback != NULL) callback(std::forward<Args>(args)...); } - auto& operator+=(const CallbackF& newCB) { + auto& operator+=(const CallbackF& newCB) const { add(newCB); return *this; } - void add(CallbackF newCB) { + void add(CallbackF newCB) const { if (callback == NULL) { callback = newCB; } else { @@ -53,7 +53,7 @@ namespace SpiderLib { } private: - CallbackF callback = NULL; + mutable CallbackF callback = NULL; }; } diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index 8016e81..3ca3499 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -27,18 +27,27 @@ qthing::Config cfg; CyanLight::CyanLightControl* ctrl; + void device_main() { // Needed for packet parsing, animation rendering and stuff qthing::power_managment_max_power(); + cyanBus = new SiliconTorch::CyanBus::CyanBus(13, 14, 12, 15, 2000000); // Pinout of CyanStripe - cyanBus = new SiliconTorch::CyanBus::CyanBus(13, 14, 12, 15); // Pinout of CyanStripe - // TODO: ??? + cyanBus->packetCallback += [](SiliconTorch::CyanBus::PacketData& data) { + ESP_LOGW("PacketDataTestCallback", "|buffer|[ %d ] buffer[ %s ]", data.length, data.buffer); + }; + + cyanBus->metricsCallback += [](SiliconTorch::CyanBus::PacketMetrics& metrics) { + ESP_LOGW("PacketMetricsTestCallback", "|buffer|[ %d ] crcâ»[ %s ]", metrics.payloadLEN, metrics.crcOK ? "✔" : "✘"); // TODO: better use ✅ / ⌠?? + }; + // TODO: ??? + qthing::enable_wifi(); cfg.apply(); -- GitLab