Skip to content

Commit b92c319

Browse files
committed
feat: basic bridge with example
1 parent 797a292 commit b92c319

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <Arduino_BridgeImola.h>
2+
3+
Bridge bridge(Serial1);
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
while (!Serial);
8+
9+
Serial1.begin(115200);
10+
while (!Serial1);
11+
12+
pinMode(LED_BUILTIN, OUTPUT);
13+
14+
bridge.begin();
15+
16+
if (!bridge.provide("set_led", set_led)) {
17+
Serial.println("Error providing method: set_led");
18+
};
19+
20+
bridge.provide("add", add);
21+
22+
bridge.provide("greet", greet);
23+
24+
}
25+
26+
bool set_led(bool state) {
27+
digitalWrite(LED_BUILTIN, state);
28+
return state;
29+
}
30+
31+
int add(int a, int b) {
32+
return a + b;
33+
}
34+
35+
String greet() {
36+
return String("Hello Friend");
37+
}
38+
39+
void loop() {
40+
float res;
41+
if (!bridge.call("multiply", res, 1.0, 2.0)) {
42+
Serial.println("Error calling method: multiply");
43+
Serial.println(bridge.get_error_code());
44+
Serial.println(bridge.get_error_message());
45+
};
46+
47+
bridge.notify("signal", 200);
48+
49+
bridge.update();
50+
}

src/Arduino_BridgeImola.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef ARDUINO_BRIDGE_IMOLA_H
2+
#define ARDUINO_BRIDGE_IMOLA_H
3+
4+
#include "Arduino.h"
5+
#include "bridge.h"
6+
7+
#endif //ARDUINO_BRIDGE_IMOLA_H

src/bridge.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#ifndef BRIDGE_IMOLA_H
4+
#define BRIDGE_IMOLA_H
5+
6+
#include <Arduino_RPClite.h>
7+
8+
9+
class Bridge: {
10+
11+
RPCClient* client = nullptr;
12+
RPCServer* server = nullptr;
13+
ITransport* transport;
14+
15+
public:
16+
Bridge(ITransport& t) : transport(&t) {}
17+
Bridge(Stream& stream) : {
18+
transport = new SerialTransport(stream);
19+
}
20+
21+
// Initialize the bridge
22+
void begin() {
23+
client = new RPCClient(*transport);
24+
server = new RPCServer(*transport);
25+
}
26+
27+
template<typename F>
28+
bool provide(const MsgPack::str_t& name, F&& func){
29+
return server->bind(name, func);
30+
}
31+
32+
void update() {
33+
server->run();
34+
}
35+
36+
template<typename RType, typename... Args>
37+
bool call(const MsgPack::str_t method, RType& result, Args&&... args) {
38+
return client->call(method, result, std::forward<Args>(args)...);
39+
}
40+
41+
template<typename... Args>
42+
void notify(const MsgPack::str_t method, Args&&... args) {
43+
client->notify(method, std::forward<Args>(args)...);
44+
}
45+
46+
String get_error_message() const {
47+
return client->lastError.traceback;
48+
}
49+
50+
uint8_t get_error_code() const {
51+
return (uint8_t) client->lastError.code;
52+
}
53+
54+
}
55+
56+
57+
#endif // BRIDGE_IMOLA_H

0 commit comments

Comments
 (0)