diff --git a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp b/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp index 0775ab8b3c4b39f46e3e29f76605f733cdcaefc5..f50c74451538466bdfa2a877d88ea7738411edea 100644 --- a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp +++ b/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp @@ -1,4 +1,4 @@ -#include "CyanBus.hpp" +#include "CyanBusBREAK.hpp" // C++ system level #include <cstring> // memset diff --git a/CLC-qthing/SiliconTorch/CyanBusCRC.cpp b/CLC-qthing/SiliconTorch/CyanBusCRC.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84a388e58efa6b033ffe3d08fb4aae2c8b0bb5c0 --- /dev/null +++ b/CLC-qthing/SiliconTorch/CyanBusCRC.cpp @@ -0,0 +1,42 @@ +#include "CyanBusCRC.hpp" + +// C++ system level +#include <cinttypes> + +// ESP32 specific +#include "esp_log.h" +#include "esp32/rom/crc.h" + +// project specific +// #include "LambdaTask.hpp" + + +static const char* TAG = "CyanBus"; + +namespace SiliconTorch { + + namespace Impl { + + + // Checks CyanBus CRC + // Note: The last 4 bytes of buffer contain the CRC to check against and are included in the given length + bool checkCRC(uint8_t* buffer, uint32_t length) { + + if (length < 4) { + ESP_LOGE(TAG, "CRC calculation failed: Input buffer too small! Got bytes[ %d ] but needs bytes[ >= 4 ]", length); + return false; + } + + // uint32_t _init = 0x00000000; + uint32_t _init = 0xFFFFFFFF; + uint32_t calculatedCRC = crc32_be(_init, buffer, length - 4); + uint32_t packetCRC = reinterpret_cast<uint32_t*>(&buffer[length - 4])[0]; + + ESP_LOGW("CheckCRC", "packetCRC = 0x%X calcCRC = 0x%X", packetCRC, calculatedCRC); + + return packetCRC == calculatedCRC; + } + + + } +} diff --git a/CLC-qthing/SiliconTorch/CyanBusCRC.hpp b/CLC-qthing/SiliconTorch/CyanBusCRC.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2b47a41f49b4791425a913b268825c9c82790735 --- /dev/null +++ b/CLC-qthing/SiliconTorch/CyanBusCRC.hpp @@ -0,0 +1,22 @@ +// C++ system level +#include <cinttypes> + +// ESP32 specific +// #include "esp_log.h" + +// project specific +// #include "LambdaTask.hpp" + + + +namespace SiliconTorch { + + namespace Impl { + + // Checks CyanBus CRC + // seems to work against Crc32Posix from python crccheck package + bool checkCRC(uint8_t* buffer, uint32_t length); + + } + +} diff --git a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp b/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp index 53adce2b6cecce3527558232cedcba2271b2c161..17257d51234317c7e5137ac89d71b43b37f5c97d 100644 --- a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp +++ b/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp @@ -1,4 +1,4 @@ -#include "CyanBus.hpp" +#include "CyanBusTOUT.hpp" // C++ system level #include <cstring> // memset @@ -124,14 +124,14 @@ namespace SiliconTorch { uint8_t headerLength = std::strlen(HEADER); uint32_t headerByteNum = 0; */ - auto Q2 = packetQ; + auto Q = packetQ; while (true) { uint32_t packetLEN = bufPTR - buffer; // what happens when we try to read 0 bytes??? - auto bytes = uart_read_bytes(_ch, bufPTR, MTU - packetLEN, 5); // 5 = ms Tout; TODO: make configurable and smaller + auto bytes = uart_read_bytes(_ch, bufPTR, MTU - packetLEN, 2); // 5 = ms Tout; TODO: make configurable and smaller if (bytes > 0) { @@ -139,10 +139,23 @@ namespace SiliconTorch { } else { - ESP_LOGI(TAG, "Received Tout â° after reading %d bytes", bufPTR - buffer); - xQueueSendToBack(Q2, buffer, portMAX_DELAY); - bufPTR = buffer; + if (bufPTR - buffer > 0) { + ESP_LOGI(TAG, "Received Tout â° after reading %d bytes", bufPTR - buffer); + // xQueueSendToBack(Q, buffer, portMAX_DELAY); + + + + char strbuf[17]; + strbuf[16] = 0x00; + + std::memcpy(strbuf, buffer, 16); + + ESP_LOGI(TAG, "PACKET DEBUG: Starts with [ %s ]", buffer); + + + bufPTR = buffer; + } } } diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index 5b5a073a1659a6d77b52969636e86a16140553c3..e98a28a04792745d11d485d80ea5d1e348dfe11c 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -9,7 +9,13 @@ #include "CyanLight.hpp" + +// ### LIBS FOR TESTING ### +#include <cstdlib> +#include "SiliconTorch/CyanBusCRC.hpp" #include "SiliconTorch/CyanBusTOUT.hpp" +// ### END LIBS ### + qthing::Config cfg; @@ -21,13 +27,24 @@ void device_main() { // Needed for packet parsing, animation rendering and stuff qthing::power_managment_max_power(); + uint8_t L = 9; + uint8_t* buffer = new uint8_t[L+1]; + std::memcpy(buffer, "fxk8y", 5); + buffer[L-4] = 0x84; + buffer[L-3] = 0xF1; + buffer[L-2] = 0xEF; + buffer[L-1] = 0x44; + buffer[L] = 0x00; + + ESP_LOGW("CheckCRC", "CRC of str[ %s ] is crc[ %s ]", buffer, SiliconTorch::Impl::checkCRC(buffer, L) ? "true" : "false"); + + return; SiliconTorch::CyanBusTOUT::CyanBus cyanBus(13, 14, 12, 15); // Pinout of CyanStripe // TODO: ??? - cfg.apply(); return;