Ingen beskrivning
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 4.1KB

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