S&B Volcano vaporizer remote control with Pi Pico W
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.

lipo.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * lipo.c
  3. *
  4. * https://github.com/raspberrypi/pico-examples/blob/master/adc/read_vsys/power_status.c
  5. *
  6. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * See <http://www.gnu.org/licenses/>.
  19. */
  20. #include "stdbool.h"
  21. #include "hardware/adc.h"
  22. #include "config.h"
  23. #include "lipo.h"
  24. #if CYW43_USES_VSYS_PIN
  25. #include "pico/cyw43_arch.h"
  26. #endif
  27. #ifndef PICO_POWER_SAMPLE_COUNT
  28. #define PICO_POWER_SAMPLE_COUNT 3
  29. #endif
  30. // Pin used for ADC 0
  31. #define PICO_FIRST_ADC_PIN 26
  32. static const float full_battery = 4.1f;
  33. static const float empty_battery = 3.2f;
  34. bool lipo_charging(void) {
  35. #if defined CYW43_WL_GPIO_VBUS_PIN
  36. return cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  37. #elif defined PICO_VBUS_PIN
  38. gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO);
  39. return gpio_get(PICO_VBUS_PIN);
  40. #else
  41. #error "No VBUS Pin available!"
  42. #endif
  43. }
  44. float lipo_voltage(void) {
  45. #ifndef PICO_VSYS_PIN
  46. #error "No VSYS Pin available!"
  47. #endif
  48. #if CYW43_USES_VSYS_PIN
  49. cyw43_thread_enter();
  50. // Make sure cyw43 is awake
  51. cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
  52. #endif
  53. // setup adc
  54. adc_gpio_init(PICO_VSYS_PIN);
  55. adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN);
  56. adc_fifo_setup(true, false, 0, false, false);
  57. adc_run(true);
  58. // We seem to read low values initially - this seems to fix it
  59. int ignore_count = PICO_POWER_SAMPLE_COUNT;
  60. while (!adc_fifo_is_empty() || ignore_count-- > 0) {
  61. (void)adc_fifo_get_blocking();
  62. }
  63. // read vsys
  64. uint32_t vsys = 0;
  65. for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
  66. uint16_t val = adc_fifo_get_blocking();
  67. vsys += val;
  68. }
  69. adc_run(false);
  70. adc_fifo_drain();
  71. vsys /= PICO_POWER_SAMPLE_COUNT;
  72. #if CYW43_USES_VSYS_PIN
  73. cyw43_thread_exit();
  74. #endif
  75. // Generate voltage
  76. const float conversion_factor = 3.3f / (1 << 12);
  77. return vsys * 3 * conversion_factor;
  78. }
  79. float lipo_percentage(float voltage) {
  80. float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
  81. if (percentage >= 100.0f) {
  82. percentage = 100.0f;
  83. } else if (percentage < 0.0f) {
  84. percentage = 0.0f;
  85. }
  86. return percentage;
  87. }