| |
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 I2CDevice _i2c2 = 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 go forwards.");
//##########################################################################
//########################## Sample Code #################################
I2CDevice.Configuration config = new I2CDevice.Configuration(
58,
100
);
_device = new I2CDevice(config);
I2CDevice.Configuration i2c2_config = new I2CDevice.Configuration(
60,
100
);
_i2c2 = new I2CDevice(i2c2_config);
System.Threading.Thread serialReaderThread = new System.Threading.Thread(i2c2_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] = 120; //120-100
wBuff[2] = 120;
I2CDevice.I2CWriteTransaction wTr = _device.CreateWriteTransaction(wBuff);
I2CDevice.I2CTransaction[] wTrArr = new I2CDevice.I2CTransaction[] { wTr };
//Go Forwards
int count = _device.Execute(wTrArr, 1000);
_consoleWindow.WriteLine("Go forwards");
}
//##########################################################################
}
//########################## Sample Code #################################
private void i2c2_Reader()
{
byte[] rBuff = new byte[2];
while (true)
{
try
{
I2CDevice.I2CReadTransaction rTr = _device.CreateReadTransaction(rBuff);
I2CDevice.I2CTransaction[] rTrArr = new I2CDevice.I2CTransaction[] { rTr };
int count = _i2c2.Execute(rTrArr, 1000);
if (rBuff != null)
{
ushort distance = Util.BytesToUShort(rBuff);
if (distance > 0 && distance < 1000)
{
byte[] wBuff = new byte[3];
wBuff[0] = 2;
wBuff[1] = 190; //190-100
wBuff[2] = 120; //0.2
I2CDevice.I2CWriteTransaction wTr = _device.CreateWriteTransaction(wBuff);
I2CDevice.I2CTransaction[] wTrArr = new I2CDevice.I2CTransaction[] { wTr };
//Turn 90
count = _device.Execute(wTrArr, 1000);
System.Threading.Thread.Sleep(500);
//Turn 90
count = _device.Execute(wTrArr, 1000);
System.Threading.Thread.Sleep(500);
wBuff[0] = 4;
wBuff[1] = 120; //0.2
wBuff[2] = 120; //0.2
count = _device.Execute(wTrArr, 500);
}
}
}
catch { }
System.Threading.Thread.Sleep(300);
}
}
private void LogInfo(string log)
{
this.Dispatcher.BeginInvoke(
new DispatcherOperationCallback(
delegate(object report)
{
_consoleWindow.WriteLine(log);
return null;
}
), null
);
}
//##########################################################################
}
}
| |