Geen omschrijving
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.

settings.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * settings.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 <inttypes.h>
  20. #include "pico/stdlib.h"
  21. #include "hardware/watchdog.h"
  22. #include "config.h"
  23. #include "buttons.h"
  24. #include "console.h"
  25. #include "encoder.h"
  26. #include "lcd.h"
  27. #include "log.h"
  28. #include "mem.h"
  29. #include "pulse.h"
  30. #include "sequence.h"
  31. #include "ui.h"
  32. #include "usb.h"
  33. #include "settings.h"
  34. static uint32_t setting = 0;
  35. static void settings_redraw(void) {
  36. char mode[64] = {0};
  37. char val[64] = {0};
  38. char bat[64] = {0};
  39. if (setting >= (NUM_CHANNELS + 1)) {
  40. snprintf(mode, sizeof(mode) - 1, "Memory");
  41. snprintf(val, sizeof(val) - 1, "Save");
  42. } else if (setting == NUM_CHANNELS) {
  43. snprintf(mode, sizeof(mode) - 1, "Boot Anim");
  44. snprintf(val, sizeof(val) - 1, "%"PRIu32"ms", mem_data()->boot_anim_ms);
  45. } else {
  46. snprintf(mode, sizeof(mode) - 1, "Channel %"PRIu32, setting);
  47. snprintf(val, sizeof(val) - 1, "%"PRIu32"ms", mem_data()->ch_timings[setting]);
  48. }
  49. lcd_draw(mode, val, bat);
  50. }
  51. static void settings_buttons(enum buttons btn, bool val) {
  52. switch (btn) {
  53. case BTN_CLICK: {
  54. if (val) {
  55. setting = (setting + 1) % (NUM_CHANNELS + 2);
  56. settings_redraw();
  57. }
  58. break;
  59. }
  60. case BTN_A:
  61. case BTN_E: {
  62. if (val) {
  63. pulse_trigger_out(0, mem_data()->ch_timings[0]);
  64. pulse_trigger_led(0, mem_data()->ch_timings[0]);
  65. pulse_trigger_led(4, mem_data()->ch_timings[0]);
  66. }
  67. break;
  68. }
  69. case BTN_B:
  70. case BTN_F: {
  71. if (val) {
  72. pulse_trigger_out(1, mem_data()->ch_timings[1]);
  73. pulse_trigger_led(1, mem_data()->ch_timings[1]);
  74. pulse_trigger_led(5, mem_data()->ch_timings[1]);
  75. }
  76. break;
  77. }
  78. case BTN_C:
  79. case BTN_G: {
  80. if (val) {
  81. pulse_trigger_out(2, mem_data()->ch_timings[2]);
  82. pulse_trigger_led(2, mem_data()->ch_timings[2]);
  83. pulse_trigger_led(6, mem_data()->ch_timings[2]);
  84. }
  85. break;
  86. }
  87. default: {
  88. break;
  89. }
  90. }
  91. }
  92. static bool settings_encoder(int32_t val) {
  93. if (val == 0) {
  94. return false;
  95. }
  96. if (setting >= (NUM_CHANNELS + 1)) {
  97. mem_write();
  98. lcd_draw("Save", "Ok!", "");
  99. return true;
  100. } else if (setting == NUM_CHANNELS) {
  101. int32_t tmp = mem_data()->boot_anim_ms + val;
  102. KEEP_IN_RANGE(tmp, 0, 10000);
  103. mem_data()->boot_anim_ms = tmp;
  104. } else {
  105. int32_t tmp = mem_data()->ch_timings[setting] + val;
  106. KEEP_IN_RANGE(tmp, 1, 100);
  107. mem_data()->ch_timings[setting] = tmp;
  108. }
  109. settings_redraw();
  110. return false;
  111. }
  112. void settings_init(void) {
  113. buttons_callback(settings_buttons);
  114. settings_redraw();
  115. }
  116. void settings_run(void) {
  117. while (1) {
  118. watchdog_update();
  119. buttons_run();
  120. encoder_run();
  121. pulse_run();
  122. usb_run();
  123. cnsl_run();
  124. if (settings_encoder(encoder_get_diff())) {
  125. break;
  126. }
  127. }
  128. }