forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.cpp
More file actions
69 lines (54 loc) · 1.66 KB
/
Copy pathPlatform.cpp
File metadata and controls
69 lines (54 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2023
// MIT License
#include <MicroOcpp/Platform.h>
#ifdef MOCPP_CUSTOM_CONSOLE
namespace MicroOcpp {
void (*mocpp_console_out_impl)(const char *msg) = nullptr;
}
void MicroOcpp::mocpp_console_out(const char *msg) {
if (mocpp_console_out_impl) {
mocpp_console_out_impl(msg);
}
}
void mo_set_console_out(void (*console_out)(const char *msg)) {
MicroOcpp::mocpp_console_out_impl = console_out;
console_out("[OCPP] console initialized\n");
}
#endif
#ifdef MOCPP_CUSTOM_TIMER
unsigned long (*mocpp_tick_ms_impl)() = nullptr;
void mocpp_set_timer(unsigned long (*get_ms)()) {
mocpp_tick_ms_impl = get_ms;
}
unsigned long mocpp_tick_ms_custom() {
if (mocpp_tick_ms_impl) {
return mocpp_tick_ms_impl();
} else {
return 0;
}
}
#endif
#if MOCPP_PLATFORM == MOCPP_PLATFORM_UNIX
#include <chrono>
namespace MicroOcpp {
std::chrono::steady_clock::time_point clock_reference;
bool clock_initialized = false;
}
unsigned long mocpp_tick_ms_unix() {
if (!MicroOcpp::clock_initialized) {
MicroOcpp::clock_reference = std::chrono::steady_clock::now();
MicroOcpp::clock_initialized = true;
}
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - MicroOcpp::clock_reference);
return (unsigned long) ms.count();
}
#endif
#if MOCPP_PLATFORM != MOCPP_PLATFORM_ARDUINO
void dtostrf(float value, int min_width, int num_digits_after_decimal, char *target){
char fmt[20];
sprintf(fmt, "%%%d.%df", min_width, num_digits_after_decimal);
sprintf(target, fmt, value);
}
#endif