Procházet zdrojové kódy

cleaning + SD config file

loading config file on SD card to define udp port, wave files read and
hysteresis for sensors
seems to work fine, need testing
Etienne Landon před 7 roky
rodič
revize
af4f95fd83
1 změnil soubory, kde provedl 218 přidání a 159 odebrání
  1. 218 159
      I_Mihalache_AudioPlayer.ino

+ 218 - 159
I_Mihalache_AudioPlayer.ino

@@ -63,7 +63,10 @@ AudioConnection          patchCord7(mixer2, rms2);
 AudioControlSGTL5000     sgtl5000_1;     //xy=573,537
 // GUItool: end automatically generated code
 
-int ID = 1 ;
+// Config variables
+int configPort = 1 ;
+String ID = "ina" ;
+//float hysteresis = 5;
 
 bool UDPalive ;
 
@@ -71,10 +74,14 @@ Metro checkUDP = Metro(1000);
 Metro checkESPserial = Metro(50);
 Metro sendRMS = Metro(50);
 
-#define LOOP_WAV "LOOP2.WAV"
-#define PRESENCE_WAV "PRESENCE.WAV"
-#define WAIT_WAV "WAIT.WAV"
+//#define LOOP_WAV "LOOP2.WAV"
+//#define PRESENCE_WAV "PRESENCE.WAV"
+//#define WAIT_WAV "WAIT.WAV"
 
+String LOOP_WAV;
+String PRESENCE_WAV ;
+
+#define configFileName "config.txt"
 
 int WAIT_TIME=10000 ;
 
@@ -94,6 +101,78 @@ unsigned long int LastPresence ;
 #define SDCARD_MOSI_PIN  7
 #define SDCARD_SCK_PIN   14
 
+////////////////////////////// CONFIG FUNCTIONS  ///////////////////////////
+
+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]) : "";
+}
+
+bool readSDconfig(){
+  File configFile = SD.open(configFileName);
+  while (configFile.available()) {                  //as long as there is data available
+    String buf = configFile.readStringUntil('\n');  // read a line
+    String keyword = getSubstring(buf, ' ', 0);     // update variable according to keyword
+    if (keyword == "port") { configPort = getSubstring(buf, ' ', 1).toInt() ; Serial.print("port : ") ; Serial.println(configPort); }
+    if (keyword == "hysteresis") { hysteresis = getSubstring(buf, ' ', 1).toInt() ; Serial.print("hysteresis : ") ; Serial.println(hysteresis);}
+    if (keyword == "ID") {
+      ID = getSubstring(buf, ' ', 1) ; 
+      Serial.print("ID : ") ; 
+      Serial.println(ID);
+      LOOP_WAV = ID + "_L.wav";
+      PRESENCE_WAV = ID + "_A.wav" ;
+      }
+  }
+  configFile.close();
+}
+  
+////////////////////////////// LDR & AUDIO FUNCTIONS  ///////////////////////////
+
+
+void playFile(const char *filename)
+{
+  Serial.print("Playing file: ");
+  Serial.println(filename);
+  PresencePlayer.play(filename);
+  // A brief delay for the library read WAV info
+  delay(5);
+
+}
+
+void printLDR(){
+  
+  Serial.print(analogRead(A3));
+  Serial.print("   ");
+  Serial.print(digitalRead(16));
+  Serial.print("   ");
+  Serial.print(analogRead(A7));
+  Serial.print("   ");
+  Serial.print(digitalRead(20));
+  Serial.println();
+}
+
+
+bool checkPresence(int Pin1, int Pin2) {
+  bool isPresent = analogRead(A7)>1010;
+  //digitalRead(Pin2); //|| digitalRead(Pin2);
+  Serial.println(isPresent);
+  return isPresent ;
+}
+
+////////////////////////////// ESP FUNCTIONS  ///////////////////////////
+
+
 void ESPSerial(){
    // Send bytes from ESP8266 -> Teensy to Computer
    bool printLine ;
@@ -113,6 +192,85 @@ void ESPSerial(){
 }
 
 
+
+bool checkUDPconnected() {
+  Serial1.println("AT+CIPSTATUS");
+  delay(50);
+  while ( Serial1.available() ) {
+    String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
+    if(buf.startsWith("STATUS:2")){
+      Serial.println("connected");
+      return true ;
+    }
+    else 
+    {
+      int port = 8000 + configPort;
+      Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
+      Serial1.println(port);
+      Serial.println ("Reconnecting UDP");
+      ESPSerial();
+      
+      return false;
+    }
+    
+  }
+}
+void sendUDP() {
+  
+  
+  if(rms1.available() ) { //&& UDPalive
+         Serial1.println("AT+CIPSEND=3");
+         delay(20);
+         Serial1.write(byte(rms1.read()*255));
+         Serial1.println("");
+         //Serial.println("udp");
+       }
+
+}
+
+//////////////////////    LDR event handlers    /////////////////////////////
+
+void Presence() {
+
+  Serial.println("Presence detected");
+  presence = true ;
+
+  if (!PresencePlayer.isPlaying()) {  //!PresencePlayer.isPlaying()) {
+    // Fade out wait file
+    Serial.println("Fade out Wait file");
+    for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
+      mixer1.gain(1, 1.0 - fadeRamp/99) ;
+    }    
+
+    // start presence file
+    mixer1.gain(0, 1.0) ;
+    PresencePlayer.play(PRESENCE_WAV.c_str()) ;
+    delay(15);
+    Serial.println("Playing presence file");   
+  }
+}
+  
+
+void Absence() {
+  LastPresence = millis() ;
+  presence = false ;
+
+  Serial.println("No presence detected");
+
+//  if (PresencePlayer.isPlaying()) {
+//      // if presence gone while playing ?
+//      Serial.println("Fading out Presence file");
+//      for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
+//          mixer1.gain(0, 1.0 - fadeRamp/100) ;
+//          delay(10);
+//        }
+//
+//      
+//      
+//    }
+    
+  }
+
 /////////////////////////////// SETUP /////////////////////////
 
 
