-
Notifications
You must be signed in to change notification settings - Fork 2
/
clsBBBController.cls
142 lines (119 loc) · 3.84 KB
/
clsBBBController.cls
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
Public Class clsBBBController
Inherits clsController
Dim reportData(23) As Byte
Private _IP As String
Public Property IP As String
Get
Return _IP
End Get
Set(value As String)
If value = _IP Then Exit Property
stopTCP()
_IP = value
setupTCP()
End Set
End Property
Public Overrides Function isReady() As Boolean
Return True
End Function
Public Overrides ReadOnly Property IFType As String
Get
Return "BBB"
End Get
End Property
Private sck As Net.Sockets.Socket
Private Function setupTCP()
sck = New Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
sck.Connect(_IP, 12345)
Return True
End Function
Private Sub stopTCP()
If sck Is Nothing Then Exit Sub
'sck.Disconnect(False)
sck.Dispose()
sck = Nothing
End Sub
Public Sub New(host As String)
_IP = host
If Not setupTCP() Then Throw New Exception("TCP Setup Failed")
reportData(0) = 129
reportData(3) = 20
reportData(4) = 0
reportData(5) = 20
For i As Integer = 6 To 23
reportData(i) = 0
Next
End Sub
Public Overrides Sub sendReport(newReport() As Byte)
'Debug.Print("Sent (" & _IP & "): " & BitConverter.ToString(newReport))
sck.Send(newReport)
End Sub
Public Overrides Function baseReport() As Byte()
Return New Byte() {129, 0, 0, 20, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
End Function
Public Overrides Function getReport() As Byte()
Dim tmp(reportData.Length - 1) As Byte
Array.Copy(reportData, tmp, reportData.Length)
tmp(6) = buttonHi
tmp(7) = buttonLo
tmp(8) = LT
tmp(9) = RT
Dim uVal As Integer
If joyLXInt >= 0 Then uVal = joyLXInt Else uVal = 65536 + joyLXInt
tmp(10) = uVal Mod 256
tmp(11) = uVal >> 8
If joyLYInt = 0 Then
uVal = 0
Else
If joyLYInt < 0 Then uVal = -1 - joyLYInt Else uVal = 65535 - joyLYInt
End If
tmp(12) = uVal Mod 256
tmp(13) = uVal >> 8
If joyRXInt >= 0 Then uVal = joyRXInt Else uVal = 65536 + joyRXInt
tmp(14) = uVal Mod 256
tmp(15) = uVal >> 8
If joyRYInt = 0 Then
uVal = 0
Else
If joyRYInt < 0 Then uVal = -1 - joyRYInt Else uVal = 65535 - joyRYInt
End If
tmp(16) = uVal Mod 256
tmp(17) = uVal >> 8
Return tmp
End Function
Protected Overrides Sub update(Optional force As Boolean = False)
reportData(6) = buttonHi
reportData(7) = buttonLo
reportData(8) = LT
reportData(9) = RT
Dim uVal As Integer
If joyLXInt >= 0 Then uVal = joyLXInt Else uVal = 65536 + joyLXInt
reportData(10) = uVal Mod 256
reportData(11) = uVal >> 8
If joyLYInt = 0 Then
uVal = 0
Else
If joyLYInt < 0 Then uVal = -1 - joyLYInt Else uVal = 65535 - joyLYInt
End If
reportData(12) = uVal Mod 256
reportData(13) = uVal >> 8
If joyRXInt >= 0 Then uVal = joyRXInt Else uVal = 65536 + joyRXInt
reportData(14) = uVal Mod 256
reportData(15) = uVal >> 8
If joyRYInt = 0 Then
uVal = 0
Else
If joyRYInt < 0 Then uVal = -1 - joyRYInt Else uVal = 65535 - joyRYInt
End If
reportData(16) = uVal Mod 256
reportData(17) = uVal >> 8
sendReport(reportData)
End Sub
Public Overrides Sub dispose()
stopTCP()
End Sub
Protected Overrides Sub Finalize()
stopTCP()
MyBase.Finalize()
End Sub
End Class