I_Mihalache_AudioPlayer.ino 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <Metro.h>
  7. /* ESP commands
  8. * AT+MODE=1
  9. * AT+CWLAP lists wifi AP
  10. * AT+CWJAP="IM_AP","raspberry" connect to IM ap
  11. * AT+CIFSR get IP
  12. * AT+CIPSTART="UDP","192.168.3.1",8000 connect to **
  13. *
  14. */
  15. /* SdWav1 : sample voix |
  16. * SdWav2 : sample attente |-> mix1 -> Lout & RMS1
  17. *
  18. * SdWav3 : sample ambiance -> mix2 -> Rout & RMS2
  19. */
  20. // GUItool: begin automatically generated code
  21. AudioPlaySdWav LoopPlayer; //xy=152,429 LOOP
  22. AudioPlaySdWav PresencePlayer; //xy=153,217 PRESENCE
  23. AudioPlaySdWav WaitPlayer; //xy=155,307 WAIT
  24. AudioMixer4 mixer1; //xy=572,215
  25. AudioMixer4 mixer2; //xy=579,344
  26. AudioAnalyzeRMS rms1; //xy=875,315
  27. AudioOutputI2S i2s1; //xy=876,225
  28. AudioAnalyzeRMS rms2; //xy=876,365
  29. AudioConnection patchCord1(LoopPlayer, 0, mixer2, 0);
  30. AudioConnection patchCord2(PresencePlayer, 0, mixer1, 0);
  31. AudioConnection patchCord3(WaitPlayer, 0, mixer1, 1);
  32. AudioConnection patchCord4(mixer1, 0, i2s1, 0);
  33. AudioConnection patchCord5(PresencePlayer, rms1);
  34. AudioConnection patchCord6(mixer2, 0, i2s1, 1);
  35. AudioConnection patchCord7(mixer2, rms2);
  36. AudioControlSGTL5000 sgtl5000_1; //xy=573,537
  37. // GUItool: end automatically generated code
  38. bool UDPalive ;
  39. Metro checkUDP = Metro(1000);
  40. Metro checkESPserial = Metro(50);
  41. Metro sendRMS = Metro(50);
  42. #define LOOP_WAV "LOOP.WAV"
  43. #define PRESENCE_WAV "PRESENCE.WAV"
  44. #define WAIT_WAV "WAIT.WAV"
  45. int WAIT_TIME=10000 ;
  46. bool presence ;
  47. uint32_t LastPresence ;
  48. // Use these with the Teensy Audio Shield
  49. #define SDCARD_CS_PIN 10
  50. #define SDCARD_MOSI_PIN 7
  51. #define SDCARD_SCK_PIN 14
  52. void ESPSerial(){
  53. // Send bytes from ESP8266 -> Teensy to Computer
  54. bool printLine ;
  55. while ( Serial1.available() ) {
  56. //String buf = Serial1.read();
  57. //Serial.write(F(buf));
  58. Serial.write( Serial1.read() );
  59. // if(buf.readStringUntil("\n")=="OK") {Serial.println("trouvé");}
  60. printLine = 1 ;
  61. }
  62. // if (printLine) {Serial.println("");}
  63. printLine = 0;
  64. // Send bytes from Computer -> Teensy back to ESP8266
  65. while ( Serial.available() ) {
  66. Serial1.write( Serial.read() );
  67. }
  68. }
  69. /////////////////////////////// SETUP /////////////////////////
  70. void setup() {
  71. Serial.begin(115200);
  72. Serial1.begin(115200);
  73. //LDR digital inputs
  74. pinMode(16, INPUT);
  75. pinMode(20,INPUT);
  76. AudioMemory(16);
  77. sgtl5000_1.enable();
  78. sgtl5000_1.lineOutLevel(13); // set output to 3.16 Vpp
  79. sgtl5000_1.volume(1); //set volume to maximum
  80. SPI.setMOSI(SDCARD_MOSI_PIN);
  81. SPI.setSCK(SDCARD_SCK_PIN);
  82. if (!(SD.begin(SDCARD_CS_PIN))) {
  83. // stop here, but print a message repetitively
  84. while (1) {
  85. Serial.println("Unable to access the SD card");
  86. delay(500);
  87. }
  88. }
  89. LoopPlayer.play(LOOP_WAV);
  90. // A brief delay for the library read WAV info
  91. delay(10);
  92. //ESP8266
  93. Serial1.println("AT+GMR");
  94. //while (!Serial1.available()) {};
  95. delay(200);
  96. ESPSerial();
  97. Serial.println("");
  98. Serial.println("");
  99. while(millis()<10000){}
  100. Serial1.println("AT+CIFSR");
  101. ESPSerial();
  102. delay(200);
  103. Serial.println ("");
  104. Serial1.println("AT+CIPSTART=\"UDP\",\"192.168.3.1\",8002");
  105. ESPSerial();
  106. }
  107. ///////////////////////////////////////////////////////////////////
  108. void playFile(const char *filename)
  109. {
  110. Serial.print("Playing file: ");
  111. Serial.println(filename);
  112. PresencePlayer.play(filename);
  113. // A brief delay for the library read WAV info
  114. delay(5);
  115. }
  116. void printLDR(){
  117. Serial.print(analogRead(A3));
  118. Serial.print(" ");
  119. Serial.print(digitalRead(16));
  120. Serial.print(" ");
  121. Serial.print(analogRead(A7));
  122. Serial.print(" ");
  123. Serial.print(digitalRead(20));
  124. Serial.println();
  125. }
  126. bool checkPresence(int Pin1, int Pin2) {
  127. bool isPresent = analogRead(A7)>1010;
  128. //digitalRead(Pin2); //|| digitalRead(Pin2);
  129. Serial.println(isPresent);
  130. return isPresent ;
  131. }
  132. bool checkUDPconnected() {
  133. Serial1.println("AT+CIPSTATUS");
  134. delay(50);
  135. while ( Serial1.available() ) {
  136. String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
  137. if(buf.startsWith("STATUS:2")){
  138. Serial.println("connected");
  139. return true ;
  140. }
  141. else return false;
  142. }
  143. }
  144. void sendUDP() {
  145. if(rms1.available() ) { //&& UDPalive
  146. Serial1.println("AT+CIPSEND=3");
  147. delay(20);
  148. Serial1.write(byte(rms1.read()*255));
  149. Serial1.println("");
  150. //Serial.println("udp");
  151. }
  152. }
  153. /////////////////////////////// LOOP ////////////////////////////////////
  154. void loop() {
  155. // LOOP FILE
  156. if (!LoopPlayer.isPlaying()) {
  157. Serial.println("Playing LOOP file");
  158. LoopPlayer.play(LOOP_WAV);
  159. // A brief delay for the library read WAV info
  160. delay(10);
  161. }
  162. // PRESENCE FILE
  163. presence = checkPresence(16, 20);
  164. //printLDR();
  165. //Serial.println(presence);
  166. //PRESENCE DETECTED
  167. if (presence ) {
  168. LastPresence = millis() ;
  169. if (!PresencePlayer.isPlaying()) {
  170. //stop WAIT_WAV if playing
  171. if (WaitPlayer.isPlaying()) {
  172. //fade out WaitPlayer
  173. Serial.println("Fade out Wait file");
  174. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  175. mixer1.gain(1, 1.0 - fadeRamp/100) ;
  176. }
  177. WaitPlayer.stop() ;
  178. mixer1.gain (1, 1.0); // set volume for next play
  179. delay(2000) ;
  180. }
  181. PresencePlayer.play(PRESENCE_WAV) ;
  182. delay(15);
  183. Serial.println("Playing presence file");
  184. }
  185. // if presence and playing, don't do anything
  186. }
  187. else if (!presence ) {
  188. if (PresencePlayer.isPlaying()) {
  189. //what if presence gone while playing ?
  190. while(rms1.read() > 0) { }
  191. PresencePlayer.stop();
  192. }
  193. if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
  194. WaitPlayer.play(WAIT_WAV);
  195. Serial.println("Playing wait file");
  196. delay(10);
  197. }
  198. }
  199. // if (checkUDP.check() ==1) {
  200. // UDPalive = checkUDPconnected();
  201. // }
  202. // if (checkESPserial.check() == 1) {
  203. //
  204. // }
  205. // ESPSerial();
  206. //
  207. // if (sendRMS.check() == 1) {
  208. // sendUDP();
  209. // }
  210. //
  211. }