/
RTLSDRIO.cs
147 lines (130 loc) · 3.67 KB
/
RTLSDRIO.cs
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
using System;
using System.Windows.Forms;
using SDRSharp.Radio;
namespace SDRSharp.RTLSDR
{
public unsafe class RtlSdrIO : IFrontendController, IDisposable
{
private readonly RtlSdrControllerDialog _gui;
private RtlDevice _rtlDevice;
private uint _frequency = 105500000;
private Radio.SamplesAvailableDelegate _callback;
public RtlSdrIO()
{
_gui = new RtlSdrControllerDialog(this);
}
~RtlSdrIO()
{
Dispose();
}
public void Dispose()
{
if (_gui != null)
{
_gui.Dispose();
}
GC.SuppressFinalize(this);
}
public void SelectDevice(uint index)
{
Close();
_rtlDevice = new RtlDevice(index);
_rtlDevice.SamplesAvailable += rtlDevice_SamplesAvailable;
_rtlDevice.Frequency = _frequency;
_gui.ConfigureGUI();
_gui.ConfigureDevice();
}
public RtlDevice Device
{
get { return _rtlDevice; }
}
public void Open()
{
var devices = DeviceDisplay.GetActiveDevices();
foreach (var device in devices)
{
try
{
SelectDevice(device.Index);
return;
}
catch (ApplicationException)
{
// Just ignore it
}
}
if (devices.Length > 0)
{
throw new ApplicationException(devices.Length + " compatible devices have been found but are all busy");
}
throw new ApplicationException("No compatible devices found");
}
public void Close()
{
if (_rtlDevice != null)
{
_rtlDevice.Stop();
_rtlDevice.SamplesAvailable -= rtlDevice_SamplesAvailable;
_rtlDevice.Dispose();
_rtlDevice = null;
}
}
public void Start(Radio.SamplesAvailableDelegate callback)
{
if (_rtlDevice == null)
{
throw new ApplicationException("No device selected");
}
_callback = callback;
try
{
_rtlDevice.Start();
}
catch
{
Open();
_rtlDevice.Start();
}
}
public void Stop()
{
_rtlDevice.Stop();
}
public bool IsSoundCardBased
{
get { return false; }
}
public string SoundCardHint
{
get { return string.Empty; }
}
public void ShowSettingGUI(IWin32Window parent)
{
_gui.Show();
}
public void HideSettingGUI()
{
_gui.Hide();
}
public double Samplerate
{
get { return _rtlDevice == null ? 0.0 : _rtlDevice.Samplerate; }
}
public long Frequency
{
get { return _frequency; }
set
{
_frequency = (uint) value;
if (_rtlDevice != null)
{
_rtlDevice.Frequency = _frequency;
}
}
}
private void rtlDevice_SamplesAvailable(object sender, SamplesAvailableEventArgs e)
{
_callback(this, e.Buffer, e.Length);
}
}
}