Skip to content

Commit 448455c

Browse files
committed
allow console output via custom implementation
1 parent f905484 commit 448455c

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/ArduinoOcpp/Debug.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@
5656
#endif
5757

5858
#ifdef AO_TRAFFIC_OUT
59-
#define AO_DBG_TRAFFIC_OUT(...) AO_CONSOLE_PRINTF("[AO] To WS lib: %s\n",__VA_ARGS__)
60-
#define AO_DBG_TRAFFIC_IN(...) AO_CONSOLE_PRINTF("[AO] From WS lib: %s\n",__VA_ARGS__)
59+
60+
#define AO_DBG_TRAFFIC_OUT(...) \
61+
do { \
62+
AO_CONSOLE_PRINTF("[AO] To WS lib: %s",__VA_ARGS__); \
63+
AO_CONSOLE_PRINTF("\n"); \
64+
} while (0)
65+
66+
#define AO_DBG_TRAFFIC_IN(...) \
67+
do { \
68+
AO_CONSOLE_PRINTF("[AO] From WS lib: %s",__VA_ARGS__); \
69+
AO_CONSOLE_PRINTF("\n"); \
70+
} while (0)
71+
6172
#else
6273
#define AO_DBG_TRAFFIC_OUT(...)
6374
#define AO_DBG_TRAFFIC_IN(...)

src/ArduinoOcpp/Platform.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// matth-x/ArduinoOcpp
2+
// Copyright Matthias Akstaller 2019 - 2022
3+
// MIT License
4+
5+
#include <ArduinoOcpp/Platform.h>
6+
7+
#ifdef AO_CUSTOM_CONSOLE
8+
9+
namespace ArduinoOcpp {
10+
void (*ao_console_out_impl)(const char *msg) = nullptr;
11+
}
12+
13+
void ArduinoOcpp::ao_console_out(const char *msg) {
14+
if (ao_console_out_impl) {
15+
ao_console_out_impl(msg);
16+
}
17+
}
18+
19+
void ao_set_console_out(void (*console_out)(const char *msg)) {
20+
ArduinoOcpp::ao_console_out_impl = console_out;
21+
console_out("[AO] console initialized\n");
22+
}
23+
24+
#endif

src/ArduinoOcpp/Platform.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
#ifndef AO_PLATFORM_H
66
#define AO_PLATFORM_H
77

8+
#ifdef AO_CUSTOM_CONSOLE
9+
10+
#ifndef AO_CUSTOM_CONSOLE_MAXMSGSIZE
11+
#define AO_CUSTOM_CONSOLE_MAXMSGSIZE 128
12+
#endif
13+
14+
void ao_set_console_out(void (*console_out)(const char *msg));
15+
16+
namespace ArduinoOcpp {
17+
void ao_console_out(const char *msg);
18+
}
19+
#define AO_CONSOLE_PRINTF(X, ...) \
20+
do { \
21+
char msg [AO_CUSTOM_CONSOLE_MAXMSGSIZE]; \
22+
snprintf(msg, AO_CUSTOM_CONSOLE_MAXMSGSIZE, X, ##__VA_ARGS__); \
23+
sprintf(msg + AO_CUSTOM_CONSOLE_MAXMSGSIZE - 7, " [...]"); \
24+
ao_console_out(msg); \
25+
} while (0)
26+
#else
27+
#define ao_set_console_out(X) \
28+
do { \
29+
X("[AO] CONSOLE ERROR: ao_set_console_out ignored if AO_CUSTOM_CONSOLE " \
30+
"not defined\n"); \
31+
char msg [100]; \
32+
snprintf(msg, 100, " > see %s:%i",__FILE__,__LINE__); \
33+
X(msg); \
34+
X("\n > see ArduinoOcpp/Platform.h\n"); \
35+
} while (0)
36+
#endif
37+
838
#ifndef AO_CONSOLE_PRINTF
939
//begin with Arduino support, add more later
1040
#include <Arduino.h>

0 commit comments

Comments
 (0)