peony3000

Gadgeteer_DD02_Oled_Gas_01

Feb 23rd, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 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_Gas_01
  17. {
  18.     public partial class Program
  19.     {
  20.  
  21.         // create a timer that ticks every half second
  22.         GT.Timer timer = new GT.Timer(500);          
  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 timer tick event handling robot
  44.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  45.  
  46.             // start the timer ticking
  47.             timer.Start();
  48.  
  49.             // turn on the gas sensor heating element
  50.             // oops USB hasn't enough power for a gas sensor and oled....!! crash!!
  51.             gasSense.SetHeatingElement(true);
  52.  
  53.             // stop the oled display redrawing every time we add a graphic element to the screen memory
  54.             oledDisplay.SimpleGraphics.AutoRedraw = false;
  55.  
  56.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  57.             Debug.Print("Program Started");
  58.         }
  59.  
  60.         void timer_Tick(GT.Timer timer)
  61.         {
  62.             // the timer has ticked!
  63.  
  64.             //read the gas sensor voltage
  65.             Debug.Print(gasSense.ReadVoltage().ToString());
  66.  
  67.             // jump out of the event handler to free it up
  68.             // print the message to the oled display
  69.             // convert the voltage number from its original form of a double to an integer and then to a string
  70.             processMessage((gasSense.ReadVoltage()).ToString());
  71.         }
  72.  
  73.         void processMessage(string recievedMessage)
  74.         {
  75.             // clear the oled display screen memory of graphics
  76.             oledDisplay.SimpleGraphics.Clear();
  77.  
  78.             // write the message to the display screen
  79.             printOledDisplay(1, recievedMessage);
  80.  
  81.             // redraw the screen with all new graphics
  82.             oledDisplay.SimpleGraphics.Redraw();
  83.  
  84.         }
  85.  
  86.         // a custom method for printing text to the OLED screen
  87.         // input a line number and the text to be printed as a text string
  88.         void printOledDisplay(uint lineNumber, string displayMessage)
  89.         {
  90.             uint xPos = 0;
  91.             oledDisplay.SimpleGraphics.DisplayText(displayMessage, displayFont, GT.Color.White, xPos, lineNumber * 12);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment