Skip to content

Commit c3f702b

Browse files
committed
x8: rpc: allow using Serial in libraries
1 parent 09f7813 commit c3f702b

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ extern analogin_config_t adcCurrentConfig;
115115
#if defined(RPC_SERIAL)
116116
#undef Serial
117117
#if __has_include("RPC.h")
118+
#include "SerialRPC.h"
118119
#define Serial SerialRPC
119120
#else
120121
extern ErrorSerialClass ErrorSerial;

libraries/RPC/src/SerialRPC.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
#include "SerialRPC.h"
2+
#include "RPC.h"
3+
4+
size_t arduino::SerialRPCClass::write(uint8_t* buf, size_t len) {
5+
tx_buffer.clear();
6+
for (size_t i=0; i < len; i++) {
7+
tx_buffer.push_back(buf[i]);
8+
}
9+
RPC.send("tty", tx_buffer);
10+
return len;
11+
}
12+
13+
int arduino::SerialRPCClass::begin(long unsigned int, uint16_t) {
14+
if (RPC.begin() == 0) {
15+
return 0;
16+
}
17+
RPC.bind("tty", mbed::callback(this, &SerialRPCClass::onWrite));
18+
return 1;
19+
}
20+
21+
arduino::SerialRPCClass::operator bool() {
22+
return RPC;
23+
}
224

325
arduino::SerialRPCClass SerialRPC;

libraries/RPC/src/SerialRPC.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
#include "RPC.h"
1+
#ifndef __SERIAL_RPC__
2+
#define __SERIAL_RPC__
3+
24
#include "Arduino.h"
5+
#include <vector>
36

47
namespace arduino {
58

@@ -29,6 +32,9 @@ class SerialRPCClass : public Stream {
2932
}
3033
}
3134

35+
int begin(long unsigned int = 0, uint16_t = 0);
36+
size_t write(uint8_t* buf, size_t len);
37+
3238
size_t write(uint8_t c) {
3339
return write(&c, 1);
3440
}
@@ -43,28 +49,9 @@ class SerialRPCClass : public Stream {
4349
return write((uint8_t*)buf, len);
4450
}
4551

46-
size_t write(uint8_t* buf, size_t len) {
47-
tx_buffer.clear();
48-
for (size_t i=0; i < len; i++) {
49-
tx_buffer.push_back(buf[i]);
50-
}
51-
RPC.send("tty", tx_buffer);
52-
return len;
53-
}
54-
5552
using Print::write;
5653

57-
int begin(long unsigned int = 0, uint16_t = 0) {
58-
if (RPC.begin() == 0) {
59-
return 0;
60-
}
61-
RPC.bind("tty", mbed::callback(this, &SerialRPCClass::onWrite));
62-
return 1;
63-
}
64-
65-
operator bool() {
66-
return RPC;
67-
}
54+
operator bool();
6855

6956
void attach(void (*fptr)(void))
7057
{
@@ -80,4 +67,6 @@ class SerialRPCClass : public Stream {
8067
};
8168
}
8269

83-
extern arduino::SerialRPCClass SerialRPC;
70+
extern arduino::SerialRPCClass SerialRPC;
71+
72+
#endif

0 commit comments

Comments
 (0)