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.

sequence.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * sequence.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 "led.h"
  21. #include "pulse.h"
  22. #include "sequence.h"
  23. #define MAX_BEATS 32
  24. static const uint32_t channel_times[NUM_CHANNELS] = CH_GPIO_TIMINGS;
  25. static uint32_t ms_per_beat = 500;
  26. static uint32_t beats = 16;
  27. static uint32_t last_t = 0;
  28. static uint32_t last_i = 0;
  29. static enum channels sequence[MAX_BEATS] = {0};
  30. void sequence_init(void) {
  31. last_t = to_ms_since_boot(get_absolute_time());
  32. last_i = 0;
  33. for (uint i = 0; i < MAX_BEATS; i++) {
  34. sequence[i] = 0;
  35. }
  36. }
  37. void sequence_set_bpm(uint32_t new_bpm) {
  38. ms_per_beat = 60000 / new_bpm;
  39. }
  40. void sequence_set_beats(uint32_t new_beats) {
  41. beats = (new_beats <= MAX_BEATS) ? new_beats : MAX_BEATS;
  42. }
  43. static void sequence_set(uint32_t beat, enum channels ch, bool value) {
  44. if (beat < MAX_BEATS) {
  45. if (value) {
  46. sequence[beat] |= ch;
  47. } else {
  48. sequence[beat] &= ~ch;
  49. }
  50. }
  51. }
  52. static bool sequence_get(uint32_t beat, enum channels ch) {
  53. if (beat < MAX_BEATS) {
  54. return (sequence[beat] & ch) != 0;
  55. }
  56. return false;
  57. }
  58. void sequence_handle_button_loopstation(enum buttons btn, bool rec) {
  59. switch (btn) {
  60. case BTN_A: {
  61. pulse_trigger_out(0, channel_times[0]);
  62. pulse_trigger_led(0, channel_times[0]);
  63. break;
  64. }
  65. case BTN_B: {
  66. pulse_trigger_out(1, channel_times[1]);
  67. pulse_trigger_led(1, channel_times[1]);
  68. break;
  69. }
  70. case BTN_C: {
  71. pulse_trigger_out(2, channel_times[2]);
  72. pulse_trigger_led(2, channel_times[2]);
  73. break;
  74. }
  75. default: {
  76. break;
  77. }
  78. }
  79. if (rec) {
  80. uint32_t beat = 42; // TODO!!
  81. switch (btn) {
  82. case BTN_A: {
  83. sequence_set(beat, CH_KICK, true);
  84. break;
  85. }
  86. case BTN_B: {
  87. sequence_set(beat, CH_SNARE, true);
  88. break;
  89. }
  90. case BTN_C: {
  91. sequence_set(beat, CH_HIHAT, true);
  92. break;
  93. }
  94. default: {
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. void sequence_handle_button_drummachine(enum buttons btn) {
  101. uint32_t beat = 42; // TODO!!
  102. switch (btn) {
  103. case BTN_A: {
  104. bool val = !sequence_get(beat, CH_KICK);
  105. sequence_set(beat, CH_KICK, val);
  106. break;
  107. }
  108. case BTN_B: {
  109. bool val = !sequence_get(beat, CH_SNARE);
  110. sequence_set(beat, CH_SNARE, val);
  111. break;
  112. }
  113. case BTN_C: {
  114. bool val = !sequence_get(beat, CH_HIHAT);
  115. sequence_set(beat, CH_HIHAT, val);
  116. break;
  117. }
  118. default: {
  119. break;
  120. }
  121. }
  122. }
  123. void sequence_run(void) {
  124. uint32_t now = to_ms_since_boot(get_absolute_time());
  125. if ((last_t + ms_per_beat) >= now) {
  126. uint32_t i = last_i + 1;
  127. if (i >= beats) i = 0;
  128. led_set(last_i, false);
  129. led_set(i, true);
  130. for (uint ch = 0; ch < NUM_CHANNELS; ch++) {
  131. if (sequence[i] & (1 << ch)) {
  132. pulse_trigger_out(i, channel_times[ch]);
  133. }
  134. }
  135. last_t = now;
  136. last_i = i;
  137. }
  138. }