diff --git a/soundcube-firmware/soundcube-firmware.ino b/soundcube-firmware/soundcube-firmware.ino index 7ab184b..a0f0838 100644 --- a/soundcube-firmware/soundcube-firmware.ino +++ b/soundcube-firmware/soundcube-firmware.ino @@ -1,27 +1,56 @@ #include #include +#include +#include -#define HAPTIC 0 +#define HAPTIC 1 +#define AURAL 1 +#define UI_SAMPLERATE 22050 + +I2S i2s(INPUT_PULLUP); TCA9555 TCA(0x20, &Wire1); -//PWMAudio ui_snd(8); +PWMAudio ui_snd(8); Adafruit_NeoPixel edge_pixels(8, 4, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel ui_pixels(72, 5, NEO_GRB + NEO_KHZ800); volatile bool flag = false; +volatile bool click = false; +volatile bool amp = false; -void tca_irq() -{ +int counter = 0; + +int16_t beep[UI_SAMPLERATE]; + +void pwm_audio_callback() { + while (ui_snd.availableForWrite()) { + if(click) { + ui_snd.write(beep[counter]); + } else { + ui_snd.write(0); + } + counter++; + if(counter == UI_SAMPLERATE) counter = 0; + } +} + +void tca_irq() { flag = true; } void setup() { + + for(int i = 0; i < UI_SAMPLERATE; i++){ + float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE; + beep[i] = (int16_t)(sin(sine_pos) * 8000.0f); + } + Serial.begin(); delay(2000); - - Serial.println("INIT WIRE"); + + Serial.println("INIT WIRE"); Wire1.setSDA(2); Wire1.setSCL(3); Wire1.begin(); @@ -38,53 +67,82 @@ void setup() { attachInterrupt(digitalPinToInterrupt(1), tca_irq, FALLING); Serial.println("SUCCESS"); - pinMode(6, OUTPUT); // Vibration Motor - pinMode(7, OUTPUT); // UI Amp Enable + pinMode(6, OUTPUT); // Vibration Motor + pinMode(7, OUTPUT); // UI Amp Enable - //ui_snd.begin(44100); + ui_snd.onTransmit(pwm_audio_callback); + ui_snd.begin(UI_SAMPLERATE); - edge_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) - ui_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + digitalWrite(7, LOW); + + edge_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + ui_pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) + + i2s.setDOUT(14); + i2s.setDIN(15); + i2s.setBCLK(16); // + i2s.swapClocks(); + i2s.setMCLK(18); + i2s.setBitsPerSample(16); + i2s.setFrequency(48000); + i2s.setSysClk(48000) + i2s.begin(); + + while (1) { + int16_t l, r; + i2s.read16(&l, &r); + i2s.write16(l, r); + } } 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' + ui_pixels.clear(); // Set all pixel colors to 'off' - if(flag){ + if (flag) { int val = TCA.read16(); Serial.println(val, BIN); flag = false; - if(HAPTIC){ + if (HAPTIC) { digitalWrite(6, HIGH); + } + if (AURAL) { + digitalWrite(7, HIGH); + click = true; delay(50); + click = false; + digitalWrite(7, LOW); + } + if(HAPTIC){ + if(!AURAL) delay(50); digitalWrite(6, LOW); } } - for(int i = 0; i < 8; i++){ - edge_pixels.setPixelColor(i,edge_pixels.Color(5,5,5)); + for (int i = 0; i < 8; i++) { + edge_pixels.setPixelColor(i, edge_pixels.Color(5, 5, 5)); } - - ui_pixels.setPixelColor(0,edge_pixels.Color(0,0,5)); - ui_pixels.setPixelColor(1,edge_pixels.Color(0,0,5)); - ui_pixels.setPixelColor(2,edge_pixels.Color(0,0,5)); - - for(int i = 0; i < 48; i++){ - if(flag){ - ui_pixels.setPixelColor(i+3,edge_pixels.Color(0,5,0)); + + ui_pixels.setPixelColor(0, edge_pixels.Color(0, 0, 5)); + ui_pixels.setPixelColor(1, edge_pixels.Color(0, 0, 5)); + ui_pixels.setPixelColor(2, edge_pixels.Color(0, 0, 5)); + + for (int i = 0; i < 48; i++) { + if (flag) { + ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 5, 0)); } else { - ui_pixels.setPixelColor(i+3,edge_pixels.Color(0,0,0)); + ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 0, 0)); } } - for(int i = 0; i < 13; i++){ - ui_pixels.setPixelColor(i+3+48+4,edge_pixels.Color(5,0,0)); + for (int i = 0; i < 13; i++) { + ui_pixels.setPixelColor(i + 3 + 48 + 4, edge_pixels.Color(5, 0, 0)); } // put your main code here, to run repeatedly: - edge_pixels.show(); // Set all pixel colors to 'off' - ui_pixels.show(); // Set all pixel colors to 'off' + edge_pixels.show(); // Set all pixel colors to 'off' + ui_pixels.show(); // Set all pixel colors to 'off' + delay(1); -} +} \ No newline at end of file