Skip to content

Commit

Permalink
Make RPC example easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Dec 15, 2020
1 parent 3c175d7 commit fbec775
Showing 1 changed file with 91 additions and 39 deletions.
130 changes: 91 additions & 39 deletions libraries/RPC/examples/RPC_m4/RPC_m4.ino
Original file line number Diff line number Diff line change
@@ -1,58 +1,110 @@
#include "Arduino.h"
#include "RPC_internal.h"

#ifdef CORE_CM7
int add(int a, int b) {
printf("calling add on M7\n");
delay(1000);
using namespace rtos;

Thread subtractThread;

/**
* Returns the CPU that's currently running the sketch (M7 or M4)
* Note that the sketch has to be uploaded to both cores.
**/
String currentCPU() {
if (HAL_GetCurrentCPUID() == CM7_CPUID) {
return "M7";
} else {
return "M4";
}
}

/**
* Adds two numbers and returns the sum
**/
int addOnM7(int a, int b) {
Serial.println(currentCPU() + ": executing add with " + String(a) + " and " + String(b));
delay(700); // Simulate work
return a + b;
}
int subtract(int a, int b) {
printf("calling subtract on M7\n");

/**
* Subtracts two numbers and returns the difference
**/
int subtractOnM7(int a, int b) {
Serial.println(currentCPU() + ": executing subtract with " + String(a) + " and " + String(b));
delay(700); // Simulate work
return a - b;
}
#endif

rtos::Thread t;

void call_substract() {
while (1) {
delay(700);
RPC1.print("Calling subtract ");
auto res = RPC1.call("sub", 12, 45).as<int>();;
RPC1.println(res);
void callSubstractFromM4() {
while (true) {
delay(700); // Wait 700ms with the next calculation
int a = random(100); // Generate a random number
int b = random(100); // Generate a random number
RPC1.println(currentCPU() + ": calling subtract with " + String(a) + " and " + String(b));

auto result = RPC1.call("remoteSubtract", a, b).as<int>();
// Prints the result of the calculation
RPC1.println(currentCPU() + ": Result is " + String(a) + " - " + String(b) + " = " + String(result));
}
}

void setup() {
// put your setup code here, to run once:

pinMode(LED_BUILTIN, OUTPUT);

// Initialize RPC library; this also boots the M4 core
RPC1.begin();
Serial.begin(115200);
//while (!Serial) {}
//Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
#ifdef CORE_CM7
RPC1.bind("add", add);
RPC1.bind("sub", subtract);
#else
t.start(call_substract);
#endif
//while (!Serial) {} // Uncomment this to wait until the Serial connection is ready

// Both CPUs will execute this instruction, just at different times
randomSeed(analogRead(A0)); // Initializes the pseudo-random number generator

if (currentCPU() == "M7") {
// M7 CPU becomes the server, so it makes two functions available under the defined names
RPC1.bind("remoteAdd", addOnM7);
RPC1.bind("remoteSubtract", subtractOnM7);
}

if (currentCPU() == "M4") {
// M4 CPU becomes the client, so spawns a thread that will call subtractOnM7() every 700ms
subtractThread.start(callSubstractFromM4);
}
}

void loop() {
// put your main code here, to run repeatedly:
#ifndef CORE_CM7
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);

RPC1.print("Calling add ");
auto res = RPC1.call("add", 12, 45).as<int>();;
RPC1.println(res);
#else
while (RPC1.available()) {
Serial.write(RPC1.read());

if (currentCPU() == "M4") {
// On M4 let's blink an LED. While it's blinking, the callSubstractFromM4() thread is running,
// so it will execute roughly 3 times (2000 / 700 ms)
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);

int a = random(100);
int b = random(100);
// PRC.print works like a Serial port, but it needs a receiver (in this case the M7)
// to actually print the strings to the Serial port
RPC1.println(currentCPU() + ": calling add with " + String(a) + " and " + String(b));
// Let's invoke addOnM7() and wait for a result.
// This will be delayed by the forced delay() in addOnM7() function
// Exercise: if you are not interested in the result of the operation, what operation would you invoke?
auto result = RPC1.call("remoteAdd", a, b).as<int>();
RPC1.println(currentCPU() + ": Result is " + String(a) + " + " + String(b) + " = " + String(result));
}
#endif

if (currentCPU() == "M7") {
// On M7, let's print everything that is received over the RPC1 stream interface
// Buffer it, otherwise all characters will be interleaved by other prints
String buffer = "";
while (RPC1.available()) {
buffer += (char)RPC1.read(); // Fill the buffer with characters
}

if (buffer.length() > 0) {
Serial.print(buffer);
}
}

}

0 comments on commit fbec775

Please sign in to comment.