PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

player_interrupts.ino 4.4KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /***************************************************
  2. This is an example for the Adafruit VS1053 Codec Breakout
  3. Designed specifically to work with the Adafruit VS1053 Codec Breakout
  4. ----> https://www.adafruit.com/products/1381
  5. Adafruit invests time and resources providing this open source code,
  6. please support Adafruit and open-source hardware by purchasing
  7. products from Adafruit!
  8. Written by Limor Fried/Ladyada for Adafruit Industries.
  9. BSD license, all text above must be included in any redistribution
  10. ****************************************************/
  11. // include SPI, MP3 and SD libraries
  12. #include <SPI.h>
  13. #include <Adafruit_VS1053.h>
  14. #include <SD.h>
  15. // These are the pins used for the breakout example
  16. #define BREAKOUT_RESET 9 // VS1053 reset pin (output)
  17. #define BREAKOUT_CS 10 // VS1053 chip select pin (output)
  18. #define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
  19. // These are the pins used for the music maker shield
  20. #define SHIELD_RESET -1 // VS1053 reset pin (unused!)
  21. #define SHIELD_CS 7 // VS1053 chip select pin (output)
  22. #define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
  23. // These are common pins between breakout and shield
  24. #define CARDCS 4 // Card chip select pin
  25. // DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
  26. #define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
  27. Adafruit_VS1053_FilePlayer musicPlayer =
  28. // create breakout-example object!
  29. //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  30. // create shield-example object!
  31. Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  32. ////
  33. void setup() {
  34. Serial.begin(9600);
  35. Serial.println("Adafruit VS1053 Library Test");
  36. // initialise the music player
  37. if (! musicPlayer.begin()) { // initialise the music player
  38. Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
  39. while (1);
  40. }
  41. Serial.println(F("VS1053 found"));
  42. musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
  43. if (!SD.begin(CARDCS)) {
  44. Serial.println(F("SD failed, or not present"));
  45. while (1); // don't do anything more
  46. }
  47. Serial.println("SD OK!");
  48. // list files
  49. printDirectory(SD.open("/"), 0);
  50. // Set volume for left, right channels. lower numbers == louder volume!
  51. musicPlayer.setVolume(20,20);
  52. /***** Two interrupt options! *******/
  53. // This option uses timer0, this means timer1 & t2 are not required
  54. // (so you can use 'em for Servos, etc) BUT millis() can lose time
  55. // since we're hitchhiking on top of the millis() tracker
  56. //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
  57. // This option uses a pin interrupt. No timers required! But DREQ
  58. // must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
  59. // that's Digital #2 or #3
  60. // See http://arduino.cc/en/Reference/attachInterrupt for other pins
  61. // *** This method is preferred
  62. if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
  63. Serial.println(F("DREQ pin is not an interrupt pin"));
  64. }
  65. void loop() {
  66. // Alternately, we can just play an entire file at once
  67. // This doesn't happen in the background, instead, the entire
  68. // file is played and the program will continue when it's done!
  69. musicPlayer.playFullFile("track001.ogg");
  70. // Start playing a file, then we can do stuff while waiting for it to finish
  71. if (! musicPlayer.startPlayingFile("track001.mp3")) {
  72. Serial.println("Could not open file track001.mp3");
  73. while (1);
  74. }
  75. Serial.println(F("Started playing"));
  76. while (musicPlayer.playingMusic) {
  77. // file is now playing in the 'background' so now's a good time
  78. // to do something else like handling LEDs or buttons :)
  79. Serial.print(".");
  80. delay(1000);
  81. }
  82. Serial.println("Done playing music");
  83. }
  84. /// File listing helper
  85. void printDirectory(File dir, int numTabs) {
  86. while(true) {
  87. File entry = dir.openNextFile();
  88. if (! entry) {
  89. // no more files
  90. //Serial.println("**nomorefiles**");
  91. break;
  92. }
  93. for (uint8_t i=0; i<numTabs; i++) {
  94. Serial.print('\t');
  95. }
  96. Serial.print(entry.name());
  97. if (entry.isDirectory()) {
  98. Serial.println("/");
  99. printDirectory(entry, numTabs+1);
  100. } else {
  101. // files have sizes, directories do not
  102. Serial.print("\t\t");
  103. Serial.println(entry.size(), DEC);
  104. }
  105. entry.close();
  106. }
  107. }