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.

ui.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * ui.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 "adc.h"
  22. #include "buttons.h"
  23. #include "lcd.h"
  24. #include "sequence.h"
  25. #include "ui.h"
  26. #define REDRAW_MS 2000
  27. static bool rec_held_down = false;
  28. static enum ui_modes ui_mode = 0;
  29. static enum machine_modes machine_mode = 0;
  30. static uint32_t last_redraw = 0;
  31. static void ui_redraw(void) {
  32. char mode[64] = {0};
  33. char val[64] = {0};
  34. char bat[64] = {0};
  35. switch (ui_mode) {
  36. case UI_BPM: {
  37. snprintf(mode, sizeof(mode) - 1, "BPM:");
  38. snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bpm());
  39. break;
  40. }
  41. case UI_MODE: {
  42. snprintf(mode, sizeof(mode) - 1, "Mode:");
  43. switch (machine_mode) {
  44. case MODE_LOOPSTATION: {
  45. snprintf(val, sizeof(val) - 1, "Loop");
  46. break;
  47. }
  48. case MODE_DRUMMACHINE: {
  49. snprintf(val, sizeof(val) - 1, "Drum");
  50. break;
  51. }
  52. default: {
  53. printf("%s: invalid mode: %d\n", __func__, machine_mode);
  54. machine_mode = 0;
  55. ui_redraw();
  56. return;
  57. }
  58. }
  59. break;
  60. }
  61. case UI_LENGTH: {
  62. snprintf(mode, sizeof(mode) - 1, "Length:");
  63. snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_beats());
  64. break;
  65. }
  66. case UI_BANK: {
  67. snprintf(mode, sizeof(mode) - 1, "Bank:");
  68. snprintf(val, sizeof(val) - 1, "%"PRIu32, sequence_get_bank());
  69. break;
  70. }
  71. default: {
  72. printf("%s: invalid mode: %d\n", __func__, ui_mode);
  73. ui_mode = 0;
  74. ui_redraw();
  75. return;
  76. }
  77. }
  78. snprintf(bat, sizeof(bat) - 1, "Bat: %.2fV", bat_get());
  79. lcd_draw(mode, val, bat);
  80. }
  81. static void ui_buttons_loopstation(enum buttons btn, bool val) {
  82. switch (btn) {
  83. case BTN_A:
  84. case BTN_B:
  85. case BTN_C: {
  86. if (val) {
  87. sequence_handle_button_loopstation(btn, rec_held_down);
  88. }
  89. break;
  90. }
  91. case BTN_REC: {
  92. rec_held_down = val;
  93. break;
  94. }
  95. default: {
  96. printf("%s: invalid btn: %d\n", __func__, btn);
  97. break;
  98. }
  99. }
  100. }
  101. static void ui_buttons_drummachine(enum buttons btn, bool val) {
  102. switch (btn) {
  103. case BTN_A:
  104. case BTN_B:
  105. case BTN_C:
  106. case BTN_REC: {
  107. if (val) {
  108. sequence_handle_button_drummachine(btn);
  109. }
  110. break;
  111. }
  112. default: {
  113. printf("%s: invalid btn: %d\n", __func__, btn);
  114. break;
  115. }
  116. }
  117. }
  118. static void ui_buttons(enum buttons btn, bool val) {
  119. switch (btn) {
  120. case BTN_CLICK: {
  121. if (val) {
  122. ui_mode = (ui_mode + 1) % UI_NUM_MODES;
  123. ui_redraw();
  124. }
  125. break;
  126. }
  127. default: {
  128. switch (machine_mode) {
  129. case MODE_LOOPSTATION: {
  130. ui_buttons_loopstation(btn, val);
  131. break;
  132. }
  133. case MODE_DRUMMACHINE: {
  134. ui_buttons_drummachine(btn, val);
  135. break;
  136. }
  137. default: {
  138. printf("%s: invalid mode: %d\n", __func__, machine_mode);
  139. machine_mode = 0;
  140. ui_buttons(btn, val);
  141. break;
  142. }
  143. }
  144. break;
  145. }
  146. }
  147. }
  148. void ui_encoder(int32_t val) {
  149. if (val == 0) {
  150. return;
  151. }
  152. switch (ui_mode) {
  153. case UI_BPM: {
  154. sequence_set_bpm(sequence_get_bpm() + val);
  155. break;
  156. }
  157. case UI_MODE: {
  158. machine_mode = (machine_mode + val) % MACHINE_NUM_MODES;
  159. break;
  160. }
  161. case UI_LENGTH: {
  162. sequence_set_beats(sequence_get_beats() + val);
  163. break;
  164. }
  165. case UI_BANK: {
  166. sequence_set_bank(sequence_get_bank() + val);
  167. break;
  168. }
  169. default: {
  170. printf("%s: invalid mode: %d\n", __func__, ui_mode);
  171. ui_mode = 0;
  172. ui_encoder(val);
  173. return;
  174. }
  175. }
  176. ui_redraw();
  177. }
  178. void ui_init(void) {
  179. buttons_callback(ui_buttons);
  180. ui_redraw();
  181. }
  182. void ui_run(void) {
  183. uint32_t now = to_ms_since_boot(get_absolute_time());
  184. if (now >= (last_redraw + REDRAW_MS)) {
  185. ui_redraw();
  186. last_redraw = now;
  187. }
  188. }