Skip to content

Commit

Permalink
make Stream aware of MTU, and implement simplified read function on M…
Browse files Browse the repository at this point in the history
…TU amount
  • Loading branch information
sjkelly committed Sep 8, 2021
1 parent caa8fb7 commit 52023cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
8 changes: 4 additions & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ channel.gain = 42u"dB"
channel.sample_rate = 2.1u"MHz"
# Open a (potentially multichannel) stream on this channel
stream = SoapySDR.Stream(ComplexF32, [channel])
stream = SoapySDR.Stream([channel])
SoapySDR.activate!(stream)
# Write out random noise
Expand All @@ -70,9 +70,9 @@ channel.gain = 42u"dB"
channel.sample_rate = 2.1u"MHz"
# Open a (potentially multichannel) stream on this channel
stream = SoapySDR.Stream(ComplexF32, [channel])
stream = SoapySDR.Stream([channel])
SoapySDR.activate!(stream)
# Collect data
Base.read(stream, 10000)
# Collect all available samples in the buffer
Base.read(stream)
```
46 changes: 33 additions & 13 deletions src/highlevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,6 @@ function Base.show(io::IO, ::MIME"text/plain", c::Channel)
end
end

"""
native_stream_format(c::Channel)
Returns the format type and fullscale resolution of the native stream.
"""
function native_stream_format(c::Channel)
fmt, fullscale = SoapySDRDevice_getNativeStreamFormat(c.device.ptr, c.direction, c.idx)
_stream_type_soapy2jl[unsafe_string(fmt)], fullscale
end

struct ChannelList <: AbstractVector{Channel}
device::Device
direction::Direction
Expand Down Expand Up @@ -458,21 +448,47 @@ struct FreqSpec{T}
kwargs::Dict{Any, String}
end

### Streams
### Stream Utility Functions

"""
stream_formats(::Channel)
Returns the stream formats supported by the device.
Note: Since Julia is a multiple dispatch and generic language, it is
preferrable to use `native_stream_format(c::Channel)` for optimal processing throughput.
Only use this function if non-standard formats such as Complex Int12 and Complex Int4
are native to the device and not handled by dispatch on `Complex`.
"""
function stream_formats(c::Channel)
slist = StringList(SoapySDRDevice_getStreamFormats(c.device.ptr, c.direction, c.idx)...)
map(StreamFormat, slist)
map(_stream_map_soapy2jl, slist)
end

# Internal, reflected in Stream.mtu
function mtu(d::Device, stream::Ptr{SoapySDRStream})
SoapySDRDevice_getStreamMTU(d.ptr, stream)
end

"""
native_stream_format(c::Channel) -> Type, fullscale
Returns the format type and fullscale resolution of the native stream.
"""
function native_stream_format(c::Channel)
fmt, fullscale = SoapySDRDevice_getNativeStreamFormat(c.device.ptr, c.direction, c.idx)
_stream_map_soapy2jl(unsafe_string(fmt)), fullscale
end

## Stream

mutable struct Stream{T}
d::Device
nchannels::Int
mtu::Int
ptr::Ptr{SoapySDRStream}
function Stream{T}(d::Device, nchannels::Int, ptr::Ptr{SoapySDRStream}) where {T}
this = new{T}(d, nchannels, ptr)
this = new{T}(d, nchannels, mtu(d, ptr), ptr)
finalizer(SoapySDRDevice_closeStream, this)
return this
end
Expand Down Expand Up @@ -544,6 +560,10 @@ function Base.read(s::Stream{T}, n::Int; kwargs...) where {T}
SampleBuffer(bufs, flags, timens)
end

function Base.read(s::Stream; kwargs...)
read(s, s.mtu; kwargs...)
end

function activate!(s::Stream; flags = 0, timens = nothing, numElems=0)
SoapySDRDevice_activateStream(s.d, s, flags, timens === nothing ? 0 : uconvert(u"ns", timens).val, numElems)
nothing
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ end
@test typeof(rx_chan) == sd.Channel
@test typeof(tx_chan) == sd.Channel

@test sd.native_stream_format(rx_chan)[1] == SoapySDR.ComplexInt{12} #, fullscale
@test sd.stream_formats(rx_chan) == [Complex{Int8}, SoapySDR.ComplexInt{12}, Complex{Int16}, ComplexF32]

# Test sensor API
sensor_list = sd.list_sensors(dev)
Expand Down

0 comments on commit 52023cb

Please sign in to comment.