peony3000

Gadgeteer_DD02_Oled_Compass_01

Feb 23rd, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 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_Compass_01
  17. {
  18.     public partial class Program
  19.     {
  20.          
  21.         // create a font that we can use on the oled display screen
  22.         Font displayFont = Resources.GetFont(Resources.FontResources.NinaB);
  23.    
  24.     // This method is run when the mainboard is powered up or reset.  
  25.         void ProgramStarted()
  26.         {
  27.             /*******************************************************************************************
  28.             Modules added in the Program.gadgeteer designer view are used by typing
  29.             their name followed by a period, e.g.  button.  or  camera.
  30.            
  31.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  32.                 button.ButtonPressed +=<tab><tab>
  33.            
  34.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  35.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  36.                 timer.Tick +=<tab><tab>
  37.                 timer.Start();
  38.             *******************************************************************************************/
  39.  
  40.             // set the time interval between each compass reading event
  41.             compass.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 500);
  42.  
  43.             // setup a compass reading robot event watcher
  44.             compass.MeasurementComplete += new Compass.MeasurementCompleteEventHandler(compass_MeasurementComplete);
  45.  
  46.             // start the compass reading
  47.             compass.StartContinuousMeasurements();
  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 compass_MeasurementComplete(Compass sender, Compass.SensorData sensorData)
  57.         {
  58.             // there is a new compass reading!
  59.             Debug.Print("Compass reading: " + sensorData.Angle);
  60.  
  61.             // jump out of the event handler to free it up
  62.             // print the message to the oled display
  63.             // convert the angle number from its original form of a double to an integer and then to a string
  64.             processMessage(((int)sensorData.Angle).ToString());
  65.         }
  66.  
  67.         void processMessage(string recievedMessage)
  68.         {
  69.             // clear the oled display screen memory of graphics
  70.             oledDisplay.SimpleGraphics.Clear();
  71.  
  72.             // write the message to the display screen
  73.             printOledDisplay(1, recievedMessage);
  74.  
  75.             // redraw the screen with all new graphics
  76.             oledDisplay.SimpleGraphics.Redraw();
  77.  
  78.         }
  79.  
  80.         // a custom method for printing text to the OLED screen
  81.         // input a line number and the text to be printed as a text string
  82.         void printOledDisplay(uint lineNumber, string displayMessage)
  83.         {
  84.             uint xPos = 0;
  85.             oledDisplay.SimpleGraphics.DisplayText(displayMessage, displayFont, GT.Color.White, xPos, lineNumber * 12);
  86.         }
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment