SHOW:
|
|
- or go back to the newest paste.
| 1 | - | //this code doesn't work just exchanging it with someone |
| 1 | + | //Tina's Rocker Version but still has that playing bug!! |
| 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); |
| 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 | button1.ButtonPressed+=new Button.ButtonEventHandler(button1_ButtonPressed); | |
| 44 | - | timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); |
| 44 | + | |
| 45 | ||
| 46 | ||
| 47 | // set the timer robot so it fires off a tick event | |
| 48 | //timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); | |
| 49 | ||
| 50 | ||
| 51 | - | accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded); |
| 51 | + | |
| 52 | - | accelerometer.EnableThresholdDetection(4.0, true, true, true, true, false, false); |
| 52 | + | |
| 53 | - | |
| 53 | + | |
| 54 | ||
| 55 | //accelerometer threshold method | |
| 56 | //accelerometer.ThresholdExceeded += new Accelerometer.ThresholdExceededEventHandler(accelerometer_ThresholdExceeded); | |
| 57 | //accelerometer.EnableThresholdDetection(4.0, true, true, true, true, false, false); | |
| 58 | //accelerometer.RequestMeasurement(); | |
| 59 | ||
| 60 | accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 200); | |
| 61 | accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete); | |
| 62 | ||
| 63 | // music finished listening robot | |
| 64 | music.MusicFinished += new Music.MusicFinishedPlayingEventHandler(music_MusicFinished); | |
| 65 | music.SetVolume(255); | |
| 66 | ||
| 67 | ||
| 68 | // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging. | |
| 69 | Debug.Print("Program Started");
| |
| 70 | } | |
| 71 | ||
| 72 | void button_ButtonPressed(Button sender, Button.ButtonState state) | |
| 73 | {
| |
| 74 | if (button.IsLedOn) | |
| 75 | - | musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151950.mp3");
|
| 75 | + | |
| 76 | - | musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151953.mp3");
|
| 76 | + | accelerometer.StopContinuousMeasurements(); |
| 77 | button.TurnLEDOff(); | |
| 78 | } | |
| 79 | else | |
| 80 | {
| |
| 81 | accelerometer.StartContinuousMeasurements(); | |
| 82 | button.TurnLEDOn(); | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration) | |
| 87 | {
| |
| 88 | ||
| 89 | Debug.Print(" X " + acceleration.X + " Y " + acceleration.Y + " Z " + acceleration.Z);
| |
| 90 | ||
| 91 | if (acceleration.X < -0.25) | |
| 92 | {
| |
| 93 | //do something | |
| 94 | if (!nowPlaying) | |
| 95 | {
| |
| 96 | StartPlayingWords(); | |
| 97 | } | |
| 98 | ||
| 99 | } | |
| 100 | else if (acceleration.X > 0.25) | |
| 101 | {
| |
| 102 | //stop | |
| 103 | if (!nowPlaying) | |
| 104 | {
| |
| 105 | StartPlayingWords(); | |
| 106 | } | |
| 107 | - | music.Play(stream); |
| 107 | + | |
| 108 | ||
| 109 | ||
| 110 | } | |
| 111 | ||
| 112 | void button1_ButtonPressed(Button sender, Button.ButtonState state) | |
| 113 | {
| |
| 114 | accelerometer.Calibrate(); | |
| 115 | } | |
| 116 | ||
| 117 | void usbHost_USBDriveDisconnected(UsbHost sender) | |
| 118 | {
| |
| 119 | musicFiles.Clear(); | |
| 120 | - | if (currentFile < musicFiles.Count - 1) |
| 120 | + | |
| 121 | } | |
| 122 | - | currentFile++; |
| 122 | + | |
| 123 | void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice) | |
| 124 | - | music.Play(stream); |
| 124 | + | |
| 125 | - | Debug.Print("Now playing: " + (String)musicFiles[currentFile]);
|
| 125 | + | |
| 126 | usbStorage = storageDevice; | |
| 127 | ||
| 128 | musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151406.mp3");
| |
| 129 | - | nowPlaying = false; |
| 129 | + | //musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151950.mp3");
|
| 130 | //musicFiles.Add("straight" + Path.DirectorySeparatorChar + "20120713 151953.mp3");
| |
| 131 | - | Debug.Print("Done playing");
|
| 131 | + | |
| 132 | } | |
| 133 | ||
| 134 | void accelerometer_ThresholdExceeded(Accelerometer sender) | |
| 135 | {
| |
| 136 | if (usbStorage != null) | |
| 137 | {
| |
| 138 | Debug.Print("bang");
| |
| 139 | ||
| 140 | if (nowPlaying == false) | |
| 141 | {
| |
| 142 | StartPlayingWords(); | |
| 143 | } | |
| 144 | } | |
| 145 | else | |
| 146 | {
| |
| 147 | accelerometer.ResetThresholdDetection(); | |
| 148 | } | |
| 149 | } | |
| 150 | ||
| 151 | - | // info.SSID = "name"; |
| 151 | + | |
| 152 | ||
| 153 | void StartPlayingWords() | |
| 154 | - | // wifi.Join(info, "pass"); |
| 154 | + | |
| 155 | if (usbStorage != null) | |
| 156 | {
| |
| 157 | currentFile = 0; | |
| 158 | nowPlaying = true; | |
| 159 | stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read); | |
| 160 | music.SetVolume(255); | |
| 161 | //music.Play(stream); | |
| 162 | //go to sleep to simulate playing music | |
| 163 | Thread.Sleep(500); | |
| 164 | Debug.Print("Starting to play: " + (String)musicFiles[currentFile]);
| |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | bool nowPlaying = false; | |
| 169 | FileStream stream; | |
| 170 | ||
| 171 | void music_MusicFinished(Music sender) | |
| 172 | {
| |
| 173 | ||
| 174 | Debug.Print("music finished");
| |
| 175 | stream.Close(); | |
| 176 | nowPlaying = false; | |
| 177 | Debug.Print("Done playing");
| |
| 178 | ||
| 179 | //if (currentFile < musicFiles.Count - 1) | |
| 180 | //{
| |
| 181 | // currentFile++; | |
| 182 | // stream = usbStorage.Open((String)musicFiles[currentFile], FileMode.Open, FileAccess.Read); | |
| 183 | // music.Play(stream); | |
| 184 | // Debug.Print("Now playing: " + (String)musicFiles[currentFile]);
| |
| 185 | //} | |
| 186 | //else | |
| 187 | //{
| |
| 188 | // nowPlaying = false; | |
| 189 | // //accelerometer.ResetThresholdDetection(); | |
| 190 | // Debug.Print("Done playing");
| |
| 191 | //} | |
| 192 | } | |
| 193 | ||
| 194 | void timer_Tick(GT.Timer timer) | |
| 195 | {
| |
| 196 | // what do we want to happen when the timer tick event fires | |
| 197 | // get some xml from the internet | |
| 198 | GetXmlFromWeb(); | |
| 199 | } | |
| 200 | ||
| 201 | void GetXmlFromWeb() | |
| 202 | {
| |
| 203 | WebClient.GetFromWeb("http://www.google.com/ig/api?weather=02903").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
| |
| 204 | } | |
| 205 | ||
| 206 | //void StartWiFi() | |
| 207 | //{
| |
| 208 | // wifi.UseDHCP(); | |
| 209 | // Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo(); | |
| 210 | // info.SSID = "R2HZL"; | |
| 211 | // info.SecMode = WiFi_RS21.SecurityMode.WEP; | |
| 212 | // info.networkType = WiFi_RS21.NetworkType.AccessPoint; | |
| 213 | // wifi.Join(info, "AA11FEF9EC"); | |
| 214 | // wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown); | |
| 215 | // wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp); | |
| 216 | //} | |
| 217 | ||
| 218 | //void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) | |
| 219 | //{
| |
| 220 | // Debug.Print("network down: connecting....");
| |
| 221 | // timer.Stop(); | |
| 222 | //} | |
| 223 | ||
| 224 | //void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) | |
| 225 | //{
| |
| 226 | // Debug.Print("network up");
| |
| 227 | // Debug.Print(wifi.NetworkSettings.IPAddress); | |
| 228 | // GetXmlFromWeb(); | |
| 229 | // timer.Start(); | |
| 230 | //} | |
| 231 | ||
| 232 | void Program_ResponseReceived(HttpRequest sender, HttpResponse response) | |
| 233 | {
| |
| 234 | string elementHolder; | |
| 235 | ||
| 236 | XmlReader reader = XmlReader.Create(response.Stream); | |
| 237 | ||
| 238 | while (reader.Read()) | |
| 239 | {
| |
| 240 | switch (reader.NodeType) | |
| 241 | {
| |
| 242 | case XmlNodeType.Element: | |
| 243 | //Debug.Print("Element: " + reader.Name);
| |
| 244 | //if (reader.HasAttributes && reader.Name.Equals("current_conditions"))
| |
| 245 | if (reader.Name.Equals("current_conditions"))
| |
| 246 | {
| |
| 247 | reader.ReadToFollowing("condition");
| |
| 248 | reader.MoveToFirstAttribute(); | |
| 249 | Debug.Print("Today: " + reader.Value);
| |
| 250 | } | |
| 251 | else if (reader.Name.Equals("forecast_conditions"))
| |
| 252 | {
| |
| 253 | reader.ReadToFollowing("day_of_week");
| |
| 254 | reader.MoveToFirstAttribute(); | |
| 255 | elementHolder = reader.Value; | |
| 256 | reader.ReadToFollowing("condition");
| |
| 257 | reader.MoveToFirstAttribute(); | |
| 258 | Debug.Print(elementHolder + ": " + reader.Value); | |
| 259 | } | |
| 260 | break; | |
| 261 | ||
| 262 | case XmlNodeType.EndElement: | |
| 263 | //Debug.Print("End of Element");
| |
| 264 | break; | |
| 265 | } | |
| 266 | ||
| 267 | } | |
| 268 | ||
| 269 | //Debug.Print(response.StatusCode); | |
| 270 | ||
| 271 | } | |
| 272 | ||
| 273 | ||
| 274 | ||
| 275 | // void button_ButtonPressed(Button sender, Button.ButtonState state) | |
| 276 | // {
| |
| 277 | // GetXmlFromWeb(); | |
| 278 | // } | |
| 279 | ||
| 280 | ||
| 281 | ||
| 282 | ||
| 283 | ||
| 284 | ||
| 285 | //void playMusic(String musicFileHolder) | |
| 286 | //{
| |
| 287 | ||
| 288 | // FileStream stream = usbStorage.Open(musicFileHolder, FileMode.Open, FileAccess.Read); | |
| 289 | // music.SetVolume(255); | |
| 290 | // music.Play(stream); | |
| 291 | ||
| 292 | ||
| 293 | // while (music.IsBusy); // You can't call music.StopPlaying or set volume with this loop running. | |
| 294 | // Thread.Sleep(10); | |
| 295 | // /* | |
| 296 | // HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://integral-data.com/NewYorkNewYork_32kbs.wma");
| |
| 297 | // music.SetVolume(200); | |
| 298 | // music.Play(httpWebRequest.GetResponse().GetResponseStream()); | |
| 299 | // while (music.IsBusy) | |
| 300 | // Thread.Sleep(10); | |
| 301 | // httpWebRequest.Dispose(); | |
| 302 | // */ | |
| 303 | //} | |
| 304 | ||
| 305 | //void usb_USBDriveDisconnected(GTM.GHIElectronics.UsbHost sender) | |
| 306 | //{
| |
| 307 | // Debug.Print("\r\nThe USB thumb drive has been removed.");
| |
| 308 | //} | |
| 309 | ||
| 310 | //void usb_USBDriveConnected(GTM.GHIElectronics.UsbHost sender, GT.StorageDevice storageDevice) | |
| 311 | //{
| |
| 312 | // Debug.Print("\r\nUSB thumb drive has been inserted.");
| |
| 313 | // Debug.Print("Using storageDevice from the USBDriveConnected event, the root director is:");
| |
| 314 | // string[] root = storageDevice.ListRootDirectoryFiles(); | |
| 315 | // foreach (string fileName in root) | |
| 316 | // {
| |
| 317 | // Debug.Print(" " + fileName);
| |
| 318 | // } | |
| 319 | // Debug.Print("\r\nPlease remove the USB thumb drive to continue");
| |
| 320 | ||
| 321 | // musicFile = "Public Enemy - Rebel Without A Pause (1989).mp3"; | |
| 322 | // usbStorage = storageDevice; | |
| 323 | //} | |
| 324 | ||
| 325 | ||
| 326 | } | |
| 327 | } |