peony3000

Gadgeteer_DD02_Oled_Accelerometer_01

Feb 23rd, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 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.Seeed;
  14. using Gadgeteer.Modules.GHIElectronics;
  15.  
  16. namespace Gadgeteer_DD02_Oled_Accelerometer_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.             // setup continuous accelerometer measuring
  41.             // create a measurement completed event handling robot
  42.             accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
  43.            
  44.             // set the measurement interval
  45.             accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 300); // 300 ms
  46.  
  47.             // start the measuring robot
  48.             accelerometer.StartContinuousMeasurements();
  49.  
  50.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  51.             Debug.Print("Program Started");
  52.         }
  53.  
  54.         // the event handling is done here
  55.         void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
  56.         {
  57.             // there are new measurements from the accelerometer!
  58.  
  59.             // the maximum accelerometer value when not moving is 'g' or 9.8 newtons
  60.             // print the readings in 'g' to the debug output window
  61.             Debug.Print("X = " + acceleration.X + " Y = " + acceleration.Y + " Z = " + acceleration.Z);
  62.  
  63.             // set the background color
  64.             // notice that if you move the device the 'g' force is more than 1
  65.             // and so the color value is out of range and you get a blank screen
  66.             // convert the acceleration value to an integer then get it's absolute value
  67.             // convert that from an integer to a byte for the color value
  68.             oledDisplay.SimpleGraphics.BackgroundColor = GT.Color.FromRGB( (byte)System.Math.Abs((int)(acceleration.X * 255)), (byte)System.Math.Abs((int)(acceleration.Y * 255)), (byte)System.Math.Abs((int)(acceleration.Z * 255)) );
  69.            
  70.             // print the acceleration readings for X to the OLED screen
  71.             //printOledDisplay(1, "X = " + (int)(acceleration.X * 255));
  72.             //printOledDisplay(2, "Y = " + (int)(acceleration.Y * 255));
  73.             //printOledDisplay(3, "Z = " + (int)(acceleration.Y * 255));
  74.            
  75.         }
  76.  
  77.         // a custom method for printing text to the OLED screen
  78.         // input a line number and the text to be printed as a text string
  79.          void printOledDisplay(uint lineNumber, string displayMessage)
  80.         {
  81.             uint xPos = 0;
  82.             oledDisplay.SimpleGraphics.DisplayText(displayMessage, displayFont, GT.Color.White, xPos, lineNumber*12);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment