-
Notifications
You must be signed in to change notification settings - Fork 0
/
evc2.go
190 lines (153 loc) · 3.15 KB
/
evc2.go
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
package evc2
import (
"encoding/binary"
"fmt"
"github.com/google/gousb"
)
const (
Manufacturer = "ElmorLabs"
Name = "EVC 2.0"
Vid = gousb.ID(0x0483)
Pid = gousb.ID(0x5740)
PidDFU = gousb.ID(0xDF11)
)
type EVC2 struct {
context *gousb.Context
device *gousb.Device
iface *gousb.Interface
inpipe *gousb.InEndpoint
outpipe *gousb.OutEndpoint
// DeviceUID STM32UID
done func()
}
func (e *EVC2) Open() (err error) {
return e.openDevice(Vid, Pid)
}
func (e *EVC2) openDevice(vid gousb.ID, pid gousb.ID) (err error) {
if e.context == nil {
e.context = gousb.NewContext()
}
if e.device == nil {
e.device, err = e.context.OpenDeviceWithVIDPID(vid, pid)
if err != nil {
return
}
}
e.iface, e.done, err = e.device.DefaultInterface()
e.inpipe, err = e.iface.InEndpoint(2)
if err != nil {
fmt.Println(err)
}
if e.inpipe != nil {
fmt.Println(e.inpipe.Desc.Address)
}
return
}
func (e *EVC2) Close() (err error) {
// if e.inpipe != nil {
// e.Close()
// }
// if e.outpipe != nil {
// e.Close()
// }
if e.iface != nil {
e.done()
e.iface.Close()
}
if e.device != nil {
e.device.Close()
}
if e.context != nil {
err = e.context.Close()
}
return
}
func (e *EVC2) WriteCommand(request byte, value uint16) (err error) {
_, err = e.device.Control(gousb.ControlOut|gousb.ControlClass|gousb.ControlInterface, request, value, 0, nil)
return
}
func (e *EVC2) Read(buffer []byte) (n int, err error) {
return e.inpipe.Read(buffer)
}
func (e *EVC2) IsConnected() (connected bool) {
if e.device != nil {
if man, err := e.device.Manufacturer(); man == Manufacturer && err == nil {
connected = true
}
}
return
}
func (e *EVC2) IsDFU() (dfu bool) {
if e.IsConnected() && e.ProductID() == PidDFU {
dfu = true
}
return
}
func (e *EVC2) SerialNumber() (sn string) {
if e.IsConnected() {
var err error
sn, err = e.device.SerialNumber()
if err != nil {
sn = fmt.Sprintln(err)
}
}
return
}
func (e *EVC2) VendorID() (vid gousb.ID) {
if e.IsConnected() {
vid = e.device.Desc.Vendor
}
return
}
func (e *EVC2) ProductID() (pid gousb.ID) {
if e.IsConnected() {
pid = e.device.Desc.Product
}
return
}
func (e *EVC2) DeviceID() (id uint16) {
if !e.IsConnected() {
return
}
e.WriteCommand(1, 0)
buffer := make([]byte, 3)
if n, err := e.Read(buffer); err == nil && n == len(buffer) {
id = binary.LittleEndian.Uint16(buffer[1:])
}
return
}
func (e *EVC2) HardwareVersion() (hw uint8) {
if !e.IsConnected() {
return
}
e.WriteCommand(3, 0)
buffer := make([]byte, 2)
if n, err := e.Read(buffer); err == nil && n == len(buffer) {
hw = buffer[1]
}
return
}
func (e *EVC2) FirmwareVersion() (fw uint16) {
if !e.IsConnected() {
return
}
if err := e.WriteCommand(4, 0); err != nil {
}
buffer := make([]byte, 3)
if n, err := e.Read(buffer); err == nil && n == len(buffer) {
fw = binary.LittleEndian.Uint16(buffer[1:])
}
return
}
func (e *EVC2) DeviceUID() (uid STM32UID) {
if !e.IsConnected() {
return
}
if err := e.WriteCommand(5, 0); err != nil {
}
buffer := make([]byte, 13)
if n, err := e.Read(buffer); err == nil && n == len(buffer) {
uid.Unpack(buffer[1:])
}
return
}