-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRFT_debugger.hpp
62 lines (47 loc) · 1.53 KB
/
RFT_debugger.hpp
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
/*
Cross-platform serial debugging support for RFT
Provides printf() and related static methods for formatted printing of
debug messages. Your Board implementation should provide and outbuf(char
method that displays the message in an appropriate way.
Copyright (c) 2021 Simon D. Levy
MIT License
*/
#pragma once
#include <stdarg.h>
#include <stdio.h>
namespace rft {
class Debugger {
public:
static void printf(const char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char buf[200];
vsnprintf(buf, 200, fmt, ap);
outbuf(buf);
va_end(ap);
}
// for boards that do not support floating-point vnsprintf
static void printfloat(float val, uint8_t prec=3)
{
uint16_t mul = 1;
for (uint8_t k=0; k<prec; ++k) {
mul *= 10;
}
char sgn = '+';
if (val < 0) {
val = -val;
sgn = '-';
}
uint32_t bigval = (uint32_t)(val*mul);
Debugger::printf("%c%d.%d", sgn, bigval/mul, bigval % mul);
}
static void printlnfloat(float val, uint8_t prec=3)
{
printfloat(val, prec);
printf("\n");
}
protected:
static void outbuf(char * buf);
}; // class Debugger
} // namespace rft