0 | module Bindings.RtlSdr.Sampling
 1 |
 2 | import Bindings.RtlSdr.Device
 3 | import Bindings.RtlSdr.Error
 4 | import Bindings.RtlSdr.Raw.Sampling
 5 |
 6 | %default total
 7 |
 8 | ||| Set the sample rate for the device, also selects the baseband filters
 9 | ||| according to the requested sample rate for tuners where this is possible.
10 | |||
11 | ||| @h is the device handle
12 | ||| @r is the sample rate to be set, possible values are:
13 | |||      225001 - 300000 Hz
14 | |||      900001 - 3200000 Hz
15 | |||      sample loss is to be expected for rates > 2400000
16 | export
17 | setSampleRate : Ptr RtlSdrHandle -> Int -> IO (Either RTLSDR_ERROR ())
18 | setSampleRate h r = do
19 |   r <- fromPrim $ set_sample_rate h r
20 |   io_pure $ if r == 0 then Right () else Left RtlSdrInvalidRate
21 |
22 | ||| Get actual sample rate the device is configured to.
23 | |||
24 | ||| @h is the device handle
25 | export
26 | getSampleRate : Ptr RtlSdrHandle -> IO Int
27 | getSampleRate h = fromPrim $ get_sample_rate h
28 |
29 | ||| Enable or disable the internal digital AGC of the RTL2832.
30 | |||
31 | ||| @h is the device handle
32 | ||| @t is the toggle of digital AGC mode, True means enabled, False means disabled
33 | export
34 | setAGCMode : Ptr RtlSdrHandle -> Bool -> IO (Either RTLSDR_ERROR ())
35 | setAGCMode h t = do
36 |   r <- fromPrim $ set_agc_mode h (if t then 1 else 0)
37 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
38 |
39 | public export
40 | data SamplingType = SAMPLING_DISABLED | SAMPLING_I_ADC_ENABLED | SAMPLING_Q_ADC_ENABLED | SAMPLING_IQ_ADC_ENABLED
41 |
42 | export
43 | Show SamplingType where
44 |   show SAMPLING_DISABLED      = "Disabled"
45 |   show SAMPLING_I_ADC_ENABLED = "I-ADC Input Enabled"
46 |   show SAMPLING_Q_ADC_ENABLED = "Q-ADC Input Enabled"
47 |   show SAMPLING_IQ_ADC_ENABLED = "IQ-ADC Input Enabled"
48 |
49 | toSamplingType : Int -> Maybe SamplingType
50 | toSamplingType 0 = Just SAMPLING_DISABLED
51 | toSamplingType 1 = Just SAMPLING_I_ADC_ENABLED
52 | toSamplingType 2 = Just SAMPLING_Q_ADC_ENABLED
53 | toSamplingType 3 = Just SAMPLING_IQ_ADC_ENABLED
54 | toSamplingType _ = Nothing
55 |
56 | fromSamplingType : SamplingType -> Int
57 | fromSamplingType SAMPLING_DISABLED      = 0
58 | fromSamplingType SAMPLING_I_ADC_ENABLED = 1
59 | fromSamplingType SAMPLING_Q_ADC_ENABLED = 2
60 | fromSamplingType SAMPLING_IQ_ADC_ENABLED = 3
61 |
62 | ||| Enable or disable the direct sampling mode. When enabled, the IF mode
63 | ||| of the RTL2832 is activated, and `setCenterFreq` will control
64 | ||| the IF-frequency of the DDC, which can be used to tune from 0 to 28.8 MHz
65 | ||| (xtal frequency of the RTL2832).
66 | |||
67 | ||| @h is the device handle
68 | ||| @t is the mode of `SampleType`
69 | export
70 | setDirectSampling : Ptr RtlSdrHandle -> SamplingType -> IO (Either RTLSDR_ERROR ())
71 | setDirectSampling h t = do
72 |   r <- fromPrim $ set_direct_sampling h (fromSamplingType t)
73 |   io_pure $ if r < 0 then Left RtlSdrError else Right ()
74 |
75 | ||| Get state of the direct sampling mode
76 | |||
77 | ||| @h is the device handle
78 | export
79 | getDirectSampling : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR (Maybe SamplingType))
80 | getDirectSampling h = do
81 |   r <- fromPrim $ get_direct_sampling h
82 |   io_pure $ if r < 0 then Left RtlSdrError else Right (toSamplingType r)
83 |