Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this code doesn't work just exchanging it with someone
- using System;
- using System.Collections;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- using System.Xml;
- using System.IO;
- using Gadgeteer.Modules.Seeed;
- namespace Gadgeteer_Weather_Crier_01
- {
- public partial class Program
- {
- string musicFile;
- GT.StorageDevice usbStorage;
- // create a time based event robot to trigger repeated tasks
- GT.Timer timer = new GT.Timer(60000, GT.Timer.BehaviorType.RunContinuously);
- ArrayList musicFiles = new ArrayList();
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- // jump to the StartWiFi method that starts the WiFi
- //StartWiFi();
- // create a button watching robot to trigger things when the button is pressed
- button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
- // set the timer robot so it fires off a tick event
- timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
- // Initialize event handling robots
- usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
- usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
- accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
- accelerometer.EnableThresholdDetection(4.0, true, true, true, true, false, false);
- // music finished listening robot
- music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
- music.SetVolume(255);
- // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
- Debug.Print("Program Started");
- }
- void usbHost_USBDriveDisconnected(UsbHost sender)
- {
- musicFiles.Clear();
- usbStorage = null;
- }
- void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
- {
- Debug.Print("USB STORAGE CONNECTED");
- usbStorage = storageDevice;
- musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151406.mp3");
- musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151950.mp3");
- musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151953.mp3");
- }
- void accelerometer_ThresholdExceeded(Accelerometer sender)
- {
- if (usbStorage != null)
- {
- Debug.Print("bang");
- if (nowPlaying == false)
- {
- StartPlayingWords();
- }
- }
- else
- {
- accelerometer.ResetThresholdDetection();
- }
- }
- int currentFile = 0;
- void StartPlayingWords()
- {
- if (usbStorage != null)
- {
- currentFile = 0;
- nowPlaying = true;
- stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
- music.SetVolume(255);
- music.Play(stream);
- Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
- }
- }
- bool nowPlaying = false;
- FileStream stream;
- void music_MusicFinished(Music sender)
- {
- stream.Close();
- Debug.Print("music finished");
- if (currentFile < musicFiles.Count - 1)
- {
- currentFile++;
- stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
- music.Play(stream);
- Debug.Print("Now playing: " + (String)musicFiles[currentFile]);
- }
- else
- {
- nowPlaying = false;
- accelerometer.ResetThresholdDetection();
- Debug.Print("Done playing");
- }
- }
- void timer_Tick(GT.Timer timer)
- {
- // what do we want to happen when the timer tick event fires
- // get some xml from the internet
- GetXmlFromWeb();
- }
- void GetXmlFromWeb()
- {
- WebClient.GetFromWeb("http://www.google.com/ig/api?weather=02903").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
- }
- //void StartWiFi()
- //{
- // wifi.UseDHCP();
- // Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
- // info.SSID = "name";
- // info.SecMode = WiFi_RS21.SecurityMode.WEP;
- // info.networkType = WiFi_RS21.NetworkType.AccessPoint;
- // wifi.Join(info, "pass");
- // wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown);
- // wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
- //}
- //void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
- //{
- // Debug.Print("network down: connecting....");
- // timer.Stop();
- //}
- //void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
- //{
- // Debug.Print("network up");
- // Debug.Print(wifi.NetworkSettings.IPAddress);
- // GetXmlFromWeb();
- // timer.Start();
- //}
- void Program_ResponseReceived(HttpRequest sender, HttpResponse response)
- {
- string elementHolder;
- XmlReader reader = XmlReader.Create(response.Stream);
- while (reader.Read())
- {
- switch (reader.NodeType)
- {
- case XmlNodeType.Element:
- //Debug.Print("Element: " + reader.Name);
- //if (reader.HasAttributes && reader.Name.Equals("current_conditions"))
- if (reader.Name.Equals("current_conditions"))
- {
- reader.ReadToFollowing("condition");
- reader.MoveToFirstAttribute();
- Debug.Print("Today: " + reader.Value);
- }
- else if (reader.Name.Equals("forecast_conditions"))
- {
- reader.ReadToFollowing("day_of_week");
- reader.MoveToFirstAttribute();
- elementHolder = reader.Value;
- reader.ReadToFollowing("condition");
- reader.MoveToFirstAttribute();
- Debug.Print(elementHolder + ": " + reader.Value);
- }
- break;
- case XmlNodeType.EndElement:
- //Debug.Print("End of Element");
- break;
- }
- }
- //Debug.Print(response.StatusCode);
- }
- void button_ButtonPressed(Button sender, Button.ButtonState state)
- {
- GetXmlFromWeb();
- }
- //void playMusic(String musicFileHolder)
- //{
- // FileStream stream = usbStorage.Open(musicFileHolder, FileMode.Open, FileAccess.Read);
- // music.SetVolume(255);
- // music.Play(stream);
- // while (music.IsBusy); // You can't call music.StopPlaying or set volume with this loop running.
- // Thread.Sleep(10);
- // /*
- // HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://integral-data.com/NewYorkNewYork_32kbs.wma");
- // music.SetVolume(200);
- // music.Play(httpWebRequest.GetResponse().GetResponseStream());
- // while (music.IsBusy)
- // Thread.Sleep(10);
- // httpWebRequest.Dispose();
- // */
- //}
- //void usb_USBDriveDisconnected(GTM.GHIElectronics.UsbHost sender)
- //{
- // Debug.Print("\r\nThe USB thumb drive has been removed.");
- //}
- //void usb_USBDriveConnected(GTM.GHIElectronics.UsbHost sender, GT.StorageDevice storageDevice)
- //{
- // Debug.Print("\r\nUSB thumb drive has been inserted.");
- // Debug.Print("Using storageDevice from the USBDriveConnected event, the root director is:");
- // string[] root = storageDevice.ListRootDirectoryFiles();
- // foreach (string fileName in root)
- // {
- // Debug.Print(" " + fileName);
- // }
- // Debug.Print("\r\nPlease remove the USB thumb drive to continue");
- // musicFile = "Public Enemy - Rebel Without A Pause (1989).mp3";
- // usbStorage = storageDevice;
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment