Advertisement
peony3000

Gadgeteer Weather Crier XML 02

Nov 13th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.14 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Threading;
  5. using Microsoft.SPOT;
  6.  
  7. using Gadgeteer.Networking;
  8. using GT = Gadgeteer;
  9. using GTM = Gadgeteer.Modules;
  10. using Gadgeteer.Modules.GHIElectronics;
  11. using System.IO;
  12. using Gadgeteer.Modules.Seeed;
  13.  
  14. // the special driver
  15. using Gadgeteer.Modules.Music_Driver;
  16.  
  17. namespace Gadgeteer_Weather_Crier_XML_02
  18. {
  19.  
  20.     public partial class Program
  21.     {
  22.  
  23.         GT.StorageDevice usbStorage;
  24.  
  25.         int currentPosition = 1;
  26.         int lastPostion = 1;
  27.  
  28.         bool musicStopRequested = false;
  29.  
  30.         FileStream stream;
  31.         ArrayList musicFiles = new ArrayList();
  32.  
  33.         // create a time based event robot to trigger repeated tasks
  34.         GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunOnce);
  35.  
  36.         // This method is run when the mainboard is powered up or reset.  
  37.         void ProgramStarted()
  38.         {
  39.             Debug.Print("Program Started");
  40.  
  41.             // jump out of the start section
  42.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  43.             timer.Start();
  44.  
  45.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  46.             Debug.Print("Program Running");
  47.         }
  48.  
  49.         void timer_Tick(GT.Timer timer)
  50.         {
  51.             // Initialize usb event handling robots
  52.             usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
  53.             usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
  54.  
  55.             // todo: do this after the usb is connected
  56.  
  57.             // set the calibration point (the device shouldn't be moving!)
  58.             // accelerometer.Calibrate();
  59.             // watcher for the measurement
  60.             accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 1000);
  61.             accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
  62.             accelerometer.StartContinuousMeasurements();
  63.  
  64.             // music finished listening robot
  65.             music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
  66.             music.SetVolume(255);
  67.  
  68.         }
  69.  
  70.         void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
  71.         {
  72.             Debug.Print(acceleration.X + " " + acceleration.Y + " " + acceleration.Z);
  73.  
  74.             // work out the position of the device
  75.             // compare postion with last position
  76.             // log the position
  77.             // if changed do something
  78.            
  79.            
  80.             if (acceleration.Z < -0.5)
  81.             {
  82.                 Debug.Print("OH NO Minus Z!");
  83.                 currentPosition = 0;
  84.  
  85.                 if (currentPosition != lastPostion)
  86.                 {
  87.                    
  88.  
  89.                     if (music.IsBusy)
  90.                     {
  91.                         Debug.Print("Interupting...");
  92.                         musicStopRequested = true;
  93.  
  94.                         //Thread.Sleep(1000);
  95.                         music.StopPlaying();
  96.                     }
  97.                     else
  98.                     {
  99.  
  100.                     }
  101.                    
  102.                 }
  103.  
  104.                 // log current position
  105.                 lastPostion = currentPosition;
  106.             }
  107.             //else if (acceleration.Z < -0.5)
  108.             //{
  109.             //    Debug.Print("OH YES Plus Z!");
  110.             //    currentPosition = 0;
  111.  
  112.             //    if (currentPosition != lastPostion)
  113.             //    {
  114.             //        if (music.IsBusy)
  115.             //        {
  116.             //            Debug.Print("Interupting...");
  117.             //            Thread.Sleep(1000);
  118.             //            music.StopPlaying();
  119.             //        }
  120.             //        else
  121.             //        {
  122.             //            // play nothing - playTracks will ingore this
  123.             //            playTracks();
  124.             //        }
  125.             //        currentPosition = lastPostion;
  126.             //    }
  127.                    
  128.             //}
  129.  
  130.             else if (acceleration.X > 0.15)
  131.             {
  132.                 Debug.Print("X > 0.15");
  133.                 Debug.Print("Postion 1");
  134.                
  135.                 currentPosition = 1;
  136.  
  137.                 if (currentPosition != lastPostion)
  138.                 {
  139.                    
  140.  
  141.                     if (music.IsBusy)
  142.                     {
  143.                         Debug.Print("Interupting...");
  144.                         musicStopRequested = true;
  145.  
  146.                         //Thread.Sleep(1000);
  147.                         music.StopPlaying();
  148.                     }
  149.                     else
  150.                     {
  151.                         musicStopRequested = true;
  152.                         playTracks();
  153.                     }
  154.                    
  155.                 }
  156.  
  157.                 lastPostion = currentPosition;
  158.             }
  159.             else if (acceleration.X < 0)
  160.             {
  161.  
  162.                 Debug.Print("X < 0");
  163.                 //
  164.                 Debug.Print("Postion 2");
  165.                
  166.                 currentPosition = 2;
  167.  
  168.                 if (currentPosition != lastPostion)
  169.                 {
  170.                    
  171.                    
  172.                     if (music.IsBusy)
  173.                     {
  174.                         Debug.Print("Interupting...");
  175.                         //Thread.Sleep(1000);
  176.                         musicStopRequested = true;
  177.  
  178.                         music.StopPlaying();
  179.                     }
  180.                     else
  181.                     {
  182.                         musicStopRequested = true;
  183.                         playTracks();
  184.                     }
  185.                    
  186.                 }
  187.  
  188.                 lastPostion = currentPosition;
  189.             }
  190.  
  191.         }
  192.  
  193.  
  194.         void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
  195.         {
  196.             Debug.Print("USB Storage connected");
  197.             usbStorage = storageDevice;
  198.             string[] usbDirectory = storageDevice.ListRootDirectoryFiles();
  199.             foreach (string fileName in usbDirectory)
  200.             {
  201.                 Debug.Print("    " + fileName);
  202.             }
  203.            
  204.             musicFiles.Add("01.mp3");
  205.             musicFiles.Add("02.mp3");
  206.             //musicFiles.Add("audio" + Path.DirectorySeparatorChar + "01.mp3");
  207.             //musicFiles.Add("audio" + Path.DirectorySeparatorChar + "02.mp3");
  208.         }
  209.  
  210.         void playTracks()
  211.         {
  212.  
  213.            
  214.                 if (currentPosition == 0)
  215.                 {
  216.                     // do nothing and wait for change of postion
  217.                 }
  218.                 else if (currentPosition == 1 || currentPosition == 2)
  219.                 {
  220.  
  221.                     Debug.Print("Moving to next audio track: currentPosition = " + currentPosition);
  222.                     Debug.Print("Opening track: " + (String)musicFiles[currentPosition - 1]);
  223.                     stream = usbStorage.Open((String)musicFiles[currentPosition - 1], FileMode.Open, FileAccess.Read);
  224.                     music.SetVolume(255);
  225.                     Debug.Print("Starting to play: " + (String)musicFiles[currentPosition - 1]);
  226.                     music.Play(stream);
  227.  
  228.                 }
  229.            
  230.             //else
  231.             //{
  232.             //  Debug.Print("failed to play");
  233.             //}
  234.         }
  235.  
  236.         void music_MusicFinished(Music sender)
  237.         {
  238.             Debug.Print("Track finishing");
  239.             //Debug.Print("Checking for new tracks");
  240.                        
  241.             if (musicStopRequested == true)
  242.             {
  243.                 musicStopRequested = false;
  244.                 Thread.Sleep(2000);
  245.                 Debug.Print("Track restart requested");
  246.                
  247.                 playTracks();
  248.             }        
  249.         }
  250.  
  251.         void usbHost_USBDriveDisconnected(UsbHost sender)
  252.         {
  253.             Debug.Print("USB Storage Disconnected");
  254.             musicFiles.Clear();
  255.             usbStorage = null;
  256.         }
  257.  
  258.  
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement