Skip to content

Commit 91f7194

Browse files
committed
RPC: fix example to avoid using RPC raw
1 parent 4c38c07 commit 91f7194

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

Diff for: libraries/RPC/examples/RPC_m4/RPC_m4.ino

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#include "Arduino.h"
22
#include "RPC.h"
3+
#include "SerialRPC.h"
34

45
using namespace rtos;
56

67
Thread subtractThread;
78

89
/**
9-
* Returns the CPU that's currently running the sketch (M7 or M4)
10-
* Note that the sketch has to be uploaded to both cores.
10+
Returns the CPU that's currently running the sketch (M7 or M4)
11+
Note that the sketch has to be uploaded to both cores.
1112
**/
1213
String currentCPU() {
1314
if (HAL_GetCurrentCPUID() == CM7_CPUID) {
@@ -18,7 +19,7 @@ String currentCPU() {
1819
}
1920

2021
/**
21-
* Adds two numbers and returns the sum
22+
Adds two numbers and returns the sum
2223
**/
2324
int addOnM7(int a, int b) {
2425
Serial.println(currentCPU() + ": executing add with " + String(a) + " and " + String(b));
@@ -27,7 +28,7 @@ int addOnM7(int a, int b) {
2728
}
2829

2930
/**
30-
* Subtracts two numbers and returns the difference
31+
Subtracts two numbers and returns the difference
3132
**/
3233
int subtractOnM7(int a, int b) {
3334
Serial.println(currentCPU() + ": executing subtract with " + String(a) + " and " + String(b));
@@ -40,11 +41,11 @@ void callSubstractFromM4() {
4041
delay(700); // Wait 700ms with the next calculation
4142
int a = random(100); // Generate a random number
4243
int b = random(100); // Generate a random number
43-
RPC.println(currentCPU() + ": calling subtract with " + String(a) + " and " + String(b));
44-
44+
SerialRPC.println(currentCPU() + ": calling subtract with " + String(a) + " and " + String(b));
45+
4546
auto result = RPC.call("remoteSubtract", a, b).as<int>();
4647
// Prints the result of the calculation
47-
RPC.println(currentCPU() + ": Result is " + String(a) + " - " + String(b) + " = " + String(result));
48+
SerialRPC.println(currentCPU() + ": Result is " + String(a) + " - " + String(b) + " = " + String(result));
4849
}
4950
}
5051

@@ -53,18 +54,17 @@ void setup() {
5354
pinMode(LED_BUILTIN, OUTPUT);
5455

5556
// Initialize RPC library; this also boots the M4 core
56-
RPC.begin();
5757
Serial.begin(115200);
58-
//while (!Serial) {} // Uncomment this to wait until the Serial connection is ready
59-
60-
// Both CPUs will execute this instruction, just at different times
61-
randomSeed(analogRead(A0)); // Initializes the pseudo-random number generator
58+
while (!Serial) {} // Uncomment this to wait until the Serial connection is ready
59+
if (!SerialRPC.begin()) {
60+
Serial.println("RPC initialization fail");
61+
}
6262

6363
if (currentCPU() == "M7") {
6464
// M7 CPU becomes the server, so it makes two functions available under the defined names
6565
RPC.bind("remoteAdd", addOnM7);
6666
RPC.bind("remoteSubtract", subtractOnM7);
67-
}
67+
}
6868

6969
if (currentCPU() == "M4") {
7070
// M4 CPU becomes the client, so spawns a thread that will call subtractOnM7() every 700ms
@@ -75,7 +75,7 @@ void setup() {
7575
void loop() {
7676

7777
if (currentCPU() == "M4") {
78-
// On M4 let's blink an LED. While it's blinking, the callSubstractFromM4() thread is running,
78+
// On M4 let's blink an LED. While it's blinking, the callSubstractFromM4() thread is running,
7979
// so it will execute roughly 3 times (2000 / 700 ms)
8080
digitalWrite(LED_BUILTIN, LOW);
8181
delay(1000);
@@ -84,27 +84,26 @@ void loop() {
8484

8585
int a = random(100);
8686
int b = random(100);
87-
// PRC.print works like a Serial port, but it needs a receiver (in this case the M7)
87+
// SerialRPC.print works like a Serial port, but it needs a receiver (in this case the M7)
8888
// to actually print the strings to the Serial port
89-
RPC.println(currentCPU() + ": calling add with " + String(a) + " and " + String(b));
89+
SerialRPC.println(currentCPU() + ": calling add with " + String(a) + " and " + String(b));
9090
// Let's invoke addOnM7() and wait for a result.
9191
// This will be delayed by the forced delay() in addOnM7() function
9292
// Exercise: if you are not interested in the result of the operation, what operation would you invoke?
93-
auto result = RPC.call("remoteAdd", a, b).as<int>();
94-
RPC.println(currentCPU() + ": Result is " + String(a) + " + " + String(b) + " = " + String(result));
93+
auto result = RPC.call("remoteAdd", a, b).as<int>();
94+
SerialRPC.println(currentCPU() + ": Result is " + String(a) + " + " + String(b) + " = " + String(result));
9595
}
96-
96+
9797
if (currentCPU() == "M7") {
98-
// On M7, let's print everything that is received over the RPC1 stream interface
98+
// On M7, let's print everything that is received over the SerialRPC stream interface
9999
// Buffer it, otherwise all characters will be interleaved by other prints
100100
String buffer = "";
101-
while (RPC.available()) {
102-
buffer += (char)RPC.read(); // Fill the buffer with characters
101+
while (SerialRPC.available()) {
102+
buffer += (char)SerialRPC.read(); // Fill the buffer with characters
103103
}
104104

105105
if (buffer.length() > 0) {
106106
Serial.print(buffer);
107107
}
108108
}
109-
110109
}

0 commit comments

Comments
 (0)