From 02139416b167ea0b862da82377d3c65a3e4d65f5 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sat, 28 Jan 2023 10:39:55 -0600 Subject: [PATCH] add helper functions to convert between bpm and ms --- main/esp32-sequencer.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/main/esp32-sequencer.c b/main/esp32-sequencer.c index 522a84e..ca6dfca 100644 --- a/main/esp32-sequencer.c +++ b/main/esp32-sequencer.c @@ -25,6 +25,7 @@ /* Controls */ #define GPIO_MUTATE_BTN (9) // Mutate Button In +#define GPIO_TAP_TEMPO_BTN (10) // Tap Tempo Button In static QueueHandle_t control_event_queue = NULL; @@ -45,8 +46,18 @@ gptimer_handle_t gate_blink_timer = NULL; uint8_t sequence[SEQ_MAX_LEN] = {0}; uint8_t seq_len = SEQ_MAX_LEN; +static const uint32_t DEFAULT_BPM = 120; + static const char *TAG = "sequencer"; +uint32_t bpm_to_ms(uint32_t bpm) { + return 60000 / bpm; +} + +uint32_t ms_to_bpm(uint32_t ms) { + return 60000 / ms; +} + static void randomize_seq(uint8_t *sequence) { for (uint8_t i = 0; i < SEQ_MAX_LEN; i++) { sequence[i] = esp_random() % 2; @@ -73,7 +84,7 @@ static void control_event_handler(void* arg) { uint32_t gpio_num; while (1) { if(xQueueReceive(control_event_queue, &gpio_num, portMAX_DELAY)) { - ESP_LOGI(TAG, "Handling control from %" PRIu32 , gpio_num); + ESP_LOGI(TAG, "Handling control from GPIO %" PRIu32 , gpio_num); switch (gpio_num) { case GPIO_MUTATE_BTN: mutate_seq(sequence); @@ -91,6 +102,13 @@ static void controls_init(void) { gpio_set_intr_type(GPIO_MUTATE_BTN, GPIO_INTR_NEGEDGE); // Hook in handler for mutate button gpio_isr_handler_add(GPIO_MUTATE_BTN, gpio_isr_handler, (void*)GPIO_MUTATE_BTN); + + // Setup tap tempo Button + gpio_reset_pin(GPIO_TAP_TEMPO_BTN); + gpio_set_direction(GPIO_TAP_TEMPO_BTN, GPIO_MODE_INPUT); + gpio_set_intr_type(GPIO_TAP_TEMPO_BTN, GPIO_INTR_NEGEDGE); + // Hook in handler for tap tempo button + gpio_isr_handler_add(GPIO_TAP_TEMPO_BTN, gpio_isr_handler, (void *)GPIO_TAP_TEMPO_BTN); } static void clk_cv_off_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data) { @@ -270,7 +288,7 @@ void app_main(void) { blink_led(LEDC_CHANNEL_1); // blink gate triggered led } printf("%i", sequence[step]); - vTaskDelay(500 / portTICK_PERIOD_MS); + vTaskDelay(bpm_to_ms(DEFAULT_BPM)/ portTICK_PERIOD_MS); } printf("\n");