public static void Main() { Display = new Driver.NextionDisplay(Driver.NextionDisplay.TemporaryChangeBaudRate("COM2", 9600, 115200), 240, 320); DisplayConfiguration.Init(Display); Display.TouchEvent += Display_TouchEvent; Display.TouchXYEvent += Display_TouchXYEvent; Display.SetSleepMode(false); Display.Backlight = 100; Display.GUI.DefaultBackgroundColor = (int)Driver.Color.Black; Display.GUI.DefaultFontColor = (int)Driver.Color.Red; Display.GUI.DefaultFontId = (byte)DisplayConfiguration.Fonts.Sanserif_40_B; Display.GUI.DefaultTextHeight = 45; RenderScorePage(); while (true) { if (State == GameState.Score) { StartGame.WaitOne(); RenderGamePage(); } } }
static void Display_TouchXYEvent(object sender, Driver.TouchEventXYArgs args) { //Process on other thread otherwise you will create deadlock. //Thread pool would be usefull in this case var newThread = new Thread(new ThreadStart(() => { lock (threadLock) { LastTouchXY = args; if (SleepMode) { if (SleepAllowWakeup < DateTime.Now) { Display.SetSleepMode(false); SleepMode = false; } } else if (CurrentPage == Page.Ready) { SemaphoreXY.Set(); } else if (CurrentPage == Page.Draw5) { //Debuncer if (Draw5AllowNextTouch > DateTime.Now) { return; } if (Draw5Count >= 5) { Display.TouchMode = Driver.TouchMode.Component; Display.PageId = DisplayConfiguration.Page2.Id; CurrentPage = Page.DateTime; } else { DisplayConfiguration.Page3.txtX.Text = LastTouchXY.X.ToString(); DisplayConfiguration.Page3.txtY.Text = LastTouchXY.Y.ToString(); var color = CricleColorPalete[Rand.Next(CricleColorPalete.Length - 1)]; int shapeId = Rand.Next(3); if (shapeId == 0) { Display.GUI.DrawCircle(LastTouchXY.X, LastTouchXY.Y, 5, (int)color); } else if (shapeId == 1) { Display.GUI.DrawRectangle(LastTouchXY.X - 5, LastTouchXY.Y - 5, LastTouchXY.X + 5, LastTouchXY.Y + 5, (int)color); } else { Display.GUI.DrawRectangleFilled(LastTouchXY.X - 5, LastTouchXY.Y - 5, LastTouchXY.X + 5, LastTouchXY.Y + 5, (int)color); } if (Draw5Count > 0) { Display.GUI.DrawLine(Draw5LastX, Draw5LastY, LastTouchXY.X, LastTouchXY.Y, (int)Driver.Color.Black); } Draw5LastX = LastTouchXY.X; Draw5LastY = LastTouchXY.Y; Draw5AllowNextTouch = DateTime.Now.AddMilliseconds(500); Draw5Count++; } } } })); newThread.Start(); }