Adafruit.Projects/Itsybitsy32u4_Trellis_AudioFXSoundboard.ino

175 lines
4.2 KiB
C++

#include <Wire.h>
#include "Adafruit_Trellis.h"
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
// Sound FX Board setup
#define SFX_TX 9
#define SFX_RX 10
#define SFX_RST 4
#define SFX_ACT A1
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
// Trellis setup
#define INTPIN A2
Adafruit_Trellis matrix0 = Adafruit_Trellis();
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0);
#define NUMTRELLIS 1
#define numKeys (NUMTRELLIS * 16)
// Sketch-specific global variables
char page1TrackNames[12][12] = {
"DOIT OGG",
"MAKEIT OGG",
"WORKIT OGG",
"",
"HARDER OGG",
"BETTER OGG",
"FASTER OGG",
"STRONGEROGG",
};
char page2TrackNames[12][12] = {
"CRICK OGG",
"FAIL OGG",
"BITEASS OGG",
"MARIO OGG",
"PADUM OGG",
"PHONE OGG",
"WINNER OGG",
"WRONG OGG"
};
char page3TrackNames[12][12] = {
"WRONG OGG"
};
char page4TrackNames[12][12] = {
"WRONG OGG"
};
int alternate[16] = {0,2,5,7,8,10,13,15,1,3,4,6,9,11,12,14};
int selectedPage = 12;
void setup() {
Serial.begin(115200);
// Serial.println("Trellis Demo");
pinMode(INTPIN, INPUT);
digitalWrite(INTPIN, HIGH);
pinMode(SFX_ACT, INPUT);
digitalWrite(SFX_ACT, HIGH); //pullup
trellis.begin(0x70);
// Test/showoff all leds
for (uint8_t i=0; i<16; i++) {
trellis.setLED(alternate[i]);
trellis.writeDisplay();
delay(50);
}
trellis.blinkRate(1);
// softwareserial at 9600 baud
ss.begin(9600);
if (!sfx.reset()) {
Serial.println("SFX not found");
while (1);
}
Serial.println("SFX board found");
/* do {
currentVolume = sfx.volDown();
Serial.print("Lowering volume to: "); Serial.println(currentVolume);
} while (currentVolume > MINVOLUME);
*/
uint8_t files = sfx.listFiles();
Serial.println("===== File Listing =====");
Serial.print("Found "); Serial.print(files); Serial.println(" Files");
for (uint8_t f=0; f<files; f++) {
Serial.print(f);
Serial.print("\tname: "); Serial.print(sfx.fileName(f));
Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
}
Serial.println("========================");
trellis.clear();
trellis.blinkRate(0);
trellis.setLED(selectedPage);
trellis.writeDisplay();
}
int lastPlayed = -1;
char * trackName;
void loop() {
delay(30); // 30ms delay is required, dont remove me!
if (!digitalRead(SFX_ACT)) {
trellis.setLED(lastPlayed);
trellis.writeDisplay();
} else {
trellis.clear();
// lastPlayed = -1;
trellis.setLED(selectedPage);
trellis.writeDisplay();
}
if (trellis.readSwitches()) {
Serial.print("Switch change: ");
for (uint8_t i=0; i<numKeys; i++) {
if (trellis.justPressed(i)){
Serial.print(i); Serial.println(" pressed...");
switch ((selectedPage * 100) + i) {
case 1200 ... 1211:
trackName = page1TrackNames[i];
playTrack(1, i, trackName);
break;
case 1300 ... 1311:
trackName = page2TrackNames[i];
playTrack(2, i, trackName);
break;
case 1400 ... 1411:
trackName = page3TrackNames[i];
playTrack(3, i, trackName);
break;
case 1500 ... 1511:
trackName = page4TrackNames[i];
playTrack(4, i, trackName);
break;
case 1212 ... 1215:
case 1312 ... 1315:
case 1412 ... 1415:
case 1512 ... 1515:
selectedPage = i;
trellis.setLED(selectedPage);
trellis.writeDisplay();
break;
}
} else if (trellis.justReleased(i)) {
Serial.print(i); Serial.println(" released...");
}
}
}
}
void playTrack(int page, int key, char* trackName) {
if (!digitalRead(SFX_ACT)) {
Serial.println("Interrupting playback..."); // check ACT lite first?
if (! sfx.stop() ) {
Serial.println("Failed to stop playback!");
}
}
int ret = sfx.playTrack(trackName);
if (!ret) {
Serial.println("Failed to play track...");
} else {
Serial.print("Playing sound "); Serial.print(trackName); Serial.print(" (page "); Serial.print(page); Serial.print(")...");
lastPlayed = key;
trellis.setLED(key);
trellis.writeDisplay();
}
}