Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
cool_serial_uart.hpp
Go to the documentation of this file.
1#pragma once
2
3
4#include "cool_serial/byte_queue.hpp"
5#include "cool_serial/continuous_parser.hpp"
6#include "cool_serial/idata_handler.hpp"
7
9#include "tap/drivers.hpp"
10
11#include <functional>
12#include <unordered_map>
13
14namespace fang::communication
15{
25 template<tap::communication::serial::Uart::UartPort kPort, int kBaudrate>
27 {
28 public:
29 using DataHandlerRef = std::reference_wrapper<coolSerial::IDataHandler>;
30 using HandlerMap = std::unordered_map<coolSerial::Byte, DataHandlerRef>;
31
33 :
34 uart_{drivers.uart},
35 handlerMap_{handlerMap}
36 {
37 }
38
40 :
41 uart_{drivers.uart}
42 {
43 }
44
46 {
47 uart_.init<kPort, kBaudrate, tap::communication::serial::Uart::Parity::Disabled>();
48 }
49
50 void update()
51 {
52 updateByteQueue();
53 parser_.update();
54 handleMessage();
55 }
56
61 void addHandler(coolSerial::Byte dataType, DataHandlerRef handler)
62 {
63 // [] needs to be able to create a default object. DataHandlerRef does not have
64 // a default constructor
65 handlerMap_.at(dataType) = handler;
66 }
67 private:
68
69 void updateByteQueue()
70 {
71 // Transfer uart buffer to byte queue
72 unsigned char byte{};
74 {
75 byteQueue_.push(byte);
76 }
77 }
78
79 void handleMessage()
80 {
81 if(!parser_.currentMessageProcessed())
82 {
83 referMessage(parser_.getCurrentMessage());
84 }
85 }
86 void referMessage(const coolSerial::CoolMessageData& message)
87 {
88 // [] needs to be able to create a default object. DataHandlerRef does not have
89 // a default constructor
90 handlerMap_.at(message.dataType).get().handleData(message.data);
91 }
92
93 HandlerMap handlerMap_{};
95 coolSerial::ByteQueue byteQueue_{};
96 coolSerial::ContinuousParser parser_{byteQueue_};
97 };
98}
Definition cool_serial_uart.hpp:27
CoolSerialUart(tap::Drivers &drivers, const HandlerMap &handlerMap)
Definition cool_serial_uart.hpp:32
std::unordered_map< coolSerial::Byte, DataHandlerRef > HandlerMap
Definition cool_serial_uart.hpp:30
void addHandler(coolSerial::Byte dataType, DataHandlerRef handler)
Definition cool_serial_uart.hpp:61
std::reference_wrapper< coolSerial::IDataHandler > DataHandlerRef
Definition cool_serial_uart.hpp:29
CoolSerialUart(tap::Drivers &drivers)
Definition cool_serial_uart.hpp:39
void update()
Definition cool_serial_uart.hpp:50
void initialize()
Definition cool_serial_uart.hpp:45
Definition drivers.hpp:70
Definition uart.hpp:48
void init()
Definition uart.hpp:81
mockable bool read(UartPort port, uint8_t *data)
Definition uart.cpp:33
@ Uart1
Definition uart.hpp:52
Definition basic_target_handler.cpp:6
fang::Drivers & drivers
Definition robot_singleton.cpp:45