Hello!
The touchscreen does work but not that well. I searched around but didn’t see any other post like this.
I find that it often takes 3 or 4 and sometimes more pushing on the screen to get for example, a button to pushdown. I think the problem is that for some strange reason, sometimes many dozens of touch up events come in after pushing down and it takes time for all these events to clear out.
Here is the code and I have commented where I see the issue in the OnInterrupt routine below: (also I didn’t write this code but trying to use it as I got it from another developer on another project)
public class FT5xx6Controller : IDisposable
{
private readonly byte[] addressBuffer = new byte[1] { 0 };
private readonly byte[] readBuffer = new byte[32];
private readonly I2CDevice i2c;
private readonly InterruptPort interrupt;
private readonly I2CDevice.I2CTransaction[] transactions;
public event TouchEventHandler TouchDown;
public event TouchEventHandler TouchUp;
public event TouchEventHandler TouchMove;
public event GestureEventHandler GestureReceived;
public int SampleCount { get; set; }
public byte version { get; set; }
private int numDownInARow = 0;
private int numUpInARow = 0;
private int numMoveInARow = 0;
private bool inPressReleaseCycle = false;
private int lastFlag = -1;
private int lastX = -1;
private int lastY = -1;
private int lastDownX = -1;
private int lastDownY = -1;
public static I2CDevice.Configuration GetI2cConfiguration() { return new I2CDevice.Configuration(0x38, 400); }
public FT5xx6Controller(I2CDevice i2c, Cpu.Pin interruptPin)
{
//this number must be less than (readBuffer.length - 3) / 6
this.SampleCount = 1;
this.version = 1;
this.i2c = i2c;
this.transactions = new I2CDevice.I2CTransaction[] {
I2CDevice.CreateWriteTransaction(this.addressBuffer),
I2CDevice.CreateReadTransaction(this.readBuffer)
};
Port.InterruptMode.InterruptEdgeLow);
this.interrupt = new InterruptPort(interruptPin, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
this.interrupt.OnInterrupt += (a, b, c) => this.OnInterrupt();
}
public void Dispose()
{
this.i2c.Dispose();
this.interrupt.Dispose();
}
private void OnInterrupt()
{
try
{
for (int i = 0; i < readBuffer.Length; i++)
{
readBuffer[i] = 0;
}
if (this.i2c.Execute(this.transactions, 1000) == 0)
{
return;
}
if (this.readBuffer[1] != 0 && this.GestureReceived != null)
this.GestureReceived(this, new GestureEventArgs((Gesture)this.readBuffer[1]));
//We do not read the TD_STATUS register because it returns a touch count _excluding_ touch up events, even though the touch registers contain the proper touch up data.
for (var i = 0; i < this.SampleCount; i++)
{
var idx = i * 6 + 3;
int flag = readBuffer[0 + idx] >> 6;
int evID = readBuffer[2 + idx] >> 4;
int x = ((this.readBuffer[0 + idx] & 0x0F) << 8) | this.readBuffer[1 + idx];
int y = ((this.readBuffer[2 + idx] & 0x0F) << 8) | this.readBuffer[3 + idx];
if (flag == 3 || evID == 0xF)
{
break;
}
**//getting numerous events where flag = 1 long after button pushed and released!!**
switch (flag)
{
case 0:
inPressReleaseCycle = true;
numDownInARow++;
numUpInARow = 0;
numMoveInARow = 0;
lastDownX = x;
lastDownY = y;
this.TouchDown(this, new TouchEventArgs(x, y));
break;
case 1:
if (inPressReleaseCycle)
{
this.TouchUp(this, new TouchEventArgs(lastDownX, lastDownY));
++numUpInARow;
numDownInARow = 0;
numMoveInARow = 0;
inPressReleaseCycle = false;
break;
}
//ignore the event if it is a repeat of previous event.
if (lastFlag == flag && lastX == x && lastY == y)
{
++numUpInARow;
numDownInARow = 0;
numMoveInARow = 0;
break;
}
if (version != 0)
{
this.TouchDown(this, new TouchEventArgs(y, x));
this.TouchUp(this, new TouchEventArgs(y, x));
}
else
{
this.TouchDown(this, new TouchEventArgs(x, y));
this.TouchUp(this, new TouchEventArgs(x, y));
}
++numUpInARow;
numDownInARow = 0;
numMoveInARow = 0;
inPressReleaseCycle = false;
break;
case 2:
++numMoveInARow;
numDownInARow = 0;
numUpInARow = 0;
this.TouchMove(this, new TouchEventArgs(x, y));
break;
default:
break;
}
lastFlag = flag;
lastX = x;
lastY = y;
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
}