0 | module Bindings.RtlSdr.Gain
 1 |
 2 | import Bindings.RtlSdr.Device
 3 | import Bindings.RtlSdr.Error
 4 | import Bindings.RtlSdr.Raw.Gain
 5 | import Bindings.RtlSdr.Raw.Support
 6 |
 7 | import System.FFI
 8 |
 9 | %default total
10 |
11 | ||| Get a list of gains supported by the tuner.
12 | |||
13 | ||| Each gain values is in tenths of a dB, 115 means 11.5 dB.
14 | |||
15 | ||| @h is the device handle
16 | export
17 | getTunerGains : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR (List Int))
18 | getTunerGains h = do
19 |   n <- fromPrim $ get_tuner_gains h (prim__castPtr prim__getNullAnyPtr)
20 |   if n < 0 then do
21 |              io_pure $ Left RtlSdrError
22 |            else do
23 |              v <- prim__castPtr <$> malloc (n*8)
24 |              _ <- fromPrim $ get_tuner_gains h v
25 |              g <- readBufPtr v n
26 |              free $ prim__forgetPtr v
27 |              io_pure $ Right g
28 |
29 | ||| Set the gain for the device.
30 | |||
31 | ||| Manual gain mode must be enabled for this to work.
32 | |||
33 | ||| Valid gain values (in tenths of a dB) for the E4000 tuner:
34 | ||| -10, 15, 40, 65, 90, 115, 140, 165, 190,
35 | ||| 215, 240, 290, 340, 420, 430, 450, 470, 490
36 | |||
37 | ||| Valid gain values may be queried with `getTunerGains`.
38 | |||
39 | ||| @h is the device handle
40 | ||| @g is in tenths of a dB, 115 means 11.5 dB.
41 | export
42 | setTunerGain : Ptr RtlSdrHandle -> Int -> IO (Either RTLSDR_ERROR ())
43 | setTunerGain h g = do
44 |   r <- fromPrim $ set_tuner_gain h g
45 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
46 |
47 | ||| Set the bandwidth for the device.
48 | |||
49 | ||| @h is the device handle
50 | ||| @bw is the bandwidth in Hz. Zero means automatic BW selection.
51 | export
52 | setTunerBandwidth : Ptr RtlSdrHandle -> Int -> IO (Either RTLSDR_ERROR ())
53 | setTunerBandwidth h bw = do
54 |   r <- fromPrim $ set_tuner_bandwidth h bw
55 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
56 |
57 | ||| Get actual gain the device is configured to.
58 | |||
59 | ||| Returned gain is in tenths of a dB, 115 means 11.5 dB.
60 | |||
61 | ||| @h is the device handle
62 | export
63 | getTunerGain : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR Int)
64 | getTunerGain h = do
65 |   r <- fromPrim $ get_tuner_gain h
66 |   io_pure $ if r == 0 then Left RtlSdrError else Right r
67 |
68 | ||| Set the intermediate frequency gain for the device.
69 | |||
70 | ||| @h is the device handle
71 | ||| @s is the stage intermediate frequency gain stage number (1 to 6 for E4000)
72 | ||| @g is the gain in tenths of a dB, -30 means -3.0 dB.
73 | export
74 | setTunerIFGain : Ptr RtlSdrHandle -> Int -> Int -> IO (Either RTLSDR_ERROR ())
75 | setTunerIFGain h s g = do
76 |   r <- fromPrim $ set_tuner_if_gain h s g
77 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
78 |
79 | ||| Set the gain mode (automatic/manual) for the device.
80 | |||
81 | ||| Manual gain mode must be enabled for the gain setter function to work.
82 | |||
83 | ||| @h is the device handle
84 | ||| @t is the manual gain mode, `True` means manual gain mode shall be enabled.
85 | export
86 | setTunerGainMode : Ptr RtlSdrHandle -> Bool -> IO (Either RTLSDR_ERROR ())
87 | setTunerGainMode h t = do
88 |   r <- fromPrim $ set_tuner_gain_mode h (if t then 1 else 0)
89 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
90 |