This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
/
utils.h
92 lines (81 loc) · 1.78 KB
/
utils.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
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
/** TRIGGER **/
byte trigger(int intval, int *lval)
{
if(*lval + intval < millis() || *lval > millis())
{
*lval = millis();
return 1;
}
return 0;
}
/** TRIGGER **/
/** StringIndex **/
String StringIndex(String data, String delim, int pos)
{
String buffer[20];
String tmp;
int len = data.length();
int index = 0;
/** TRIM **/
while(data.indexOf(delim + delim) > 0)
{
data.replace(delim+delim,delim);
}
/** TRIM **/
for(int i=0;i<len;i++)
{
tmp = data.substring(i,i+1);
if(tmp != " ")
{
buffer[index] += tmp;
}
else
{
index++;
}
}
return buffer[pos];
}
/** StringIndex **/
/** hexDump **/
void hexDump(uint8_t *data, int len)
{
int wrap = 0;
char buff[6];
for(int i=0;i<len;i++)
{
buff[0] = 0x00;
sprintf(buff, "[%02x] %c", data[i],data[i]);
Serial.print(buff);
wrap++;
if(wrap >= 6)
{
Serial.println("");
wrap = 0;
}
}
return;
}
/** hexDump **/
/** HEXLIB **/
inline byte hexToByte(char* hex)
{
byte high = hex[0];
byte low = hex[1];
if (high > '9'){
high -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
}
if (low > '9'){
low -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
}
return (((high & 0xF)<<4) | (low & 0xF));
}
inline unsigned int hexToInt(char* hex)
{
return ((hexToByte(hex) << 8) | hexToByte(hex+2));
}
inline unsigned long hexToLong(char* hex)
{
return ((((unsigned long)hexToInt(hex)) << 16) | hexToInt(hex+4));
}
/** HEXLIB **/