This repository has been archived on 2023-02-06. You can view files and clone it, but cannot push or open issues/pull-requests.
esp32-sequencer/main/esp32-sequencer.c

102 lines
2.4 KiB
C

#include <stdio.h>
#include <stdint.h>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_random.h"
#include "driver/ledc.h"
#include "hal/ledc_types.h"
#define SEQ_MAX_LEN 8
/* Inputs */
// Clock In
// Reset In
/* Outputs */
// CV Out
// Gate Out
/* LED GPIO Pins */
#define LEDC_CLK_OUT (5)
#define LEDC_GATE_OUT (6)
static void ledc_init(void) {
// Setup LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 5000,
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
// Setup Clock LED
ledc_channel_config_t ledc_clk_channel = {
.channel = LEDC_CHANNEL_0,
.gpio_num = LEDC_CLK_OUT,
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.duty = 0,
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_clk_channel));
// Setup Gate LED
ledc_channel_config_t ledc_gate_channel = {
.channel = LEDC_CHANNEL_1,
.gpio_num = LEDC_GATE_OUT,
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_sel = LEDC_TIMER_0,
.intr_type = LEDC_INTR_DISABLE,
.duty = 0,
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_gate_channel));
}
void blink_led(ledc_channel_t channel) {
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, channel, 4095));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, channel));
vTaskDelay(10 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, channel, 0));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, channel));
}
void gen_rand_seq(uint8_t *sequence) {
for (uint8_t i = 0; i < SEQ_MAX_LEN; i++) {
sequence[i] = esp_random() % 2;
}
}
void app_main(void) {
/* Initialize Peripherals */
/* Initialize Monitor LEDs */
ledc_init();
/* Initialize sequence */
uint8_t sequence[SEQ_MAX_LEN] = {0};
uint8_t seq_len = SEQ_MAX_LEN;
/* Main Loop */
while (1) {
for (uint8_t step = 0; step < seq_len; step++) {
blink_led(LEDC_CHANNEL_0); // blink clock led
// if step has been set for sequence
if (sequence[step] == 1) {
blink_led(LEDC_CHANNEL_1); // blink gate triggered led
}
printf("%i", sequence[step]);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
gen_rand_seq(sequence);
printf("\n");
}
}