| |
C# GPIO programming - Sample code for GPIO TristatePort programming
1. Download and open C# project file
- Download source code.
2. C# source code for TristatePort.Sample1
- Program.cs
| |
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Hardware;
using SPL.Script.Engine;
namespace SPLMFConsoleWindow1
{
public class Program : Microsoft.SPOT.Application
{
public static void Main()
{
Program myApplication = new Program();
Window mainWindow = myApplication.CreateWindow();
GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
myApplication.Run(mainWindow);
}
private Window mainWindow;
//########################## Sample Code #################################
private ConsoleWindow _consoleWindow;
private TristatePort _port = null;
//##########################################################################
public Window CreateWindow()
{
mainWindow = new Window();
mainWindow.Height = SystemMetrics.ScreenHeight;
mainWindow.Width = SystemMetrics.ScreenWidth;
//########################## Sample Code #################################
Font font = Resources.GetFont(Resources.FontResources.small);
_consoleWindow = new ConsoleWindow(font);
mainWindow.Child = _consoleWindow;
//##########################################################################
mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
mainWindow.Visibility = Visibility.Visible;
Buttons.Focus(mainWindow);
//########################## Sample Code #################################
_consoleWindow.WriteLine("Please push any button!.");
_consoleWindow.WriteLine("Select button will clear screen.");
//##########################################################################
//########################## Sample Code #################################
_port = new TristatePort(Cpu.Pin.GPIO_Pin6, false, false, Port.ResistorMode.PullDown);
_port.Active = true;
//##########################################################################
return mainWindow;
}
private void OnButtonUp(object sender, ButtonEventArgs e)
{
Debug.Print(e.Button.ToString());
//########################## Sample Code #################################
if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
{
_consoleWindow.ClearConsole();
}
else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_LEFT)
{
if (!_port.Active)
_port.Active = true;
_consoleWindow.WriteLine("Active was set as true.");
}
else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_RIGHT)
{
if (_port.Active)
_port.Active = false;
_consoleWindow.WriteLine("Active was set as false.");
}
else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
{
bool state = _port.Read();
_consoleWindow.WriteLine("Curruent state is " + state.ToString());
}
else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_DOWN)
{
bool state = _port.Read();
state = !state;
_port.Write(state);
_consoleWindow.WriteLine("Curruent state was changed as " + state.ToString());
}
//##########################################################################
}
}
}
| |
|
3. Execute sample code
- Open "Properties" page of C# project.
- Choose ".NET Micro Framework" tab.
- Select "SPLMFConsoleEmulator" from the emulator list.
- Press "F5" key to run sample code.
- Click Up-Button, then you can see the current state of port is printed.
4. Change the Active property of port
- Click "Left Button" to set the Active property as true.
- In this mode, you can change the state of port.
- Click Up-Button or Down-Button.
- Click "Right Button" to set the Active property as false.
- In this mode, you can't change the state of port. Only you can read the state.
- Click Up-Button to read current state.
|