Advertisement
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;
- 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)
- {
- Debug.Print(acceleration.X + " " + acceleration.Y + " " + acceleration.Z);
- // work out the position of the device
- // compare postion with last position
- // log the position
- // if changed do something
- if (acceleration.Z < -0.5)
- {
- Debug.Print("OH NO Minus Z!");
- currentPosition = 0;
- if (currentPosition != lastPostion)
- {
- if (music.IsBusy)
- {
- Debug.Print("Interupting...");
- musicStopRequested = true;
- //Thread.Sleep(1000);
- music.StopPlaying();
- }
- else
- {
- }
- }
- // log current position
- lastPostion = currentPosition;
- }
- //else if (acceleration.Z < -0.5)
- //{
- // Debug.Print("OH YES Plus Z!");
- // currentPosition = 0;
- // if (currentPosition != lastPostion)
- // {
- // if (music.IsBusy)
- // {
- // Debug.Print("Interupting...");
- // Thread.Sleep(1000);
- // music.StopPlaying();
- // }
- // else
- // {
- // // play nothing - playTracks will ingore this
- // playTracks();
- // }
- // currentPosition = lastPostion;
- // }
- //}
- else if (acceleration.X > 0.15)
- {
- Debug.Print("X > 0.15");
- Debug.Print("Postion 1");
- 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("Postion 2");
- currentPosition = 2;
- if (currentPosition != lastPostion)
- {
- if (music.IsBusy)
- {
- Debug.Print("Interupting...");
- //Thread.Sleep(1000);
- musicStopRequested = true;
- music.StopPlaying();
- }
- else
- {
- musicStopRequested = true;
- playTracks();
- }
- }
- lastPostion = currentPosition;
- }
- }
- 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("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: currentPosition = " + currentPosition);
- Debug.Print("Opening track: " + (String)musicFiles[currentPosition - 1]);
- stream = usbStorage.Open((String)musicFiles[currentPosition - 1], FileMode.Open, FileAccess.Read);
- music.SetVolume(255);
- Debug.Print("Starting to play: " + (String)musicFiles[currentPosition - 1]);
- 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
Advertisement