PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/GammaJul.LgLcd.Samples.Wpf/App.cs

#
C# | 106 lines | 61 code | 15 blank | 30 comment | 16 complexity | 28315dcd7cd9c38959c004b512e716c4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows;
  4. using System.Windows.Threading;
  5. using GammaJul.LgLcd.Wpf;
  6. namespace GammaJul.LgLcd.Samples.Wpf {
  7. /// <summary>
  8. /// Application class.
  9. /// </summary>
  10. internal class App : Application {
  11. private LcdApplet _applet;
  12. private DispatcherTimer _timer;
  13. private SampleControl _sampleControl;
  14. private LcdDeviceQvga _qvgaDevice;
  15. private delegate void Action();
  16. /// <summary>
  17. /// On startup, we are creation a new Applet.
  18. /// </summary>
  19. /// <param name="e"></param>
  20. protected override void OnStartup(StartupEventArgs e) {
  21. base.OnStartup(e);
  22. _applet = new LcdApplet("GammaJul LgLcd WPF Sample", LcdAppletCapabilities.Qvga);
  23. // Register to events to know when a device arrives, then connects the applet to the LCD Manager
  24. _applet.DeviceArrival += Applet_DeviceArrival;
  25. _applet.Connect();
  26. }
  27. /// <summary>
  28. /// Simple utility method to always executes a method on the Application's thread.
  29. /// </summary>
  30. /// <param name="action">Method to execute.</param>
  31. private void Invoke(Action action) {
  32. if (CheckAccess())
  33. action();
  34. else
  35. Dispatcher.BeginInvoke(action, DispatcherPriority.Render);
  36. }
  37. /// <summary>
  38. /// This event handler will be called whenever a new device of a given type arrives in the system.
  39. /// This is where you should opens the device you want to shows the applet on.
  40. /// Take special care for thread-safety as the SDK calls this handler in another thread.
  41. /// </summary>
  42. private void Applet_DeviceArrival(object sender, LcdDeviceTypeEventArgs e) {
  43. // since with specified LcdAppletCapabilities.Qvga at the Applet's creation,
  44. // we will only receive QVGA arrival notifications.
  45. Debug.Assert(e.DeviceType == LcdDeviceType.Qvga);
  46. Invoke(OnQvgaDeviceArrived);
  47. }
  48. private void OnQvgaDeviceArrived() {
  49. // First device arrival, creates the device
  50. if (_qvgaDevice == null) {
  51. _qvgaDevice = (LcdDeviceQvga) _applet.OpenDeviceByType(LcdDeviceType.Qvga);
  52. _sampleControl = new SampleControl();
  53. _qvgaDevice.CurrentPage = new LcdWpfPage(_qvgaDevice) {
  54. Element = _sampleControl
  55. };
  56. _qvgaDevice.SoftButtonsChanged += QvgaDevice_SoftButtonsChanged;
  57. // Starts a timer to update the screen
  58. _timer = new DispatcherTimer(TimeSpan.FromMilliseconds(5.0), DispatcherPriority.Render, Timer_Tick, Dispatcher.CurrentDispatcher);
  59. }
  60. // Subsequent device arrival means the device was removed and replugged, simply reopens it
  61. else
  62. _qvgaDevice.ReOpen();
  63. _qvgaDevice.DoUpdateAndDraw();
  64. }
  65. /// <summary>
  66. /// Updates the LCD screen.
  67. /// </summary>
  68. private void Timer_Tick(object sender, EventArgs e) {
  69. if (_applet.IsEnabled && _qvgaDevice != null && !_qvgaDevice.IsDisposed)
  70. _qvgaDevice.DoUpdateAndDraw();
  71. }
  72. /// <summary>
  73. /// When soft buttons are pressed, switch to previous image if left arrow button was clicked,
  74. /// switch to next if the right arrow button was clicked, or closes the application if
  75. /// the cancel button was clicked.
  76. /// </summary>
  77. private void QvgaDevice_SoftButtonsChanged(object sender, LcdSoftButtonsEventArgs e) {
  78. if ((e.SoftButtons & LcdSoftButtons.Cancel) == LcdSoftButtons.Cancel)
  79. Invoke(Shutdown);
  80. else if ((e.SoftButtons & LcdSoftButtons.Left) == LcdSoftButtons.Left)
  81. Invoke(_sampleControl.PreviousImage);
  82. else if ((e.SoftButtons & LcdSoftButtons.Right) == LcdSoftButtons.Right)
  83. Invoke(_sampleControl.NextImage);
  84. }
  85. [STAThread]
  86. internal static void Main() {
  87. App app = new App();
  88. app.Run();
  89. }
  90. }
  91. }