-
Notifications
You must be signed in to change notification settings - Fork 17
/
common.cpp
executable file
·236 lines (171 loc) · 5.07 KB
/
common.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Header files
extern "C" {
#include <asf.h>
}
#include <ctype.h>
#include <float.h>
#include <string.h>
#include "common.h"
// Supporting function implementation
void ulltoa(uint64_t value, char *buffer) noexcept {
// Initialize variables
uint8_t i = INT_BUFFER_SIZE - sizeof('-') - 1;
// Add terminating character
buffer[i] = 0;
// Go through all digits
do {
// Set digit in buffer
buffer[--i] = value % 10 + 0x30;
value /= 10;
} while(value);
// Move string to the start of the buffer
memmove(buffer, &buffer[i], INT_BUFFER_SIZE - i - sizeof('-'));
}
void lltoa(int64_t value, char *buffer) noexcept {
// Convert value to string
if(value < 0) {
buffer[0] = '-';
ulltoa(value * -1, buffer + sizeof('-'));
}
else
ulltoa(value, buffer);
}
void ftoa(float value, char *buffer) noexcept {
// Initialize variables
uint8_t i = FLOAT_BUFFER_SIZE - 1;
bool negative = value < 0;
// Add terminating character
buffer[i] = 0;
// Add decimal character
i -= sizeof('.') + NUMBER_OF_DECIMAL_PLACES;
buffer[i] = '.';
uint8_t j = i;
// Make value positive
value = fabs(value);
// Go through all digits
uint32_t temp = value;
value -= temp;
do {
// Set digit in buffer
buffer[--i] = temp % 10 + 0x30;
temp /= 10;
} while(temp);
// Go through all decimals
temp = value * pow(10, NUMBER_OF_DECIMAL_PLACES);
for(uint8_t k = NUMBER_OF_DECIMAL_PLACES; k; --k) {
// Set decimal digit in buffer
buffer[j + k] = temp % 10 + 0x30;
temp /= 10;
}
// Prepend minus sign if value is negative
if(negative)
buffer[--i] = '-';
// Move string to the start of the buffer
memmove(buffer, &buffer[i], FLOAT_BUFFER_SIZE - i);
}
uint64_t strtoull(const char *nptr, char **endptr) noexcept {
// Initialize variables
uint64_t value = 0;
// Skip plus sign
if(*nptr == '+' && isdigit(nptr[1]))
++nptr;
// Go through all characters
for(; isdigit(*nptr); ++nptr) {
// Set digit in value
value *= 10;
value += *nptr - 0x30;
}
// Set end pointer to last valid character
if(endptr)
*endptr = const_cast<char *>(nptr);
// Return value
return value;
}
int64_t strtoll(const char *nptr, char **endptr) noexcept {
// Return value converted to a long long
return *nptr == '-' && isdigit(nptr[1]) ? strtoull(nptr + sizeof('-'), endptr) * -1 : strtoull(nptr, endptr);
}
float strtof(const char *nptr, char **endptr) noexcept {
// Initialize variables
float value;
bool negative = false;
// Check if value contains a sign
if((*nptr == '-' || *nptr == '+') && (isdigit(nptr[1]) || (nptr[1] == '.' && isdigit(nptr[2])))) {
// Set negative
negative = *nptr == '-';
// Skip sign
++nptr;
}
// Get the integer and fractional part of the value
for(bool firstPass = true;; firstPass = false) {
// Initialize variables
float *currentValue;
float decimalValue;
// Check if getting integer part of the value
if(firstPass)
// Set current value
currentValue = &value;
// Otherwise
else {
// Set current value
currentValue = &decimalValue;
// Check if a value contains a fractional part
if(*nptr == '.' && isdigit(nptr[1]))
// Move to first decimal digit
++nptr;
}
// Clear current value
*currentValue = 0;
// Go through all characters
uint8_t numberOfDigits = 0;
for(; isdigit(*nptr); ++nptr)
// Check if value is valid
if(firstPass || numberOfDigits != FLT_DIG) {
// Set digit in current value
*currentValue *= 10;
*currentValue += *nptr - 0x30;
// Increment number of digits
++numberOfDigits;
}
// Check if getting fractional part of the value
if(!firstPass) {
// Convert current value into a fractional value
for(uint8_t i = numberOfDigits; i > 0; --i)
decimalValue /= 10;
// Append fractional value to integer value
value += decimalValue;
// Break
break;
}
}
// Set end pointer to last valid character
if(endptr)
*endptr = const_cast<char *>(nptr);
// Return value
return negative ? value * -1 : value;
}
void sendDataToUsb(const char *data, bool checkBufferSize) noexcept {
// Check if data can be sent
uint8_t length = strlen(data);
if(!checkBufferSize || udi_cdc_multi_get_free_tx_buffer(UDI_CDC_PORT_NB - 1) >= length)
// Send data
udi_cdc_multi_write_buf(UDI_CDC_PORT_NB - 1, data, length);
}
float getValueInRange(float value, float minValue, float maxValue) noexcept {
// Return value limited by range
return min(maxValue, max(minValue, value));
}
uint32_t minimumOneCeil(float value) noexcept {
// Return ceiling of value that is at least one
return getValueInRange(ceil(value), 1, UINT32_MAX);
}
void delayHundredsOfMicroseconds(uint16_t hundredsOfMicroseconds, bool *condition) noexcept {
// Delay until an emergency stop has occured, or until condition is false or total number of hundreds of microseconds has passed
for(; !emergencyStopRequest && (condition ? *condition : true) && hundredsOfMicroseconds; --hundredsOfMicroseconds)
// Delay one hundred microsecond
delay_us(100);
}
char lowerCase(char value) noexcept {
// Return value as upper case
return value | ('A' ^ 'a');
}