From c9b6220449ce6aa590674bc600f1609585863749 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 28 May 2025 00:31:00 +0200 Subject: [PATCH] switch to fastled because of too few pios --- soundcube-firmware/soundcube-firmware.ino | 133 +++++++++++----------- 1 file changed, 64 insertions(+), 69 deletions(-) diff --git a/soundcube-firmware/soundcube-firmware.ino b/soundcube-firmware/soundcube-firmware.ino index 7d89681..3502744 100644 --- a/soundcube-firmware/soundcube-firmware.ino +++ b/soundcube-firmware/soundcube-firmware.ino @@ -22,8 +22,8 @@ PWMAudio ui_snd(8); enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3}; -Adafruit_NeoPixel edge_pixels(11, 4, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel ui_pixels(74, 5, NEO_GRB + NEO_KHZ800); +Adafruit_NeoPixel edge_pixels(11, 4, NEO_GRB + NEO_KHZ800); volatile bool flag = false; volatile bool click = false; @@ -72,7 +72,7 @@ void pwm_audio_callback() { click = false; } } else { - digitalWriteFast(7, LOW); + digitalWrite(7, LOW); ui_snd.write(0); counter = 0; } @@ -85,9 +85,7 @@ void tca_irq() { void setup() { Serial.begin(); - while (!Serial) { - ; - } + delay(2000); pinMode(21, INPUT_PULLUP); sd_card_detected = !digitalRead(21); @@ -102,13 +100,14 @@ void setup() { if(sdInitialized && SD.exists("/click.wav")) { File click = SD.open("/click.wav"); int click_bytes = 0; - int32_t maxv = 0; + int16_t maxv = 0; while (click.available() || click_bytes == UI_SAMPLERATE-1) { - int32_t sample = click.read() * 64; + int16_t sample = click.read() * 32; if(abs(sample) > maxv) maxv = sample; beep[click_bytes] = sample; click_bytes++; } + click.close(); beep_length = click_bytes; Serial.print("Read "); @@ -153,9 +152,14 @@ void setup() { digitalWrite(7, LOW); - edge_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) - ui_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + edge_pixels.begin(); + delay(100); + ui_pixels.begin(); + digitalWrite(6, HIGH); + delay(50); + digitalWrite(6, LOW); + // i2s.setDOUT(14); // i2s.setDIN(15); // i2s.setBCLK(16); // @@ -172,6 +176,7 @@ void setup() { // i2s.write16(l, r); // } } + int active = 0; int active_led_ring = 0; @@ -179,91 +184,81 @@ uint32_t lastTime = 0; int32_t position = 0; void loop() { - edge_pixels.clear(); // Set all pixel colors to 'off' ui_pixels.clear(); // Set all pixel colors to 'off' + edge_pixels.clear(); // Set all pixel colors to 'off' + position = ENC.getCumulativePosition(); + sd_card_detected = !digitalRead(21); if(sd_card_detected) edge_pixels.setPixelColor(8, edge_pixels.Color(0,25,0)); if(!sd_card_detected) edge_pixels.setPixelColor(8, edge_pixels.Color(25,0,0)); - position = ENC.getCumulativePosition(); - - // if (millis() - lastTime >= 100) - // { - // lastTime = millis(); - // Serial.print(ENC.getCumulativePosition()); - // Serial.print("\t"); - // Serial.println(ENC.getRevolutions()); - // } - - // LED Ring leeren - for (int i = 0; i < 48; i++) { - ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 0, 0)); - } - - // flag = true when a button is pushed - if (flag) { - int val = TCA.read16(); - Serial.println(val, BIN); - - for(int i = 0; i < 16; i++){ - buttons[i] = ~(val >> i) & 0x01; - Serial.print(buttons[i]); - Serial.print(" "); - } - Serial.println(); - - flag = false; - if (HAPTIC) { - digitalWrite(6, HIGH); - } - // Make beep - if (AURAL) { - digitalWriteFast(7, HIGH); - click = true; - } - // Make vibration - if(HAPTIC){ - if(!AURAL) delay(50); - digitalWrite(6, LOW); - } - // Flash led ring - for (int i = 0; i < 48; i++) { - ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 15, 0)); - } - // Switch through LED matrix - if(buttons[RIGHT]) active++; - if(buttons[LEFT]) active--; - if(active == 13) active = 0; - if(active == -1) active = 12; - } - // EDGE LEDs for (int i = 0; i < 8; i++) { edge_pixels.setPixelColor(i, edge_pixels.Color(config.edge_color_r, config.edge_color_g, config.edge_color_b)); } + + // LED Ring leeren + for (int i = 0; i < 48; i++) { + ui_pixels.setPixelColor(i + 3, ui_pixels.Color(0, 0, 0)); + } + + // flag = true when a button is pushed + if (flag) { + int val = TCA.read16(); + + for(int i = 0; i < 16; i++){ + buttons[i] = ~(val >> i) & 0x01; + } + + // Make vibration + if (HAPTIC) { + digitalWrite(6, HIGH); + delay(50); + digitalWrite(6, LOW); + } + + // Make beep + if (AURAL) { + digitalWrite(7, HIGH); + click = true; + } + + // Flash led ring + for (int i = 0; i < 48; i++) { + ui_pixels.setPixelColor(i + 3, ui_pixels.Color(0, 15, 0)); + } + + // Switch through LED matrix + if(buttons[RIGHT]) active++; + if(buttons[LEFT]) active--; + if(active == 13) active = 0; + if(active == -1) active = 12; + + flag = false; + } // Rotary button LEDs - ui_pixels.setPixelColor(0, edge_pixels.Color(0, 0, 15)); - ui_pixels.setPixelColor(1, edge_pixels.Color(0, 0, 15)); - ui_pixels.setPixelColor(2, edge_pixels.Color(0, 0, 15)); + ui_pixels.setPixelColor(0, ui_pixels.Color(0, 0, 15)); + ui_pixels.setPixelColor(1, ui_pixels.Color(0, 0, 15)); + ui_pixels.setPixelColor(2, ui_pixels.Color(0, 0, 15)); // empty LED matrix for (int i = 0; i < 13; i++) { - ui_pixels.setPixelColor(i + 3 + 48 + 4, edge_pixels.Color(0, 0, 0)); + ui_pixels.setPixelColor(i + 3 + 48 + 4, ui_pixels.Color(0, 0, 0)); } // set active LED matrix LED - ui_pixels.setPixelColor(active + 3 + 48 + 4, edge_pixels.Color(255, 255, 255)); + ui_pixels.setPixelColor(active + 3 + 48 + 4, ui_pixels.Color(255, 255, 255)); if(position < 0) position += 4096; active_led_ring = (position / 32) % 48; // set active LED ring LED - ui_pixels.setPixelColor(active_led_ring + 3, edge_pixels.Color(255, 255, 255)); + ui_pixels.setPixelColor(active_led_ring + 3, ui_pixels.Color(255, 255, 255)); - edge_pixels.show(); ui_pixels.show(); + edge_pixels.show(); delay(1); // wait 1ms } \ No newline at end of file