No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * util.c
  3. *
  4. * Copyright (c) 2022 - 2024 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <string.h>
  19. #include "pico/bootrom.h"
  20. #include "hardware/watchdog.h"
  21. #include "config.h"
  22. #include "lcd.h"
  23. #include "log.h"
  24. #include "util.h"
  25. bool str_startswith(const char *str, const char *start) {
  26. size_t l = strlen(start);
  27. if (l > strlen(str)) {
  28. return false;
  29. }
  30. return (strncmp(str, start, l) == 0);
  31. }
  32. void reset_to_bootloader(void) {
  33. lcd_bye();
  34. reset_usb_boot(0, 0);
  35. }
  36. void reset_to_main(void) {
  37. watchdog_enable(1, false);
  38. while (1);
  39. }
  40. void hexdump(const uint8_t *buff, size_t len) {
  41. for (size_t i = 0; i < len; i += 16) {
  42. for (size_t j = 0; (j < 16) && ((i + j) < len); j++) {
  43. print("0x%02X", buff[i + j]);
  44. if ((j < 15) && ((i + j) < (len - 1))) {
  45. print(" ");
  46. }
  47. }
  48. println();
  49. }
  50. }
  51. float map(float value, float leftMin, float leftMax, float rightMin, float rightMax) {
  52. float leftSpan = leftMax - leftMin;
  53. float rightSpan = rightMax - rightMin;
  54. float valueScaled = (value - leftMin) / leftSpan;
  55. return rightMin + (valueScaled * rightSpan);
  56. }