forked from triffid/FiveD_on_Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsersendf.c
130 lines (123 loc) · 2.26 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
#include "sersendf.h"
#include <stdarg.h>
#ifndef SIMULATION
#include <avr/pgmspace.h>
#endif
#include "serial.h"
#include "sermsg.h"
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;
case 'u':
if (j == 4)
serwrite_uint32(va_arg(args, uint32_t));
else
serwrite_uint16(va_arg(args, unsigned int));
j = 0;
break;
case 'd':
if (j == 4)
serwrite_int32(va_arg(args, int32_t));
else
serwrite_int16(va_arg(args, int));
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, unsigned int));
j = 0;
break;
case 'c':
serial_writechar(va_arg(args, unsigned int));
j = 0;
break;
case 's':
serial_writestr(va_arg(args, uint8_t *));
j = 0;
break;
default:
j = 0;
break;
}
}
else {
if (c == '%') {
j = 2;
}
else {
serial_writechar(c);
}
}
}
va_end(args);
}
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, unsigned int));
j = 0;
break;
case 'd':
if (j == 4)
serwrite_int32(va_arg(args, int32_t));
else
serwrite_int16(va_arg(args, int));
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 'c':
serial_writechar(va_arg(args, uint16_t));
case 'p':
serwrite_hex16(va_arg(args, uint16_t));
default:
j = 0;
break;*/
}
}
else {
if (c == '%') {
j = 2;
}
else {
serial_writechar(c);
}
}
}
va_end(args);
}