forked from W4D/soundcube-firmware
804 lines
20 KiB
C++
804 lines
20 KiB
C++
#include <ArduinoJson.h>
|
|
#include <ArduinoJson.hpp>
|
|
|
|
#include <TCA9555.h>
|
|
#include <AS5600.h>
|
|
#include <DAC8552.h>
|
|
|
|
#define FASTLED_FORCE_SOFTWARE_SPI
|
|
#include <FastLED.h>
|
|
|
|
#include <PWMAudio.h>
|
|
#include <I2S.h>
|
|
#include <SPI.h>
|
|
#include <SD.h>
|
|
#include "codec.h"
|
|
#include "wavestream.h"
|
|
|
|
bool core1_separate_stack = true;
|
|
bool core1_disable_systick = true;
|
|
|
|
#define HAPTIC 1
|
|
#define AURAL 1
|
|
#define UI_SAMPLERATE 22050
|
|
|
|
#define BUFFERSIZE 256
|
|
#define NSTREAMS 16
|
|
|
|
I2S i2s(INPUT_PULLUP);
|
|
|
|
int16_t buffer[BUFFERSIZE];
|
|
|
|
WaveStream stream[NSTREAMS];
|
|
|
|
TLV320AIC3204 codec;
|
|
|
|
TCA9555 TCA(0x20, &Wire1);
|
|
AS5600 ENC(&Wire1);
|
|
|
|
DAC8552 dac(9, &SPI1);
|
|
|
|
PWMAudio ui_snd(8);
|
|
|
|
CRGB ui_leds[74];
|
|
CRGB edge_leds[11];
|
|
|
|
enum STATE {BANK, SAMPLE, SEQUENCE, CTEMPO};
|
|
STATE state = BANK;
|
|
|
|
//enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
|
|
enum BUTTON {MODE, LOOP, INL, INR, OUTR, OUTL, TEMPO, RESET, BACK, POWER, SELECT, DEBUG1, DEBUG2, DEBUG3};
|
|
|
|
int lut_ring_cw[48] = {39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,50,49,48,47,46,45,44,43,42,41,40};
|
|
int lut_ring_ccw[48] = {40,41,42,43,44,45,46,47,48,49,50,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
|
|
|
|
int lut_ring_cw_3[48] = {40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,50,49,48,47,46,45,44,43,42,41};
|
|
int lut_ring_ccw_3[48] = {39,40,41,42,43,44,45,46,47,48,49,50,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38};
|
|
|
|
int lut_matrix[13] = {56,55,57,58,59,62,61,60,63,64,65,67,66};
|
|
|
|
int lut_banks[4] = {56,55,67,66};
|
|
int lut_samples[4] = {62,58,60,64};
|
|
|
|
int active = 0;
|
|
int active_led_ring = 0;
|
|
|
|
int selected_bank = 0;
|
|
int selected_sample = 0;
|
|
|
|
uint32_t lastTime = 0;
|
|
int32_t position = 0;
|
|
|
|
volatile bool setup0_finished = false;
|
|
volatile bool setup1_finished = false;
|
|
|
|
volatile bool buttonChanged = false;
|
|
volatile bool sdInitialized = false;
|
|
|
|
volatile bool wire_ready = false;
|
|
|
|
volatile bool ui_click = false;
|
|
volatile bool ui_beep = false;
|
|
bool amp = false;
|
|
volatile bool streams_loaded = false;
|
|
bool speakerToggle = false;
|
|
bool sd_card_detected = false;
|
|
|
|
volatile bool i2s_ready = false;
|
|
volatile bool codec_ready = false;
|
|
volatile bool config_loaded = false;
|
|
|
|
bool buttons[16] = {false};
|
|
int buttonsDir[16] = {0};
|
|
|
|
int counter = 0;
|
|
|
|
int16_t ui_click_snd[UI_SAMPLERATE];
|
|
uint16_t click_length = 0;
|
|
|
|
int16_t ui_beep_snd[UI_SAMPLERATE];
|
|
uint16_t beep_length = 0;
|
|
|
|
struct Color {
|
|
int r;
|
|
int g;
|
|
int b;
|
|
int r_active;
|
|
int g_active;
|
|
int b_active;
|
|
};
|
|
|
|
struct Config {
|
|
String boxid = "";
|
|
Color edge_color = {0,0,50,0,50,0};
|
|
Color ring_color = {0,50,50,0,80,50};
|
|
String sampleFiles[16];
|
|
int bpm = 90;
|
|
};
|
|
|
|
Config config;
|
|
|
|
struct Sample{
|
|
float volume = 1.0;
|
|
WaveStream *file;
|
|
};
|
|
|
|
Sample samples[NSTREAMS];
|
|
|
|
struct SequenceStep{
|
|
int len = 0;
|
|
Sample *samples[4];
|
|
void trigger(){
|
|
for(int i = 0; i < len; i++){
|
|
samples[i]->file->play(true);
|
|
}
|
|
}
|
|
};
|
|
|
|
SequenceStep steps[16];
|
|
|
|
void loadConfiguration(Config& config) {
|
|
File file = SD.open("/config.txt");
|
|
JsonDocument doc;
|
|
|
|
DeserializationError error = deserializeJson(doc, file);
|
|
if(error) Serial.println(F("Failed to read file, using default configuration"));
|
|
|
|
config.edge_color.r = doc["edge"]["color"]["idle"]["r"] | 0;
|
|
config.edge_color.g = doc["edge"]["color"]["idle"]["g"] | 0;
|
|
config.edge_color.b = doc["edge"]["color"]["idle"]["b"] | 50;
|
|
config.edge_color.r_active = doc["edge"]["color"]["active"]["r"] | 0;
|
|
config.edge_color.g_active = doc["edge"]["color"]["active"]["g"] | 50;
|
|
config.edge_color.b_active = doc["edge"]["color"]["active"]["b"] | 0;
|
|
|
|
config.ring_color.r = doc["ring"]["color"]["idle"]["r"] | 0;
|
|
config.ring_color.g = doc["ring"]["color"]["idle"]["g"] | 50;
|
|
config.ring_color.b = doc["ring"]["color"]["idle"]["b"] | 50;
|
|
config.ring_color.r_active = doc["ring"]["color"]["active"]["r"] | 0;
|
|
config.ring_color.g_active = doc["ring"]["color"]["active"]["g"] | 80;
|
|
config.ring_color.b_active = doc["ring"]["color"]["active"]["b"] | 50;
|
|
|
|
JsonArray _sampleFiles = doc["samples"];
|
|
config.boxid = doc["boxid"].as<String>();
|
|
int i = 0;
|
|
for(JsonVariant item : _sampleFiles){
|
|
config.sampleFiles[i] = item.as<String>();
|
|
i++;
|
|
}
|
|
|
|
file.close();
|
|
}
|
|
|
|
size_t count;
|
|
size_t tape_write = 0;
|
|
|
|
void codec_transmit() {
|
|
for(int i = 0; i < count; i+=2){
|
|
if(streams_loaded){
|
|
for(int k = 0; k < NSTREAMS; k++){
|
|
//int32_t twosamples;
|
|
int16_t twosamples[2];
|
|
if(stream[k].isPlaying()){
|
|
stream[k].getDMA((int32_t*)&twosamples);
|
|
|
|
int16_t sample_l = twosamples[0];// >> 16;
|
|
int16_t sample_r = twosamples[1];// & 0xFFFF;
|
|
|
|
// int16_t *sample_l = stream[k].wavefile.buffer.getReadPointer(); //twosamples >> 16;
|
|
// stream[k].wavefile.buffer.pointerPop(2);
|
|
// int16_t *sample_r = stream[k].wavefile.buffer.getReadPointer(); //twosamples & 0xFFFF;
|
|
// stream[k].wavefile.buffer.pointerPop(2);
|
|
|
|
// buffer[i] += (*sample_l / 16);
|
|
// buffer[i+1] += (*sample_r / 16);
|
|
|
|
buffer[i] += (sample_l / 16);
|
|
buffer[i+1] += (sample_r / 16);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
i2s.write((const uint8_t *)&buffer, count * sizeof(int16_t));
|
|
}
|
|
|
|
void codec_receive(){
|
|
count = i2s.read((uint8_t *)&buffer, BUFFERSIZE * sizeof(int16_t)) * sizeof(uint32_t) / sizeof(int16_t);
|
|
for(int i = 0; i < count; i++){
|
|
buffer[i] *= -1;
|
|
}
|
|
}
|
|
|
|
void pwm_audio_callback() {
|
|
while (ui_snd.availableForWrite()) {
|
|
if(ui_click || ui_beep) {
|
|
if(ui_beep) {
|
|
ui_snd.write(ui_beep_snd[counter]);
|
|
counter++;
|
|
if(counter == beep_length) {
|
|
counter = 0;
|
|
ui_beep = false;
|
|
}
|
|
continue;
|
|
}
|
|
if(ui_click) {
|
|
ui_snd.write(ui_click_snd[counter]);
|
|
counter++;
|
|
if(counter == click_length) {
|
|
counter = 0;
|
|
ui_click = false;
|
|
}
|
|
}
|
|
} else {
|
|
digitalWrite(7, LOW);
|
|
ui_snd.write(0);
|
|
counter = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool load_ui_sounds(const char* file, int16_t *buffer, uint16_t &length){
|
|
if(SD.exists(file)) {
|
|
File f_click = SD.open(file);
|
|
f_click.seek(44, SeekSet);
|
|
int click_bytes = 0;
|
|
while (f_click.available() && click_bytes < UI_SAMPLERATE) {
|
|
uint8_t samplebyte[2];
|
|
f_click.read(samplebyte, 2);
|
|
int16_t sample = (samplebyte[1] << 8) + samplebyte[0];
|
|
buffer[click_bytes] = sample;
|
|
click_bytes++;
|
|
}
|
|
f_click.close();
|
|
length = click_bytes;
|
|
return true;
|
|
} else {
|
|
for(int i = 0; i < UI_SAMPLERATE; i++){
|
|
float sine_pos = (2.0f * M_PI * 3000.0f * (float)i) / (float)UI_SAMPLERATE;
|
|
buffer[i] = (int16_t)(sin(sine_pos) * 8000.0f);
|
|
}
|
|
Serial.println("Synthesized 8kHz sine into beep array.");
|
|
length = UI_SAMPLERATE;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool load_samples(){
|
|
for(int i = 0; i < NSTREAMS; i++){
|
|
stream[i].begin();
|
|
char filename[64];
|
|
sprintf(filename, "/sound/%s/%s", config.boxid.c_str(), config.sampleFiles[i].c_str());
|
|
Serial.println(filename);
|
|
|
|
if(SD.exists(filename)){
|
|
bool loaded = stream[i].load(SD.open(filename));
|
|
if(loaded) {
|
|
Serial.print("file read: ");
|
|
Serial.print(stream[i].wavefile.length);
|
|
Serial.print(" bytes | ");
|
|
Serial.print(stream[i].wavefile.samplerate);
|
|
Serial.print(" kHz | ");
|
|
Serial.print(stream[i].wavefile.bitspersample);
|
|
Serial.print(" bits | ");
|
|
Serial.print(stream[i].wavefile.channels);
|
|
Serial.print(" channels | ");
|
|
Serial.print(stream[i].wavefile.blockalign);
|
|
Serial.println(" bytes");
|
|
//stream[i].wavefile.loop = true;
|
|
} else {
|
|
Serial.println("file loading error");
|
|
}
|
|
} else {
|
|
for(int k = 0; k < 3; k++){
|
|
digitalWrite(6, HIGH);
|
|
delay(20);
|
|
digitalWrite(6, LOW);
|
|
delay(25);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void tca_irq() {
|
|
buttonChanged = true;
|
|
}
|
|
|
|
void speaker(bool state){
|
|
digitalWrite(12, state ? HIGH : LOW);
|
|
digitalWrite(13, state ? HIGH : LOW);
|
|
}
|
|
|
|
struct Vibration{
|
|
int32_t last;
|
|
bool start = false;
|
|
bool wait = false;
|
|
uint16_t on = 0;
|
|
uint16_t off = 0;
|
|
int n = 1;
|
|
|
|
void update(){
|
|
int32_t delta = millis() - last;
|
|
|
|
if(start && !wait){
|
|
digitalWrite(6, HIGH);
|
|
start = false;
|
|
wait = true;
|
|
last = millis();
|
|
}
|
|
|
|
if(delta >= on && wait){
|
|
digitalWrite(6, LOW);
|
|
wait = false;
|
|
n--;
|
|
last = millis();
|
|
}
|
|
|
|
if(delta >= off && n > 0){
|
|
start = true;
|
|
last = millis();
|
|
}
|
|
}
|
|
|
|
void trigger(int _n, int _on, int _off){
|
|
n = _n;
|
|
on = _on;
|
|
off = _off;
|
|
start = true;
|
|
last = millis();
|
|
}
|
|
};
|
|
Vibration vibration;
|
|
|
|
// -------------------------------------------- SETUP 0
|
|
|
|
void setup() {
|
|
i2s.setSysClk(48000);
|
|
|
|
delay(500);
|
|
|
|
Serial.print("1: INIT WIRE: ");
|
|
Wire1.setSDA(2);
|
|
Wire1.setSCL(3);
|
|
Wire1.begin();
|
|
Serial.println("SUCCESS");
|
|
|
|
wire_ready = true;
|
|
|
|
Serial.print("1: INIT SPI: ");
|
|
SPI1.setSCK(10);
|
|
SPI1.setTX(11);
|
|
SPI1.begin();
|
|
Serial.println("SUCCESS");
|
|
delay(100);
|
|
|
|
Serial.print("1: ENABLE CODEC: ");
|
|
pinMode(20, OUTPUT); // CODEC reset (enable)
|
|
digitalWrite(20, HIGH);
|
|
Serial.println("SUCCESS");
|
|
|
|
Serial.print("1: ENABLE I2S MCLK: ");
|
|
pinMode(19, OUTPUT); // MCLK enable
|
|
digitalWrite(19, HIGH); // enable MCLK
|
|
Serial.println("SUCCESS");
|
|
|
|
Serial.print("1: STARTUP");
|
|
|
|
Serial.print("1: INIT TCA: ");
|
|
TCA.begin();
|
|
TCA.pinMode16(0xFFFF);
|
|
TCA.setPolarity16(0x0000);
|
|
Serial.println(" SUCCESS");
|
|
|
|
Serial.print("1: INIT TCA INTERRUPT: ");
|
|
pinMode(1, INPUT_PULLUP);
|
|
attachInterrupt(digitalPinToInterrupt(1), tca_irq, FALLING);
|
|
Serial.println("SUCCESS");
|
|
|
|
Serial.print("1: INIT ROTARY ENCODER: ");
|
|
ENC.begin(); // set direction pin.
|
|
ENC.setDirection(AS5600_CLOCK_WISE);
|
|
ENC.resetCumulativePosition();
|
|
Serial.println("SUCCESS");
|
|
|
|
Serial.print("1: INIT DAC: ");
|
|
dac.begin();
|
|
Serial.println("SUCCESS");
|
|
|
|
pinMode(6, OUTPUT); // Vibration Motor
|
|
pinMode(7, OUTPUT); // UI Amp Enable
|
|
|
|
//ui_snd.onTransmit(pwm_audio_callback);
|
|
//ui_snd.begin(UI_SAMPLERATE);
|
|
|
|
digitalWrite(7, LOW); // UI amp off
|
|
|
|
Serial.print("1: INIT LEDS: ");
|
|
FastLED.addLeds<NEOPIXEL, 4>(edge_leds, 11);
|
|
FastLED.addLeds<NEOPIXEL, 5>(ui_leds, 74);
|
|
Serial.println("SUCCESS");
|
|
|
|
Serial.print("1: STARTUP COMPLETE");
|
|
setup1_finished = true;
|
|
digitalWrite(6, HIGH);
|
|
delay(25);
|
|
digitalWrite(6, LOW);
|
|
Serial.begin();
|
|
|
|
Serial.print("0: INIT CODEC: ");
|
|
codec.begin(&Wire1);
|
|
Serial.println("SUCCESS");
|
|
|
|
codec_ready = true;
|
|
|
|
pinMode(12, OUTPUT); // speaker enable l
|
|
pinMode(13, OUTPUT); // speaker enable r
|
|
speaker(false);
|
|
|
|
pinMode(21, INPUT_PULLUP);
|
|
sd_card_detected = !digitalRead(21);
|
|
delay(500);
|
|
|
|
while(!sdInitialized){
|
|
sdInitialized = SD.begin(22, 23, 24);
|
|
delay(250);
|
|
Serial.println("0: Initializing SD Card");
|
|
}
|
|
|
|
if(sdInitialized) loadConfiguration(config);
|
|
|
|
if(sdInitialized){
|
|
load_ui_sounds("/ui/click.wav", ui_click_snd, click_length);
|
|
load_ui_sounds("/ui/beep.wav", ui_beep_snd, beep_length);
|
|
}
|
|
|
|
config_loaded = true;
|
|
|
|
if(sdInitialized) {
|
|
streams_loaded = load_samples();
|
|
}
|
|
|
|
if(streams_loaded) {
|
|
for(int i = 0; i < NSTREAMS; i++){
|
|
samples[i].file = &stream[i];
|
|
}
|
|
}
|
|
|
|
|
|
i2s.onTransmit(codec_transmit);
|
|
i2s.onReceive(codec_receive);
|
|
|
|
i2s.setDOUT(15);
|
|
i2s.setDIN(14);
|
|
i2s.setBCLK(16); // Note: LRCLK = BCLK + 1
|
|
i2s.setMCLK(18);
|
|
i2s.setMCLKmult(512); // 256 = 12.288.000Hz 512 = 25MHz
|
|
i2s.swapClocks();
|
|
|
|
// i2s.setFrequency(48000);
|
|
i2s.setBitsPerSample(16);
|
|
|
|
i2s.setBuffers(4, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t));
|
|
|
|
if(!i2s.begin(48000)){
|
|
Serial.println("0: I2S error!");
|
|
while(100);
|
|
}
|
|
Serial.println("0: I2S OK");
|
|
|
|
delay(100);
|
|
|
|
i2s_ready = true;
|
|
|
|
Serial.print("0: STARTUP COMPLETE");
|
|
|
|
digitalWrite(6, HIGH);
|
|
delay(25);
|
|
digitalWrite(6, LOW);
|
|
setup0_finished = true;
|
|
setup1_finished = true;
|
|
}
|
|
|
|
void setup1(){
|
|
}
|
|
|
|
uint32_t last = 0;
|
|
volatile int bar = 0;
|
|
volatile int bar_old = -1;
|
|
int set_bar = 0;
|
|
int16_t angle = 0;
|
|
int32_t position_last = 0;
|
|
|
|
int16_t encdelta_raw = 0;
|
|
int16_t encdeltadiv = 512;
|
|
|
|
void loop1() {
|
|
if(setup0_finished && setup1_finished){
|
|
position = ENC.getCumulativePosition();
|
|
angle = ENC.readAngle();
|
|
encdelta_raw += (position - position_last);
|
|
|
|
// Serial.print(encdelta_raw);
|
|
// Serial.print(" \t");
|
|
|
|
int encdelta = 0;
|
|
if(abs(encdelta_raw) > encdeltadiv) {
|
|
encdelta = encdelta_raw > 0 ? 1 : -1;
|
|
encdelta_raw = 0;
|
|
}
|
|
|
|
// Serial.print(angle);
|
|
// Serial.print(" \t");
|
|
// Serial.print(position);
|
|
// Serial.print(" \t");
|
|
// Serial.print(position_last);
|
|
// Serial.print(" \t");
|
|
// Serial.print(encdelta_raw);
|
|
// Serial.print(" \t");
|
|
// Serial.println(encdelta);
|
|
|
|
sd_card_detected = !digitalRead(21);
|
|
edge_leds[8] = sd_card_detected ? CRGB(0,10,0) : CRGB(10,0,0);
|
|
|
|
// EDGE LEDs
|
|
for (int i = 0; i < 8; i++) {
|
|
edge_leds[i] = CRGB(config.edge_color.r, config.edge_color.g, config.edge_color.b);
|
|
}
|
|
|
|
// LED Ring leeren
|
|
for (int i = 0; i < 48; i++) {
|
|
ui_leds[i + 3] = CRGB(0, 0, 0);
|
|
}
|
|
|
|
// Rotary button LEDs
|
|
ui_leds[0] = CRGB(0, 0, 0);
|
|
ui_leds[1] = CRGB(0, 0, 0);
|
|
ui_leds[2] = CRGB(0, 0, 0);
|
|
|
|
// buttonChanged = true when a button is pushed
|
|
if (buttonChanged) {
|
|
int buttonValues = TCA.read16();
|
|
|
|
bool buttonsNew[16] = {false};
|
|
|
|
bool buttonUp = false;
|
|
bool buttonDown = false;
|
|
|
|
for(int i = 0; i < 16; i++){
|
|
buttonsNew[i] = ~(buttonValues >> i) & 0x01;
|
|
if(buttonsNew[i] == true && buttons[i] == false) {
|
|
buttonsDir[i] = -1;
|
|
buttonDown = true;
|
|
}
|
|
if(buttonsNew[i] == false && buttons[i] == true) {
|
|
buttonsDir[i] = 1;
|
|
buttonUp = true;
|
|
}
|
|
buttons[i] = buttonsNew[i];
|
|
}
|
|
|
|
// Make vibration
|
|
if(HAPTIC && buttonDown) {
|
|
vibration.trigger(1, 25, 25);
|
|
}
|
|
|
|
// Make beep
|
|
if(AURAL && buttonDown) {
|
|
digitalWrite(7, HIGH);
|
|
ui_click = true;
|
|
}
|
|
|
|
if(buttons[TEMPO] && buttonDown){
|
|
state = CTEMPO;
|
|
}
|
|
|
|
if(buttons[BACK] && buttonDown){
|
|
}
|
|
|
|
switch(state){
|
|
case BANK:
|
|
encdeltadiv = 512;
|
|
encdelta_raw = 0;
|
|
encdelta = 0;
|
|
if(buttons[SELECT] && buttonDown){
|
|
state = SAMPLE;
|
|
}
|
|
break;
|
|
case SAMPLE:
|
|
encdeltadiv = 512;
|
|
encdelta_raw = 0;
|
|
encdelta = 0;
|
|
if(buttons[SELECT] && buttonDown){
|
|
state = SEQUENCE;
|
|
}
|
|
if(buttons[BACK] && buttonDown){
|
|
state = BANK;
|
|
}
|
|
break;
|
|
case SEQUENCE:
|
|
encdeltadiv = 256;
|
|
encdelta_raw = 0;
|
|
encdelta = 0;
|
|
if(buttons[SELECT] && buttonDown){
|
|
int n = steps[set_bar].len;
|
|
steps[set_bar].samples[n] = &samples[selected_bank*4 + selected_sample];
|
|
steps[set_bar].len = (steps[set_bar].len + 1) % 4;
|
|
}
|
|
if(buttons[BACK] && buttonDown){
|
|
state = SAMPLE;
|
|
}
|
|
break;
|
|
case CTEMPO:
|
|
encdeltadiv = 64;
|
|
encdelta_raw = 0;
|
|
encdelta = 0;
|
|
if(buttons[SELECT] && buttonDown){
|
|
state = BANK;
|
|
}
|
|
if(buttons[BACK] && buttonDown){
|
|
state = BANK;
|
|
}
|
|
break;
|
|
}
|
|
|
|
// Set bar
|
|
// if(buttons[BACK]) set_bar++;
|
|
// if(buttons[POWER]) set_bar--;
|
|
|
|
if(buttons[SELECT]){
|
|
// Flash encoder leds
|
|
ui_leds[0] = CRGB(0, 100, 50);
|
|
ui_leds[1] = CRGB(0, 100, 50);
|
|
ui_leds[2] = CRGB(0, 100, 50);
|
|
}
|
|
|
|
if(buttons[DEBUG1]) {
|
|
Serial.println("vol down");
|
|
codec.volumeDown();
|
|
}
|
|
|
|
if(buttons[DEBUG3]) {
|
|
Serial.println("vol up");
|
|
codec.volumeUp();
|
|
}
|
|
|
|
if(buttons[DEBUG2]) {
|
|
speakerToggle = !speakerToggle;
|
|
speaker(speakerToggle);
|
|
}
|
|
|
|
|
|
Serial.println(codec.getVolumeL());
|
|
|
|
buttonChanged = false;
|
|
}
|
|
|
|
// if(position < 0) position += 4096;
|
|
|
|
switch(state){
|
|
case BANK:
|
|
selected_bank += encdelta;
|
|
if(selected_bank == 4) selected_bank = 0;
|
|
if(selected_bank == -1) selected_bank = 3;
|
|
break;
|
|
case SAMPLE:
|
|
selected_sample += encdelta;
|
|
if(selected_sample == 4) selected_sample = 0;
|
|
if(selected_sample == -1) selected_sample = 3;
|
|
break;
|
|
case SEQUENCE:
|
|
set_bar += encdelta;
|
|
if(set_bar == 16) set_bar = 0;
|
|
if(set_bar == -1) set_bar = 15;
|
|
break;
|
|
case CTEMPO:
|
|
config.bpm += encdelta;
|
|
if(config.bpm < 15) config.bpm = 15;
|
|
if(config.bpm > 300) config.bpm = 300;
|
|
break;
|
|
}
|
|
|
|
|
|
//dac.setValue(0, dactest ? 0 : sin((float)millis() / 100.0f) * 32768 + 32768);
|
|
|
|
// empty LED matrix
|
|
for (int i = 0; i < 4; i++) {
|
|
ui_leds[lut_banks[i]] = CRGB(0, 0, 0);
|
|
ui_leds[lut_samples[i]] = CRGB(0,0,0);
|
|
}
|
|
|
|
// set active LED matrix LED
|
|
ui_leds[lut_banks[selected_bank]] = CRGB(100, 50, 50);
|
|
ui_leds[lut_samples[selected_sample]] = CRGB(100, 0, 50);
|
|
|
|
for(int i = 0; i < 48; i++){
|
|
int step = floor(i/3);
|
|
if(step == set_bar){
|
|
ui_leds[lut_ring_cw_3[i]] = CRGB(config.ring_color.r_active, config.ring_color.g_active, config.ring_color.b_active);
|
|
}
|
|
if(step == bar){
|
|
ui_leds[lut_ring_cw_3[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
|
|
}
|
|
if(steps[step].len > 0) {
|
|
ui_leds[lut_ring_cw_3[i]] = CRGB(0, 10, 0);
|
|
}
|
|
|
|
}
|
|
|
|
FastLED.show();
|
|
|
|
if(buttonChanged){
|
|
for(int i = 0; i < 16; i++){
|
|
if(buttonsDir[i] == 1) buttonsDir[i] = 0;
|
|
}
|
|
}
|
|
|
|
vibration.update();
|
|
position_last = position;
|
|
delay(1);
|
|
}
|
|
}
|
|
//int sc = 0;
|
|
int streamcnt = 0;
|
|
void loop(){
|
|
if(setup1_finished && setup0_finished){
|
|
|
|
uint32_t delta_bpm = floor((60000 / config.bpm) / 16);
|
|
uint32_t delta = millis() - last;
|
|
|
|
if(delta >= delta_bpm) {
|
|
|
|
|
|
bar = (bar + 1) % 16;
|
|
if(steps[bar].len > 0) {
|
|
steps[bar].trigger();
|
|
}
|
|
|
|
if(streams_loaded) {
|
|
for(int i = 0; i < NSTREAMS; i++){
|
|
Serial.print(stream[i].wavefile.buffer.size());
|
|
Serial.print("\t");
|
|
}
|
|
Serial.println();
|
|
}
|
|
|
|
// Serial.print(delta_bpm);
|
|
// Serial.print(" ");
|
|
// Serial.println(delta);
|
|
|
|
last = millis();
|
|
}
|
|
|
|
if(streams_loaded) {
|
|
// for(int i = 0; i < NSTREAMS; i++){
|
|
stream[streamcnt].stream();
|
|
// }
|
|
}
|
|
streamcnt = (streamcnt + 1) % 16;
|
|
|
|
// int16_t sample_l = 0;
|
|
// int16_t sample_r = 0;
|
|
//
|
|
// if(streams_loaded){
|
|
// for(int k = 0; k < NSTREAMS; k++){
|
|
// sample_l += stream[k].get() >> 2;
|
|
// sample_r += stream[k].get() >> 2;
|
|
// }
|
|
// }
|
|
//
|
|
// int16_t l = 0, r = 0;
|
|
// i2s.read16(&l, &r);
|
|
// l += sample_l;
|
|
// r += sample_r;
|
|
// i2s.write16(l, r);
|
|
|
|
if(i2s.getOverflow()) Serial.println("overflow");
|
|
if(i2s.getUnderflow()) Serial.println("underflow");
|
|
|
|
bar_old = bar;
|
|
|
|
}
|
|
}
|
|
|
|
|