11#include " UartService.h"
22
3+ /*
34void UartService::configure(unsigned long baud, uint32_t config, uint8_t rx, uint8_t tx, bool inverted) {
45 Serial1.end();
56 Serial1.begin(baud, config, rx, tx, inverted);
@@ -26,9 +27,50 @@ void UartService::configure(unsigned long baud, uint32_t config, uint8_t rx, uin
2627 buffersAllocated = true;
2728 }
2829}
30+ */
31+
32+ void UartService::configure (unsigned long baud,
33+ uint32_t config,
34+ uint8_t rx,
35+ uint8_t tx,
36+ bool inverted,
37+ HardwareSerial* serial,
38+ bool noBuffer) {
39+
40+ _serial = serial;
41+
42+ _serial->end ();
43+ _serial->begin (baud, config, rx, tx, inverted);
44+
45+ if (noBuffer) {
46+ return ;
47+ }
48+
49+ if (!buffersAllocated) {
50+
51+ edgeIntervals = (uint32_t *) heap_caps_malloc (
52+ sizeof (uint32_t ) * 50 ,
53+ MALLOC_CAP_INTERNAL
54+ );
55+
56+ edgeCounts = (uint32_t *) heap_caps_malloc (
57+ sizeof (uint32_t ) * 64 ,
58+ MALLOC_CAP_INTERNAL
59+ );
60+
61+ if (!edgeIntervals || !edgeCounts) {
62+ buffersAllocated = false ;
63+ return ;
64+ }
65+
66+ memset ((void *)edgeCounts, 0 , sizeof (uint32_t ) * 64 );
67+
68+ buffersAllocated = true ;
69+ }
70+ }
2971
3072void UartService::release () {
31- Serial1. end ();
73+ _serial-> end ();
3274
3375 if (buffersAllocated) {
3476 heap_caps_free ((void *)edgeIntervals);
@@ -40,27 +82,27 @@ void UartService::release() {
4082}
4183
4284void UartService::end () {
43- Serial1. end ();
85+ _serial-> end ();
4486}
4587
4688std::string UartService::readLine () {
4789 std::string input;
4890 bool lastWasCR = false ;
4991
5092 while (true ) {
51- if (!Serial1. available ()) continue ;
93+ if (!_serial-> available ()) continue ;
5294
53- char c = Serial1. read ();
95+ char c = _serial-> read ();
5496
5597 if (c == ' \r ' ) {
5698 lastWasCR = true ;
57- Serial1. println ();
99+ _serial-> println ();
58100 break ;
59101 }
60102
61103 if (c == ' \n ' ) {
62104 if (!lastWasCR) {
63- Serial1. println ();
105+ _serial-> println ();
64106 break ;
65107 }
66108 continue ;
@@ -69,11 +111,11 @@ std::string UartService::readLine() {
69111 if (c == ' \b ' || c == 127 ) {
70112 if (!input.empty ()) {
71113 input.pop_back ();
72- Serial1. print (" \b \b " );
114+ _serial-> print (" \b \b " );
73115 }
74116 } else {
75117 input += c;
76- Serial1. print (c);
118+ _serial-> print (c);
77119 lastWasCR = false ;
78120 }
79121 }
@@ -82,27 +124,27 @@ std::string UartService::readLine() {
82124}
83125
84126void UartService::print (const std::string& msg) {
85- Serial1. print (msg.c_str ());
127+ _serial-> print (msg.c_str ());
86128}
87129
88130void UartService::println (const std::string& msg) {
89- Serial1. println (msg.c_str ());
131+ _serial-> println (msg.c_str ());
90132}
91133
92134bool UartService::available () const {
93- return Serial1. available ();
135+ return _serial-> available ();
94136}
95137
96138char UartService::read () {
97- return Serial1. read ();
139+ return _serial-> read ();
98140}
99141
100142void UartService::write (char c) {
101- Serial1. write (c);
143+ _serial-> write (c);
102144}
103145
104146void UartService::write (const std::string& str) {
105- Serial1. write (reinterpret_cast <const uint8_t *>(str.c_str ()), str.length ());
147+ _serial-> write (reinterpret_cast <const uint8_t *>(str.c_str ()), str.length ());
106148}
107149
108150std::string UartService::executeByteCode (const std::vector<ByteCode>& bytecodes) {
@@ -115,15 +157,15 @@ std::string UartService::executeByteCode(const std::vector<ByteCode>& bytecodes)
115157 switch (code.getCommand ()) {
116158 case ByteCodeEnum::Write:
117159 for (uint32_t i = 0 ; i < code.getRepeat (); ++i) {
118- Serial1. write (code.getData ());
160+ _serial-> write (code.getData ());
119161 }
120162 break ;
121163
122164 case ByteCodeEnum::Read:
123165 start = millis ();
124166 while (received < code.getRepeat () && (millis () - start < timeout)) {
125- if (Serial1. available ()) {
126- char c = Serial1. read ();
167+ if (_serial-> available ()) {
168+ char c = _serial-> read ();
127169 result += c;
128170 ++received;
129171 } else {
@@ -149,11 +191,11 @@ std::string UartService::executeByteCode(const std::vector<ByteCode>& bytecodes)
149191}
150192
151193void UartService::switchBaudrate (unsigned long newBaud) {
152- Serial1. updateBaudRate (newBaud);
194+ _serial-> updateBaudRate (newBaud);
153195}
154196
155197void UartService::flush () {
156- Serial. flush ();
198+ _serial-> flush ();
157199}
158200
159201void UartService::clearUartBuffer () {
@@ -213,7 +255,7 @@ void UartService::setXmodemSendHandler(void (*handler)(void*, size_t, byte*, siz
213255}
214256
215257void UartService::initXmodem () {
216- xmodem.begin (Serial1 , xmodemProtocol);
258+ xmodem.begin (*_serial , xmodemProtocol);
217259 xmodem.setDataSize (xmodemBlockSize);
218260 xmodem.setIdSize (xmodemIdSize);
219261}
0 commit comments