-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserial_bus.h
More file actions
64 lines (53 loc) · 2.03 KB
/
serial_bus.h
File metadata and controls
64 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include "../utils/otb.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "module.h"
#include "serial.h"
#include <cstdint>
#include <vector>
class SerialBus;
using SerialBus_ptr = std::shared_ptr<SerialBus>;
class SerialBus : public Module {
public:
static constexpr size_t PAYLOAD_CAPACITY = 256;
const ConstSerial_ptr serial;
const uint8_t node_id;
SerialBus(const std::string &name, const ConstSerial_ptr serial, const uint8_t node_id);
void step() override;
void call(const std::string method_name, const std::vector<ConstExpression_ptr> arguments) override;
static const std::map<std::string, Variable_ptr> get_defaults();
private:
struct IncomingMessage {
uint8_t sender;
uint8_t receiver;
size_t length;
char payload[PAYLOAD_CAPACITY];
};
struct OutgoingMessage {
uint8_t receiver;
size_t length;
char payload[PAYLOAD_CAPACITY];
};
std::vector<uint8_t> peer_ids;
QueueHandle_t outbound_queue = nullptr;
QueueHandle_t inbound_queue = nullptr;
TaskHandle_t communication_task = nullptr;
bool is_polling = false;
unsigned long poll_start_millis = 0;
size_t poll_index = 0;
uint8_t requesting_node = 0;
uint8_t echo_target_id = 0; // node ID that should receive relayed echo output (0 = no relay)
otb::BusOtbSession otb_session;
[[noreturn]] static void communication_loop(void *param);
void process_uart();
bool parse_message(const char *message_line, IncomingMessage &message) const;
void handle_incoming_message(const IncomingMessage &message);
void enqueue_outgoing_message(const uint8_t receiver, const char *payload, const size_t length);
bool send_outgoing_queue();
void send_message(const uint8_t receiver, const char *payload, const size_t length) const;
void print_to_incoming_queue(const char *format, ...) const;
void handle_echo(const char *line);
bool is_coordinator() const { return !this->peer_ids.empty(); }
};