Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/AP_Common.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15* AP_Common.cpp - common utility functions16*/171819#include <AP_HAL/AP_HAL.h>20#include "AP_Common.h"2122extern const AP_HAL::HAL& hal;2324/* assert that const vals are float, not double. so 100.0 means 100.0f */25static_assert(sizeof(1e6) == sizeof(float), "Compilation needs to use single-precision constants");2627/*28Return true if value is between lower and upper bound inclusive.29False otherwise.30*/31bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound)32{33if ((lower_bound <= upper_bound) &&34(value >= lower_bound) && (value <= upper_bound)) {35return true;36}3738return false;3940}4142/**43* return the numeric value of an ascii hex character44*45* @param [in] a Hexa character46* @param [out] res uint8 value47* @retval true Conversion OK48* @retval false Input value error49* @Note Input character is 0-9, A-F, a-f50* A 0x41, a 0x61, 0 0x3051*/52bool hex_to_uint8(uint8_t a, uint8_t &res)53{54uint8_t nibble_low = a & 0xf;5556switch (a & 0xf0) {57case 0x30: // 0-58if (nibble_low > 9) {59return false;60}61res = nibble_low;62break;63case 0x40: // uppercase A-64case 0x60: // lowercase a-65if (nibble_low == 0 || nibble_low > 6) {66return false;67}68res = nibble_low + 9;69break;70default:71return false;72}73return true;74}7576/*77strncpy without the warning for not leaving room for nul termination78*/79size_t strncpy_noterm(char *dest, const char *src, size_t n)80{81size_t len = strnlen(src, n);82size_t ret = len; // return value is length of src83if (len < n) {84// include nul term if it fits85len++;86}87memcpy(dest, src, len);88return ret;89}9091/**92* return the numeric value of an ascii hex character93*94* @param[in] a Hexadecimal character95* @return Returns a binary value. If 'a' is not a valid hex character 255 (AKA -1) is returned96*/97uint8_t char_to_hex(char a)98{99if (a >= 'A' && a <= 'F') {100return a - 'A' + 10;101} else if (a >= 'a' && a <= 'f') {102return a - 'a' + 10;103} else if (a >= '0' && a <= '9') {104return a - '0';105}106return 255;107}108109110