-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
70 lines (61 loc) · 1.86 KB
/
context.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
// Copyright (c) 2015-2024 The usbtmc developers. All rights reserved.
// Project site: https://github.com/gotmc/usbtmc
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package usbtmc
import (
"github.com/gotmc/usbtmc/driver"
)
// Context hold the USB context for the registered driver.
type Context struct {
driver driver.Driver
libusbContext driver.Context
}
// NewContext creates a new USB context using the registered driver.
func NewContext() (*Context, error) {
var context Context
context.driver = libusbDriver
ctx, err := libusbDriver.NewContext()
if err != nil {
return nil, err
}
context.libusbContext = ctx
return &context, nil
}
// NewDeviceByVIDPID creates new USBTMC compliant device based on the given the
// vendor ID and product ID. If multiple USB devices matching the VID and PID
// are found, only the first is returned.
func (c *Context) NewDeviceByVIDPID(VID, PID int) (*Device, error) {
d := defaultDevice()
usbDevice, err := c.libusbContext.NewDeviceByVIDPID(VID, PID)
if err != nil {
return nil, err
}
d.usbDevice = usbDevice
return &d, nil
}
// NewDevice creates a new USBTMC compliant device based on the given VISA
// address string.
func (c *Context) NewDevice(address string) (*Device, error) {
v, err := NewVisaResource(address)
if err != nil {
return nil, err
}
return c.NewDeviceByVIDPID(v.manufacturerID, v.modelCode)
}
func defaultDevice() Device {
return Device{
termChar: '\n',
bTag: 1,
termCharEnabled: true,
}
}
// Close closes the USB context for the underlying USB driver.
func (c *Context) Close() error {
return c.libusbContext.Close()
}
// SetDebugLevel sets the debug level for the underlying USB device using the
// given integer.
func (c *Context) SetDebugLevel(level int) {
c.libusbContext.SetDebugLevel(level)
}