forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
133 lines (107 loc) · 4.72 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using Driver;
using System;
using System.Diagnostics;
using System.Threading;
using Windows.Devices.Gpio;
using static Driver.STMPE811;
namespace I2CDemoApp
{
public class Program
{
static STMPE811 _touchController;
public static GpioPin _touchInterrupt;
public static ManualResetEvent _touchEvent;
public static void Main()
{
// F429I_DISCO: PG14 is LEDLD4
GpioPin led = GpioController.GetDefault().OpenPin(PinNumber('G', 14));
led.SetDriveMode(GpioPinDriveMode.Output);
// create the event NOT signalled
_touchEvent = new ManualResetEvent(false);
// STMPE811 touchscreen controller in STM32F429 DISCOVERY board has default I2C address 1000001 = 0x41 ...
// ... and it's connected to I2C3 bus
_touchController = new STMPE811(0x41, "I2C3");
// ... INT signal is connected to PA15 with a pull-up, setup the GPIO and an event handler to handle the device interrupt requests
var gpioController = GpioController.GetDefault();
_touchInterrupt = gpioController.OpenPin(PinNumber('A', 15));
_touchInterrupt.SetDriveMode(GpioPinDriveMode.Input);
_touchInterrupt.ValueChanged += TouchScreenInterruptRequest;
// initialize STMPE811
if (_touchController.Initialize())
{
// start the touch screen controller
_touchController.Start();
// in the F429I DISCOVERY the INT signal has a pull-up so we need to configure the interrupt polarity to active low
_touchController.SetInterruptPolarity(InterruptPolarity.Low);
// as for the type better use level to improve detection
_touchController.SetInterruptType(InterruptType.Level);
// enable interrupts from the FIFO and the touch controller
_touchController.EnableInterruptSource(InterruptSource.FifoAboveThreshold | InterruptSource.Touch);
_touchController.EnableGlobalInterrupt();
// output the ID and revision of the device
Debug.WriteLine("ChipID " + _touchController.ChipID.ToString("X4"));
Debug.WriteLine("Rev " + _touchController.RevisionNumber.ToString());
// launch touch tracking thread
new Thread(new ThreadStart(TouchTracking)).Start();
}
else
{
// failed to init the device
Debug.WriteLine("***** FATAL ERROR: failed to initialise the touch screen controller *****");
}
// infinite loop to keep main thread active
for (; ; )
{
led.Toggle();
Thread.Sleep(1000);
}
}
public static void TouchScreenInterruptRequest(object sender, GpioPinValueChangedEventArgs e)
{
// get interrupt status from touch controller
InterruptSource interruptStatus = _touchController.ReadGlobalInterruptStatus();
if ((interruptStatus & InterruptSource.Touch) == InterruptSource.Touch)
{
// set the touch event
_touchEvent.Set();
}
// clear interrupt flags
_touchController.ClearGlobalInterrupt(interruptStatus);
}
public static void TouchTracking()
{
Reading touchReading;
while (true)
{
// wait for the touch event
_touchEvent.WaitOne();
// disable all FIFO interrupts
_touchController.DisableInterruptSource(InterruptSource.AllFIFOs);
// get touch readings from FIFO until its empty
while (_touchController.FifoSize() > 0)
{
touchReading = _touchController.ReadTouch();
if (touchReading.IsValid)
{
Debug.WriteLine("Touchscreen pressed @ (" + touchReading.X + "," + touchReading.Y + ")");
}
}
// enable back FIFO threshold interrupt
_touchController.EnableInterruptSource(InterruptSource.FifoAboveThreshold);
Debug.WriteLine("Touchscreen released");
// clear event
_touchEvent.Reset();
}
}
static int PinNumber(char port, byte pin)
{
if (port < 'A' || port > 'J')
throw new ArgumentException();
return ((port - 'A') * 16) + pin;
}
}
}