-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoting.bas
257 lines (162 loc) · 5.33 KB
/
Voting.bas
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
Attribute VB_Name = "Voting"
Option Explicit
Public votes As Dictionary
Private started As Boolean
Private COMPort As Integer
Private timerID As Variant
Private lastSlideIndex As Integer
Public Sub Start()
On Error GoTo HandleError
COMPort = settings("voting.port")
Dim status As Long
status = CommOpen(COMPort, "COM" & COMPort, "baud=115200 parity=N data=8 stop=1")
If status <> 0 Then
Dim errorString As String
status = CommGetError(errorString)
Err.Raise status, Description:=errorString
End If
CommSetLine COMPort, LINE_RTS, True
CommSetLine COMPort, LINE_DTR, True
timerID = System.SetTimer(0, 0, 100, AddressOf Run) ' Repeat every 100 ms
started = True
LogInfoAt "Voting.start", "Communication started on COM port " & COMPort & ", frequency 115200 bauds, 8N1 configuration"
Exit Sub
HandleError:
LogLastErrorAt "Voting.Start"
End Sub
Sub Run()
On Error GoTo HandleError
Dim key As Variant
For Each key In Utils.GetBlankSlideKeys()
If System.GetAsyncKeyState(key) Then
With diagram
If .Visible Then
.Hide
ActivePresentation.SlideShowWindow.View.GotoSlide lastSlideIndex
Else
lastSlideIndex = ActivePresentation.SlideShowWindow.View.slide.slideIndex
loadVotes
.Show
End If
End With
End If
Next key
If diagram.Visible Then
For Each key In Utils.GetBackwardKeys()
If System.GetAsyncKeyState(key) Then
clearVotes
diagram.Hide
ActivePresentation.SlideShowWindow.View.GotoSlide lastSlideIndex
End If
Next key
For Each key In Utils.GetForwardKeys()
If System.GetAsyncKeyState(key) Then LoadVotes
Next key
End If
Exit Sub
HandleError:
LogLastErrorAt "Voting.Run"
End Sub
Public Sub Finish()
On Error GoTo HandleError
If started Then
System.KillTimer 0, timerID
CommSetLine COMPort, LINE_RTS, False
CommSetLine COMPort, LINE_DTR, False
CommClose COMPort
LogInfoAt "Voting.Finish", "COM communication shut down"
End If
started = False
Exit Sub
HandleError:
started = False
LogLastErrorAt "Voting.Finish"
End Sub
Private Sub SendMessage(msg As String)
On Error GoTo HandleError
Dim status As Long
Dim size As Long
size = Len(msg)
status = CommWrite(COMPort, msg)
If status <> size Then
Dim description As String
status = CommGetError(description)
Err.Raise status, Description:=description
End If
Exit Sub
HandleError:
LogLastErrorAt "Voting.SendMessage"
End Sub
Public Sub ClearVotes()
On Error GoTo HandleError
diagram.ClearVotes
SendMessage "c"
LogInfoAt "Voting.ClearVotes", "All votes cleared from the server"
Exit Sub
HandleError:
LogLastErrorAt "Voting.ClearVotes"
End Sub
Public Sub LoadVotes()
On Error GoTo HandleError
SendMessage "g"
System.Sleep 100
Dim status As Long
Dim readData As String
status = CommRead(COMPort, readData, 1)
If status < 0 Then
Dim description As String
status = CommGetError(description)
Err.Raise status, Description:=description
End If
Set votes = Json.ParseJson(readData)
If Not votes.Exists("total") Then
Err.Raise 0, Description:="Invalid JSON respond: no 'total' key"
End If
LogVotes votes
' Copy voter("total") {Array of Variant} to totalVotes {Array of Integer}
Dim totalVotes(7) As Integer
Dim i As Integer
For i = 1 To 7
totalVotes(i) = votes("total")(i)
Next i
diagram.DrawValues totalVotes
Exit Sub
HandleError:
LogLastErrorAt "Voting.LoadVotes"
End Sub
Public Sub CheckConnection()
On Error GoTo HandleError
COMPort = settings("voting.port")
Dim status As Long
status = CommOpen(COMPort, "COM" & COMPort, "baud=115200 parity=N data=8 stop=1")
If status <> 0 Then
MsgBox "Failed to connect (code: 1)", vbCritical, "PPVoting error"
Exit Sub
End If
CommSetLine COMPort, LINE_RTS, True
CommSetLine COMPort, LINE_DTR, True
status = CommWrite(COMPort, "k")
If status <> 1 Then
MsgBox "Failed to send validation request (code: 2)", vbCritical, "PPVoting error"
GoTo Terminate
End If
System.Sleep 100
Dim readData As String
status = CommRead(COMPort, readData, 2)
If status < 0 Then
MsgBox "Failed to get validation respond (code: 3)", vbCritical, "PPVoting error"
GoTo Terminate
End If
If readData <> "OK" Then
MsgBox "Validation failed (code: 4)", vbCritical, "PPVoting error"
GoTo Terminate
End If
MsgBox "Success", vbInformation, "PPVoting info"
Terminate:
CommSetLine COMPort, LINE_RTS, False
CommSetLine COMPort, LINE_DTR, False
CommClose COMPort
Exit Sub
HandleError:
LogLastErrorAt "Voting.CheckConnection"
End Sub