-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ino
180 lines (149 loc) · 3.56 KB
/
util.ino
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "util.h"
util::util(void) {
}
util::~util(void) {
}
void util::hexdump(unsigned char *buf, unsigned int len, unsigned int dump_width) {
#ifdef DEBUG
#ifdef DESKTOP
unsigned char *p;
unsigned char *q;
unsigned char *e = buf + len;
unsigned char i;
struct timeval tp;
if (gettimeofday(&tp, NULL) != -1) {
printf("%s", ctime(&tp.tv_sec));
}
for (p = q = buf; p <= e; ) {
if (p == e || (p - q) == (int)dump_width) {
for (i = 0; i < dump_width - (p - q); i++) {
printf(" ");
}
printf(": ");
for (; q < p; q++) {
printf("%c", (*q >= 0x20 && *q <= 0x7e) ? *q : '.');
}
printf("\n");
q = p;
if (p == e) {
break;
}
} else {
printf("%02x ", *p);
p++;
}
}
printf("\n");
fflush(stdout);
#endif /* DESKTOP */
#endif /* DEBUG */
}
void util::perror(const char *str) {
#ifdef DEBUG
#ifdef DESKTOP
::perror(str);
#endif /* DESKTOP */
#endif /* DEBUG */
}
void util::msg(const char *fmt, ...) {
#ifdef DEBUG
#ifdef DESKTOP
char buffer[512] = { 0 };
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, 512, fmt, ap);
va_end(ap);
printf("%s", buffer);
fflush(stdout);
#endif /* DESKTOP */
#endif /* DEBUG */
}
void util::die(const char *str) {
#ifdef DEBUG
if (str) {
perror(str);
}
#endif
#ifdef DESKTOP
exit(1);
#else
for (;;) {
util::blink(10, 200, 200);
}
#endif
}
void util::blink(int iterations, int msec_on, int msec_off, int led_pin) {
#ifdef DESKTOP
char spinner[] = "\\|-/";
static char *c = spinner;
unsigned long timeout = util::millis() + iterations * (msec_on + msec_off);
while (util::millis() < timeout) {
util::msg("%c\r", *c++);
util::delay(msec_on);
if (!*c) {
c = spinner;
}
util::msg(" \r");
util::delay(msec_off);
}
#else
digitalWrite(led_pin, LOW);
while (iterations--) {
digitalWrite(led_pin, HIGH);
util::delay(msec_on);
digitalWrite(led_pin, LOW);
util::delay(msec_off);
}
digitalWrite(led_pin, LOW);
#endif
}
void util::delay(int msec) {
#ifdef DESKTOP
struct timeval tv = { 0 };
tv.tv_usec = msec * 1000;
select(0, NULL, NULL, NULL, &tv);
#else
::delay(msec);
#endif
}
/*
* XXX Uh, does Arduino have a strncmp?
*/
int util::strncmp(const char *s1, const char *s2, int len) {
while (*s1 == *s2 && *s1 != 0 && *s2 != 0 && --len) {
s1++;
s2++;
}
return len == 0 && *s1 == *s2 ? 0 : /* XXX bogus */
*s1 == 0 && *s2 == 0 ? 0 :
*s1 == 0 ? -1 :
1;
}
unsigned long util::millis(void) {
#ifdef DESKTOP
static struct timeval start = { 0 };
struct timeval now = { 0 };
if (start.tv_sec == 0 && start.tv_usec == 0) {
gettimeofday(&start, NULL);
}
gettimeofday(&now, NULL);
return ((now.tv_sec - start.tv_sec) * 1000 * 1000 +
(now.tv_usec - start.tv_usec)) / 1000;
#else
return ::millis();
#endif
}
#ifdef UNITTEST
int main(void) {
unsigned char buf[256];
unsigned int i;
util::msg("testing %s %d %f\n", "foobar", 1, 12.34);
for (i = 0; i < 256; i++) {
buf[i] = i;
}
util::hexdump(buf, 256);
util::msg("%d\n", util::strncmp("foo1", "foo2")); /* 1 */
util::msg("%d\n", util::strncmp("foo1", "foo2", 2)); /* 0 */
util::msg("%d\n", util::strncmp("foo1", "foo2", 4)); /* 1 */
}
#endif /* MAIN */