AP_Common.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. * AP_Common.cpp - common utility functions
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include "AP_Common.h"
  18. extern const AP_HAL::HAL& hal;
  19. /*
  20. Return true if value is between lower and upper bound inclusive.
  21. False otherwise.
  22. */
  23. bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound)
  24. {
  25. if ((lower_bound <= upper_bound) &&
  26. (value >= lower_bound) && (value <= upper_bound)) {
  27. return true;
  28. }
  29. return false;
  30. }
  31. /**
  32. * return the numeric value of an ascii hex character
  33. *
  34. * @param [in] a Hexa character
  35. * @param [out] res uint8 value
  36. * @retval true Conversion OK
  37. * @retval false Input value error
  38. * @Note Input character is 0-9, A-F, a-f
  39. * A 0x41, a 0x61, 0 0x30
  40. */
  41. bool hex_to_uint8(uint8_t a, uint8_t &res)
  42. {
  43. uint8_t nibble_low = a & 0xf;
  44. switch (a & 0xf0) {
  45. case 0x30: // 0-
  46. if (nibble_low > 9) {
  47. return false;
  48. }
  49. res = nibble_low;
  50. break;
  51. case 0x40: // uppercase A-
  52. case 0x60: // lowercase a-
  53. if (nibble_low == 0 || nibble_low > 6) {
  54. return false;
  55. }
  56. res = nibble_low + 9;
  57. break;
  58. default:
  59. return false;
  60. }
  61. return true;
  62. }