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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "pulse.h"
  28. #include "sequence.h"
  29. #include "ui.h"
  30. #include "main.h"
  31. #define WATCHDOG_PERIOD_MS 100
  32. #define LOGO_INIT_MS 1500
  33. static const uint gpio_hw_detect = 21;
  34. enum hw_versions hw_type = HW_UNKNOWN;
  35. static bool debug_buttons[NUM_BTNS] = {0};
  36. static void debug_buttons_callback(enum buttons btn, bool v) {
  37. debug_buttons[btn] = v;
  38. }
  39. static void reset_to_bootloader(void) {
  40. lcd_draw_bye();
  41. #ifdef PICO_DEFAULT_LED_PIN
  42. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  43. #else // ! PICO_DEFAULT_LED_PIN
  44. reset_usb_boot(0, 0);
  45. #endif // PICO_DEFAULT_LED_PIN
  46. }
  47. void handle_serial_input(void) {
  48. int c = getchar_timeout_us(0);
  49. if (c == 0x18) {
  50. reset_to_bootloader();
  51. } else if (c != PICO_ERROR_TIMEOUT) {
  52. printf("%c", c);
  53. }
  54. }
  55. int main(void) {
  56. stdio_init_all();
  57. gpio_init(gpio_hw_detect);
  58. gpio_set_dir(gpio_hw_detect, GPIO_IN);
  59. gpio_pull_up(gpio_hw_detect);
  60. if (gpio_get(gpio_hw_detect)) {
  61. hw_type = HW_PROTOTYPE;
  62. } else {
  63. hw_type = HW_V2;
  64. }
  65. bat_init();
  66. buttons_init();
  67. encoder_init();
  68. lcd_init();
  69. led_init();
  70. // read out button state for debug options
  71. buttons_callback(debug_buttons_callback);
  72. for (uint i = 0; i < (DEBOUNCE_DELAY_MS + 5); i++) {
  73. buttons_run();
  74. handle_serial_input();
  75. sleep_ms(1);
  76. }
  77. if (debug_buttons[BTN_REC]) {
  78. lcd_debug_buttons();
  79. } else if (!debug_buttons[BTN_H]) {
  80. // show splash for a bit and animate LEDs
  81. for (uint i = 0; i < LED_COUNT; i++) {
  82. handle_serial_input();
  83. led_set(i, true);
  84. sleep_ms(LOGO_INIT_MS / LED_COUNT);
  85. }
  86. }
  87. // turn off LEDs at end of init
  88. for (uint i = 0; i < LED_COUNT; i++) {
  89. led_set(i, false);
  90. }
  91. sequence_init();
  92. ui_init();
  93. printf("init done\n");
  94. watchdog_enable(WATCHDOG_PERIOD_MS, 1);
  95. int32_t last_epos = 0;
  96. while (1) {
  97. watchdog_update();
  98. buttons_run();
  99. encoder_run();
  100. sequence_run();
  101. pulse_run();
  102. ui_run();
  103. int32_t epos = encoder_pos();
  104. if (epos != last_epos) {
  105. ui_encoder(epos - last_epos);
  106. last_epos = epos;
  107. }
  108. handle_serial_input();
  109. }
  110. return 0;
  111. }