TrellisSound.ino
  1  // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <Wire.h>
  6  #include "Adafruit_Trellis.h"
  7  #include <SoftwareSerial.h>
  8  #include "Adafruit_Soundboard.h"
  9   
 10  /************ sound board setup ***********/
 11  // Choose any two pins that can be used with SoftwareSerial to RX & TX
 12  #define SFX_TX 5
 13  #define SFX_RX 6
 14  // Connect to the RST pin on the Sound Board
 15  #define SFX_RST 4
 16   
 17  #define SFX_ACT 1 // the 'ACT'ivity LED, to tell us if we're still playing
 18   
 19  // You can also monitor the ACT pin for when audio is playing!
 20  // we'll be using software serial
 21  SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
 22  // pass the software serial to Adafruit_soundboard, the second
 23  // argument is the debug port (not used really) and the third 
 24  // arg is the reset pin
 25  Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
 26   
 27  /************ Trellis setup ***********/
 28   
 29  Adafruit_Trellis matrix0 = Adafruit_Trellis();
 30  Adafruit_TrellisSet trellis =  Adafruit_TrellisSet(&matrix0);
 31   
 32  // set to however many you're working with here, up to 8
 33  #define NUMTRELLIS 1
 34  #define numKeys (NUMTRELLIS * 16)
 35   
 36  // Connect Trellis Vin to 5V and Ground to ground.
 37  // Connect the INT wire to pin #A2 (can change later!)
 38  #define INTPIN A2
 39  // Connect I2C SDA pin to your Arduino SDA line
 40  // Connect I2C SCL pin to your Arduino SCL line
 41   
 42   
 43  char PadToTrack[numKeys][12] = {"T00RAND0WAV",
 44                                  "T00RAND1WAV",
 45                                  "T00RAND2WAV",
 46                                  "T00RAND3WAV",
 47                                  "T10HOLDLWAV",
 48                                 
 49  };       
 50   
 51  /************ main setup ***********/
 52   
 53  void setup() {
 54    Serial.begin(115200);
 55    Serial.println("Trellis Demo");
 56   
 57    // INT pin requires a pullup
 58    pinMode(INTPIN, INPUT);
 59    digitalWrite(INTPIN, HIGH);
 60    // ACT LED
 61    pinMode(SFX_ACT, INPUT);
 62    digitalWrite(SFX_ACT, HIGH); //pullup
 63    
 64    // begin() with the addresses of each panel in order
 65    trellis.begin(0x70);  // only one
 66  
 67    // light up all the LEDs in order
 68    for (uint8_t i=0; i<numKeys; i++) {
 69      trellis.setLED(i);
 70      trellis.writeDisplay();    
 71      delay(50);
 72    }
 73    // then turn them off
 74    for (uint8_t i=0; i<numKeys; i++) {
 75      trellis.clrLED(i);
 76      trellis.writeDisplay();    
 77      delay(50);
 78    }
 79   
 80    // softwareserial at 9600 baud
 81    ss.begin(9600);
 82   
 83    if (!sfx.reset()) {
 84      Serial.println("SFX not found");
 85      while (1);
 86    }
 87    Serial.println("SFX board found");
 88   
 89    uint8_t files = sfx.listFiles();
 90   
 91    Serial.println("File Listing");
 92    Serial.println("========================");
 93    Serial.println();
 94    Serial.print("Found "); Serial.print(files); Serial.println(" Files");
 95    for (uint8_t f=0; f<files; f++) {
 96      Serial.print(f); 
 97      Serial.print("\tname: "); Serial.print(sfx.fileName(f));
 98      Serial.print("\tsize: "); Serial.println(sfx.fileSize(f));
 99    }
100    Serial.println("========================");  
101    
102    trellis.clear();
103    trellis.writeDisplay();
104  }
105   
106  int currentPlaying = -1;
107   
108  void loop() {
109    delay(30); // 30ms delay is required, dont remove me!
110    
111    if (digitalRead(SFX_ACT) && (currentPlaying != -1)) {
112       // *not* playing anything according to ACT lite
113       trellis.clear();
114       trellis.writeDisplay();
115       currentPlaying = -1;
116    }
117    
118    // If a button was just pressed or released...
119    if (trellis.readSwitches()) {
120      // go through every button
121      for (uint8_t i=0; i<numKeys; i++) {
122         // if it was pressed, turn it on
123         if (trellis.isKeyPressed(i) && (i != currentPlaying)) {
124            Serial.print("v"); Serial.println(i);
125      
126            trellis.clear();
127            
128  //          if (! digitalRead(SFX_ACT)) {
129  //            Serial.println("stop..."); // check ACT lite first?
130  //            if (! sfx.stop() ) {
131  //                Serial.println("Failed to stop");
132  //            }
133  //          }
134   
135            // play!         
136            char * filename = PadToTrack[i];
137            int ret = sfx.playTrack(filename);
138            Serial.print("Playing "); Serial.println(filename);
139            
140            if (! ret) {
141              Serial.println("Failed to play track?");
142            } else {
143              trellis.setLED(i);
144            }
145            trellis.writeDisplay();
146            
147            delay(25);  // give it a chance to start playing
148            currentPlaying = i;
149            break;
150        }
151      }
152    }
153  }