-
Notifications
You must be signed in to change notification settings - Fork 7
/
limedrv.go
104 lines (87 loc) · 2.39 KB
/
limedrv.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
// limedrv is a LMS7 API Wrapper to Go made to be easy to use.
// Currently this documentation is WIP. Some examples are available
// in _examples folder.
package limedrv
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/racerxdl/limedrv/limewrap"
"runtime"
"unsafe"
)
type i_deviceinfo struct {
DeviceName [64]byte
FirmwareVersion [16]byte
HardwareVersion [16]byte
ProtocolVersion [16]byte
BoardSerialNumber uint64
GatewareVersion [16]byte
GatewareTargetBoard [32]byte
}
// DeviceInfo is a struct with driver information required to open a connection
type DeviceInfo struct {
DeviceName string
Media string
Module string
Addr string
Serial string
ProtocolVersion string
FirmwareVersion string
HardwareVersion string
GatewareVersion string
GatewareTargetBoard string
origDevInfo i_deviceinfo
}
func (d *i_deviceinfo) toOrigDevString() string {
var buf bytes.Buffer
err := binary.Write(&buf, binary.LittleEndian, d)
if err != nil {
panic(err)
}
return buf.String()
}
// GetDevices return an array of available devices in the LMS7 driver.
func GetDevices() []DeviceInfo {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
devCount := limewrap.LMS_GetDeviceList(nil)
ret := make([]DeviceInfo, devCount)
if devCount > 0 {
var z [128]i_deviceinfo
t := (*string)(unsafe.Pointer(&z))
limewrap.LMS_GetDeviceList(t)
for i := 0; i < devCount; i++ {
ret[i] = idev2dev(z[i])
}
}
return ret
}
// Open opens a device specified by a DeviceInfo instance and returns a reference to LMSDevice
func Open(device DeviceInfo) *LMSDevice {
var ret = LMSDevice{
DeviceInfo: device,
IQFormat: FormatInt16,
controlChan: make(chan bool),
}
ret.Advanced = LMSDeviceAdvanced{}
var origString = device.origDevInfo.toOrigDevString()
ptr := uintptr(0)
runtime.LockOSThread()
v := limewrap.LMS_Open(&ptr, origString, 0)
runtime.UnlockOSThread()
ret.dev = ptr
if v != 0 {
panic(fmt.Sprintf("Failed to open %s at %s.", device.DeviceName, device.Media))
}
ret.init()
return &ret
}
// Close closes a LMSDevice. This makes the LMSDevice instance useless.
func Close(device *LMSDevice) {
if limewrap.LMS_Close(device.dev) != 0 {
panic(fmt.Sprintf("Failed to close %s at %s.", device.DeviceInfo.DeviceName, device.DeviceInfo.Media))
} else {
device.dev = 0
}
}