@@ -124,11 +282,9 @@ void setup() {
   pinMode(16, INPUT);
   pinMode(20,INPUT);
   
-  AudioMemory(16);
-  sgtl5000_1.enable();
-  sgtl5000_1.lineOutLevel(13);  // set output to 3.16 Vpp
-  sgtl5000_1.volume(1); //set volume to maximum
 
+
+  // load SD config file
   SPI.setMOSI(SDCARD_MOSI_PIN);
   SPI.setSCK(SDCARD_SCK_PIN);
   if (!(SD.begin(SDCARD_CS_PIN))) {
@@ -138,24 +294,25 @@ void setup() {
       delay(500);
     }
   }
-
-  File configFile = SD.open("config.txt");
-  String buf = configFile.readStringUntil('\n');
-  ID = buf.toInt();
-  Serial.print("ID : ");
-  Serial.println(ID);
-  configFile.close();  
-  Serial.println("Playing LOOP and WAIT file");
-  LoopPlayer.play(LOOP_WAV);
-  //WaitPlayer.play(WAIT_WAV);
+  readSDconfig();
+  delay(500);
+  // Start audio
+  AudioMemory(16);
+  sgtl5000_1.enable();
+  sgtl5000_1.lineOutLevel(13);  // set output to 3.16 Vpp
+  sgtl5000_1.volume(1); //set volume to maximum
+  Serial.print("Playing LOOP file ");
+  Serial.println(PRESENCE_WAV);
+  LoopPlayer.play(LOOP_WAV.c_str());
+//  LoopPlayer.play(PRESENCE_WAV.c_str());
 
   // A brief delay for the library read WAV info
   delay(10); 
 
-  
-  setPoint.begin(threshold, hysteresis);
-  setPoint.attach(RISING_EDGE, Presence);
-  setPoint.attach(FALLING_EDGE, Absence);
+//  //Start sensors for calibration
+//  setPoint.begin(threshold, hysteresis);
+//  setPoint.attach(RISING_EDGE, Presence);
+//  setPoint.attach(FALLING_EDGE, Absence);
   
   //ESP8266
   Serial1.println("AT+GMR");
@@ -173,7 +330,7 @@ void setup() {
      counter++;
      delay(100);
     }
-    threshold = sum / counter + hysteresis ;
+    threshold = sum / counter + hysteresis/2 ;
   Serial.print("Threshold calibrated at : ");
   Serial.println(threshold);
   
@@ -181,83 +338,23 @@ void setup() {
   ESPSerial();
   delay(200);
   Serial.println ("");
-  int port = 8000 + ID;
+  int port = 8000 + configPort;
   Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
   Serial1.println(port);
   ESPSerial();
   Serial.println ("");
+
+  // Start sensors handlers
   setPoint.begin(threshold, hysteresis);
   setPoint.attach(RISING_EDGE, Presence);
   setPoint.attach(FALLING_EDGE, Absence);
+  setPoint.update(0);
 }
 
 ///////////////////////////////////////////////////////////////////
 
 
-void playFile(const char *filename)
-{
-  Serial.print("Playing file: ");
-  Serial.println(filename);
-  PresencePlayer.play(filename);
-  // A brief delay for the library read WAV info
-  delay(5);
-
-}
-
-void printLDR(){
-  
-  Serial.print(analogRead(A3));
-  Serial.print("   ");
-  Serial.print(digitalRead(16));
-  Serial.print("   ");
-  Serial.print(analogRead(A7));
-  Serial.print("   ");
-  Serial.print(digitalRead(20));
-  Serial.println();
-}
-
-
-bool checkPresence(int Pin1, int Pin2) {
-  bool isPresent = analogRead(A7)>1010;
-  //digitalRead(Pin2); //|| digitalRead(Pin2);
-  Serial.println(isPresent);
-  return isPresent ;
-}
-
-bool checkUDPconnected() {
-  Serial1.println("AT+CIPSTATUS");
-  delay(50);
-  while ( Serial1.available() ) {
-    String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
-    if(buf.startsWith("STATUS:2")){
-      Serial.println("connected");
-      return true ;
-    }
-    else 
-    {
-      int port = 8000 + ID;
-      Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
-      Serial1.println(port);
-      Serial.println ("Reconnecting UDP");
-      ESPSerial();
-      
-      return false;
-    }
-    
-  }
-}
-void sendUDP() {
-  
-  
-  if(rms1.available() ) { //&& UDPalive
-         Serial1.println("AT+CIPSEND=3");
-         delay(20);
-         Serial1.write(byte(rms1.read()*255));
-         Serial1.println("");
-         //Serial.println("udp");
-       }
 
-}
 
 
 ///////////////////////////////  LOOP ////////////////////////////////////
@@ -272,39 +369,51 @@ void loop() {
   setPoint.update(value);
 
 
-  // LOOP is alwaiys playing
+  // LOOP is always playing
   if (!LoopPlayer.isPlaying()) {
     Serial.println("Playing LOOP file");
-    LoopPlayer.play(LOOP_WAV);
+    LoopPlayer.play(LOOP_WAV.c_str());
     // A brief delay for the library read WAV info
     delay(10); 
-    // also restarts WAIT file
-//    WaitPlayer.play(WAIT_WAV);
-//    delay(10);
   }
-  if (presence) {
+
+  
+  if (presence ) {      //|| millis()-LastPresence < WAIT_TIME
+      // update presence timer
       LastPresence = millis() ;
+      // restart presence player if ended
       if (!PresencePlayer.isPlaying()) {
-        PresencePlayer.play(PRESENCE_WAV);
+        PresencePlayer.play(PRESENCE_WAV.c_str());
         delay(10);
+      }  
+  }
+  
+   //WAIT fades up if no presence during longer than WAIT_TIME
+  if (millis()-LastPresence > WAIT_TIME && presence == false ) { // && 
+    
+    if (PresencePlayer.isPlaying()){
+      Serial.println("Stopping PRESENCE file");
+      
+      Serial.println("Fading down presence and fading up WAIT file");
+      for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
+            mixer1.gain(1, fadeRamp/99) ;
+            mixer1.gain(0, 1.0 - fadeRamp/99) ;
+            delay(10);
       }
-//  if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
+    }
+//      Serial.println("Stopping PRESENCE file");
 //      
-//      mixer1.gain (1, 1.0);
-//      Serial.println("Fading up wait file");
-//      delay(10);
+//      Serial.println("Fading down presence and fading up WAIT file");
+//      for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
+//            mixer1.gain(1, fadeRamp/100) ;
+//            mixer1.gain(0, 1.0 - fadeRamp/100) ;
+//            
+//      }      
+      PresencePlayer.stop();
+      delay(10);
+      Serial.println(PresencePlayer.isPlaying());
 //    }
-
-  
-  }
-   //WAIT fades up if PresencePlayer stops ( 
-  if (millis()-LastPresence > WAIT_TIME && PresencePlayer.isPlaying() && presence == false) {
-    Serial.println("Stopping PRESENCE file");
-    PresencePlayer.stop();
-    Serial.println("Fading up WAIT file");
-    for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
-          mixer1.gain(1, fadeRamp/100) ;
-        }
+//    PresencePlayer.stop();
   }
 
 //  // PRESENCE FILE
@@ -333,55 +442,5 @@ void loop() {
   
 }
 
-//////////////////////    LDR event handlers    /////////////////////////////
-
-void Presence() {
-
-  Serial.println("Presence detected");
-  presence = true ;
-
-  if (!PresencePlayer.isPlaying()) {
-    //stop WAIT_WAV if playing
-    if (1){//WaitPlayer.isPlaying()) {
-        //fade out WaitPlayer
-        Serial.println("Fade out Wait file");
-        for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
-          mixer1.gain(1, 1.0 - fadeRamp/100) ;
-        }
-//        WaitPlayer.stop() ;
-//        mixer1.gain (1, 1.0); // set volume for next play
-//        delay(2000) ; 
-      
-    }
-    PresencePlayer.play(PRESENCE_WAV) ;
-    delay(15);
-    Serial.println("Playing presence file");
-  }   
-    
-    // if presence and playing, fade up to ensure
-    for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
-          mixer1.gain(0, fadeRamp/100) ;
-        }
-        Serial.println("Fading up Presence file");
-  }
-
-void Absence() {
-  LastPresence = millis() ;
-  presence = false ;
-
-  Serial.println("No presence detected");
 
-  if (PresencePlayer.isPlaying()) {
-      //what if presence gone while playing ?
-      Serial.println("Fading out Presence file");
-      for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
-          mixer1.gain(0, 1.0 - fadeRamp/100) ;
-          delay(10);
-        }
-//      while(rms1.read() > 0) {  }
-      
-      
-    }
-    
-  }