I started experimenting with Glide and experiencing couple is issues:
[ol]Touch works only when I use the CapacitiveTouchController class and raise TouchUp and TouchDown events manually.
When I call Glide.MainWindow = this.calibrationWindow; . the calibration windows shows up with “Touch the screen to start” and when I do, it runs the calibration on itself. I mean I can see the red crosses showing up in the corners and disappears.[/ol]
Am I missing something in the GlideTouch initialization?
Here is the complete code:
using GHI.Glide;
using GHI.Glide.Display;
using GHI.Processor;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Touch;
using System.Threading;
namespace MFWindowApplication1
{
public class Program : Microsoft.SPOT.Application
{
private GHI.Glide.Display.Window mainWindow;
private CalibrationWindow calibrationWindow;
public static void Main()
{
Display.Width = 480;
Display.Height = 272;
Display.OutputEnableIsFixed = false;
Display.OutputEnablePolarity = true;
Display.PixelPolarity = false;
Display.PixelClockRateKHz = 20000;
Display.HorizontalSyncPolarity = false;
Display.HorizontalSyncPulseWidth = 41;
Display.HorizontalBackPorch = 2;
Display.HorizontalFrontPorch = 2;
Display.VerticalSyncPolarity = false;
Display.VerticalSyncPulseWidth = 10;
Display.VerticalBackPorch = 2;
Display.VerticalFrontPorch = 2;
Display.Type = Display.DisplayType.Lcd;
if (Display.Save())
PowerState.RebootDevice(false);
Program app = new Program();
app.Run();
}
public Program()
{
// Touch works only when I use the CapacitiveTouchController class and raise TouchUp and TouchDown events manually-
// Without this code block no touch responce
CapacitiveTouchController touch = new CapacitiveTouchController(Cpu.Pin.GPIO_Pin6);
touch.ScreenPressed += Touch_ScreenPressed;
touch.ScreenReleased += Touch_ScreenReleased;
// ----------------------------------------------------------------------------------------------------------------------------------------------------------
mainWindow = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window));
GlideTouch.Initialize();
GHI.Glide.Display.DisplayObject btn = mainWindow.GetChildByName("cmd1");
btn.TapEvent += OnTap;
calibrationWindow = new CalibrationWindow(false, false);
calibrationWindow.CloseEvent += OnClose;
Glide.MainWindow = mainWindow;
Thread.Sleep(Timeout.Infinite);
}
private void OnClose(object sender)
{
Glide.MainWindow = this.mainWindow;
}
private void OnTap(object sender)
{
Glide.MainWindow = this.calibrationWindow;
}
private void Touch_ScreenReleased(CapacitiveTouchController sender, CapacitiveTouchController.TouchEventArgs e)
{
GlideTouch.RaiseTouchUpEvent(null, new TouchEventArgs(new TouchInput[] { new GlideTouchInput(e.X, e.Y ) }));
}
private void Touch_ScreenPressed(CapacitiveTouchController sender, CapacitiveTouchController.TouchEventArgs e)
{
GlideTouch.RaiseTouchDownEvent(null, new TouchEventArgs(new TouchInput[] { new GlideTouchInput(e.X, e.Y) }));
}
}
public class GlideTouchInput : TouchInput
{
public GlideTouchInput(int x, int y) : base()
{
base.X = x;
base.Y = y;
}
}
public class CapacitiveTouchController
{
private InterruptPort touchInterrupt;
private I2CDevice i2cBus;
private I2CDevice.I2CTransaction[] transactions;
private byte[] addressBuffer;
private byte[] resultBuffer;
public delegate void TouchEventHandler(CapacitiveTouchController sender, TouchEventArgs e);
public event TouchEventHandler ScreenPressed;
public event TouchEventHandler ScreenReleased;
public CapacitiveTouchController(Cpu.Pin interruptPin)
{
this.transactions = new I2CDevice.I2CTransaction[2];
this.resultBuffer = new byte[1];
this.addressBuffer = new byte[1];
this.i2cBus = new I2CDevice(new I2CDevice.Configuration(0x38, 400));
this.touchInterrupt = new InterruptPort(interruptPin, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
this.touchInterrupt.OnInterrupt += (a, b, c) => this.OnTouchEvent();
}
private void OnTouchEvent()
{
for (var i = 0; i < 5; i++)
{
var first = this.ReadRegister((byte)(3 + i * 6));
var x = ((first & 0x0F) << 8) + this.ReadRegister((byte)(4 + i * 6));
var y = ((this.ReadRegister((byte)(5 + i * 6)) & 0x0F) << 8) + this.ReadRegister((byte)(6 + i * 6));
if (x == 4095 && y == 4095)
break;
if (((first & 0xC0) >> 6) == 1)
{
this.ScreenReleased(this, new TouchEventArgs(x, y));
}
else {
this.ScreenPressed(this, new TouchEventArgs(x, y));
}
}
}
private byte ReadRegister(byte address)
{
this.addressBuffer[0] = address;
this.transactions[0] = I2CDevice.CreateWriteTransaction(this.addressBuffer);
this.transactions[1] = I2CDevice.CreateReadTransaction(this.resultBuffer);
this.i2cBus.Execute(this.transactions, 1000);
return this.resultBuffer[0];
}
public class TouchEventArgs : EventArgs
{
public int X { get; set; }
public int Y { get; set; }
internal TouchEventArgs(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}
}