From ffdbe9fa6deaacc1f542763af4fb3baa5ff5e7dd Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 20 Jun 2025 15:52:02 +0200 Subject: [PATCH] sampling in progress --- soundcube-firmware/soundcube-firmware.ino | 188 ++++++++++++++++------ 1 file changed, 143 insertions(+), 45 deletions(-) diff --git a/soundcube-firmware/soundcube-firmware.ino b/soundcube-firmware/soundcube-firmware.ino index c1631ef..375d283 100644 --- a/soundcube-firmware/soundcube-firmware.ino +++ b/soundcube-firmware/soundcube-firmware.ino @@ -1,4 +1,3 @@ - #include #include @@ -25,8 +24,6 @@ int16_t buffer[BUFFERSIZE]; WaveStream stream[NSTREAMS]; -bool streams_loaded = false; - I2S i2s(INPUT_PULLUP); TLV320AIC3204 codec; @@ -38,7 +35,7 @@ DAC8552 dac(9, &SPI1); PWMAudio ui_snd(8); -enum STATE {IDLE, MODE_A, MODE_B, MODE_C}; +enum STATE {IDLE, BANK_A, BANK_B, BANK_C, BANK_D, CTEMPO}; STATE state = IDLE; //enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3}; @@ -51,8 +48,6 @@ int lut_matrix[13] = {56,55,57,58,59,62,61,60,63,64,65,67,66}; int active = 0; int active_led_ring = 0; -bool speakerToggle = false; - uint32_t lastTime = 0; int32_t position = 0; @@ -60,13 +55,18 @@ CRGB ui_leds[74]; CRGB edge_leds[11]; volatile bool buttonChanged = false; -volatile bool ui_click = false; -volatile bool ui_beep = false; -volatile bool amp = false; -int counter = 0; +bool ui_click = false; +bool ui_beep = false; +bool amp = false; +bool streams_loaded = false; +bool speakerToggle = false; +bool sd_card_detected = false; bool buttons[16] = {false}; +int buttonsDir[16] = {0}; + +int counter = 0; int16_t ui_click_snd[UI_SAMPLERATE]; uint16_t click_length = 0; @@ -74,8 +74,6 @@ uint16_t click_length = 0; int16_t ui_beep_snd[UI_SAMPLERATE]; uint16_t beep_length = 0; -bool sd_card_detected = false; - struct Color { int r; int g; @@ -269,6 +267,46 @@ void speaker(bool state){ 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; + } +}; +Vibration vibration; + void setup() { Serial.begin(); delay(1000); @@ -380,29 +418,43 @@ void setup() { digitalWrite(6, LOW); } -bool dactest = false; uint32_t last = 0; -bool tick = false; int bar = 0; 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 = 1; void loop() { position = ENC.getCumulativePosition(); + angle = ENC.readAngle(); + encdelta_raw += (position - position_last); + + 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(encdelta_raw); + Serial.print(" \t"); + Serial.println(encdelta); uint32_t delta_bpm = floor((60000 / config.bpm) / 16); if(millis() - last >= delta_bpm) { - tick = true; + bar = (bar + 1) % 16; last = millis(); } - if(tick) bar = (bar + 1) % 16; - tick = false; - sd_card_detected = !digitalRead(21); - if(sd_card_detected) edge_leds[8] = CRGB(0,10,0); - if(!sd_card_detected) edge_leds[8] = CRGB(10,0,0); + edge_leds[8] = sd_card_detected ? CRGB(0,10,0) : CRGB(10,0,0); // EDGE LEDs for (int i = 0; i < 8; i++) { @@ -424,39 +476,74 @@ void loop() { int buttonValues = TCA.read16(); bool buttonsNew[16] = {false}; - int buttonsDir[16] = {0}; 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; + buttonsDir[i] = -1; buttonDown = true; } if(buttonsNew[i] == false && buttons[i] == true) { - buttonsDir[i] = -1; + buttonsDir[i] = 1; buttonUp = true; } buttons[i] = buttonsNew[i]; } // Make vibration - if (HAPTIC && buttonDown) { - digitalWrite(6, HIGH); - delay(50); - digitalWrite(6, LOW); + if(HAPTIC && buttonDown) { + vibration.trigger(1, 25, 25); } // Make beep - if (AURAL && buttonDown) { + if(AURAL && buttonDown) { digitalWrite(7, HIGH); ui_click = true; } + if(buttons[TEMPO]){ + state = CTEMPO; + } + + switch(state){ + case IDLE: + break; + case BANK_A: + if(buttons[SELECT] && buttonDown){ + int n = steps[set_bar].len; + steps[set_bar].samples[n] = &samples[0]; + steps[set_bar].len = (steps[set_bar].len + 1) % 4; + } + break; + case BANK_B: + break; + case BANK_C: + break; + case BANK_D: + break; + case CTEMPO: + encdeltadiv = 64; + encdelta_raw = 0; + encdelta = 0; + if(buttons[SELECT] && buttonDown){ + state = IDLE; + } + 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"); @@ -473,19 +560,6 @@ void loop() { speaker(speakerToggle); } - if(buttons[SELECT]){ - //stream[active % NSTREAMS].toggle(); - //ui_beep = true; - - int n = steps[set_bar].len; - steps[set_bar].samples[n] = &samples[0]; - steps[set_bar].len = (steps[set_bar].len + 1) % 4; - - // Flash encoder leds - ui_leds[0] = CRGB(0, 100, 50); - ui_leds[1] = CRGB(0, 100, 50); - ui_leds[2] = CRGB(0, 100, 50); - } Serial.println(codec.getVolumeL()); @@ -493,10 +567,26 @@ void loop() { } if(position < 0) position += 4096; - set_bar = (position / 256) % 16; + + switch(state){ + case IDLE: + break; + case BANK_A: + set_bar = (position / 256) % 16; + break; + case BANK_B: + break; + case BANK_C: + break; + case BANK_D: + break; + case CTEMPO: + config.bpm += encdelta; + if(config.bpm < 15) config.bpm = 15; + if(config.bpm > 300) config.bpm = 300; + break; + } -// if(set_bar == 16) set_bar = 0; -// if(set_bar == -1) set_bar = 15; //dac.setValue(0, dactest ? 0 : sin((float)millis() / 100.0f) * 32768 + 32768); @@ -545,9 +635,17 @@ void loop() { } } + if(buttonChanged){ + for(int i = 0; i < 16; i++){ + if(buttonsDir[i] == 1) buttonsDir[i] = 0; + } + } + if(i2s.getOverflow()) Serial.println("overflow"); if(i2s.getUnderflow()) Serial.println("underflow"); //delay(20); // wait 1ms bar_old = bar; -} \ No newline at end of file + vibration.update(); + position_last = position; +}