-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ino
64 lines (52 loc) · 1.53 KB
/
utils.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
// 16 bit data access (little endian / big endian)
void set16BitLE(unsigned char *pData, UINT16 value) {
*(pData+0) = value & 0xFF;
*(pData+1) = (value >> 8) & 0xFF;
}
UINT16 get16BitLE(unsigned char *pData) {
UINT16 value = 0;
value = *(pData+0);
value += (*(pData+1) << 8) & 0xFF00;
return value;
}
void set16BitBE(unsigned char *pData, UINT16 value) {
*(pData+0) = (value >> 8) & 0xFF;
*(pData+1) = value & 0xFF;
}
UINT16 get16BitBE(unsigned char *pData) {
UINT16 value = 0;
value = (*(pData+0) << 8) & 0xFF00;
value += *(pData+1);
return value;
}
// 32 bit data access (little endian / big endian)
void set32BitLE(unsigned char *pData, UINT32 value) {
*(pData+0) = value & 0xFF;
*(pData+1) = (value >> 8) & 0xFF;
*(pData+2) = (value >> 16) & 0xFF;
*(pData+3) = (value >> 24) & 0xFF;
}
UINT32 get32BitLE(unsigned char *pData) {
UINT32 value = 0;
value = *(pData+0);
value += *(pData+1) << 8;
value += *(pData+2) << 16;
value += *(pData+3) << 24;
return value;
}
void set32BitBE(unsigned char *pData, UINT32 value) {
*(pData+0) = (value >> 24) & 0xFF;
*(pData+1) = (value >> 16) & 0xFF;
*(pData+2) = (value >> 8) & 0xFF;
*(pData+3) = value & 0xFF;
}
// Checksum calculation
unsigned short calcCheckSum(unsigned char Message[], int nBytes) {
unsigned short sum = 0;
while(nBytes-- > 0) {
sum += *(Message++);
}
sum = (~sum);
sum++;
return sum;
}