-
Notifications
You must be signed in to change notification settings - Fork 1
/
modDreamCheeky.vb
371 lines (311 loc) · 13.7 KB
/
modDreamCheeky.vb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
Imports HidLibrary
Module modDreamCheeky
'Heavily based on this Github project from mbenford: https://github.com/mbenford/dreamcheeky-big-red-button-dotnet
'And this one from MrRenaud: https://github.com/MrRenaud/DreamCheekyUSB
'Significant adaption help provided via: http://codeconverter.sharpdevelop.net/SnippetConverter.aspx
Dim BigRedButtonIndex As Integer
Dim WebMailNotifierIndex As Integer
Public Sub CreateButton()
Dim BigRedButton As HABigRedButton = New HABigRedButton
If BigRedButton.IsConnected = True Then
DeviceCollection.Add(BigRedButton)
BigRedButtonIndex = DeviceCollection.IndexOf(BigRedButton)
My.Application.Log.WriteEntry("Big Red Button has a device index of " & BigRedButtonIndex)
Else
My.Application.Log.WriteEntry("Big Red Button not found")
BigRedButton.Dispose()
End If
End Sub
Public Sub CreateNotifier()
Dim WebMailNotifier As HAWebMailNotifier = New HAWebMailNotifier
If WebMailNotifier.IsConnected = True Then
DeviceCollection.Add(WebMailNotifier)
WebMailNotifierIndex = DeviceCollection.IndexOf(WebMailNotifier)
My.Application.Log.WriteEntry("WebMail Notifier has a device index of " & WebMailNotifierIndex)
DeviceCollection(WebMailNotifierIndex).SetColor("CornflowerBlue")
DeviceCollection(WebMailNotifierIndex).TurnOn()
Else
My.Application.Log.WriteEntry("WebMail Notifier not found")
WebMailNotifier.Dispose()
End If
End Sub
Function Disable() As String
Unload()
My.Settings.DreamCheeky_Enable = False
My.Application.Log.WriteEntry("DreamCheeky module is disabled")
Return "DreamCheeky module disabled"
End Function
Function Enable() As String
My.Settings.DreamCheeky_Enable = True
My.Application.Log.WriteEntry("DreamCheeky module is enabled")
Load()
Return "DreamCheeky module enabled"
End Function
Function Load() As String
If My.Settings.DreamCheeky_Enable = True Then
My.Application.Log.WriteEntry("Loading DreamCheeky module")
CreateButton()
CreateNotifier()
Return "DreamCheeky module loaded"
Else
My.Application.Log.WriteEntry("DreamCheeky module is disabled, module not loaded")
Return "DreamCheeky module is disabled, module not loaded"
End If
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading DreamCheeky module")
Return "DreamCheeky module unloaded"
End Function
Public Class HABigRedButton
Inherits HAUSBDevice
Private ReadOnly device As IHidDevice
Private thread As Threading.Thread
Private ReadOnly StatusReport As Byte() = {0, 0, 0, 0, 0, 0, 0, 2}
Private IsTerminated As Boolean
Public Sub Close()
My.Application.Log.WriteEntry("HABigRedButton - Close Device")
[Stop]()
End Sub
Public Overloads Sub Dispose()
If Me.IsConnected = True Then
Me.Close()
End If
End Sub
Public Function GetStatus() As BigRedButtonDeviceStatus
My.Application.Log.WriteEntry("HABigRedButton - Getting Status")
If Not device.Write(StatusReport, 100) Then
Return BigRedButtonDeviceStatus.Errored
End If
Dim data As HidDeviceData = device.Read(100)
If data.Status <> HidDeviceData.ReadStatus.Success Then
Return BigRedButtonDeviceStatus.Errored
End If
Return DirectCast(CInt(data.Data(1)), BigRedButtonDeviceStatus)
End Function
Public Sub New()
Me.DeviceName = "Big Red Button"
Me.DeviceType = "Controller"
Me.DeviceUID = "usb_0x1D34_0x000D"
Me.Model = "Dream Cheeky 902 Big Red Button"
Me.VendorID = 7476 '0x1D34
Me.DeviceID = 13 '0x000D
My.Application.Log.WriteEntry("HABigRedButton - Create Device")
Dim hidEnumerator As HidEnumerator = New HidEnumerator
device = hidEnumerator.Enumerate(VendorID, DeviceID).FirstOrDefault()
If device Is Nothing Then
'Throw New InvalidOperationException("Device not found")
Me.IsConnected = False
Else
Me.IsConnected = True
Me.Open()
End If
End Sub
Public Sub Open()
My.Application.Log.WriteEntry("HABigRedButton - Open Device")
device.OpenDevice()
My.Application.Log.WriteEntry("HABigRedButton - Create Thread")
thread = New Threading.Thread(AddressOf ThreadCallback)
thread.Start()
End Sub
Private Sub ThreadCallback()
Dim lastStatus = BigRedButtonDeviceStatus.Unknown
While Not IsTerminated
Dim status As BigRedButtonDeviceStatus = Me.GetStatus()
If status <> BigRedButtonDeviceStatus.Errored Then
If status = BigRedButtonDeviceStatus.LidClosed AndAlso lastStatus = BigRedButtonDeviceStatus.LidOpen Then
OnLidClosed()
ElseIf status = BigRedButtonDeviceStatus.ButtonPressed AndAlso lastStatus <> BigRedButtonDeviceStatus.ButtonPressed Then
OnButtonPressed()
ElseIf status = BigRedButtonDeviceStatus.LidOpen AndAlso lastStatus = BigRedButtonDeviceStatus.LidClosed Then
OnLidOpen()
End If
lastStatus = status
End If
Threading.Thread.Sleep(100)
End While
End Sub
Public Sub [Stop]()
IsTerminated = True
thread.Join()
device.CloseDevice()
End Sub
Private Sub OnLidOpen()
My.Application.Log.WriteEntry("HABigRedButton - Lid Open")
RaiseEvent LidOpen(Me, EventArgs.Empty)
End Sub
Private Sub OnLidClosed()
My.Application.Log.WriteEntry("HABigRedButton - Lid Closed")
RaiseEvent LidClosed(Me, EventArgs.Empty)
End Sub
Private Sub OnButtonPressed()
My.Application.Log.WriteEntry("HABigRedButton - Button Pushed")
RaiseEvent ButtonPressed(Me, EventArgs.Empty)
End Sub
Public Event LidOpen As EventHandler
Public Event LidClosed As EventHandler
Public Event ButtonPressed As EventHandler
End Class
<Serializable()>
Public Class HAWebMailNotifier
Inherits HAUSBDevice
Public Property Color As Color
Public Property DevicePath As String
Private ReadOnly device As IHidDevice
Private ReadOnly maxColorValue As Byte = 64 'Colors are capped at 64, so scale accordingly
Private ReadOnly init01 As Byte() = {0, 31, 2, 0, 95, 0, 0, 31, 3}
Private ReadOnly init02 As Byte() = {0, 0, 2, 0, 95, 0, 0, 31, 4}
Private ReadOnly init03 As Byte() = {0, 0, 0, 0, 0, 0, 0, 31, 5}
Private ReadOnly init04 As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 1}
Private writeLock As New Object
Private IsInitialized As Boolean = False
Public Sub Blink(Optional ByVal Times As Integer = 1, Optional ByVal BlinkMS As Integer = 500)
If (Times <= 0 Or BlinkMS <= 0) Then
Throw New ArgumentOutOfRangeException("Cannot blink negative times or for negative duration")
End If
Dim i As Integer = 0
While i < Times
TurnOn()
Threading.Thread.Sleep(BlinkMS)
TurnOff()
Threading.Thread.Sleep(BlinkMS)
i = i + 1
End While
End Sub
Public Sub Close()
My.Application.Log.WriteEntry("WebMail Notifier - Close Device", TraceEventType.Verbose)
device.CloseDevice()
End Sub
Private Sub device_Inserted()
My.Application.Log.WriteEntry("WebMail Notifier - Device Inserted")
Open()
End Sub
Private Sub device_Removed()
My.Application.Log.WriteEntry("WebMail Notifier - Device Unplugged", TraceEventType.Warning)
Me.IsConnected = False
Me.IsInitialized = False
End Sub
Public Overloads Sub Dispose()
If Me.IsConnected = True Then
Me.Close()
End If
End Sub
Public Sub FadeIn(ByVal TotalMS As Integer)
If (TotalMS <= 0) Then
Throw New ArgumentOutOfRangeException("Cannot fade out negative length of time")
End If
Dim clrTemp As Byte()
Dim t As Integer = 0
Dim s As Integer = 35
Dim r As Double = 0
Dim bytRed, bytGreen, bytBlue As Byte
While t < TotalMS
Threading.Thread.Sleep(s)
r = CDbl(t) / CDbl(TotalMS)
bytRed = CByte(Me.Color.R * r)
bytGreen = CByte(Me.Color.G * r)
bytBlue = CByte(Me.Color.B * r)
clrTemp = {0, bytRed, bytGreen, bytBlue, 0, 0, 0, 31, 5}
Me.Write(clrTemp)
t += s
End While
End Sub
Public Sub FadeOut(ByVal TotalMS As Integer)
If (TotalMS <= 0) Then
Throw New ArgumentOutOfRangeException("Cannot fade out negative length of time")
End If
Dim clrTemp As Byte()
Dim t As Integer = 0
Dim s As Integer = 35
Dim r As Double = 0
Dim bytRed, bytGreen, bytBlue As Byte
While t < TotalMS
Threading.Thread.Sleep(s)
r = CDbl(t) / CDbl(TotalMS)
bytRed = CByte(Me.Color.R - (Me.Color.R * r))
bytGreen = CByte(Me.Color.G - (Me.Color.G * r))
bytBlue = CByte(Me.Color.B - (Me.Color.B * r))
clrTemp = {0, bytRed, bytGreen, bytBlue, 0, 0, 0, 31, 5}
Me.Write(clrTemp)
t += s
End While
End Sub
Public Sub New()
Me.DeviceName = "WebMail Notifier"
Me.DeviceType = "Display"
Me.DeviceUID = "usb_0x1D34_0x0004"
Me.Model = "Dream Cheeky 815 WebMail Notifier"
Me.VendorID = 7476 '0x1D34
Me.DeviceID = 4 '0x0004
Me.Color = Drawing.Color.White
My.Application.Log.WriteEntry("WebMail Notifier - Create Device")
Dim hidEnumerator As HidEnumerator = New HidEnumerator
device = hidEnumerator.Enumerate(VendorID, DeviceID).FirstOrDefault()
If device Is Nothing Then
'Throw New InvalidOperationException("Device not found")
Me.IsConnected = False
Else
Me.DevicePath = device.DevicePath
device.MonitorDeviceEvents = True
AddHandler device.Inserted, AddressOf device_Inserted
AddHandler device.Removed, AddressOf device_Removed
Me.IsConnected = True
Me.Open()
End If
End Sub
Public Sub Open()
My.Application.Log.WriteEntry("WebMail Notifier - Open Device", TraceEventType.Verbose)
device.OpenDevice()
SyncLock writeLock
My.Application.Log.WriteEntry("WebMail Notifier - Initialize Device", TraceEventType.Verbose)
device.Write(init01)
Threading.Thread.Sleep(100)
device.Write(init02)
Threading.Thread.Sleep(100)
device.Write(init03)
Threading.Thread.Sleep(100)
device.Write(init04)
Threading.Thread.Sleep(100)
Me.IsInitialized = True
My.Application.Log.WriteEntry("WebMail Notifier - Initialized", TraceEventType.Verbose)
End SyncLock
End Sub
Public Sub SetColor(ByVal bytRed As Byte, ByVal bytGreen As Byte, ByVal bytBlue As Byte)
Me.Color = Color.FromArgb(bytRed, bytGreen, bytBlue)
My.Application.Log.WriteEntry("WebMail Notifier color set to " & bytRed & ", " & bytGreen & ", " & bytBlue)
End Sub
Public Sub SetColor(ByVal NewColor As Color)
Me.Color = NewColor
My.Application.Log.WriteEntry("WebMail Notifier color set to " & Me.Color.Name)
End Sub
Public Sub SetColor(ByVal colColor As String)
Me.Color = Color.FromName(colColor)
My.Application.Log.WriteEntry("WebMail Notifier color set to " & Me.Color.Name)
End Sub
Public Sub TurnOff()
Dim clrOff As Byte() = {0, 0, 0, 0, 0, 0, 0, 31, 5}
Me.Write(clrOff)
End Sub
Public Sub TurnOn()
'Call this publicly to turn on the previously determined color
Dim bytRed As Byte = CByte(Math.Truncate((CSng(Color.R) / 255.0F) * maxColorValue))
Dim bytGreen As Byte = CByte(Math.Truncate((CSng(Color.G) / 255.0F) * maxColorValue))
Dim bytBlue As Byte = CByte(Math.Truncate((CSng(Color.B) / 255.0F) * maxColorValue))
Dim clrCustom As Byte() = {0, bytRed, bytGreen, bytBlue, 0, 0, 0, 31, 5}
Me.Write(clrCustom)
End Sub
Private Sub Write(ByVal arrBytes As Byte())
If Me.IsInitialized = True Then
SyncLock writeLock
device.Write(arrBytes)
End SyncLock
End If
End Sub
End Class
Public Enum BigRedButtonDeviceStatus
Unknown = 0
Errored = 1
LidClosed = 21
ButtonPressed = 22
LidOpen = 23
End Enum
End Module