peony3000

Bear Head Tweet

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