peony3000

weather crier code attempt

Oct 14th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.82 KB | None | 0 0
  1. //this code doesn't work just exchanging it with someone
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Threading;
  6. using Microsoft.SPOT;
  7. using Microsoft.SPOT.Presentation;
  8. using Microsoft.SPOT.Presentation.Controls;
  9. using Microsoft.SPOT.Presentation.Media;
  10. using Microsoft.SPOT.Touch;
  11.  
  12. using Gadgeteer.Networking;
  13. using GT = Gadgeteer;
  14. using GTM = Gadgeteer.Modules;
  15. using Gadgeteer.Modules.GHIElectronics;
  16. using System.Xml;
  17. using System.IO;
  18. using Gadgeteer.Modules.Seeed;
  19.  
  20.  
  21. namespace Gadgeteer_Weather_Crier_01
  22. {
  23.  
  24.     public partial class Program
  25.     {
  26.  
  27.         string musicFile;
  28.         GT.StorageDevice usbStorage;
  29.         // create a time based event robot to trigger repeated tasks
  30.         GT.Timer timer = new GT.Timer(60000, GT.Timer.BehaviorType.RunContinuously);
  31.  
  32.         ArrayList musicFiles = new ArrayList();
  33.  
  34.         // This method is run when the mainboard is powered up or reset.  
  35.         void ProgramStarted()
  36.         {
  37.             // jump to the StartWiFi method that starts the WiFi
  38.             //StartWiFi();
  39.  
  40.             // create a button watching robot to trigger things when the button is pressed
  41.             button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
  42.            
  43.             // set the timer robot so it fires off a tick event
  44.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  45.  
  46.      
  47.             // Initialize event handling robots
  48.             usbHost.USBDriveConnected += new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
  49.             usbHost.USBDriveDisconnected += new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
  50.  
  51.             accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded);
  52.             accelerometer.EnableThresholdDetection(4.0, true, true, true, true, false, false);
  53.                  
  54.             // music finished listening robot
  55.             music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished);
  56.             music.SetVolume(255);
  57.  
  58.  
  59.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  60.             Debug.Print("Program Started");
  61.         }
  62.  
  63.         void usbHost_USBDriveDisconnected(UsbHost sender)
  64.         {
  65.             musicFiles.Clear();
  66.             usbStorage = null;
  67.         }
  68.  
  69.         void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)
  70.         {
  71.             Debug.Print("USB STORAGE CONNECTED");
  72.             usbStorage = storageDevice;
  73.  
  74.             musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151406.mp3");
  75.             musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151950.mp3");
  76.             musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151953.mp3");
  77.  
  78.         }
  79.  
  80.         void accelerometer_ThresholdExceeded(Accelerometer sender)
  81.         {
  82.             if (usbStorage != null)
  83.             {
  84.                 Debug.Print("bang");
  85.  
  86.                 if (nowPlaying == false)
  87.                 {
  88.                     StartPlayingWords();
  89.                 }
  90.             }
  91.             else
  92.             {
  93.                 accelerometer.ResetThresholdDetection();
  94.             }
  95.         }
  96.  
  97.         int currentFile = 0;
  98.  
  99.         void StartPlayingWords()
  100.         {
  101.             if (usbStorage != null)
  102.             {
  103.                 currentFile = 0;
  104.                 nowPlaying = true;
  105.                 stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
  106.                 music.SetVolume(255);
  107.                 music.Play(stream);
  108.                 Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
  109.             }
  110.         }
  111.  
  112.         bool nowPlaying = false;
  113.         FileStream stream;
  114.  
  115.         void music_MusicFinished(Music sender)
  116.         {
  117.             stream.Close();
  118.  
  119.             Debug.Print("music finished");
  120.             if (currentFile < musicFiles.Count - 1)
  121.             {
  122.                 currentFile++;
  123.                 stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read);
  124.                 music.Play(stream);
  125.                 Debug.Print("Now playing: " + (String)musicFiles[currentFile]);
  126.             }
  127.             else
  128.             {
  129.                 nowPlaying = false;
  130.                 accelerometer.ResetThresholdDetection();
  131.                 Debug.Print("Done playing");
  132.             }
  133.         }
  134.  
  135.         void timer_Tick(GT.Timer timer)
  136.         {
  137.             // what do we want to happen when the timer tick event fires
  138.             // get some xml from the internet
  139.             GetXmlFromWeb();
  140.         }
  141.  
  142.         void GetXmlFromWeb()
  143.         {
  144.             WebClient.GetFromWeb("http://www.google.com/ig/api?weather=02903").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
  145.         }
  146.  
  147.         //void StartWiFi()
  148.         //{
  149.         //    wifi.UseDHCP();
  150.         //    Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
  151.         //    info.SSID = "name";
  152.         //    info.SecMode = WiFi_RS21.SecurityMode.WEP;
  153.         //    info.networkType = WiFi_RS21.NetworkType.AccessPoint;
  154.         //    wifi.Join(info, "pass");
  155.         //    wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown);
  156.         //    wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
  157.         //}
  158.        
  159.         //void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
  160.         //{
  161.         //    Debug.Print("network down: connecting....");
  162.         //    timer.Stop();
  163.         //}
  164.  
  165.         //void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
  166.         //{
  167.         //    Debug.Print("network up");
  168.         //    Debug.Print(wifi.NetworkSettings.IPAddress);
  169.         //    GetXmlFromWeb();
  170.         //    timer.Start();
  171.         //}
  172.  
  173.         void Program_ResponseReceived(HttpRequest sender, HttpResponse response)
  174.         {
  175.             string elementHolder;
  176.                
  177.                 XmlReader reader = XmlReader.Create(response.Stream);
  178.  
  179.                 while (reader.Read())
  180.                 {
  181.                         switch (reader.NodeType)
  182.                         {
  183.                             case XmlNodeType.Element:
  184.                                 //Debug.Print("Element: " + reader.Name);
  185.                                 //if (reader.HasAttributes && reader.Name.Equals("current_conditions"))
  186.                                 if (reader.Name.Equals("current_conditions"))
  187.                                     {
  188.                                     reader.ReadToFollowing("condition");
  189.                                     reader.MoveToFirstAttribute();
  190.                                     Debug.Print("Today: " + reader.Value);
  191.                                     }
  192.                                 else if (reader.Name.Equals("forecast_conditions"))
  193.                                     {
  194.                                     reader.ReadToFollowing("day_of_week");
  195.                                     reader.MoveToFirstAttribute();
  196.                                     elementHolder = reader.Value;
  197.                                     reader.ReadToFollowing("condition");
  198.                                     reader.MoveToFirstAttribute();
  199.                                     Debug.Print(elementHolder + ": " + reader.Value);
  200.                                     }
  201.                                 break;
  202.                            
  203.                             case XmlNodeType.EndElement:
  204.                                 //Debug.Print("End of Element");
  205.                                 break;
  206.                         }
  207.  
  208.                 }
  209.          
  210.                 //Debug.Print(response.StatusCode);
  211.        
  212.         }
  213.  
  214.        
  215.  
  216.         void button_ButtonPressed(Button sender, Button.ButtonState state)
  217.         {
  218.             GetXmlFromWeb();
  219.         }
  220.  
  221.    
  222.    
  223.  
  224.    
  225.  
  226.         //void playMusic(String musicFileHolder)
  227.         //{
  228.            
  229.         //    FileStream stream = usbStorage.Open(musicFileHolder, FileMode.Open, FileAccess.Read);
  230.         //    music.SetVolume(255);    
  231.         //    music.Play(stream);
  232.  
  233.  
  234.         //    while (music.IsBusy); // You can't call music.StopPlaying or set volume with this loop running.    
  235.         //        Thread.Sleep(10);
  236.         //    /*
  237.         //        HttpWebRequest&nbsp;httpWebRequest&nbsp;= (HttpWebRequest)WebRequest.Create("http://integral-data.com/NewYorkNewYork_32kbs.wma");
  238.         //        music.SetVolume(200);
  239.         //        music.Play(httpWebRequest.GetResponse().GetResponseStream());
  240.         //        while (music.IsBusy)
  241.         //            Thread.Sleep(10);
  242.         //        httpWebRequest.Dispose();
  243.         //    */
  244.         //}
  245.            
  246.         //void usb_USBDriveDisconnected(GTM.GHIElectronics.UsbHost sender)
  247.         //{
  248.         //    Debug.Print("\r\nThe USB thumb drive has been removed.");
  249.         //}
  250.  
  251.         //void usb_USBDriveConnected(GTM.GHIElectronics.UsbHost sender, GT.StorageDevice storageDevice)
  252.         //{
  253.         //    Debug.Print("\r\nUSB thumb drive has been inserted.");
  254.         //    Debug.Print("Using storageDevice from the USBDriveConnected event, the root director is:");
  255.         //    string[] root = storageDevice.ListRootDirectoryFiles();
  256.         //    foreach (string fileName in root)
  257.         //    {
  258.         //        Debug.Print("    " + fileName);
  259.         //    }
  260.         //    Debug.Print("\r\nPlease remove the USB thumb drive to continue");
  261.  
  262.         //    musicFile = "Public Enemy - Rebel Without A Pause (1989).mp3";
  263.         //    usbStorage = storageDevice;
  264.         //}
  265.  
  266.        
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment