-
Notifications
You must be signed in to change notification settings - Fork 1
/
modMatrixLCD.vb
393 lines (341 loc) · 14.2 KB
/
modMatrixLCD.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
Module modMatrixLCD
Public MatrixLCDConnected As Boolean = False
Public MatrixLCDisplayIndex As Integer
Public DashMode As Boolean = False
Public intToast As Integer = 0
Function Disable() As String
Unload()
My.Settings.MatrixLCD_Enable = False
My.Application.Log.WriteEntry("Matrix LCD module is disabled")
Return "Matrix LCD module disabled"
End Function
Function Enable() As String
My.Settings.MatrixLCD_Enable = True
My.Application.Log.WriteEntry("Matrix LCD module is enabled")
Load()
Return "Matrix LCD module enabled"
End Function
Function Load() As String
If My.Settings.MatrixLCD_Enable = True Then
My.Application.Log.WriteEntry("Loading Matrix LCD module")
Dim MatrixLCDisplay As HAMatrixLCD = New HAMatrixLCD
If MatrixLCDisplay.IsConnected = True Then
MatrixLCDConnected = True
DeviceCollection.Add(MatrixLCDisplay)
MatrixLCDisplayIndex = DeviceCollection.IndexOf(MatrixLCDisplay)
My.Application.Log.WriteEntry("Matrix LCD has a device index of " & MatrixLCDisplayIndex)
Return "Matrix LCD module loaded"
Else
My.Application.Log.WriteEntry("Matrix LCD not found")
MatrixLCDisplay.Dispose()
Return "Matrix LCD not found"
End If
Else
My.Application.Log.WriteEntry("Matrix LCD module is disabled, module not loaded")
Return "Matrix LCD module is disabled, module not loaded"
End If
End Function
Sub ShowNotification(ByVal strLine1 As String, Optional ByVal strLine2 As String = "", Optional ByVal IsToast As Boolean = True)
If MatrixLCDConnected = True Then
Dim MatrixLCDisplay As HAMatrixLCD = DeviceCollection.Item(MatrixLCDisplayIndex)
If IsToast = True Then
intToast = My.Settings.MatrixLCD_ToastHoldTime
End If
MatrixLCDisplay.Clear()
If strLine1.Length > MatrixLCDisplay.Cols Then
strLine1 = strLine1.Substring(0, MatrixLCDisplay.Cols)
ElseIf strLine2 <> "" Then
Do While MatrixLCDisplay.Cols > strLine1.Length
'Yes, I did this.
strLine1 = strLine1 + " "
Loop
End If
MatrixLCDisplay.WriteString(strLine1)
If strLine2.Length >= MatrixLCDisplay.Cols Then
strLine2 = strLine2.Substring(0, MatrixLCDisplay.Cols)
MatrixLCDisplay.SetAutoscrollOff()
End If
If strLine2 <> "" Then
MatrixLCDisplay.WriteString(strLine2)
End If
End If
End Sub
Function Unload() As String
My.Application.Log.WriteEntry("Unloading Matrix LCD module")
If MatrixLCDConnected = True Then
Dim MatrixLCDisplay As HAMatrixLCD = DeviceCollection.Item(MatrixLCDisplayIndex)
MatrixLCDisplay.Dispose()
End If
Return "Matrix LCD module unloaded"
End Function
<Serializable()>
Public Class HAMatrixLCD
Inherits HASerialDevice
Public Property Cols As Integer
Public Property Rows As Integer
Public Property BacklightColor As Color
Public Property WarningColor As Color
Public Sub Blink(Optional ByVal Times As Integer = 1, Optional ByVal BlinkMS As Integer = 500)
If (Times <= 0 OrElse BlinkMS <= 0) Then
Throw New ArgumentOutOfRangeException("Cannot blink negative times or for negative duration")
End If
Dim i As Integer = 0
While i < Times
TurnOff()
Threading.Thread.Sleep(BlinkMS)
TurnOn()
Threading.Thread.Sleep(BlinkMS)
i = i + 1
End While
End Sub
Public Sub Clear()
Command("Clear")
End Sub
Public Sub Command(ByVal strCommand As String)
Dim invCommand As Boolean = False
Dim data(2) As Byte
data(0) = 254 '0xFE
Select Case strCommand
Case "AutoscrollOff"
data(1) = 82 '0x52
Case "AutoscrollOn"
data(1) = 81 '0x51
Case "BacklightOff"
data(1) = 70 '0x46
Case "BlockCursorOff"
data(1) = 84 '0x54
Case "BlockCursorOn"
data(1) = 83 '0x53
Case "Clear"
data(1) = 88 '0x58
Case "CursorBack"
data(1) = 76 '0x4C
Case "CursorForward"
data(1) = 77 '0x4D
Case "CursorHome"
data(1) = 72 '0x48
Case "UnderlineCursorOff"
data(1) = 75 '0x4B
Case "UnderlineCursorOn"
data(1) = 74 '0x4A
Case Else
My.Application.Log.WriteEntry("Invalid Matrix LCD command", TraceEventType.Error)
invCommand = True
End Select
If invCommand = False Then
Try
SerialPort.Write(data, 0, 2)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End If
End Sub
Public Sub Command(ByVal strCommand As String, ByVal Value As Byte)
Dim invCommand As Boolean = False
Dim data(3) As Byte
data(0) = 254 '0xFE
data(2) = Value
Select Case strCommand
Case "BacklightOn"
data(1) = 66 '0x42
Case "Brightness"
data(1) = 153 '0x99
Case "BrightnessSave"
data(1) = 152 '0x98
Case "Contrast"
data(1) = 80 '0x50
Case "ContrastSave"
data(1) = 145 '0x91
Case Else
My.Application.Log.WriteEntry("Invalid Matrix LCD command", TraceEventType.Error)
invCommand = True
End Select
If invCommand = False Then
Try
SerialPort.Write(data, 0, 3)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End If
End Sub
Public Sub New()
Me.DeviceName = "Matrix LCD"
Me.DeviceType = "Display"
Me.Model = "Matrix Orbital LCD or compatible"
Me.Cols = 16
Me.Rows = 2
Me.BacklightColor = Color.Aqua
Me.WarningColor = Color.Red
My.Application.Log.WriteEntry("Matrix LCD - Create Device")
SerialPort = New System.IO.Ports.SerialPort
If My.Settings.MatrixLCD_LastGoodCOMPort = "" Then
SerialPort.PortName = InputBox("Enter the COM port for a Matrix Orbital-compatible LCD.", "Matrix LCD")
Else
SerialPort.PortName = My.Settings.MatrixLCD_LastGoodCOMPort
End If
SerialPort.BaudRate = 9600
SerialPort.DataBits = 8
SerialPort.Handshake = IO.Ports.Handshake.None
SerialPort.Parity = IO.Ports.Parity.None
SerialPort.StopBits = 1
Try
My.Application.Log.WriteEntry("Trying to connect on port " + SerialPort.PortName)
SerialPort.Open()
Catch IOExcep As System.IO.IOException
My.Application.Log.WriteException(IOExcep)
If My.Settings.Global_SmartCOM = True And My.Settings.MatrixLCD_COMPortDeviceName <> "" Then
If modComputer.GetCOMPortFromFriendlyName(My.Settings.MatrixLCD_COMPortDeviceName) <> "" Then
SerialPort.PortName = modComputer.GetCOMPortFromFriendlyName(My.Settings.MatrixLCD_COMPortDeviceName)
If My.Settings.Global_CarMode = True Then
modSpeech.Say("Smart " & SerialPort.PortName, False)
End If
Try
My.Application.Log.WriteEntry("Trying to connect on port " + SerialPort.PortName)
SerialPort.Open()
Catch IOExcep2 As System.IO.IOException
My.Application.Log.WriteException(IOExcep2)
My.Application.Log.WriteEntry("SmartCOM failed to connect to Matrix LCD")
modSpeech.Say("Unable to connect to Matrix LCD")
End Try
Else
If My.Settings.Global_CarMode = True Then
modSpeech.Say("No potential Matrix LCD COM port found")
End If
End If
Else
modSpeech.Say("Unable to connect to Matrix LCD")
End If
Catch UnauthExcep As System.UnauthorizedAccessException
My.Application.Log.WriteException(UnauthExcep)
modSpeech.Say("Unable to connect to Matrix LCD")
End Try
If SerialPort.IsOpen = True Then
My.Application.Log.WriteEntry("Serial connection opened on port " + SerialPort.PortName)
Me.IsConnected = True
My.Settings.MatrixLCD_LastGoodCOMPort = SerialPort.PortName
My.Settings.MatrixLCD_COMPortDeviceName = modComputer.GetCOMPortFriendlyName(SerialPort.PortName)
SetColor(Me.BacklightColor)
SetSize(Me.Cols, Me.Rows)
Dim strSplashString = "HAController"
Do While strSplashString.Length < Cols
strSplashString = strSplashString + " "
Loop
strSplashString = strSplashString + My.Application.Info.Version.ToString
Do While strSplashString.Length < (Cols * 2)
strSplashString = strSplashString + " "
Loop
SetSplash(strSplashString)
If Date.Now.Hour >= 7 AndAlso Date.Now.Hour < 19 Then
SetBrightness(255)
Else
SetBrightness(10)
End If
SetContrast(200)
End If
End Sub
Public Sub NewLine()
Dim data(1) As Byte
data(0) = 10 '0x0A
Try
SerialPort.Write(data, 0, 1)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End Sub
Public Sub SetAutoscrollOff()
Command("AutoscrollOff")
End Sub
Public Sub SetAutoscrollOn()
Command("AutoscrollOn")
End Sub
Public Sub SetBright()
Command("Brightness", 255)
End Sub
Public Sub SetBrightness(ByVal Value As Byte)
Command("Brightness", Value)
End Sub
Public Sub SetColor(ByVal bytRed As Byte, ByVal bytGreen As Byte, ByVal bytBlue As Byte)
Dim data(5) As Byte
data(0) = 254 '0xFE
data(1) = 208 '0xD0
data(2) = bytRed
data(3) = bytGreen
data(4) = bytBlue
Try
SerialPort.Write(data, 0, 5)
My.Application.Log.WriteEntry("Matrix LCD color set to " & bytRed & ", " & bytGreen & ", " & bytBlue)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End Sub
Public Sub SetColor(ByVal NewColor As Color)
SetColor(NewColor.R, NewColor.G, NewColor.B)
End Sub
Public Sub SetColor(ByVal Name As String)
Dim NewColor As Color = Color.FromName(Name)
SetColor(NewColor.R, NewColor.G, NewColor.B)
End Sub
Public Sub SetContrast(ByVal Value As Byte)
Command("Contrast", Value)
End Sub
Public Sub SetDim()
Command("Brightness", 136)
End Sub
Public Sub SetNite()
Command("Brightness", 68)
End Sub
Public Sub SetSize(ByVal Length As Integer, ByVal Height As Integer)
Dim data(4) As Byte
data(0) = 254 '0xFE
data(1) = 209 '0xD1
data(2) = CByte(Length)
data(3) = CByte(Height)
Try
SerialPort.Write(data, 0, 4)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End Sub
Public Sub SetSplash(ByVal strInput As String)
'TODO: Define by LCD size, fill rest of bytes with spaces automatically
'Space is 0x20 or 32
Dim data(strInput.Length + 2) As Byte
data(0) = 254 '0xFE
data(1) = 64 '0x40
For i As Integer = 0 To strInput.Length - 1
data(i + 2) = Asc(strInput(i))
Next
Try
SerialPort.Write(data, 0, strInput.Length + 2)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End Sub
Public Sub SetSoft()
Command("Brightness", 190)
End Sub
Public Sub TestLCD()
Command("Clear")
SetColor(0, 255, 255)
WriteString("hello world")
Blink(3)
End Sub
Public Sub TurnOff()
Command("BacklightOff")
End Sub
Public Sub TurnOn()
Command("BacklightOn", 0)
End Sub
Public Sub WriteString(ByVal strInput As String)
Dim data(strInput.Length) As Byte
For i As Integer = 0 To strInput.Length - 1
data(i) = Asc(strInput(i))
Next
Try
SerialPort.Write(data, 0, strInput.Length)
Catch Excep As System.InvalidOperationException
My.Application.Log.WriteException(Excep)
End Try
End Sub
End Class
End Module