Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Threading;
- using Microsoft.SPOT;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- using System.IO;
- using Gadgeteer.Modules.Seeed;
- // the special driver
- using Gadgeteer.Modules.Music_Driver;
- namespace Gadgeteer_Weather_Crier_XML_02
- {
- public partial class Program
- {
- GT.StorageDevice usbStorage;
- int currentPosition = 1;
- int lastPostion = 1;
- //int currentPosition = 1;
- //int lastPostion = 1;
- double currentZ;
- double currentX;
- double currentY;
- double lastZ;
- double lastX;
- double lastY;
- double accelerationTolerance = 0.5;
- bool playedOnce = true;
- bool musicStopRequested = false;
- FileStream stream;
- ArrayList musicFiles = new ArrayList();
- // create a time based event robot to trigger repeated tasks
- GT.Timer timer = new GT.Timer(1000, GT.Timer.BehaviorType.RunOnce);
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- Debug.Print("Program Started");
- // jump out of the start section
- timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
- timer.Start();
- // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
- Debug.Print("Program Running");
- }
- void timer_Tick(GT.Timer timer)
- {
- // Initialize usb event handling robots
- usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
- usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
- // todo: do this after the usb is connected
- // set the calibration point (the device shouldn't be moving!)
- // accelerometer.Calibrate();
- // watcher for the measurement
- accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 1000);
- accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
- accelerometer.StartContinuousMeasurements();
- // music finished listening robot
- music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
- music.SetVolume(255);
- }
- void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
- {
- // start timer to reset movement detection in half a second
- // compare current state to last state
- // do stuff when movement changes to not moving
- //throw new NotImplementedException();
- //}
- //void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
- //{
- Debug.Print(acceleration.X + " " + acceleration.Y + " " + acceleration.Z);
- currentX = acceleration.X;
- currentY = acceleration.Y;
- currentZ = acceleration.Z;
- // work out the position of the device
- // compare postion with last position
- // log the position
- // if changed do something
- if ((currentX < lastX + accelerationTolerance)
- && (currentX > lastX - accelerationTolerance)
- && (currentY < lastY + accelerationTolerance)
- && (currentY > lastY - accelerationTolerance)
- && (currentZ < lastZ + accelerationTolerance)
- && (currentZ > lastZ - accelerationTolerance)
- //&&(!moving)
- )
- {
- if (!playedOnce)
- {
- playedOnce = true;
- Debug.Print("Static within tolerance");
- //currentPosition = 1;
- //if (currentPosition != lastPostion)
- //{
- if (music.IsBusy)
- {
- //Debug.Print("Interupting...");
- //musicStopRequested = true;
- //Thread.Sleep(1000);
- //music.StopPlaying();
- }
- else
- {
- //musicStopRequested = true;
- playTracks();
- }
- }
- //lastPostion = currentPosition;
- }
- else //if (acceleration.X < 0)
- {
- //Debug.Print("X < 0");
- //
- Debug.Print("Moving");
- //currentPosition = 2;
- //if (playedOnce)
- //{
- playedOnce = false;
- if (music.IsBusy)
- {
- Debug.Print("Interupting...");
- //Thread.Sleep(1000);
- //musicStopRequested = true;
- music.StopPlaying();
- }
- else
- {
- //musicStopRequested = true;
- //playTracks();
- }
- //}
- //lastPostion = currentPosition;
- }
- // log the acceleration
- lastX = currentX;
- lastY = currentY;
- lastZ = currentZ;
- }
- void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
- {
- Debug.Print("USB Storage connected");
- usbStorage = storageDevice;
- string[] usbDirectory = storageDevice.ListRootDirectoryFiles();
- foreach (string fileName in usbDirectory)
- {
- Debug.Print(" " + fileName);
- }
- musicFiles.Add("01.MP3");
- musicFiles.Add("02.MP3");
- musicFiles.Add("03.MP3");
- musicFiles.Add("04.MP3");
- musicFiles.Add("05.MP3");
- musicFiles.Add("06.MP3");
- musicFiles.Add("07.MP3");
- musicFiles.Add("08.MP3");
- musicFiles.Add("09.MP3");
- musicFiles.Add("10.MP3");
- musicFiles.Add("11.MP3");
- musicFiles.Add("12.MP3");
- musicFiles.Add("13.MP3");
- musicFiles.Add("14.MP3");
- musicFiles.Add("15.MP3");
- musicFiles.Add("16.MP3");
- musicFiles.Add("17.MP3");
- musicFiles.Add("18.MP3");
- musicFiles.Add("19.MP3");
- musicFiles.Add("20.MP3");
- //musicFiles.Add("audio" + Path.DirectorySeparatorChar + "01.mp3");
- //musicFiles.Add("audio" + Path.DirectorySeparatorChar + "02.mp3");
- }
- void playTracks()
- {
- //if (currentPosition == 0)
- //{
- // do nothing and wait for change of postion
- //}
- //else if (currentPosition == 1 || currentPosition == 2)
- //{
- Debug.Print("Moving to next audio track");
- // generate random track number and assign to stream
- Random random = new Random();
- //int randomNumber = random.Next(0, 100);
- int trackNumber = random.Next(musicFiles.Count);
- Debug.Print("Opening track: " + (String)musicFiles[trackNumber]);
- stream = usbStorage.Open((String)musicFiles[trackNumber], FileMode.Open, FileAccess.Read);
- music.SetVolume(255);
- Debug.Print("Starting to play: " + (String)musicFiles[trackNumber]);
- music.Play(stream);
- //else
- //{
- // Debug.Print("failed to play");
- //}
- }
- void music_MusicFinished(Music sender)
- {
- Debug.Print("Track finishing");
- //Debug.Print("Checking for new tracks");
- //if (musicStopRequested == true)
- //{
- // musicStopRequested = false;
- //Thread.Sleep(2000);
- //Debug.Print("Track restart requested");
- //playTracks();
- //}
- }
- void usbHost_USBDriveDisconnected(UsbHost sender)
- {
- Debug.Print("USB Storage Disconnected");
- musicFiles.Clear();
- usbStorage = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment