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.

sequence.c 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "sequence.h"
  22. #define MAX_BEATS 32
  23. static uint32_t ms_per_beat = 500;
  24. static uint32_t beats = 16;
  25. static uint32_t last_t = 0;
  26. static uint32_t last_i = 0;
  27. static bool sequence[MAX_BEATS] = {0};
  28. void sequence_init(void) {
  29. last_t = to_ms_since_boot(get_absolute_time());
  30. last_i = 0;
  31. }
  32. void sequence_set_bpm(uint32_t new_bpm) {
  33. ms_per_beat = 60000 / new_bpm;
  34. }
  35. void sequence_set_beats(uint32_t new_beats) {
  36. beats = (new_beats <= MAX_BEATS) ? new_beats : MAX_BEATS;
  37. }
  38. void sequence_set(uint32_t beat, bool value) {
  39. if (beat < MAX_BEATS) {
  40. sequence[beat] = value;
  41. }
  42. }
  43. void sequence_run(void) {
  44. uint32_t now = to_ms_since_boot(get_absolute_time());
  45. if ((last_t + ms_per_beat) >= now) {
  46. uint32_t i = last_i + 1;
  47. if (i >= beats) i = 0;
  48. led_set(last_i, false);
  49. led_set(i, true);
  50. if (sequence[i]) {
  51. // TODO trigger GPIO impulse
  52. }
  53. last_t = now;
  54. last_i = i;
  55. }
  56. }