Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.Seeed;
- using Gadgeteer.Modules.GHIElectronics;
- namespace Gadgeteer_DD02_Oled_Accelerometer_01
- {
- public partial class Program
- {
- // create a font that we can use on the oled display screen
- Font displayFont = Resources.GetFont(Resources.FontResources.NinaB);
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- /*******************************************************************************************
- Modules added in the Program.gadgeteer designer view are used by typing
- their name followed by a period, e.g. button. or camera.
- Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
- button.ButtonPressed +=<tab><tab>
- If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
- GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
- timer.Tick +=<tab><tab>
- timer.Start();
- *******************************************************************************************/
- // setup continuous accelerometer measuring
- // create a measurement completed event handling robot
- accelerometer.MeasurementComplete += new Accelerometer.MeasurementCompleteEventHandler(accelerometer_MeasurementComplete);
- // set the measurement interval
- accelerometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 300); // 300 ms
- // start the measuring robot
- accelerometer.StartContinuousMeasurements();
- // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
- Debug.Print("Program Started");
- }
- // the event handling is done here
- void accelerometer_MeasurementComplete(Accelerometer sender, Accelerometer.Acceleration acceleration)
- {
- // there are new measurements from the accelerometer!
- // the maximum accelerometer value when not moving is 'g' or 9.8 newtons
- // print the readings in 'g' to the debug output window
- Debug.Print("X = " + acceleration.X + " Y = " + acceleration.Y + " Z = " + acceleration.Z);
- // set the background color
- // notice that if you move the device the 'g' force is more than 1
- // and so the color value is out of range and you get a blank screen
- // convert the acceleration value to an integer then get it's absolute value
- // convert that from an integer to a byte for the color value
- 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)) );
- // print the acceleration readings for X to the OLED screen
- //printOledDisplay(1, "X = " + (int)(acceleration.X * 255));
- //printOledDisplay(2, "Y = " + (int)(acceleration.Y * 255));
- //printOledDisplay(3, "Z = " + (int)(acceleration.Y * 255));
- }
- // a custom method for printing text to the OLED screen
- // input a line number and the text to be printed as a text string
- void printOledDisplay(uint lineNumber, string displayMessage)
- {
- uint xPos = 0;
- oledDisplay.SimpleGraphics.DisplayText(displayMessage, displayFont, GT.Color.White, xPos, lineNumber*12);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment