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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "log.h"
  23. #include "util.h"
  24. bool str_startswith(const char *str, const char *start) {
  25. size_t l = strlen(start);
  26. if (l > strlen(str)) {
  27. return false;
  28. }
  29. return (strncmp(str, start, l) == 0);
  30. }
  31. void reset_to_bootloader(void) {
  32. #ifdef PICO_DEFAULT_LED_PIN
  33. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  34. #else // ! PICO_DEFAULT_LED_PIN
  35. reset_usb_boot(0, 0);
  36. #endif // PICO_DEFAULT_LED_PIN
  37. }
  38. void reset_to_main(void) {
  39. watchdog_enable(1, false);
  40. while (1);
  41. }
  42. void hexdump(const uint8_t *buff, size_t len) {
  43. for (size_t i = 0; i < len; i += 16) {
  44. for (size_t j = 0; (j < 16) && ((i + j) < len); j++) {
  45. print("0x%02X", buff[i + j]);
  46. if ((j < 15) && ((i + j) < (len - 1))) {
  47. print(" ");
  48. }
  49. }
  50. println();
  51. }
  52. }
  53. float map(float value, float leftMin, float leftMax, float rightMin, float rightMax) {
  54. float leftSpan = leftMax - leftMin;
  55. float rightSpan = rightMax - rightMin;
  56. float valueScaled = (value - leftMin) / leftSpan;
  57. return rightMin + (valueScaled * rightSpan);
  58. }