77using Iot . Device . Ft6xx6x ;
88using nanoFramework . M5Core2 ;
99#elif TOUGH
10- using Iot . Device . Chs6540 ;
10+ using Iot . Device . Chsc6540 ;
1111using nanoFramework . Tough ;
1212#endif
1313using Iot . Device . Axp192 ;
2121using UnitsNet ;
2222using System . Threading ;
2323using nanoFramework . Runtime . Events ;
24+ using System . Diagnostics ;
2425
2526namespace nanoFramework . M5Stack
2627{
@@ -37,14 +38,15 @@ public static partial class Tough
3738#if M5CORE2
3839 private static Ft6xx6x _touchController ;
3940 private static bool _vibrate ;
40- #elif TOUGH
41- private static Chs6540 _touchController ;
42- private static bool _backLight ;
43- #endif
4441 private static Thread _callbackThread ;
4542 private static CancellationTokenSource _cancelThread ;
4643 private static CancellationTokenSource _startThread ;
4744 private static Point _lastPoint ;
45+ #elif TOUGH
46+ private static Chsc6540 _touchController ;
47+ private static bool _backLight ;
48+ private static TouchEventCategory _touchCategory ;
49+ #endif
4850
4951 /// <summary>
5052 /// Touch event handler for the touch event.
@@ -136,7 +138,7 @@ public static Ft6xx6x TouchController
136138 /// <summary>
137139 /// Gets the touch controller.
138140 /// </summary>
139- public static Chs6540 TouchController
141+ public static Chsc6540 TouchController
140142 {
141143 get
142144 {
@@ -165,22 +167,25 @@ public static void InitializeScreen(int memoryBitMapAllocation = Screen.DefaultM
165167 Console . Font = Resource . GetFont ( Resource . FontResources . consolas_regular_16 ) ;
166168#if M5CORE2
167169 _touchController = new ( I2cDevice . Create ( new I2cConnectionSettings ( 1 , Ft6xx6x . DefaultI2cAddress ) ) ) ;
168- #elif TOUGH
169- _touchController = new ( I2cDevice . Create ( new I2cConnectionSettings ( 1 , Chs6540 . DefaultI2cAddress ) ) ) ;
170- #endif
171- _touchController . SetInterruptMode ( false ) ;
172170 _lastPoint = new ( ) ;
173171 _cancelThread = new ( ) ;
174172 _startThread = new ( ) ;
175173 _callbackThread = new ( ThreadTouchCallback ) ;
176174 _callbackThread . Start ( ) ;
175+ #elif TOUGH
176+ _touchController = new ( I2cDevice . Create ( new I2cConnectionSettings ( 1 , Chsc6540 . DefaultI2cAddress ) ) ) ;
177+ #endif
178+ _touchController . SetInterruptMode ( false ) ;
179+
177180 GpioController . OpenPin ( TouchPinInterrupt , PinMode . Input ) ;
178181 GpioController . RegisterCallbackForPinValueChangedEvent ( TouchPinInterrupt , PinEventTypes . Rising | PinEventTypes . Falling , TouchCallback ) ;
179182 }
180183 }
181184
182185 private static void TouchCallback ( object sender , PinValueChangedEventArgs pinValueChangedEventArgs )
183186 {
187+ #if M5CORE2
188+
184189 if ( pinValueChangedEventArgs . ChangeType == PinEventTypes . Falling )
185190 {
186191 _cancelThread = new ( ) ;
@@ -190,6 +195,7 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal
190195 {
191196 _startThread = new ( ) ;
192197 _cancelThread . Cancel ( ) ;
198+
193199 var point = _touchController . GetPoint ( true ) ;
194200 if ( ( _lastPoint . X != point . X ) && ( _lastPoint . Y != point . Y ) )
195201 {
@@ -198,8 +204,26 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal
198204 TouchEvent ? . Invoke ( _touchController , new TouchEventArgs ( ) { TimeStamp = DateTime . UtcNow , EventCategory = EventCategory . Touch , TouchEventCategory = touchCategory , X = point . X , Y = point . Y , Id = point . TouchId } ) ;
199205 }
200206 }
207+ #else
208+ if ( pinValueChangedEventArgs . ChangeType == PinEventTypes . Falling )
209+ {
210+ var dp = _touchController . GetDoublePoints ( ) ;
211+
212+ if ( dp . Point1 . Event == Event . PressDown )
213+ {
214+ _touchCategory = TouchEventCategory . ScreenTouch ;
215+ }
216+ else if ( dp . Point1 . Event == Event . PressDown )
217+ {
218+ _touchCategory = TouchEventCategory . TouchGone ;
219+ }
220+
221+ TouchEvent ? . Invoke ( _touchController , new TouchEventArgs ( ) { TimeStamp = DateTime . UtcNow , EventCategory = EventCategory . Touch , TouchEventCategory = _touchCategory , X = dp . Point1 . X , Y = dp . Point1 . Y } ) ;
222+ }
223+ #endif
201224 }
202225
226+ #if M5CORE2
203227 private static void ThreadTouchCallback ( )
204228 {
205229 start :
@@ -210,9 +234,11 @@ private static void ThreadTouchCallback()
210234
211235 int touchNumber ;
212236 TouchEventCategory touchCategory ;
237+
213238 do
214239 {
215240 touchNumber = _touchController . GetNumberPoints ( ) ;
241+
216242 if ( touchNumber == 1 )
217243 {
218244 var point = _touchController . GetPoint ( true ) ;
@@ -235,6 +261,7 @@ private static void ThreadTouchCallback()
235261 // This is necessary to give time to the touch sensor
236262 // In theory, the wait should be calculated with the period
237263 _cancelThread . Token . WaitHandle . WaitOne ( 10 , true ) ;
264+
238265 } while ( ! _cancelThread . IsCancellationRequested ) ;
239266
240267 // If both token are cancelled, we exit. This is in case this won't become static and will have a dispose.
@@ -274,6 +301,7 @@ private static TouchEventCategory CheckIfInButtons(int x, int y, TouchEventCateg
274301
275302 return touchCategory ;
276303 }
304+ #endif
277305
278306#if M5CORE2
279307 static M5Core2 ( )
0 commit comments