| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Threading;
- namespace DreamCheeky
- {
- public class BigRedButton : IDisposable
- {
- private readonly Device device;
- private volatile bool terminated;
- private Thread thread;
- public BigRedButton()
- {
- device = new Device();
- }
- public void Start()
- {
- device.Open();
- thread = new Thread(ThreadCallback);
- thread.Start();
- }
- private void ThreadCallback()
- {
- var lastStatus = DeviceStatus.Unknown;
- while (!terminated)
- {
- DeviceStatus status = device.GetStatus();
- if (status != DeviceStatus.Errored)
- {
- if (status == DeviceStatus.LidClosed && lastStatus == DeviceStatus.LidOpen)
- {
- OnLidClosed();
- }
- else if (status == DeviceStatus.ButtonPressed && lastStatus != DeviceStatus.ButtonPressed)
- {
- OnButtonPressed();
- }
- else if (status == DeviceStatus.LidOpen && lastStatus == DeviceStatus.LidClosed)
- {
- OnLidOpen();
- }
-
- lastStatus = status;
- }
- Thread.Sleep(100);
- }
- }
- public void Stop()
- {
- terminated = true;
- thread.Join();
- device.Close();
- }
- public void Dispose()
- {
- Stop();
- }
- private void OnLidOpen()
- {
- if (LidOpen != null)
- {
- LidOpen(this, EventArgs.Empty);
- }
- }
- private void OnLidClosed()
- {
- if (LidClosed != null)
- {
- LidClosed(this, EventArgs.Empty);
- }
- }
- private void OnButtonPressed()
- {
- if (ButtonPressed != null)
- {
- ButtonPressed(this, EventArgs.Empty);
- }
- }
- public EventHandler LidOpen;
- public EventHandler LidClosed;
- public EventHandler ButtonPressed;
- }
- }
|