I_Mihalache_AudioPlayer.ino 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. int ID = 1 ;
  42. bool UDPalive ;
  43. Metro checkUDP = Metro(1000);
  44. Metro checkESPserial = Metro(50);
  45. Metro sendRMS = Metro(50);
  46. #define LOOP_WAV "LOOP.WAV"
  47. #define PRESENCE_WAV "PRESENCE.WAV"
  48. #define WAIT_WAV "WAIT.WAV"
  49. int WAIT_TIME=10000 ;
  50. const int LDR_PIN = A7;
  51. ResponsiveAnalogRead ldr(LDR_PIN, false);
  52. float threshold = 935;
  53. float hysteresis = 5;
  54. int sensorPin = A7;
  55. SetPoint setPoint;
  56. bool presence ;
  57. uint32_t LastPresence ;
  58. // Use these with the Teensy Audio Shield
  59. #define SDCARD_CS_PIN 10
  60. #define SDCARD_MOSI_PIN 7
  61. #define SDCARD_SCK_PIN 14
  62. void ESPSerial(){
  63. // Send bytes from ESP8266 -> Teensy to Computer
  64. bool printLine ;
  65. while ( Serial1.available() ) {
  66. //String buf = Serial1.read();
  67. //Serial.write(F(buf));
  68. Serial.write( Serial1.read() );
  69. // if(buf.readStringUntil("\n")=="OK") {Serial.println("trouvé");}
  70. printLine = 1 ;
  71. }
  72. // if (printLine) {Serial.println("");}
  73. printLine = 0;
  74. // Send bytes from Computer -> Teensy back to ESP8266
  75. while ( Serial.available() ) {
  76. Serial1.write( Serial.read() );
  77. }
  78. }
  79. /////////////////////////////// SETUP /////////////////////////
  80. void setup() {
  81. Serial.begin(115200);
  82. Serial1.begin(115200);
  83. //LDR digital inputs
  84. pinMode(16, INPUT);
  85. pinMode(20,INPUT);
  86. AudioMemory(16);
  87. sgtl5000_1.enable();
  88. sgtl5000_1.lineOutLevel(13); // set output to 3.16 Vpp
  89. sgtl5000_1.volume(1); //set volume to maximum
  90. SPI.setMOSI(SDCARD_MOSI_PIN);
  91. SPI.setSCK(SDCARD_SCK_PIN);
  92. if (!(SD.begin(SDCARD_CS_PIN))) {
  93. // stop here, but print a message repetitively
  94. while (1) {
  95. Serial.println("Unable to access the SD card");
  96. delay(500);
  97. }
  98. }
  99. File configFile = SD.open("config.txt");
  100. String buf = configFile.readStringUntil('\n');
  101. ID = buf.toInt();
  102. Serial.print("ID : ");
  103. Serial.println(ID);
  104. configFile.close();
  105. Serial.println("Playing LOOP and WAIT file");
  106. LoopPlayer.play(LOOP_WAV);
  107. WaitPlayer.play(WAIT_WAV);
  108. // A brief delay for the library read WAV info
  109. delay(10);
  110. setPoint.begin(threshold, hysteresis);
  111. setPoint.attach(RISING_EDGE, Presence);
  112. setPoint.attach(FALLING_EDGE, Absence);
  113. //ESP8266
  114. Serial1.println("AT+GMR");
  115. //while (!Serial1.available()) {};
  116. delay(200);
  117. ESPSerial();
  118. Serial.println("");
  119. Serial.println("");
  120. float sum ;
  121. float counter ;
  122. while(millis()<10000){ // wait for ESP to connect to its configured wifi
  123. //make a mean value of LDR readings in between
  124. ldr.update();
  125. sum += ldr.getValue();
  126. counter++;
  127. delay(100);
  128. }
  129. threshold = sum / counter + hysteresis ;
  130. Serial.print("Threshold calibrated at : ");
  131. Serial.println(threshold);
  132. Serial1.println("AT+CIFSR");
  133. ESPSerial();
  134. delay(200);
  135. Serial.println ("");
  136. int port = 8000 + ID;
  137. Serial1.print("AT+CIPSTART=\"UDP\",\"192.168.3.1\",");
  138. Serial1.println(port);
  139. ESPSerial();
  140. Serial.println ("");
  141. setPoint.begin(threshold, hysteresis);
  142. setPoint.attach(RISING_EDGE, Presence);
  143. setPoint.attach(FALLING_EDGE, Absence);
  144. }
  145. ///////////////////////////////////////////////////////////////////
  146. void playFile(const char *filename)
  147. {
  148. Serial.print("Playing file: ");
  149. Serial.println(filename);
  150. PresencePlayer.play(filename);
  151. // A brief delay for the library read WAV info
  152. delay(5);
  153. }
  154. void printLDR(){
  155. Serial.print(analogRead(A3));
  156. Serial.print(" ");
  157. Serial.print(digitalRead(16));
  158. Serial.print(" ");
  159. Serial.print(analogRead(A7));
  160. Serial.print(" ");
  161. Serial.print(digitalRead(20));
  162. Serial.println();
  163. }
  164. bool checkPresence(int Pin1, int Pin2) {
  165. bool isPresent = analogRead(A7)>1010;
  166. //digitalRead(Pin2); //|| digitalRead(Pin2);
  167. Serial.println(isPresent);
  168. return isPresent ;
  169. }
  170. bool checkUDPconnected() {
  171. Serial1.println("AT+CIPSTATUS");
  172. delay(50);
  173. while ( Serial1.available() ) {
  174. String buf = Serial1.readStringUntil('\n'); //StringUntil('\n') ;
  175. if(buf.startsWith("STATUS:2")){
  176. Serial.println("connected");
  177. return true ;
  178. }
  179. else return false;
  180. }
  181. }
  182. void sendUDP() {
  183. if(rms1.available() ) { //&& UDPalive
  184. Serial1.println("AT+CIPSEND=3");
  185. delay(20);
  186. Serial1.write(byte(rms1.read()*255));
  187. Serial1.println("");
  188. //Serial.println("udp");
  189. }
  190. }
  191. /////////////////////////////// LOOP ////////////////////////////////////
  192. void loop() {
  193. // LOOP FILE
  194. ldr.update();
  195. int value = ldr.getValue();
  196. //Serial.println(value);
  197. setPoint.update(value);
  198. // LOOP is alwaiys playing
  199. if (!LoopPlayer.isPlaying()) {
  200. Serial.println("Playing LOOP file");
  201. LoopPlayer.play(LOOP_WAV);
  202. // A brief delay for the library read WAV info
  203. delay(10);
  204. // also restarts WAIT file
  205. WaitPlayer.play(WAIT_WAV);
  206. delay(10);
  207. }
  208. if ( (millis() - LastPresence > WAIT_TIME) && !WaitPlayer.isPlaying() && !PresencePlayer.isPlaying()) {
  209. mixer1.gain (1, 1.0);
  210. Serial.println("Fading up wait file");
  211. delay(10);
  212. }
  213. if (presence) {
  214. LastPresence = millis() ;
  215. }
  216. //WAIT fades up if PresencePlayer stops (
  217. if (millis()-LastPresence > WAIT_TIME && PresencePlayer.isPlaying() && presence == false) {
  218. Serial.println("Stopping PRESENCE file");
  219. PresencePlayer.stop();
  220. Serial.println("Fading up WAIT file");
  221. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  222. mixer1.gain(1, fadeRamp/100) ;
  223. }
  224. }
  225. // // PRESENCE FILE
  226. // presence = checkPresence(16, 20);
  227. // //printLDR();
  228. // //Serial.println(presence);
  229. //
  230. // //PRESENCE DETECTED
  231. // if (presence ) {
  232. //
  233. // else if (!presence ) {
  234. // if (checkUDP.check() ==1) {
  235. // UDPalive = checkUDPconnected();
  236. // }
  237. // if (checkESPserial.check() == 1) {
  238. //
  239. // }
  240. // ESPSerial();
  241. //
  242. // if (sendRMS.check() == 1) {
  243. // sendUDP();
  244. // }
  245. //
  246. }
  247. ////////////////////// LDR event handlers /////////////////////////////
  248. void Presence() {
  249. Serial.println("Presence detected");
  250. presence = true ;
  251. if (!PresencePlayer.isPlaying()) {
  252. //stop WAIT_WAV if playing
  253. if (WaitPlayer.isPlaying()) {
  254. //fade out WaitPlayer
  255. Serial.println("Fade out Wait file");
  256. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  257. mixer1.gain(1, 1.0 - fadeRamp/100) ;
  258. }
  259. // WaitPlayer.stop() ;
  260. // mixer1.gain (1, 1.0); // set volume for next play
  261. // delay(2000) ;
  262. }
  263. PresencePlayer.play(PRESENCE_WAV) ;
  264. delay(15);
  265. Serial.println("Playing presence file");
  266. }
  267. // if presence and playing, fade up to ensure
  268. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  269. mixer1.gain(0, fadeRamp/100) ;
  270. }
  271. }
  272. void Absence() {
  273. LastPresence = millis() ;
  274. presence = false ;
  275. Serial.println("No presence detected");
  276. if (PresencePlayer.isPlaying()) {
  277. //what if presence gone while playing ?
  278. Serial.println("Fading out Presence file");
  279. for (float fadeRamp ; fadeRamp < 100 ; fadeRamp++ ) {
  280. mixer1.gain(0, 1.0 - fadeRamp/100) ;
  281. delay(10);
  282. }
  283. // while(rms1.read() > 0) { }
  284. }
  285. }