forked from Traumflug/Teacup_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sersendf.c
164 lines (151 loc) · 3.3 KB
/
sersendf.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "sersendf.h"
/** \file sersendf.c
\brief Simplified printf implementation
*/
#include <stdarg.h>
#include <avr/pgmspace.h>
#include "serial.h"
#include "sermsg.h"
/// for sending hex data
PGM_P str_ox = "0x";
// void sersendf(char *format, ...) {
// va_list args;
// va_start(args, format);
//
// uint16_t i = 0;
// uint8_t c, j = 0;
// while ((c = format[i++])) {
// if (j) {
// switch(c) {
// case 'l':
// j = 4;
// break;
// case 'u':
// if (j == 4)
// serwrite_uint32(va_arg(args, uint32_t));
// else
// serwrite_uint16(va_arg(args, uint16_t));
// j = 0;
// break;
// case 'd':
// if (j == 4)
// serwrite_int32(va_arg(args, int32_t));
// else
// serwrite_int16(va_arg(args, int16_t));
// j = 0;
// break;
// case 'p':
// case 'x':
// serial_writestr_P(str_ox);
// if (j == 4)
// serwrite_hex32(va_arg(args, uint32_t));
// else
// serwrite_hex16(va_arg(args, uint16_t));
// j = 0;
// break;
// case 'c':
// serial_writechar(va_arg(args, uint16_t));
// j = 0;
// break;
// case 's':
// serial_writestr(va_arg(args, uint8_t *));
// j = 0;
// break;
// default:
// serial_writechar(c);
// j = 0;
// break;
// }
// }
// else {
// if (c == '%') {
// j = 2;
// }
// else {
// serial_writechar(c);
// }
// }
// }
// va_end(args);
// }
/** \brief Simplified printf
\param format pointer to output format specifier string stored in FLASH.
\param ... output data
Implements only a tiny subset of printf's format specifiers :-
%[ls][udcx%]
l - following data is (32 bits)\n
s - following data is short (8 bits)\n
none - following data is 16 bits.
u - unsigned int\n
d - signed int\n
c - character\n
x - hex\n
% - send a literal % character
Example:
\code sersendf_P(PSTR("X:%ld Y:%ld temp:%u.%d flags:%sx Q%su/%su%c\n"), target.X, target.Y, current_temp >> 2, (current_temp & 3) * 25, dda.allflags, mb_head, mb_tail, (queue_full()?'F':(queue_empty()?'E':' '))) \endcode
*/
void sersendf_P(PGM_P format, ...) {
va_list args;
va_start(args, format);
uint16_t i = 0;
uint8_t c = 1, j = 0;
while ((c = pgm_read_byte(&format[i++]))) {
if (j) {
switch(c) {
case 's':
j = 1;
break;
case 'l':
j = 4;
break;
case 'u':
if (j == 4)
serwrite_uint32(va_arg(args, uint32_t));
else
serwrite_uint16(va_arg(args, uint16_t));
j = 0;
break;
case 'd':
if (j == 4)
serwrite_int32(va_arg(args, int32_t));
else
serwrite_int16(va_arg(args, int16_t));
j = 0;
break;
case 'c':
serial_writechar(va_arg(args, uint16_t));
j = 0;
break;
case 'x':
serial_writestr_P(str_ox);
if (j == 4)
serwrite_hex32(va_arg(args, uint32_t));
else if (j == 1)
serwrite_hex8(va_arg(args, uint16_t));
else
serwrite_hex16(va_arg(args, uint16_t));
j = 0;
break;
/* case 'p':
serwrite_hex16(va_arg(args, uint16_t));*/
case 'q':
serwrite_int32_vf(va_arg(args, int32_t), 3);
j = 0;
break;
default:
serial_writechar(c);
j = 0;
break;
}
}
else {
if (c == '%') {
j = 2;
}
else {
serial_writechar(c);
}
}
}
va_end(args);
}