Skip to content

Commit 2232f88

Browse files
authored
Merge pull request #6 from bcmi-labs/zephyr_threading
Zephyr threading
2 parents 83386d5 + f57682e commit 2232f88

File tree

9 files changed

+692
-56
lines changed

9 files changed

+692
-56
lines changed

LICENSE

Lines changed: 373 additions & 21 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
In this repo it will be implemented an Arduino library wrapper for RPClite to be run on Arduino UNO Q boards.
22

3-
The desired API is shown in https://github.com/bcmi-labs/Arduino_RouterBridge/blob/main/desired.ino.
4-
5-
This is WIP. Expects changes soon.
6-
73
## The Bridge object ##
84

95
Including Arduino_RouterBridge.h gives the user access to a Bridge object that can be used both as a RPC client and/or server to execute and serve RPCs to/from the CPU Host running a GOLANG router.
@@ -16,6 +12,8 @@ Including Arduino_RouterBridge.h gives the user access to a Bridge object that c
1612

1713

1814
```cpp
15+
#include <Arduino_RouterBridge.h>
16+
1917
bool set_led(bool state) {
2018
digitalWrite(LED_BUILTIN, state);
2119
return state;
@@ -50,7 +48,5 @@ void loop() {
5048
};
5149

5250
Bridge.notify("signal", 200);
53-
54-
//Bridge.update(); // Thread-unsafe update execution is granted in its own thread. It can be called manually with caution
5551
}
5652
```

examples/monitor/monitor.ino

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
This file is part of the Arduino_RouterBridge library.
3+
4+
Copyright (c) 2025 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include <Arduino_RouterBridge.h>
13+
14+
15+
bool set_led(bool state) {
16+
digitalWrite(LED_BUILTIN, state);
17+
return state;
18+
}
19+
20+
int add(int a, int b) {
21+
return a + b;
22+
}
23+
24+
String greet() {
25+
return String("Hello Friend");
26+
}
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
31+
if (!Bridge.begin()) {
32+
Serial.println("cannot setup Bridge");
33+
}
34+
35+
if(!Monitor.begin()){
36+
Serial.println("cannot setup Monitor");
37+
}
38+
39+
pinMode(LED_BUILTIN, OUTPUT);
40+
41+
if (!Bridge.provide("set_led", set_led)) {
42+
Serial.println("Error providing method: set_led");
43+
} else {
44+
Serial.println("Registered method: set_led");
45+
}
46+
47+
Bridge.provide("add", add);
48+
Bridge.provide("greet", greet);
49+
50+
}
51+
52+
void loop() {
53+
54+
Bridge.notify("signal", 200);
55+
56+
Monitor.println("DEBUG: a debug message");
57+
58+
if (Monitor.available()) {
59+
String input = Monitor.readStringUntil('\n'); // Read until newline
60+
Monitor.print("You entered: ");
61+
Monitor.println(input);
62+
}
63+
64+
delay(500);
65+
}

