CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/AP_Common.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
/*
16
* AP_Common.cpp - common utility functions
17
*/
18
19
20
#include <AP_HAL/AP_HAL.h>
21
#include "AP_Common.h"
22
23
extern const AP_HAL::HAL& hal;
24
25
/* assert that const vals are float, not double. so 100.0 means 100.0f */
26
static_assert(sizeof(1e6) == sizeof(float), "Compilation needs to use single-precision constants");
27
28
/*
29
Return true if value is between lower and upper bound inclusive.
30
False otherwise.
31
*/
32
bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound)
33
{
34
if ((lower_bound <= upper_bound) &&
35
(value >= lower_bound) && (value <= upper_bound)) {
36
return true;
37
}
38
39
return false;
40
41
}
42
43
/**
44
* return the numeric value of an ascii hex character
45
*
46
* @param [in] a Hexa character
47
* @param [out] res uint8 value
48
* @retval true Conversion OK
49
* @retval false Input value error
50
* @Note Input character is 0-9, A-F, a-f
51
* A 0x41, a 0x61, 0 0x30
52
*/
53
bool hex_to_uint8(uint8_t a, uint8_t &res)
54
{
55
uint8_t nibble_low = a & 0xf;
56
57
switch (a & 0xf0) {
58
case 0x30: // 0-
59
if (nibble_low > 9) {
60
return false;
61
}
62
res = nibble_low;
63
break;
64
case 0x40: // uppercase A-
65
case 0x60: // lowercase a-
66
if (nibble_low == 0 || nibble_low > 6) {
67
return false;
68
}
69
res = nibble_low + 9;
70
break;
71
default:
72
return false;
73
}
74
return true;
75
}
76
77
/*
78
strncpy without the warning for not leaving room for nul termination
79
*/
80
size_t strncpy_noterm(char *dest, const char *src, size_t n)
81
{
82
size_t len = strnlen(src, n);
83
size_t ret = len; // return value is length of src
84
if (len < n) {
85
// include nul term if it fits
86
len++;
87
}
88
memcpy(dest, src, len);
89
return ret;
90
}
91
92
/**
93
* return the numeric value of an ascii hex character
94
*
95
* @param[in] a Hexadecimal character
96
* @return Returns a binary value. If 'a' is not a valid hex character 255 (AKA -1) is returned
97
*/
98
uint8_t char_to_hex(char a)
99
{
100
if (a >= 'A' && a <= 'F') {
101
return a - 'A' + 10;
102
} else if (a >= 'a' && a <= 'f') {
103
return a - 'a' + 10;
104
} else if (a >= '0' && a <= '9') {
105
return a - '0';
106
}
107
return 255;
108
}
109
110