| |
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 I2CDevice _device = null;
private SPI _spi1 = 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("Push Select-Button to make a robot turn.");
//##########################################################################
//########################## Sample Code #################################
I2CDevice.Configuration config = new I2CDevice.Configuration(
58,
100
);
_device = new I2CDevice(config);
SPI.Configuration spi_config = new SPI.Configuration(
Cpu.Pin.GPIO_Pin11,
false,
1,
1,
true,
false,
15000,
SPI.SPI_module.SPI1
);
_spi1 = new SPI(spi_config);
System.Threading.Thread serialReaderThread = new System.Threading.Thread(spi_Reader);
serialReaderThread.Start();
//##########################################################################
return mainWindow;
}
private void OnButtonUp(object sender, ButtonEventArgs e)
{
//########################## Sample Code #################################
if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
{
byte[] wBuff = new byte[3];
wBuff[0] = 4;
wBuff[1] = 90; //-0.1
wBuff[2] = 110; //0.1
I2CDevice.I2CWriteTransaction wTr = _device.CreateWriteTransaction(wBuff);
I2CDevice.I2CTransaction[] wTrArr = new I2CDevice.I2CTransaction[] { wTr };
//Go Forwards
int count = _device.Execute(wTrArr, 1000);
_consoleWindow.WriteLine("Turn left");
}
//##########################################################################
}
//########################## Sample Code #################################
private void spi_Reader()
{
ushort[] wBuff = new ushort[1] { 0x0000 }; //dumy data
ushort[] rBuff = new ushort[1];
while (true)
{
try
{
_spi1.WriteRead(wBuff, rBuff);
if (rBuff != null)
{
ushort brightness = rBuff[0];
LogInfo("Detected brightness is " + brightness.ToString());
}
}
catch { }
System.Threading.Thread.Sleep(500);
}
}
private void LogInfo(string log)
{
this.Dispatcher.BeginInvoke(
new DispatcherOperationCallback(
delegate(object report)
{
_consoleWindow.WriteLine(log);
return null;
}
), null
);
}
//##########################################################################
}
}
| |