examples/simple_bridge/simple_bridge.ino

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
/*
2+
This file is part of the Arduino_RouterBridge library.
3+
4+
Copyright (c) 2025 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
112
#include <Arduino_RouterBridge.h>
213

3-
//BridgeClass Bridge(Serial1);
414

515
bool set_led(bool state) {
616
digitalWrite(LED_BUILTIN, state);
@@ -44,6 +54,4 @@ void loop() {
4454
};
4555

4656
Bridge.notify("signal", 200);
47-
48-
//Bridge.update(); // Thread-unsafe update execution is granted in its own thread. It can be called manually with caution
4957
}

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"url": "https://github.com/bcmi-labs/Arduino_RouterBridge",
1212
"maintainer": true
1313
},
14-
"version": "0.1.0",
15-
"license": "MIT",
14+
"version": "0.1.2",
15+
"license": "MPL2.0",
1616
"frameworks": "arduino",
1717
"platforms": "*",
1818
"dependencies":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Arduino_RouterBridge
2-
version=0.1.0
2+
version=0.1.2
33
author=BCMI-labs
44
maintainer=BCMI-labs
55
sentence=A RPC bridge for Arduino UNO Q boards

src/Arduino_RouterBridge.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
This file is part of the Arduino_RouterBridge library.
3+
4+
Copyright (c) 2025 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
112
#ifndef ARDUINO_ROUTER_BRIDGE_H
213
#define ARDUINO_ROUTER_BRIDGE_H
314

src/bridge.h

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
This file is part of the Arduino_RouterBridge library.
3+
4+
Copyright (c) 2025 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
112
#pragma once
213

314
#ifndef ROUTER_BRIDGE_H
@@ -81,41 +92,67 @@ class BridgeClass {
8192

8293
void update() {
8394

84-
k_msleep(1);
8595
// Lock read mutex
86-
k_mutex_lock(&read_mutex, K_FOREVER);
87-
if (!server->get_rpc()) {
96+
if (k_mutex_lock(&read_mutex, K_MSEC(10)) != 0 ) return;
97+
98+
RPCRequest<> req;
99+
if (!server->get_rpc(req)) {
88100
k_mutex_unlock(&read_mutex);
101+
k_msleep(1);
89102
return;
90103
}
104+
91105
k_mutex_unlock(&read_mutex);
92106

93-
server->process_request();
107+
server->process_request(req);
94108

95109
// Lock write mutex
96-
k_mutex_lock(&write_mutex, K_FOREVER);
97-
server->send_response();
98-
k_mutex_unlock(&write_mutex);
110+
while (true) {
111+
112+
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
113+
server->send_response(req);
114+
k_mutex_unlock(&write_mutex);
115+
k_msleep(1);
116+
break;
117+
} else {
118+
k_msleep(1);
119+
}
120+
121+
}
99122

100123
}
101124

102125
template<typename RType, typename... Args>
103126
bool call(const MsgPack::str_t method, RType& result, Args&&... args) {
104127

128+
uint32_t msg_id_wait;
129+
105130
// Lock write mutex
106-
k_mutex_lock(&write_mutex, K_FOREVER);
107-
client->send_rpc(method, std::forward<Args>(args)...);
108-
k_mutex_unlock(&write_mutex);
131+
while (true) {
132+
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0) {
133+
client->send_rpc(method, msg_id_wait, std::forward<Args>(args)...);
134+
k_mutex_unlock(&write_mutex);
135+
k_msleep(1);
136+
break;
137+
} else {
138+
k_msleep(1);
139+
}
140+
}
109141

110142
// Lock read mutex
111-
while(1) {
112-
k_mutex_lock(&read_mutex, K_FOREVER);
113-
if (client->get_response(result)) {
143+
while(true) {
144+
if (k_mutex_lock(&read_mutex, K_MSEC(10)) == 0 ) {
145+
if (client->get_response(msg_id_wait, result)) {
146+
k_mutex_unlock(&read_mutex);
147+
k_msleep(1);
148+
break;
149+
}
114150
k_mutex_unlock(&read_mutex);
115-
break;
151+
k_msleep(1);
152+
} else {
153+
k_msleep(1);
116154
}
117-
k_mutex_unlock(&read_mutex);
118-
k_msleep(1);
155+
119156
}
120157

121158
return (client->lastError.code == NO_ERR);
@@ -140,21 +177,33 @@ class BridgeClass {
140177
void update_safe() {
141178

142179
// Lock read mutex
143-
k_msleep(1);
144-
k_mutex_lock(&read_mutex, K_FOREVER);
145-
if (!server->get_rpc()) {
180+
if (k_mutex_lock(&read_mutex, K_MSEC(10)) != 0 ) return;
181+
182+
RPCRequest<> req;
183+
if (!server->get_rpc(req, "__safe__")) {
146184
k_mutex_unlock(&read_mutex);
185+
k_msleep(1);
147186
return;
148187
}
188+
149189
k_mutex_unlock(&read_mutex);
150190

151-
server->process_request("__safe__");
191+
server->process_request(req);
152192

153193
// Lock write mutex
154-
k_mutex_lock(&write_mutex, K_FOREVER);
155-
server->send_response();
156-
k_mutex_unlock(&write_mutex);
194+
while (true) {
195+
196+
if (k_mutex_lock(&write_mutex, K_MSEC(10)) == 0){
197+
server->send_response(req);
198+
k_mutex_unlock(&write_mutex);
199+
k_msleep(1);
200+
break;
201+
} else {
202+
k_msleep(1);
203+
}
157204

205+
}
206+
158207
}
159208

160209
friend class BridgeClassUpdater;
@@ -176,6 +225,7 @@ BridgeClass Bridge(Serial1);
176225
void updateEntryPoint(void *, void *, void *){
177226
while(1){
178227
Bridge.update();
228+
k_msleep(1);
179229
}
180230
}
181231

@@ -184,6 +234,7 @@ static void safeUpdate(){
184234
}
185235

186236
void __loopHook(){
237+
k_msleep(1);
187238
safeUpdate();
188239
}
189240

0 commit comments

Comments
 (0)