forked from racerxdl/limedrv
-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
233 lines (205 loc) · 5.51 KB
/
helpers.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
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
package limedrv
import "C"
import (
"context"
"encoding/binary"
"fmt"
"github.com/myriadrf/limedrv/limewrap"
"github.com/racerxdl/fastconvert"
"runtime"
"strings"
"unsafe"
)
const floatSize = 4
const int16Size = 2
const samplesWait = 100
func cleanString(s string) string {
return strings.Trim(s, "\u0000 ")
}
type channelMessage struct {
channel int
data []complex64
data16 []int16
timestamp uint64
}
func FastI16BufferIQConvert(data []byte) []complex64 {
var i16samples = len(data) / 2
var out = make([]complex64, i16samples/2) // Each complex is 2 i16
var pos = 0
var itemsToRead = i16samples / 2
for idx := 0; idx < itemsToRead; idx++ {
var r = int16(binary.LittleEndian.Uint16(data[pos : pos+2]))
var i = int16(binary.LittleEndian.Uint16(data[pos+2 : pos+4]))
out[idx] = complex(float32(r)/32768, float32(i)/32768)
pos += 4
}
return out
}
func ConvertC64toI16(dst []int16, src []complex64) {
samples := len(src)
if len(dst)/2 < samples {
samples = len(dst) / 2
}
for idx := 0; idx < samples; idx++ {
c := src[idx]
dst[idx*2+0] = int16(real(c) * 32768)
dst[idx*2+1] = int16(imag(c) * 32768)
}
}
func streamTXLoop(ctx context.Context, channel LMSChannel, txCb func([]complex64, int)) {
defer func() {
if r := recover(); r != nil {
fmt.Println("got panic recovered: ", r)
}
}()
//fmt.Fprintf(os.Stderr,"Worker Started")
running := true
sampleLength := floatSize
if channel.parent.IQFormat == FormatInt16 || channel.parent.IQFormat == FormatInt12 {
sampleLength = int16Size
}
var buffPtr uintptr
var buff interface{}
if sampleLength == int16Size {
i16buff := make([]int16, fifoSize*2)
buff = i16buff
buffPtr = uintptr(unsafe.Pointer(&i16buff[0]))
} else {
c64buff := make([]complex64, fifoSize)
buff = c64buff
buffPtr = uintptr(unsafe.Pointer(&c64buff[0]))
}
rxData := make([]complex64, fifoSize)
m := limewrap.NewLms_stream_meta_t()
m.SetTimestamp(0)
m.SetFlushPartialPacket(false)
m.SetWaitForTimestamp(false)
//fmt.Fprintf(os.Stderr,"Worker Running")
for running {
select {
case _ = <-ctx.Done():
//fmt.Fprintf(os.Stderr,"Worker Received stop", b)
running = false
return
default:
}
if txCb != nil {
txCb(rxData, channel.parentIndex) // Fill buffer
}
if sampleLength == floatSize {
copy(buff.([]complex64), rxData)
} else {
ConvertC64toI16(buff.([]int16), rxData)
}
runtime.LockOSThread()
sentSamples := limewrap.LMS_SendStream(channel.stream, buffPtr, fifoSize, m, samplesWait)
runtime.UnlockOSThread()
if sentSamples != fifoSize {
fmt.Printf("Error sending samples. Expected %d sent %d\n", fifoSize, sentSamples)
}
runtime.Gosched()
}
}
func streamRXLoop(ctx context.Context, c chan<- channelMessage, channel LMSChannel, send16 bool) {
defer func() {
if r := recover(); r != nil {
fmt.Println("got panic recovered: ", r)
}
}()
//fmt.Fprintf(os.Stderr,"Worker Started")
running := true
sampleLength := floatSize
if channel.parent.IQFormat == FormatInt16 || channel.parent.IQFormat == FormatInt12 {
sampleLength = int16Size
}
buff := make([]byte, fifoSize*sampleLength*2) // 16k IQ samples
buffPtr := uintptr(unsafe.Pointer(&buff[0]))
m := limewrap.NewLms_stream_meta_t()
m.SetTimestamp(0)
m.SetFlushPartialPacket(false)
m.SetWaitForTimestamp(false)
//fmt.Println("Worker Running")
for running {
select {
case _ = <-ctx.Done():
//fmt.Println("Worker Received stop")
running = false
return
default:
}
runtime.LockOSThread()
recvSamples := limewrap.LMS_RecvStream(channel.stream, buffPtr, fifoSize, m, samplesWait)
runtime.UnlockOSThread()
if recvSamples > 0 {
chunk := buff[:sampleLength*recvSamples*2]
cm := channelMessage{
channel: channel.parentIndex,
timestamp: m.GetTimestamp(),
}
if sampleLength == floatSize {
// Float32
cm.data = fastconvert.ByteArrayToComplex64Array(chunk)
} else {
// Int16
if send16 {
cm.data16 = fastconvert.ByteArrayToInt16LEArray(chunk)
} else {
cm.data = FastI16BufferIQConvert(chunk)
}
}
c <- cm
} else if recvSamples == -1 {
fmt.Printf("Error receiving samples from channel %d\n", channel.parentIndex)
}
runtime.Gosched()
}
//fmt.Println("Worker stopped")
}
func createLms_range_t() limewrap.Lms_range_t {
return limewrap.NewLms_range_t()
}
func createLms_stream_t() limewrap.Lms_stream_t {
return limewrap.NewLms_stream_t()
}
func idev2dev(deviceinfo i_deviceinfo) DeviceInfo {
var deviceStr = string(deviceinfo.DeviceName[:64])
var z = strings.Split(deviceStr, ",")
var DeviceName string
var Media string
var Module string
var Addr string
var Serial string
for i := 0; i < len(z); i++ {
var k = strings.Split(z[i], "=")
if len(k) == 1 {
DeviceName = k[0]
} else {
switch strings.ToLower(strings.Trim(k[0], " ")) {
case "media":
Media = cleanString(k[1])
break
case "module":
Module = cleanString(k[1])
break
case "addr":
Addr = cleanString(k[1])
break
case "serial":
Serial = cleanString(k[1])
break
}
}
}
return DeviceInfo{
DeviceName: DeviceName,
Media: Media,
Module: Module,
Addr: Addr,
Serial: Serial,
FirmwareVersion: cleanString(string(deviceinfo.FirmwareVersion[:16])),
HardwareVersion: cleanString(string(deviceinfo.HardwareVersion[:16])),
GatewareVersion: cleanString(string(deviceinfo.GatewareVersion[:16])),
GatewareTargetBoard: cleanString(string(deviceinfo.GatewareTargetBoard[:16])),
origDevInfo: deviceinfo,
}
}