peony3000

Gadgeteer_DD02_Oled_GPS_01

Feb 23rd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.84 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Presentation;
  6. using Microsoft.SPOT.Presentation.Controls;
  7. using Microsoft.SPOT.Presentation.Media;
  8. using Microsoft.SPOT.Touch;
  9.  
  10. using Gadgeteer.Networking;
  11. using GT = Gadgeteer;
  12. using GTM = Gadgeteer.Modules;
  13. using Gadgeteer.Modules.GHIElectronics;
  14. using Gadgeteer.Modules.Seeed;
  15.  
  16. namespace Gadgeteer_DD02_Oled_GPS_01
  17. {
  18.     public partial class Program
  19.     {
  20.         // create a program wide variable switch
  21.         // has the gps worked out the position yet?
  22.         bool postionCalculated = false;
  23.  
  24.         // create a font that we can use on the oled display screen
  25.         Font displayFont = Resources.GetFont(Resources.FontResources.NinaB);
  26.  
  27.         // This method is run when the mainboard is powered up or reset.  
  28.         void ProgramStarted()
  29.         {
  30.             /*******************************************************************************************
  31.             Modules added in the Program.gadgeteer designer view are used by typing
  32.             their name followed by a period, e.g.  button.  or  camera.
  33.            
  34.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  35.                 button.ButtonPressed +=<tab><tab>
  36.            
  37.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  38.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  39.                 timer.Tick +=<tab><tab>
  40.                 timer.Start();
  41.             *******************************************************************************************/
  42.  
  43.             // create a gps new postion recieved event handling robot
  44.             gps.PositionReceived += new GPS.PositionReceivedHandler(gps_PositionReceived);
  45.  
  46.             // create a gps new NMEA signal recieved event handling robot
  47.             gps.NMEASentenceReceived += new GPS.NMEASentenceReceivedHandler(gps_NMEASentenceReceived);
  48.  
  49.             // stop the oled display redrawing every time we add a graphic element to the screen memory
  50.             oledDisplay.SimpleGraphics.AutoRedraw = false;
  51.  
  52.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  53.             Debug.Print("Program Started");
  54.         }
  55.  
  56.         void gps_NMEASentenceReceived(GPS sender, string nmeaSentence)
  57.         {
  58.             // the gps has recieved another message from a satellite! (or maybe it is a nearby ship?)
  59.  
  60.             // have we calculated the postion yet?
  61.             // if we have then stop printing the NMEA messages
  62.             if (postionCalculated == false)
  63.             {
  64.                 // print the NMEA message just for fun
  65.                 Debug.Print(nmeaSentence);
  66.  
  67.                 // jump out of the event handler to free it up
  68.                 // print the message to the oled display
  69.                 processNMEAMessage(nmeaSentence);
  70.             }
  71.  
  72.         }
  73.  
  74.         void gps_PositionReceived(GPS sender, GPS.Position position)
  75.         {
  76.             // the gps has recieved enough information and the first position has been calculated!
  77.             Debug.Print(position);
  78.  
  79.             // flick the switch to note that the position has now been calculated and to stop showing NMEA messages
  80.             postionCalculated = true;
  81.  
  82.             // jump out of the event handler to free it up
  83.             // send the message to be processed and sent to the oled display screen
  84.             processGPSMessage(position);
  85.         }
  86.  
  87.         void processNMEAMessage(string recievedMessage)
  88.         {
  89.             // clear the oled display screen memory of graphics
  90.             oledDisplay.SimpleGraphics.Clear();
  91.  
  92.             // write the message to the display screen
  93.             printOledDisplay(1, recievedMessage);
  94.  
  95.             // write part of the message to the the next line on the screen?
  96.             // careful as it will crash if the message is shorter than you think
  97.             //printOledDisplay(2, recievedMessage.Substring(15, 15));
  98.  
  99.             // redraw the screen with all new graphics
  100.             oledDisplay.SimpleGraphics.Redraw();
  101.  
  102.         }
  103.  
  104.         void processGPSMessage(string recievedMessage)
  105.         {
  106.             // clear the oled display screen memory of graphics
  107.             oledDisplay.SimpleGraphics.Clear();
  108.  
  109.             // write the message to the display screen
  110.             // cut out the first part of the message
  111.             // Indexof give the position of a word and Substring gives the letters between two numbers from a text string  
  112.             printOledDisplay(1, recievedMessage.Substring(0, recievedMessage.IndexOf("Long ")));
  113.             // cut out the second part of the message
  114.             printOledDisplay(2, recievedMessage.Substring(recievedMessage.IndexOf("Long "), recievedMessage.IndexOf("Speed ") - recievedMessage.IndexOf("Long ")));
  115.             // cut out the third part of the message
  116.             printOledDisplay(3, recievedMessage.Substring(recievedMessage.IndexOf("Speed "), recievedMessage.IndexOf("Course ") - recievedMessage.IndexOf("Speed ")));
  117.             // leave out the forth part of the message
  118.             // cut out the ast part if the fifth part of the message
  119.             printOledDisplay(4, recievedMessage.Substring(recievedMessage.IndexOf("2013 ") + 5));
  120.  
  121.             // redraw the screen with all new graphics
  122.             oledDisplay.SimpleGraphics.Redraw();
  123.  
  124.         }
  125.  
  126.         // a custom method for printing text to the OLED screen
  127.         // input a line number and the text to be printed as a text string
  128.         void printOledDisplay(uint lineNumber, string displayMessage)
  129.         {
  130.             uint xPos = 0;
  131.             oledDisplay.SimpleGraphics.DisplayText(displayMessage, displayFont, GT.Color.White, xPos, lineNumber * 12);
  132.         }
  133.  
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment