-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ino
More file actions
134 lines (123 loc) · 4.16 KB
/
utils.ino
File metadata and controls
134 lines (123 loc) · 4.16 KB
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
//============================================================================
// This file contains some general useful functions
//============================================================================
//============================================================================
// Sends output to the serial console with 1ms delay between characters
//============================================================================
template <typename T>
void toSerialConsole(T data) {
#if defined(ALLOWDEBUG)
// this will work for int, float, double, char, etc.
String Str = String(data);
// Send the String one char at the time
for (unsigned int i = 0; i < Str.length(); i++) {
Serial.write(Str[i]);
delay(1); // 1ms delay for esp32s2 and c3 mcu with native USB comms.
}
Serial.flush();
#endif
}
// version for floats where you can specify the number of decimals
void toSerialConsole(double data, int decimals) {
#if defined(ALLOWDEBUG)
String Str = String(data, decimals);
for (unsigned int i = 0; i < Str.length(); i++) {
Serial.write(Str[i]);
delay(1);
}
Serial.flush();
#endif
}
//============================================================================
// Calculate CRC to be used in the TX line
//============================================================================
uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) {
unsigned int i;
crc = crc ^ ((uint16_t)data << 8);
for (i = 0; i < 8; i++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc <<= 1;
}
}
return crc;
}
//============================================================================
// Fast CRC16 code. Used in the Horus binary packets.
//============================================================================
unsigned int crc16(unsigned char *string, unsigned int len) {
unsigned int i;
unsigned int crc;
crc = 0xFFFF; // Standard CCITT seed for CRC16.
// Calculate the sum, ignore $ sign's
for (i = 0; i < len; i++) {
crc = _crc_xmodem_update(crc, (uint8_t)string[i]);
}
return crc;
}
//============================================================================
// Print characters in Hex format
//============================================================================
void PrintHex(uint8_t *data, uint8_t length, char *tmp) {
// Print char data as hex
byte first;
int j = 0;
for (uint8_t i = 0; i < length; i++) {
first = ((uint8_t)data[i] >> 4) | 48;
if (first > 57) tmp[j] = first + (byte)39;
else tmp[j] = first;
j++;
first = ((uint8_t)data[i] & 0x0F) | 48;
if (first > 57) tmp[j] = first + (byte)39;
else tmp[j] = first;
j++;
}
tmp[length * 2] = 0;
}
//============================================================================
// Convert a float to a 8 bit two's complement number
// aNumber should be between -127 and 127
//============================================================================
int8_t Decto2compl(float aNumber) {
// Get a rounded integer
int8_t dec = round(aNumber);
if (dec >= -128 && dec <= 127) {
if (dec >= 0) // for positive number twocomplement is the same
{
toSerialConsole("Temperatuur in 2-comp positief: ");
toSerialConsole((int8_t)dec);toSerialConsole("\n");
return dec;
} else {
// take abs value
int8_t absvalue = -dec;
// Invert the bits
int8_t oneComplement = ~dec;
// Add 1
int8_t twoComplement = oneComplement + 1;
// return the value
toSerialConsole("Temperatuur in 2-comp negatief: ");
toSerialConsole((int8_t)twoComplement);toSerialConsole("\n");
return (int8_t)twoComplement;
}
} else {
// error
return 0;
}
}
//============================================================================
// Evaluate the results of a call to the Radiolib library
// If the call does not succeed, wait 60 seconds, reset the esp32 and try again.
//============================================================================
void Radiolib_assert(int16_t lState) {
if (lState >= RADIOLIB_ERR_NONE) // Change this to (state == ERR_NONE) if you use an older radiolib library
{
toSerialConsole("done\n");
} else
{
toSerialConsole("failed, code ");
toSerialConsole(lState); toSerialConsole("\n");
delay(60000);
ESP.restart();
}
}