Files
soundcube-firmware/soundcube-firmware/soundcube-firmware.ino

191 lines
4.6 KiB
Arduino
Raw Normal View History

2025-05-27 13:13:29 +02:00
#include <TCA9555.h>
#include <AS5600.h>
#include <Adafruit_NeoPixel.h>
#include <PWMAudio.h>
#include <I2S.h>
#define HAPTIC 1
#define AURAL 1
#define UI_SAMPLERATE 22050
I2S i2s(INPUT_PULLUP);
TCA9555 TCA(0x20, &Wire1);
AS5600 ENC(&Wire1);
PWMAudio ui_snd(8);
enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
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;
int counter = 0;
bool buttons[16] = {false};
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");
Wire1.setSDA(2);
Wire1.setSCL(3);
Wire1.begin();
Serial.println("SUCCESS");
Serial.println("INIT TCA");
TCA.begin();
TCA.pinMode16(0xFFFF);
TCA.setPolarity16(0x0000);
Serial.println("SUCCESS");
Serial.println("INIT INTERRUPT");
pinMode(1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(1), tca_irq, FALLING);
Serial.println("SUCCESS");
// Rotary Encoder
ENC.begin(); // set direction pin.
ENC.setDirection(AS5600_COUNTERCLOCK_WISE);
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);
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);
// }
}
int active = 0;
int active_led_ring = 0;
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'
position = ENC.getCumulativePosition();
// if (millis() - lastTime >= 100)
// {
// lastTime = millis();
// Serial.print(ENC.getCumulativePosition());
// Serial.print("\t");
// Serial.println(ENC.getRevolutions());
// }
for (int i = 0; i < 48; i++) {
ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 0, 0));
}
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);
}
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 < 48; i++) {
ui_pixels.setPixelColor(i + 3, edge_pixels.Color(0, 15, 0));
}
if(buttons[RIGHT]) active++;
if(buttons[LEFT]) active--;
if(active == 13) active = 0;
if(active == -1) active = 12;
}
// 11111110 11111111 button right
// EDGE LEDs
for (int i = 0; i < 8; i++) {
edge_pixels.setPixelColor(i, edge_pixels.Color(15, 15, 15));
}
// 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));
for (int i = 0; i < 13; i++) {
ui_pixels.setPixelColor(i + 3 + 48 + 4, edge_pixels.Color(0, 0, 0));
}
ui_pixels.setPixelColor(active + 3 + 48 + 4, edge_pixels.Color(255, 255, 255));
active_led_ring = abs((position / 256) % 48);
ui_pixels.setPixelColor(active_led_ring + 3, edge_pixels.Color(255, 255, 255));
// 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'
delay(1);
2025-05-27 10:28:11 +02:00
}