Bladeren bron

SD parser

added SD file parsing line by line
each line starts with a selector that triggers different functions
audio first implementation
Etienne Landon 8 jaren geleden
bovenliggende
commit
954a487f8a
1 gewijzigde bestanden met toevoegingen van 98 en 26 verwijderingen
  1. 98 26
      DownTigerDown/DownTigerDown.ino

+ 98 - 26
DownTigerDown/DownTigerDown.ino

@@ -2,6 +2,7 @@
 #include "Adafruit_TLC59711.h"
 #include <SD.h>
 #include <SPI.h>
+#include <Tsunami.h> 
 
 const int BUTTON_PIN[10] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39} ;
 int currentButton ;
@@ -15,9 +16,14 @@ uint32_t ledState[12] ;
 
 // SD card
 File btnFile ;
-String buffer;
+String buf;
 boolean SDfound;
 
+//Tsunami wav player
+Tsunami tsunami;
+int  gNumTracks;                // Number of tracks on SD card
+char gTsunamiVersion[VERSION_STRING_LEN];    // Tsunami version string
+
 //Eventually pointers to manage creation/deletion of listeners
 EvtManager mgr;
 
@@ -33,11 +39,13 @@ EvtListener *plist2;
 //Functions prototypes
 void EvtResetButtonContext ();
 void tlcWrite();
+void buttonFocus(int);
 //////////////////////// SETUP /////////////////////////////////
 
 void setup() {
   Serial.begin(9600);
 
+  // TLC
   tlc.begin();
   tlc.write();
 
@@ -50,12 +58,43 @@ void setup() {
       tlcWrite();
    }  
 
+   // SD
+   if (SDfound == 0) {
+    if (!SD.begin(BUILTIN_SDCARD)) {
+      Serial.print("The SD card cannot be found");
+      while(1);
+    }
+  }
+  SDfound = 1;
+
+  // TSUNAMI
+  // We should wait for the Tsunami to finish reset 
+  delay(1000);
+  tsunami.start();
+  delay(10);
+  tsunami.stopAllTracks();
+  tsunami.samplerateOffset(0, 0);
+  tsunami.setReporting(true);
+  delay(100); 
+  if (tsunami.getVersion(gTsunamiVersion, VERSION_STRING_LEN)) {
+    Serial.print(gTsunamiVersion);
+    Serial.print("\n");
+    gNumTracks = tsunami.getNumTracks();
+    Serial.print("Number of tracks = ");
+    Serial.print(gNumTracks);
+    Serial.print("\n");
+  }
+  else
+    Serial.print("WAV Trigger response not available");
+
   //Set button pins as input
   for (int i ; i < sizeof(BUTTON_PIN)/sizeof(int) ; i++) {
     pinMode(BUTTON_PIN[i],INPUT_PULLUP) ; 
   }
 
-  EvtResetButtonContext();
+  //EvtResetButtonContext();
+  buttonFocus(0) ;
+  
 //  plist = new EvtTimeListener(1000, true, (EvtAction)blinkme);
 //  plist2 = new EvtTimeListener(100, true, (EvtAction)blinkme2);
 //  mgr.addListener( plist ); 
@@ -63,37 +102,70 @@ void setup() {
 }
 
 
-/////////////////////// SD ////////////////////////////////////////////
+///////////////////////// SD ////////////////////////////////////////////
 
-void readSDFile() {
- 
+String getSubstring(String dataString, char separator, int index)
+{
+    int found = 0;
+    int strIndex[] = { 0, -1 };
+    int maxIndex = dataString.length() - 1;
 
+    for (int i = 0; i <= maxIndex && found <= index; i++) {
+        if (dataString.charAt(i) == separator || i == maxIndex) {
+            found++;
+            strIndex[0] = strIndex[1] + 1;
+            strIndex[1] = (i == maxIndex) ? i+1 : i;
+        }
+    }
+    return found > index ? dataString.substring(strIndex[0], strIndex[1]) : "";
+}
 
+
+
+void readSDFile() {
+ 
   while (btnFile.available()) {
-    buffer = btnFile.readStringUntil('\n');
+    buf = btnFile.readStringUntil('\n');
     //check selector (1st word of line) and execute corresponding function
-    String selector = buffer.substring(0, buffer.indexOf(" "));
+    String selector = getSubstring(buf, ' ', 0);
     Serial.println(selector);
-//    switch (selector) {
-//      case playWav :
-//      
-    delay(500);
-//  }
+    if (selector == "sound") {
+      String track = getSubstring(buf, ' ', 2) ;
+      String out = getSubstring(buf, ' ', 1) ;
+      Serial.println("Playing track " + track + "to output " + out);
+      tsunami.trackPlayPoly(track.toInt(), out.toInt(), true);
+      
+    }
+    else if (selector == "led") {
+      Serial.println("Led mode " + getSubstring(buf, ' ', 1));
+    }
+    else if (selector == "dimmer") {
+      Serial.println("Dimmer fade à " + getSubstring(buf, ' ', 1) + " en " + getSubstring(buf, ' ', 2) + "ms");
+    }
+    else if (selector == "delay") {
+      String wait = getSubstring(buf, ' ', 1);
+      Serial.println("Waiting " + wait + " ms");
+      delay(wait.toInt());
+    }
+    else {Serial.println("pas compris");}
+    
+    
+  }
   Serial.println("EOF");
   btnFile.close();
-  }
+  
 }
 
-///////////////////////////Events/////////////////////////////////
-/*resetButtonContext create a listener for each button
- * when triggered, thoses listener execute the same functions with the index of the pressed button  :
- * resetButtonContext and kill any running sequence 
- * buttonFocus -> lights the current button and shuts the others
- * readSequence -> reads a text file on the SD card with the sequence of actions to execute
- */
-
-
-
+/////////////////////////////Events/////////////////////////////////
+///*resetButtonContext create a listener for each button
+// * when triggered, thoses listener execute the same functions with the index of the pressed button  :
+// * resetButtonContext and kill any running sequence 
+// * buttonFocus -> lights the current button and shuts the others
+// * readSequence -> reads a text file on the SD card with the sequence of actions to execute
+// */
+//
+//
+//
 void tlcWrite(){
   for (int ledIndex ; ledIndex < 12 ; ledIndex++) { //sizeof(ledState)/sizeof(uint32_t)
     
@@ -126,11 +198,11 @@ void buttonFocus(int index) {
   String file = "button";
   file+=index ;
   file+=".txt";
-  btnFile = SD.open(file) ;
-  Serial.println(file);
+  btnFile = SD.open("button0.txt") ;
+  Serial.println("opening " + file);
   if (!file) {
     Serial.print("The text file cannot be opened");
-    return false;
+//    return false;
    }
    readSDFile();