AP_OSD_Backend.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. */
  16. #include <AP_OSD/AP_OSD_Backend.h>
  17. #include <AP_HAL/Util.h>
  18. #include <ctype.h>
  19. extern const AP_HAL::HAL& hal;
  20. #define SYM_DIG_OFS_1 0x90
  21. #define SYM_DIG_OFS_2 0xA0
  22. void AP_OSD_Backend::write(uint8_t x, uint8_t y, bool blink, const char *fmt, ...)
  23. {
  24. if (blink && (blink_phase < 2)) {
  25. return;
  26. }
  27. char buff[32+1]; // +1 for snprintf null-termination
  28. va_list ap;
  29. va_start(ap, fmt);
  30. int res = hal.util->vsnprintf(buff, sizeof(buff), fmt, ap);
  31. res = MIN(res, int(sizeof(buff)));
  32. if (res > 0 && check_option(AP_OSD::OPTION_DECIMAL_PACK)) {
  33. // automatically use packed decimal characters
  34. // based on fiam idea implemented in inav osd
  35. char *p = strchr(&buff[1],'.');
  36. if (p && isdigit(p[1]) && isdigit(p[-1])) {
  37. p[-1] += SYM_DIG_OFS_1;
  38. p[1] += SYM_DIG_OFS_2;
  39. memmove(p, p+1, strlen(p+1)+1);
  40. res--;
  41. }
  42. }
  43. if (res < int(sizeof(buff))-1) {
  44. write(x, y, buff);
  45. }
  46. va_end(ap);
  47. }