Date: Thu, 28 Mar 2024 23:06:23 +0000 (UTC) Message-ID: <1421310279.1419.1711667183729@6f1fcedf7f04> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_1418_327151526.1711667183728" ------=_Part_1418_327151526.1711667183728 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
/* * PushButton * - Plays a random mp3 file from the SD card. *=20 * - Place mp3s in a folder off of root. * - Remove all hidden files. *=20 * For Mac User * NOTE: If you are using Mac OS X to copy the mp3, the file system will aut= omatically add hidden files like: "._0001.mp3"=20 * for index, which this module will handle as valid mp3 files. It is really= annoying. So you can run following command in=20 * terminal to eliminate those files. *=20 * dot_clean /Volumes/<SDVolumeName> *=20 * Please replace the to the volume name of your SD card */ #include <SoftwareSerial.h> #include <DFMiniMp3.h> #include <JC_Button.h> #define BUTTON_PIN D1 /** * Mp3Notify Notification Class */ class Mp3Notify { public: static void OnError(uint16_t errorCode) { =09// see DfMp3_Error for code meaning =09=09Serial.println(); =09=09Serial.print("Com Error "); =09=09Serial.println(errorCode); =09} =09static void OnPlayFinished(uint16_t track) =09{ =09=09Serial.print("Play finished for #"); =09=09Serial.println(track);=20 =09} =09static void OnCardOnline(uint16_t code) =09{ =09=09Serial.println("Card online "); =09} =09static void OnUsbOnline(uint16_t code) =09{ =09=09Serial.println("USB Disk online "); =09} =09static void OnCardInserted(uint16_t code) =09{ =09=09Serial.println("Card inserted "); =09} =09static void OnUsbInserted(uint16_t code) =09{ =09=09Serial.println("USB Disk inserted "); =09} =09static void OnCardRemoved(uint16_t code) =09{ =09=09Serial.println("Card removed "); =09} =09static void OnUsbRemoved(uint16_t code) =09{ =09=09Serial.println("USB Disk removed "); =09} }; SoftwareSerial secondarySerial(D5, D0); // RX, TX DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial); Button button(BUTTON_PIN);=20 int *sounds; int track=3D0; int count=3D0; /** * setup */ void setup()=20 { =09Serial.begin(115200); =09Serial.println("\n\nInitializing..."); =09button.begin();=20 =09mp3.begin(); =09mp3.reset();=20 =09// show some properties and set the volume =09uint16_t volume =3D mp3.getVolume(); =09Serial.print("volume "); =09Serial.println(volume); =09// mp3.setVolume(23); =09mp3.setVolume(18); =09count =3D mp3.getTotalTrackCount(); =09Serial.print("files "); =09Serial.println(count); =09uint16_t mode =3D mp3.getPlaybackMode(); =09Serial.print("playback mode "); =09Serial.println(mode); =09Serial.println("starting..."); =09mp3.setRepeatPlay(false); =09sounds =3D new int[count]; =09for(int i=3D0;i<count;i++){ =09=09sounds[i]=3Di+1; =09}=09 =09randomize (sounds, count);=20 } /** * swap=20 * - function to swap to integers */ void swap (int *a, int *b)=20 {=20 =09int temp =3D *a;=20 =09*a =3D *b;=20 =09*b =3D temp;=20 }=20 /**=20 * printArray=20 * - function to print an array */ void printArray (int arr[], int n)=20 {=20 =09for (int i =3D 0; i < n; i++)=20 =09=09printf("%d \n", arr[i]);=20 =09printf("\n");=20 }=20 /**=20 * randomize=20 * - function to generate a random permutation of arr[]=20 */ void randomize ( int arr[], int n )=20 {=20 =09Serial.printf("Randomizing...\n"); =09// Use a different seed value so that we don't get same=20 =09// result each time we run this program=20 =09uint32_t randNumber =3D RANDOM_REG32; =09srand ( randNumber );=20 =09// Start from the last element and swap one by one. We don't=20 =09// need to run for the first element that's why i > 0=20 =09for (int i =3D n-1; i > 0; i--)=20 =09{=20 =09=09// Pick a random index from 0 to i=20 =09=09int j =3D rand() % (i+1);=20 =09=09// Swap arr[i] with the element at random index=20 =09=09swap(&arr[i], &arr[j]);=20 =09}=20 }=20 /** * Loop */ void loop()=20 { =09// calling mp3.loop() periodically allows for notifications=20 =09// to be handled without interrupts =09mp3.loop(); =09button.read(); // read the button =09if (button.wasReleased()) // if the button was released, change songs =09{ =09=09playRandom(); =09} } /** * playRandom */ void playRandom(){ =09Serial.printf("Playing track: %d [%d]\n",sounds[track], track); =09mp3.playGlobalTrack(sounds[track]); =09track++; =09if(track>=3Dcount){ =09=09randomize (sounds, count);=20 =09=09track=3D0; =09} }
NOTE: The order you copy the mp3 into micro SD card wil= l affect the order mp3 played , which means play(1) function will play the = first mp3 copied into micro SD card.
For Mac User
NOTE: If you are using Mac OS X to copy the mp3, the file system will autom= atically add hidden files like: "._0001.mp3" for index, which this module w= ill handle as valid mp3 files. It is really annoying. So you can run follow= ing command in terminal to eliminate those files.
Please replace the to the volu= me name of your SD card. |