Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * main.c
  3. *
  4. * Copyright (c) 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 "pico/stdlib.h"
  19. #include "pico/bootrom.h"
  20. #include "hardware/watchdog.h"
  21. #include "config.h"
  22. #include "adc.h"
  23. #include "buttons.h"
  24. #include "console.h"
  25. #include "encoder.h"
  26. #include "lcd.h"
  27. #include "led.h"
  28. #include "log.h"
  29. #include "logo.h"
  30. #include "mem.h"
  31. #include "pulse.h"
  32. #include "sequence.h"
  33. #include "settings.h"
  34. #include "ui.h"
  35. #include "usb.h"
  36. #include "main.h"
  37. static const uint gpio_hw_detect = 21;
  38. enum hw_versions hw_type = HW_UNKNOWN;
  39. static bool debug_buttons[NUM_BTNS] = {0};
  40. static void debug_buttons_callback(enum buttons btn, bool v) {
  41. debug_buttons[btn] = v;
  42. }
  43. static uint32_t debug_count_buttons(void) {
  44. uint32_t cnt = 0;
  45. for (uint32_t i = 0; i < NUM_BTNS; i++) {
  46. if (debug_buttons[i]) {
  47. cnt++;
  48. }
  49. }
  50. return cnt;
  51. }
  52. void reset_to_bootloader(void) {
  53. lcd_draw_bye();
  54. #ifdef PICO_DEFAULT_LED_PIN
  55. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  56. #else // ! PICO_DEFAULT_LED_PIN
  57. reset_usb_boot(0, 0);
  58. #endif // PICO_DEFAULT_LED_PIN
  59. }
  60. void reset_to_main(void) {
  61. watchdog_enable(1, 0);
  62. while (1);
  63. }
  64. static void sleep_ms_wd(uint32_t ms) {
  65. for (uint32_t i = 0; i < ms; i++) {
  66. watchdog_update();
  67. sleep_ms(1);
  68. }
  69. }
  70. static void hw_type_detection(void) {
  71. gpio_init(gpio_hw_detect);
  72. gpio_set_dir(gpio_hw_detect, GPIO_IN);
  73. gpio_pull_up(gpio_hw_detect);
  74. if (gpio_get(gpio_hw_detect)) {
  75. hw_type = HW_PROTOTYPE;
  76. } else {
  77. hw_type = HW_V2;
  78. }
  79. }
  80. static void animate_boot_combos(void) {
  81. // read out button state for debug options
  82. buttons_callback(debug_buttons_callback);
  83. for (uint i = 0; i < (DEBOUNCE_DELAY_MS + 5); i++) {
  84. watchdog_update();
  85. buttons_run();
  86. usb_run();
  87. cnsl_run();
  88. sleep_ms_wd(1);
  89. }
  90. uint32_t cnt = debug_count_buttons();
  91. // handle special button combos on boot
  92. if ((cnt == 2) && debug_buttons[BTN_CLEAR] && debug_buttons[BTN_CLICK]) {
  93. lcd_debug_buttons();
  94. } else if ((cnt == 1) && debug_buttons[BTN_CLEAR]) {
  95. // enter settings menu
  96. settings_init();
  97. settings_run();
  98. sleep_ms_wd(mem_data()->boot_anim_ms);
  99. } else if (cnt == 3) {
  100. // skip splash screen
  101. } else if ((cnt == 1) && debug_buttons[BTN_CLICK]) {
  102. // show version info
  103. lcd_draw_version();
  104. // wait until button is released
  105. uint32_t last = to_ms_since_boot(get_absolute_time());
  106. bool state = false;
  107. while (debug_buttons[BTN_CLICK]) {
  108. watchdog_update();
  109. buttons_run();
  110. usb_run();
  111. cnsl_run();
  112. uint32_t now = to_ms_since_boot(get_absolute_time());
  113. if ((now - last) >= 250) {
  114. state = !state;
  115. last = now;
  116. led_set(0, state);
  117. }
  118. }
  119. led_set(0, false);
  120. } else {
  121. // show splash for a bit and animate LEDs
  122. for (uint i = 0; i < LED_COUNT; i++) {
  123. watchdog_update();
  124. usb_run();
  125. cnsl_run();
  126. led_set(i, true);
  127. sleep_ms_wd(mem_data()->boot_anim_ms / LED_COUNT);
  128. }
  129. }
  130. // turn off LEDs at end of init
  131. for (uint i = 0; i < LED_COUNT; i++) {
  132. led_set(i, false);
  133. }
  134. }
  135. void main_loop_hw(void) {
  136. watchdog_update();
  137. usb_run();
  138. cnsl_run();
  139. buttons_run();
  140. encoder_run();
  141. sequence_run();
  142. pulse_run();
  143. ui_run();
  144. ui_encoder(encoder_get_diff());
  145. }
  146. int main(void) {
  147. watchdog_enable(WATCHDOG_PERIOD_MS, 1);
  148. cnsl_init();
  149. usb_init();
  150. mem_load();
  151. hw_type_detection();
  152. bat_init();
  153. buttons_init();
  154. encoder_init();
  155. lcd_init();
  156. led_init();
  157. // show logo
  158. lcd_draw_bitmap(logo_data,
  159. LOGO_WIDTH, LOGO_HEIGHT,
  160. 0, 0);
  161. animate_boot_combos();
  162. sequence_init();
  163. ui_init();
  164. debug("init done\n");
  165. while (1) {
  166. main_loop_hw();
  167. }
  168. return 0;
  169. }