File tree Expand file tree Collapse file tree 5 files changed +79
-1
lines changed
Expand file tree Collapse file tree 5 files changed +79
-1
lines changed Original file line number Diff line number Diff line change 1- #include < RpcLite .h>
1+ #include < RPClite .h>
22#include < HardwareSerial.h>
33
44HardwareSerial* uart = new HardwareSerial(0 );
Original file line number Diff line number Diff line change 1+ #include < RPClite.h>
2+
3+ int add (int x, int y) {
4+ return x + y;
5+ }
6+
7+ void say_hello () {
8+ Serial.println (" Hello!" );
9+ }
10+
11+ auto wrapped_add = wrap(add);
12+ auto wrapped_hello = wrap(say_hello);
13+
14+ void blink_before (){
15+ digitalWrite (LED_BUILTIN, HIGH);
16+ delay (200 );
17+ digitalWrite (LED_BUILTIN, LOW);
18+ delay (200 );
19+ digitalWrite (LED_BUILTIN, HIGH);
20+ delay (200 );
21+ digitalWrite (LED_BUILTIN, LOW);
22+ delay (200 );
23+ digitalWrite (LED_BUILTIN, HIGH);
24+ delay (200 );
25+ digitalWrite (LED_BUILTIN, LOW);
26+ delay (200 );
27+ }
28+
29+ void setup () {
30+ Serial.begin (9600 );
31+ }
32+
33+ void loop () {
34+ blink_before ();
35+ wrapped_add (5 , 3 );
36+ wrapped_hello ();
37+ delay (1000 );
38+ }
Original file line number Diff line number Diff line change 1111#include "transport.h"
1212#include "client.h"
1313#include "server.h"
14+ #include "wrapper.h"
1415
1516#include "DummyTransport.h"
1617#include "SerialTransport.h"
Original file line number Diff line number Diff line change 66#define RPCLITE_SERVER_H
77
88#include " rpc.h"
9+ #include " error.h"
910
1011class RPCServer {
1112 ITransport& transport;
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < functional>
3+ #include < tuple>
4+
5+ // --- FunctionWrapper definition ---
6+ template <typename R, typename ... Args>
7+ class FunctionWrapper {
8+ public:
9+ using FuncType = R (*)(Args...);
10+
11+ FunctionWrapper (FuncType f) : func(f) {}
12+
13+ R operator ()(Args... args) {
14+ Serial.println (" Calling function with args..." );
15+
16+ if constexpr (std::is_void<R>::value) {
17+ func (args...);
18+ Serial.println (" Return: void" );
19+ return ;
20+ } else {
21+ R result = func (args...);
22+ Serial.print (" Result: " );
23+ Serial.print (result);
24+ Serial.println ();
25+ return result;
26+ }
27+
28+ }
29+
30+ private:
31+ FuncType func;
32+ };
33+
34+ // --- Wraps function function pointers only. Lambdas and member functions not currently supported ---
35+ template <typename R, typename ... Args>
36+ FunctionWrapper<R, Args...> wrap (R (*f)(Args...)) {
37+ return FunctionWrapper<R, Args...>(f);
38+ }
You can’t perform that action at this time.
0 commit comments