Skip to content

Commit

Permalink
Merge pull request #20 from JuliaTelecom/sjk/rangeprinting1
Browse files Browse the repository at this point in the history
handle AbstractRange in Unit printing, closes #19
  • Loading branch information
sjkelly committed Sep 8, 2021
2 parents 7101a96 + aed2059 commit 90c5414
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/highlevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ function print_unit_interval(io::IO, min, max)
print_unit(io, max)
end
end

function print_unit_steprange(io::IO, min, max, step)
print_unit(io, min)
print(io, ":")
print_unit(io, step)
print(io, ":")
print_unit(io, max)
end

print_unit_interval(io::IO, x::Interval{<:Any, Closed, Closed}) =
print_unit_interval(io, minimum(x), maximum(x))

Expand All @@ -210,6 +219,11 @@ function print_hz_range(io::IO, x::Interval{<:Any, Closed, Closed})
print_unit_interval(io, min, max)
end

function print_hz_range(io::IO, x::AbstractRange)
min, step, max = pick_freq_unit(first(x)), pick_freq_unit(Base.step(x)), pick_freq_unit(last(x))
print_unit_steprange(io, min, max, step)
end

function Base.show(io::IO, ::MIME"text/plain", c::Channel)
println(io, c.direction == Tx ? "TX" : "RX", " Channel #", c.idx + 1, " on ", c.device.hardware)
if !get(io, :compact, false)
Expand Down Expand Up @@ -372,7 +386,10 @@ function _gainrange(soapyr::SoapySDRRange)
# Represents an interval rather than a range
return (soapyr.minimum*dB)..(soapyr.maximum*dB)
end
return range(soapyr.minimum*dB; stop=soapyr.maximum*dB, step=soapyr.step*dB)
# TODO
@warn "Step ranges are not supported for gain elements. Returning Interval instead. Step: $(soapyr.step*dB)"
#return range(soapyr.minimum*dB; stop=soapyr.maximum*dB, step=soapyr.step*dB)
return (soapyr.minimum*dB)..(soapyr.maximum*dB)
end

function gainrange(c::Channel)
Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using SoapySDR
using Test
using Unitful
using Unitful.DefaultSymbols
const dB = u"dB"
using Intervals

const sd = SoapySDR
Expand Down Expand Up @@ -46,6 +48,30 @@ end
# Should throw
@test_throws ErrorException sd.StreamFormat("nonsense")
end
@testset "Ranges/Display" begin
intervalrange = sd.SoapySDRRange(0, 1, 0)
steprange = sd.SoapySDRRange(0, 1, 0.1)

intervalrangedb = sd._gainrange(intervalrange)
steprangedb = sd._gainrange(steprange) #TODO

intervalrangehz = sd._hzrange(intervalrange)
steprangehz = sd._hzrange(steprange)

hztype = typeof(1.0*Hz)

@test typeof(intervalrangedb) == Interval{Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Float64}, Closed, Closed}
@test typeof(steprangedb) == Interval{Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Float64}, Closed, Closed}
@test typeof(intervalrangehz) == Interval{hztype, Closed, Closed}
@test typeof(steprangehz) == StepRangeLen{hztype, Base.TwicePrecision{hztype}, Base.TwicePrecision{hztype}}

io = IOBuffer(read=true, write=true)

sd.print_hz_range(io, intervalrangehz)
@test String(take!(io)) == "00..0.001 kHz"
sd.print_hz_range(io, steprangehz)
@test String(take!(io)) == "00 Hz:0.0001 kHz:0.001 kHz"
end
@testset "High Level API" begin

@test length(Devices()) == 1
Expand Down

0 comments on commit 90c5414

Please sign in to comment.