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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 void reset_to_bootloader(void) {
  36. #ifdef PICO_DEFAULT_LED_PIN
  37. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  38. #else // ! PICO_DEFAULT_LED_PIN
  39. reset_usb_boot(0, 0);
  40. #endif // PICO_DEFAULT_LED_PIN
  41. }
  42. int main(void) {
  43. stdio_init_all();
  44. gpio_init(gpio_hw_detect);
  45. gpio_set_dir(gpio_hw_detect, GPIO_IN);
  46. gpio_pull_up(gpio_hw_detect);
  47. if (gpio_get(gpio_hw_detect)) {
  48. hw_type = HW_PROTOTYPE;
  49. } else {
  50. hw_type = HW_V2;
  51. }
  52. bat_init();
  53. buttons_init();
  54. encoder_init();
  55. lcd_init();
  56. led_init();
  57. // show splash for a bit and animate LEDs
  58. for (uint i = 0; i < LED_COUNT; i++) {
  59. led_set(i, true);
  60. sleep_ms(LOGO_INIT_MS / LED_COUNT);
  61. }
  62. // turn off LEDs at end of init
  63. for (uint i = 0; i < LED_COUNT; i++) {
  64. led_set(i, false);
  65. }
  66. sequence_init();
  67. ui_init();
  68. printf("init done\n");
  69. watchdog_enable(WATCHDOG_PERIOD_MS, 1);
  70. int32_t last_epos = 0;
  71. while (1) {
  72. watchdog_update();
  73. buttons_run();
  74. encoder_run();
  75. sequence_run();
  76. pulse_run();
  77. ui_run();
  78. int32_t epos = encoder_pos();
  79. if (epos != last_epos) {
  80. ui_encoder(epos - last_epos);
  81. last_epos = epos;
  82. }
  83. int c = getchar_timeout_us(0);
  84. if (c == 0x18) {
  85. reset_to_bootloader();
  86. } else if (c != PICO_ERROR_TIMEOUT) {
  87. printf("%c", c);
  88. }
  89. }
  90. return 0;
  91. }