I_Mihalache_AudioPlayer.ino 7.0 KB

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