Skip to content
Snippets Groups Projects
Commit 3c6cd075 authored by fxk8y's avatar fxk8y :spider:
Browse files

Preparing for parsing

parent 0197aab7
No related branches found
No related tags found
No related merge requests found
#include "CyanBus.hpp"
// C++ system level
// #include <cstdlib> // TODO: is this for memcpy?
#include <cinttypes>
#include <functional>
......@@ -30,7 +31,7 @@ namespace SiliconTorch {
namespace CyanBus {
CyanBus::CyanBus(Reader reader) : reader(reader) {
CyanBus::CyanBus(SiliconTorch::Impl::RS485 reader) : reader(reader) {
readerTaskHandle = new SiliconTorch::Util::LambdaTask([&](){ readerTask(); });
......@@ -38,23 +39,67 @@ namespace SiliconTorch {
void CyanBus::readerTask() {
const uint8_t* header = (const uint8_t*)"fxCyan";
uint8_t* buffer = new uint8_t[CyanBusMTU];
uint8_t* bufPTR = buffer;
enum FSM { SearchHeader };
FSM fsm = SearchHeader;
uint16_t packetLength = 0;
uint8_t headerIDX = 0;
enum FSM { SearchHeader, ReadLenght, ReadBody, CheckCRC };
FSM fsm = SearchHeader;
while (true) {
ESP_LOGW(TAG, "Hello, ReaderTask()! My header is: %s", header);
switch (fsm) {
case SearchHeader: {
vTaskDelay(420);
reader.read(bufPTR, 1);
continue;
if (bufPTR[0] == header[bufPTR - buffer]) {
// got the right char
bufPTR++;
switch (fsm) {
case SearchHeader: {
if (bufPTR - buffer >= sizeof(header)) {
fsm = ReadLenght;
}
} else {
// start over
bufPTR = buffer;
}
break;
}
case ReadLenght: {
reader.read(bufPTR, 2);
packetLength = (bufPTR[0] << 8) | bufPTR[1];
bufPTR += 2;
ESP_LOGW(TAG, "Got a packet with length[ %d ] \\o/", packetLength);
fsm = ReadBody;
break;
}
case ReadBody: {
reader.read(bufPTR, packetLength + 4); // +4 = CRC
fsm = CheckCRC;
break;
}
case CheckCRC: {
// TODO: check the CRC!
// If it matches: handle packet!
// But if it not matches?
// search whole packet for header? (how would we implement this???)
// or just discard? (leads to possible desynchronisation!!!)
break;
}
......
......@@ -6,6 +6,7 @@
#include <functional>
// project specific
#include "RS485.hpp"
#include "LambdaTask.hpp"
......@@ -13,11 +14,9 @@ namespace SiliconTorch {
namespace CyanBus {
typedef std::function<uint8_t*(uint32_t)> Reader;
class CyanBus {
public:
CyanBus(Reader reader);
CyanBus(SiliconTorch::Impl::RS485 reader);
// TODO: How do we wanna set baudRates when we only have a reader?!
// TODO: Elaborate on this topic!
......@@ -26,7 +25,7 @@ namespace SiliconTorch {
private:
Reader reader;
SiliconTorch::Impl::RS485 reader;
void readerTask();
......
......@@ -3,6 +3,7 @@
// C++ system level
#include <string>
#include <cinttypes>
#include <functional>
......@@ -10,7 +11,6 @@ namespace SiliconTorch {
namespace Impl {
class RS485 {
public:
RS485(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate = 115200, uint8_t uartChannel = 1);
......
......@@ -9,6 +9,8 @@
#include "CyanLight.hpp"
#include "SiliconTorch/RS485.hpp"
#include "SiliconTorch/CyanBus.hpp"
qthing::Config cfg;
......@@ -18,6 +20,13 @@ CyanLight::CyanLightControl* ctrl;
void device_main() {
SiliconTorch::Impl::RS485 rs485(25, 21, 22, 23);
SiliconTorch::CyanBus::CyanBus cyanBus(rs485);
cfg.apply();
ctrl = new CyanLight::CyanLightControl(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment