-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.h
57 lines (48 loc) · 1.18 KB
/
helper.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
#ifndef HELPER_H
#define HELPER_H
unsigned long getLong(byte p[]) {
unsigned long result = 0;
for (byte i = 0; i < 4; i++)
result = (result << 8) | p[i];
return result;
}
uint16_t getUInt(byte p[]) {
uint16_t result = 0;
for (byte i = 0; i < 2; i++)
result = (result << 8) | p[i];
return result;
}
String intPad(int value, uint8_t len) {
char format[5];
sprintf(format, "%%%dd", len);
char result[len + 1];
sprintf(result, format, value);
return String(result);
}
String strPad(String value, uint8_t len) {
char format[5];
sprintf(format, "%%%ds", len);
char result[len + 1];
sprintf(result, format, value.c_str());
return String(result);
}
double roundOff(double n) {
double d = n * 10.0;
int i = d + 0.5;
d = (float)i / 10.0;
return d;
}
String convertSize(unsigned long size_kb) {
static const char *SIZES[] = { "KB", "MB", "GB", "TB" };
int div = 0;
unsigned long rem = 0;
while (size_kb >= 1024 && div < (sizeof SIZES / sizeof * SIZES)) {
rem = (size_kb % 1024);
div++;
size_kb /= 1024;
}
double size_d = (float)size_kb + (float)rem / 1024.0;
String result = String(size_d, 1) + SIZES[div];
return result;
}
#endif