HARDT - The Ham Radio DSP Toolkit
hrtl2832reader.h
1 #ifndef __HRTL2832READER_H
2 #define __HRTL2832READER_H
3 
4 #include <mutex>
5 #include <condition_variable>
6 #include <thread>
7 #include "rtl-sdr.h"
8 
9 #include "hardt.h"
10 #include "hobject.h"
11 #include "hreader.h"
12 #include "hfft.h"
13 #include "hprobe.h"
14 #include "hcommand.h"
15 #include "hrtl2832.h"
16 
17 #define NUMBER_OF_RTL_BUFFERS 15
18 
22 template <class T>
23 class HRtl2832Reader : public HReader<T>
24 {
25  public:
26 
27  private:
28 
29  HRtl2832::MODE _mode;
30  int _buflen;
31  HFft<T>* _fft;
32  T* _buffer;
33  std::thread* _current;
34  rtlsdr_dev_t *_dev;
35  size_t _blocksize;
36  HProbe<T>* _probe;
37 
38  // Data accessed by the (static) callback function
39  struct CallbackData
40  {
41  int wrloc;
42  int rdloc;
43  unsigned char* buffer;
44  std::mutex mtx;
45  std::condition_variable lock;
46  } _cbd;
47 
48  // Indicate if the device has been started
49  bool _isStarted;
50 
51  // Indicate if the device has been initialize
52  bool _isInitialized;
53 
57  bool Start();
58 
62  bool Stop();
63 
64  void CopyBlock(T* dest, unsigned char* src, size_t blocksize);
65 
66  public:
67 
84  HRtl2832Reader(int device, H_SAMPLE_RATE rate, HRtl2832::MODE mode, int gain, int32_t frequency, int blocksize, bool offset = 0, int correction = 0, HProbe<T>* probe = nullptr);
85 
90 
97  int Read(T* dest, size_t blocksize);
98 
107  static void callback( unsigned char *buffer, uint32_t length, void* context);
108 
112  bool Command(HCommand* command) {
113 
114  // Handle known commands
115  switch( command->Class ) {
116  case H_COMMAND_CLASS::TUNER: {
117  switch( command->Opcode ) {
118  case H_COMMAND_OPCODE::SET_FREQUENCY: {
119  return SetCenterFrequency(command->Data.Value);
120  }
121  case H_COMMAND_OPCODE::SET_GAIN: {
122  return SetGain(command->Data.Value);
123  }
124  }
125  }
126  case H_COMMAND_CLASS::ANY: {
127  switch( command->Opcode ) {
128  case H_COMMAND_OPCODE::SET_GAIN: {
129  return SetGain(command->Data.Value);
130  }
131  }
132  }
133  }
134 
135  // Any other command returns true
136  return true;
137  }
138 
146  bool EnableBiasTee(int gpio = 0);
147 
153  bool DisableBiasTee(int gpio = 0);
154 
161  bool SetCenterFrequency(uint32_t frequency);
162 
168  uint32_t GetCenterFrequency();
169 
176  bool SetGain(int gain);
177 
183  int GetGain();
184 };
185 
186 #endif
HCommand::Opcode
int16_t Opcode
Definition: hcommand.h:87
HCommand::Data
HCommandData Data
Definition: hcommand.h:103
HRtl2832Reader::SetGain
bool SetGain(int gain)
Definition: hrtl2832reader.cpp:404
HCommandData::Value
int32_t Value
Definition: hcommand.h:71
HRtl2832Reader::HRtl2832Reader
HRtl2832Reader(int device, H_SAMPLE_RATE rate, HRtl2832::MODE mode, int gain, int32_t frequency, int blocksize, bool offset=0, int correction=0, HProbe< T > *probe=nullptr)
Definition: hrtl2832reader.cpp:14
HRtl2832Reader
Definition: hrtl2832reader.h:23
HRtl2832Reader::callback
static void callback(unsigned char *buffer, uint32_t length, void *context)
Definition: hrtl2832reader.cpp:267
HRtl2832Reader::~HRtl2832Reader
~HRtl2832Reader()
Definition: hrtl2832reader.cpp:143
HReader
Definition: hreader.h:24
HRtl2832Reader::DisableBiasTee
bool DisableBiasTee(int gpio=0)
Definition: hrtl2832reader.cpp:369
HProbe
Definition: hprobe.h:10
HRtl2832Reader::Command
bool Command(HCommand *command)
Definition: hrtl2832reader.h:112
HCommand::Class
int16_t Class
Definition: hcommand.h:84
HCommand
Definition: hcommand.h:81
HFft
Definition: hfft.h:12
HRtl2832Reader::GetCenterFrequency
uint32_t GetCenterFrequency()
Definition: hrtl2832reader.cpp:399
HRtl2832Reader::Read
int Read(T *dest, size_t blocksize)
Definition: hrtl2832reader.cpp:199
HRtl2832Reader::EnableBiasTee
bool EnableBiasTee(int gpio=0)
Definition: hrtl2832reader.cpp:349
HRtl2832::MODE
MODE
Definition: hrtl2832.h:80
HRtl2832Reader::SetCenterFrequency
bool SetCenterFrequency(uint32_t frequency)
Definition: hrtl2832reader.cpp:389
HRtl2832Reader::GetGain
int GetGain()
Definition: hrtl2832reader.cpp:428