This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
/
Blink1.cs
106 lines (86 loc) · 3.32 KB
/
Blink1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.HumanInterfaceDevice;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI;
namespace ThingM
{
public sealed class Blink1 : IDisposable
{
private static EventWaitHandle _devicesWaitHandle = new ManualResetEvent(false);
private static readonly DeviceWatcher _watcher;
private static readonly IDictionary<string, Blink1> _devices = new Dictionary<string, Blink1>();
private readonly DeviceInformation _deviceInformation;
private readonly Lazy<IAsyncOperation<HidDevice>> _device;
static Blink1()
{
var selector = HidDevice.GetDeviceSelector(0xFF00, 0x0001, 0x27B8, 0x01ED);
_watcher = DeviceInformation.CreateWatcher(selector);
_watcher.Added += HandleDeviceAdded;
_watcher.Updated += HandleDeviceUpdated;
_watcher.Removed += HandleDeviceRemoved;
_watcher.EnumerationCompleted += HandleEnumerationCompleted;
_watcher.Start();
}
private static void HandleEnumerationCompleted(DeviceWatcher sender, object args)
{
_devicesWaitHandle.Set();
sender.EnumerationCompleted -= HandleEnumerationCompleted;
}
private Blink1(DeviceInformation deviceInformation)
{
_deviceInformation = deviceInformation;
_device = new Lazy<IAsyncOperation<HidDevice>>(
() => HidDevice.FromIdAsync(_deviceInformation.Id, FileAccessMode.ReadWrite));
}
public static event EventHandler<Blink1> DeviceAdded;
public static event EventHandler<Blink1> DeviceRemoved;
public static IEnumerable<Blink1> Devices
{
get
{
_devicesWaitHandle.WaitOne();
return _devices.Values;
}
}
public async Task<uint> SetColorAsync(Color color)
{
var device = await _device.Value;
var report = device.CreateFeatureReport(1);
var buffer = report.Data.ToArray();
buffer[1] = (byte)'n';
buffer[2] = color.R;
buffer[3] = color.G;
buffer[4] = color.B;
report.Data = buffer.AsBuffer();
return await device.SendFeatureReportAsync(report);
}
public void Dispose()
{
if (_device.IsValueCreated)
{
_device.Value.GetResults().Dispose();
}
}
private static void HandleDeviceAdded(DeviceWatcher sender, DeviceInformation args)
{
var device = new Blink1(args);
_devices.Add(args.Id, device);
DeviceAdded?.Invoke(sender, device);
}
private static void HandleDeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
=> _devices[args.Id]._deviceInformation.Update(args);
private static void HandleDeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate args)
{
var device = _devices[args.Id];
_devices.Remove(args.Id);
DeviceRemoved?.Invoke(sender, device);
}
}
}