Advertisement
peony3000

Gadgeteer_USB_Music_Rocker_03

Oct 30th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Tina's Rocker Version that works
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Threading;
  6. using Microsoft.SPOT;
  7. using Microsoft.SPOT.Presentation;
  8. using Microsoft.SPOT.Presentation.Controls;
  9. using Microsoft.SPOT.Presentation.Media;
  10. using Microsoft.SPOT.Touch;
  11.  
  12. using Gadgeteer.Networking;
  13. using GT = Gadgeteer;
  14. using GTM = Gadgeteer.Modules;
  15. using Gadgeteer.Modules.GHIElectronics;
  16. using System.Xml;
  17. using System.IO;
  18. using Gadgeteer.Modules.Seeed;
  19.  
  20.  
  21. namespace Gadgeteer_Weather_Crier_02
  22. {
  23.  
  24.     public partial class Program
  25.     {
  26.  
  27.         //string musicFile;
  28.         GT.StorageDevice usbStorage;
  29.         int currentFile = 0;
  30.         bool nowPlaying = false;
  31.         FileStream stream;
  32.         ArrayList musicFiles = new ArrayList();
  33.  
  34.         // This method is run when the mainboard is powered up or reset.  
  35.         void ProgramStarted()
  36.         {
  37.             Debug.Print("Program Started");
  38.  
  39.             // Initialize event handling robots
  40.             usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
  41.             usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
  42.  
  43.             //accelerometer threshold bump method
  44.             //accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
  45.             //accelerometer.EnableThresholdDetection(4.0, true, true, true, true, false, false);
  46.             //accelerometer.RequestMeasurement();
  47.  
  48.             accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 200);
  49.             accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
  50.             accelerometer.StartContinuousMeasurements();
  51.  
  52.             // music finished listening robot
  53.             music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
  54.             music.SetVolume(255);
  55.  
  56.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  57.             Debug.Print("Program Running");
  58.         }
  59.  
  60.         void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
  61.         {
  62.  
  63.             //Debug.Print(" X " + acceleration.X + " Y " + acceleration.Y + " Z " + acceleration.Z);
  64.  
  65.             if (acceleration.X > -0.25 && acceleration.X < 0.25 && usbStorage != null && !nowPlaying)
  66.             {
  67.                 if (currentFile == 0)
  68.                 {
  69.                     Debug.Print("Starting the first audio track: currentFile = " + currentFile);
  70.                 }
  71.                 playTracks();
  72.             }
  73.  
  74.         }
  75.  
  76.         void usbHost_USBDriveDisconnected(UsbHost sender)
  77.         {
  78.             Debug.Print("USB STORAGE DIS-CONNECTED");
  79.             musicFiles.Clear();
  80.             usbStorage = null;
  81.         }
  82.  
  83.         void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
  84.         {
  85.             Debug.Print("USB STORAGE CONNECTED");
  86.             usbStorage = storageDevice;
  87.  
  88.              musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 151953.mp3");//Good Morning
  89.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152009.mp3");//It looks like
  90.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152027.mp3");//It going to be mostly sunny
  91.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152211.mp3");//Tomorrow
  92.             musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152044.mp3");//There's a chance of a storm
  93.          
  94.         }
  95.  
  96.         void playTracks()
  97.         {
  98.             nowPlaying = true;
  99.            
  100.             stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
  101.             music.SetVolume(255);
  102.             Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
  103.             music.Play(stream);
  104.             while (music.IsBusy)
  105.             {
  106.                 // You can't call music.StopPlaying or set volume with this loop running.    
  107.                 Thread.Sleep(10);
  108.             }
  109.         }
  110.  
  111.         void music_MusicFinished(Music sender)
  112.         {
  113.  
  114.             Debug.Print("Track finished");
  115.  
  116.             nowPlaying = false;
  117.             currentFile++;
  118.             if (currentFile < musicFiles.Count - 1)
  119.             {
  120.                 Debug.Print("Moving to next audio track: currentFile = " + currentFile);
  121.             }
  122.             else
  123.             {
  124.                 Debug.Print("Resetting audio: currentFile = 0");
  125.                 currentFile = 0;
  126.             }
  127.         }
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement