generated from pimoroni/pico-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
62 lines (48 loc) · 1.15 KB
/
util.h
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
#ifndef LEDCONTROL_UTIL_H
#define LEDCONTROL_UTIL_H
#include "button.hpp"
float wrap(float v, float min, float max) {
if(v <= min)
v += (max - min);
if(v > max)
v -= (max - min);
return v;
}
signed int limiting_wrap(signed int v, int min, int max) {
if(v < min)
v += (max - min);
if(v >= max)
v -= (max - min);
return v;
}
inline uint32_t millis() {
return to_ms_since_boot(get_absolute_time());
}
uint8_t wait_for_long_button(pimoroni::Button b, uint16_t long_duration) {
uint8_t cur_mode = 0;
uint32_t start = millis();
while (b.raw()) {
uint32_t diff = millis() - start;
if (cur_mode == 0 && diff < long_duration) {
cur_mode = 1;
} else if (cur_mode == 1 && diff >= long_duration) {
cur_mode = 2;
break;
}
sleep_ms(50);
}
return cur_mode;
}
void print_buf(const uint8_t *buf, size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%02x", buf[i]);
if (i % 16 == 15)
printf("\n");
else
printf(" ");
}
}
static inline bool strstarts(const char *str, const char *prefix) {
return strncmp(str, prefix, strlen(prefix)) == 0;
}
#endif //LEDCONTROL_UTIL_H