S&B Volcano vaporizer remote control with Pi Pico W
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

util.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * util.c
  3. *
  4. * Copyright (c) 2022 - 2023 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/stdlib.h"
  20. #include "pico/bootrom.h"
  21. #include "hardware/watchdog.h"
  22. #ifdef CYW43_WL_GPIO_LED_PIN
  23. #include "pico/cyw43_arch.h"
  24. #endif // CYW43_WL_GPIO_LED_PIN
  25. #include "config.h"
  26. #include "log.h"
  27. #include "util.h"
  28. #define HEARTBEAT_INTERVAL_MS 500
  29. void heartbeat_init(void) {
  30. #ifdef PICO_DEFAULT_LED_PIN
  31. gpio_init(PICO_DEFAULT_LED_PIN);
  32. gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
  33. gpio_put(PICO_DEFAULT_LED_PIN, 1);
  34. #endif // PICO_DEFAULT_LED_PIN
  35. #ifdef CYW43_WL_GPIO_LED_PIN
  36. cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
  37. #endif // CYW43_WL_GPIO_LED_PIN
  38. }
  39. void heartbeat_run(void) {
  40. #if defined(PICO_DEFAULT_LED_PIN) || defined(CYW43_WL_GPIO_LED_PIN)
  41. static uint32_t last_heartbeat = 0;
  42. uint32_t now = to_ms_since_boot(get_absolute_time());
  43. if (now >= (last_heartbeat + HEARTBEAT_INTERVAL_MS)) {
  44. last_heartbeat = now;
  45. #ifdef PICO_DEFAULT_LED_PIN
  46. gpio_xor_mask(1 << PICO_DEFAULT_LED_PIN);
  47. #endif // PICO_DEFAULT_LED_PIN
  48. #ifdef CYW43_WL_GPIO_LED_PIN
  49. cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, !cyw43_arch_gpio_get(CYW43_WL_GPIO_LED_PIN));
  50. #endif // CYW43_WL_GPIO_LED_PIN
  51. }
  52. #endif // defined(PICO_DEFAULT_LED_PIN) || defined(CYW43_WL_GPIO_LED_PIN)
  53. }
  54. bool str_startswith(const char *str, const char *start) {
  55. size_t l = strlen(start);
  56. if (l > strlen(str)) {
  57. return false;
  58. }
  59. return (strncmp(str, start, l) == 0);
  60. }
  61. int32_t convert_two_complement(int32_t b) {
  62. if (b & 0x8000) {
  63. b = -1 * ((b ^ 0xffff) + 1);
  64. }
  65. return b;
  66. }
  67. void reset_to_bootloader(void) {
  68. #ifdef PICO_DEFAULT_LED_PIN
  69. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  70. #else // ! PICO_DEFAULT_LED_PIN
  71. reset_usb_boot(0, 0);
  72. #endif // PICO_DEFAULT_LED_PIN
  73. }
  74. void reset_to_main(void) {
  75. watchdog_enable(1, 1);
  76. while (1) {
  77. // wait 1ms until watchdog kills us
  78. asm volatile("nop");
  79. }
  80. }
  81. void hexdump(const uint8_t *buff, size_t len) {
  82. for (size_t i = 0; i < len; i += 16) {
  83. for (size_t j = 0; (j < 16) && ((i + j) < len); j++) {
  84. print("0x%02X", buff[i + j]);
  85. if ((j < 15) && ((i + j) < (len - 1))) {
  86. print(" ");
  87. }
  88. }
  89. println();
  90. }
  91. }
  92. float map(float value, float leftMin, float leftMax, float rightMin, float rightMax) {
  93. float leftSpan = leftMax - leftMin;
  94. float rightSpan = rightMax - rightMin;
  95. float valueScaled = (value - leftMin) / leftSpan;
  96. return rightMin + (valueScaled * rightSpan);
  97. }