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