Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Update C:\Program Files (x86)\GHI Electronics\GHI NETMF v4.1 SDK\EMX\Firmware\
- 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 Gadgeteer.Modules.Music_Driver;
- using System.Xml;
- using System.IO;
- using Gadgeteer.Modules.Seeed;
- namespace Gadgeteer_Weather_Crier_xml_01
- {
- public partial class Program
- {
- GT.StorageDevice usbStorage;
- //bool messageComplete = true;
- FileStream stream;
- ArrayList musicFiles = new ArrayList();
- // create a time based event robot to trigger repeated tasks
- //GT.Timer timer = new GT.Timer(60000, GT.Timer.BehaviorType.RunContinuously);
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- Debug.Print("Program Started");
- // Initialize event handling robots
- usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
- usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
- //accelerometer threshold bump method
- accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
- accelerometer.MeasurementRange = GTM.Seeed.Accelerometer.Range.TwoG;
- // set the calibration point (the device shouldn't be moving!)
- accelerometer.Calibrate();
- accelerometer.EnableThresholdDetection(-3.0, false, false, true, true, false, false);
- //start Wifi
- StartWiFi();
- // 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 Running");
- }
- void accelerometer_ThresholdExceeded(Accelerometer sender)
- {
- Debug.Print("Bump detected!");
- GetXmlFromWeb();
- // check that the USB is connected and that we're not in the middle of playing the whole message
- //if (usbStorage != null && messageComplete)
- //{
- // messageComplete = false;
- // Debug.Print("Starting the first audio track");
- // playTracks();
- //}
- }
- void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
- {
- Debug.Print("USB Storage connected");
- usbStorage = storageDevice;
- string[] root = storageDevice.ListFiles("audio");
- //string[] root = storageDevice.ListRootDirectoryFiles();
- foreach (string fileName in root)
- {
- Debug.Print(" " + fileName);
- if (fileName.Equals("Good Morning.mp3"))
- {
- Debug.Print("Check: " + fileName);
- }
- }
- // musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 151953.mp3");//Good Morning
- // musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152009.mp3");//It looks like
- // musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152027.mp3");//It going to be mostly sunny
- // musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152211.mp3");//Tomorrow
- // musicFiles.Add("audio" + Path.DirectorySeparatorChar + "20120713 152044.mp3");//There's a chance of a storm
- }
- void playTracks()
- {
- for (int currentFile = 0; currentFile < musicFiles.Count; currentFile++)
- {
- Debug.Print("Moving to next audio track: currentFile = " + currentFile);
- Debug.Print("Opening track: " + (String)musicFiles[currentFile]);
- stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
- music.SetVolume(255);
- Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
- music.Play(stream);
- while (music.IsBusy)
- {
- // You can't call music.StopPlaying or set volume with this loop running.
- Thread.Sleep(10);
- // Check for a condition to break the music
- //if (false)
- //{
- // break;
- //}
- }
- }
- Debug.Print("Resetting bump and audio");
- accelerometer.ResetThresholdDetection();
- //messageComplete = true;
- }
- void music_MusicFinished(Music sender)
- {
- Debug.Print("Track finished");
- }
- void usbHost_USBDriveDisconnected(UsbHost sender)
- {
- Debug.Print("USB Storage Disconnected");
- musicFiles.Clear();
- usbStorage = null;
- }
- void GetXmlFromWeb()
- {
- WebClient.GetFromWeb("http://inbetwixt.com/weather/weather.xml").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
- //WebClient.GetFromWeb("http://api.wunderground.com/api/511db05bdbc6d1b9/forecast/q/02906.xml").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
- }
- //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 StartWiFi()
- {
- wifi.UseDHCP();
- Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
- info.SSID = "RISD-MiscDevices";
- info.SecMode = WiFi_RS21.SecurityMode.WPA;
- //info.SSID = "name";
- //info.SecMode = WiFi_RS21.SecurityMode.WEP;
- info.networkType = WiFi_RS21.NetworkType.AccessPoint;
- wifi.Join(info, "R1sdB34ch2012");
- //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)
- {
- // check the time
- Debug.Print("Adding audio Goodbye");
- musicFiles.Add("audio" + Path.DirectorySeparatorChar + "Good Morning.mp3");
- XmlReader reader = XmlReader.Create(response.Stream);
- while (reader.Read())
- {
- switch (reader.NodeType)
- {
- case XmlNodeType.Element:
- //Debug.Print("Element: " + reader.Name);
- if (reader.Name.Equals("simpleforecast"))
- {
- reader.ReadToFollowing("forecastday");
- reader.ReadToFollowing("period");
- if (reader.ReadElementString().Equals("1"))
- {
- reader.ReadToFollowing("conditions");
- string todaysCondition = reader.ReadElementString();
- Debug.Print("Today's forecast is: " + todaysCondition);
- musicFiles.Add("audio" + Path.DirectorySeparatorChar + "Today.mp3");
- musicFiles.Add("audio" + Path.DirectorySeparatorChar + todaysCondition+".mp3");
- }
- reader.ReadToFollowing("forecastday");
- reader.ReadToFollowing("period");
- if (reader.ReadElementString().Equals("2"))
- {
- reader.ReadToFollowing("conditions");
- string tomorrowsCondition = reader.ReadElementString();
- Debug.Print("Tomorrow's forecast is: " + tomorrowsCondition);
- musicFiles.Add("audio" + Path.DirectorySeparatorChar + "Tomorrow.mp3");
- musicFiles.Add("audio" + Path.DirectorySeparatorChar + tomorrowsCondition + ".mp3");
- }
- Debug.Print("Goodbye");
- Debug.Print("End of message construction");
- }
- break;
- case XmlNodeType.EndElement:
- //Debug.Print("End of Element");
- break;
- case XmlNodeType.Attribute:
- //Debug.Print("Attribute");
- break;
- case XmlNodeType.Text:
- //Debug.Print("Text");
- break;
- }
- }
- playTracks();
- //Debug.Print(response.StatusCode);
- Debug.Print("Resetting bump");
- accelerometer.ResetThresholdDetection();
- }
- // void button_ButtonPressed(Button sender, Button.ButtonState state)
- // {
- // GetXmlFromWeb();
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment