waveblender/drone.c
2020-04-24 23:50:43 +02:00

110 lines
2.4 KiB
C

#include <stdint.h>
#include "system.h"
#include "display.h"
#include "systick.h"
#include "button.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "unicore-mx/stm32/gpio.h"
#include "unicore-mx/stm32/rcc.h"
#include "ui.h"
#include "timer.h"
#include "led.h"
#include "drone.h"
#include "drums.h"
#include "pot.h"
#include "settings.h"
#define MUTE { 0 , 0 }
static struct drone_slot drone_pattern[MAX_PATTERN_LEN] = {
{ 28, 10 }, { 28, 20}, {36,20}, {40,20},
{ 40, 10 }, { 40, 10}, {36, 10}, {32,10},
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
MUTE, MUTE, MUTE, MUTE,
};
void drone_mute(void)
{
pot_set(0, 0);
pot_set(1, 0);
gpio_clear(GPIOE, GPIO2);
gpio_clear(GPIOE, GPIO3);
}
void drone_beat(int pattern_pos)
{
struct drone_slot *key = &drone_pattern[pattern_pos];
static int test_lfo_step = 0;
static int test_lfo = 0;
int lfo_depth = 80;
int lfo_rate = 7;
int lfo_step = 2;
int tgt_env;
if ((key->pitch == 0) && (key->env == 0)) {
pot_set(0, 99);
pot_set(1, 0);
gpio_clear(GPIOE, GPIO2);
gpio_clear(GPIOE, GPIO3);
return;
}
pot_set(0, 0);
pot_set(1, 99);
gpio_set(GPIOE, GPIO2);
gpio_set(GPIOE, GPIO3);
if ((pattern_pos % lfo_step) == 0) {
if (test_lfo_step == 0) {
test_lfo+=lfo_rate;
if (test_lfo > lfo_depth)
test_lfo_step = 1;
} else {
test_lfo-=lfo_rate;
if (test_lfo < (0 - lfo_depth))
test_lfo_step = 0;
}
}
tgt_env = key->env + test_lfo;
if (tgt_env < 0) {
tgt_env = 0;
test_lfo_step = 0;
}
if (tgt_env > 99) {
tgt_env = 99;
test_lfo_step = 1;
}
pot_set(0, key->pitch);
pot_set(1, tgt_env);
}
#define TRIG_PIN (GPIO2 | GPIO3)
void drone_init(void)
{
rcc_periph_clock_enable(RCC_GPIOE);
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, TRIG_PIN);
gpio_set_output_options(GPIOE, GPIO_OTYPE_OD, GPIO_OSPEED_100MHZ, TRIG_PIN);
gpio_clear(GPIOE, TRIG_PIN);
}