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.

main.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "fat_disk.h"
  29. #include "debug_disk.h"
  30. #include "buttons.h"
  31. #include "ble.h"
  32. #include "lcd.h"
  33. #include "text.h"
  34. #include "image.h"
  35. #include "mem.h"
  36. #include "state.h"
  37. #include "serial.h"
  38. #include "workflow.h"
  39. #include "wifi.h"
  40. #include "http.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. }
  52. void networking_init(void) {
  53. debug("wifi_init");
  54. wifi_init();
  55. debug("http_init");
  56. http_init();
  57. }
  58. void networking_deinit(void) {
  59. debug("http_deinit");
  60. http_deinit();
  61. debug("wifi_deinit");
  62. wifi_deinit();
  63. }
  64. void networking_run(void) {
  65. http_run();
  66. wifi_run();
  67. }
  68. int main(void) {
  69. watchdog_enable(WATCHDOG_PERIOD_MS, 1);
  70. // required for debug console
  71. cnsl_init();
  72. #ifndef NDEBUG
  73. serial_init();
  74. #endif
  75. usb_init();
  76. debug("mem_load");
  77. mem_load();
  78. debug("lcd_init");
  79. lcd_init();
  80. watchdog_update();
  81. debug("draw_splash");
  82. draw_splash();
  83. lcd_set_backlight(mem_data()->backlight);
  84. if (watchdog_caused_reboot()) {
  85. debug("reset by watchdog");
  86. }
  87. debug("buttons_init");
  88. buttons_init();
  89. // required for LiPo voltage reading
  90. debug("adc_init");
  91. adc_init();
  92. // required for BLE and LiPo voltage reading
  93. debug("cyw43_arch_init");
  94. if (cyw43_arch_init()) {
  95. debug("cyw43_arch_init failed");
  96. lcd_set_backlight(0x00FF);
  97. while (1) {}
  98. }
  99. watchdog_update();
  100. debug("heartbeat_init");
  101. heartbeat_init();
  102. debug("ble_init");
  103. ble_init();
  104. debug("fat_disk_init");
  105. fat_disk_init();
  106. debug("debug_disk_init");
  107. debug_disk_init();
  108. #ifdef AUTO_MOUNT_MASS_STORAGE
  109. msc_set_medium_available(true);
  110. #endif // AUTO_MOUNT_MASS_STORAGE
  111. watchdog_update();
  112. debug("init done");
  113. battery_run();
  114. // wait for BLE stack to be ready before using it
  115. debug("wait for bt stack");
  116. while (!ble_is_ready()) {
  117. sleep_ms(1);
  118. }
  119. if (mem_data()->enable_wifi) {
  120. debug("networking_init");
  121. networking_init();
  122. } else {
  123. debug("wifi not enabled");
  124. }
  125. debug("starting app");
  126. state_switch(STATE_SCAN);
  127. while (1) {
  128. main_loop_hw();
  129. buttons_run();
  130. cnsl_run();
  131. battery_run();
  132. state_run();
  133. wf_run();
  134. }
  135. return 0;
  136. }