S&B Volcano vaporizer remote control with Pi Pico W
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * main.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 "pico/stdlib.h"
  19. #include "pico/cyw43_arch.h"
  20. #include "hardware/watchdog.h"
  21. #include "hardware/adc.h"
  22. #include "config.h"
  23. #include "util.h"
  24. #include "console.h"
  25. #include "log.h"
  26. #include "usb.h"
  27. #include "usb_msc.h"
  28. #include "debug_disk.h"
  29. #include "buttons.h"
  30. #include "ble.h"
  31. #include "lcd.h"
  32. #include "text.h"
  33. #include "image.h"
  34. #include "mem.h"
  35. #include "state.h"
  36. #include "serial.h"
  37. #include "workflow.h"
  38. #include "wifi.h"
  39. #include "http.h"
  40. #include "cache.h"
  41. #include "main.h"
  42. void main_loop_hw(void) {
  43. watchdog_update();
  44. usb_run();
  45. serial_run();
  46. heartbeat_run();
  47. if (lcd_get_backlight() != mem_data()->backlight) {
  48. lcd_set_backlight(mem_data()->backlight);
  49. }
  50. networking_run();
  51. cache_run();
  52. }
  53. void networking_init(void) {
  54. debug("wifi_init");
  55. wifi_init();
  56. debug("http_init");
  57. http_init();
  58. }
  59. void networking_deinit(void) {
  60. debug("wifi_deinit");
  61. wifi_deinit();
  62. }
  63. void networking_run(void) {
  64. wifi_run();
  65. }
  66. int main(void) {
  67. watchdog_enable(WATCHDOG_PERIOD_MS, 1);
  68. // required for debug console
  69. cnsl_init();
  70. #ifndef NDEBUG
  71. serial_init();
  72. #endif
  73. usb_init();
  74. debug("mem_load");
  75. mem_load();
  76. debug("lcd_init");
  77. lcd_init();
  78. watchdog_update();
  79. debug("draw_splash");
  80. draw_splash();
  81. lcd_set_backlight(mem_data()->backlight);
  82. if (watchdog_caused_reboot()) {
  83. debug("reset by watchdog");
  84. }
  85. debug("buttons_init");
  86. buttons_init();
  87. // required for LiPo voltage reading
  88. debug("adc_init");
  89. adc_init();
  90. // required for BLE and LiPo voltage reading
  91. debug("cyw43_arch_init");
  92. if (cyw43_arch_init_with_country(COUNTRY_CODE)) {
  93. debug("cyw43_arch_init failed");
  94. lcd_set_backlight(0x00FF);
  95. while (1) {}
  96. }
  97. watchdog_update();
  98. debug("heartbeat_init");
  99. heartbeat_init();
  100. debug("ble_init");
  101. ble_init();
  102. debug("cache_init");
  103. cache_init();
  104. #ifdef AUTO_MOUNT_MASS_STORAGE
  105. msc_set_medium_available(true);
  106. #endif // AUTO_MOUNT_MASS_STORAGE
  107. watchdog_update();
  108. debug("init done");
  109. battery_run();
  110. // wait for BLE stack to be ready before using it
  111. debug("wait for bt stack");
  112. while (!ble_is_ready()) {
  113. sleep_ms(1);
  114. }
  115. if (mem_data()->enable_wifi) {
  116. debug("networking_init");
  117. networking_init();
  118. } else {
  119. debug("wifi not enabled");
  120. }
  121. debug("starting app");
  122. state_switch(STATE_SCAN);
  123. while (1) {
  124. main_loop_hw();
  125. buttons_run();
  126. cnsl_run();
  127. battery_run();
  128. state_run();
  129. wf_run();
  130. }
  131. return 0;
  132. }