No Description
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.

main.c 3.9KB

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