Module: TurtleShell::RTLSDR

Defined in:
ext/librtlsdr/librtlsdr.c

Defined Under Namespace

Classes: Device

Constant Summary collapse

TunerTypes =
tuner_hash

Class Method Summary collapse

Class Method Details

.all_devicesObject



31
32
33
34
35
36
37
38
39
40
41
# File 'ext/librtlsdr/librtlsdr.c', line 31

static VALUE turtleshell_all_devices() {
  int i, count;
  VALUE devices_array = rb_ary_new();
  count = rtlsdr_get_device_count();

  for (i = 0; i < count; ++i) {
    rb_ary_push(devices_array, rb_str_new2(rtlsdr_get_device_name(i)));
  }

  return devices_array;
}

.close_device(device) ⇒ Object



65
66
67
68
69
70
# File 'ext/librtlsdr/librtlsdr.c', line 65

static VALUE turtleshell_close(VALUE self, VALUE device) {
  rtlsdr_dev_t *dev = NULL;
  Data_Get_Struct(device, rtlsdr_dev_t, dev);
  rtlsdr_close(dev);
  return Qnil;
}

.countObject

count of devices



8
9
10
# File 'ext/librtlsdr/librtlsdr.c', line 8

static VALUE turtleshell_count() {
  return INT2NUM(rtlsdr_get_device_count());
}

.first_deviceObject

life cycle of devices



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'ext/librtlsdr/librtlsdr.c', line 12

static VALUE turtleshell_first_device() {
  rtlsdr_dev_t *device = NULL;
  VALUE wrapped_device;
  VALUE hash;
  int count = rtlsdr_get_device_count();

  // ensure we have at least one device
  if (!count) { return Qnil; }

  rtlsdr_open(&device, 0);
  wrapped_device = Data_Wrap_Struct(c_device, NULL, NULL, device);

  hash = rb_hash_new();
  rb_hash_aset(hash, rb_str_new2("name"), rb_str_new2(rtlsdr_get_device_name(0)));
  rb_hash_aset(hash, rb_str_new2("device_handle"), wrapped_device);

  return hash;
}

.get_center_freq(wrapped_device) ⇒ Object



178
179
180
181
182
# File 'ext/librtlsdr/librtlsdr.c', line 178

static VALUE turtleshell_get_center_frequency(VALUE self, VALUE wrapped_device) {
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  return UINT2NUM(rtlsdr_get_center_freq(device));
}

.get_gain(wrapped_device) ⇒ Object



194
195
196
197
198
# File 'ext/librtlsdr/librtlsdr.c', line 194

static VALUE turtleshell_get_gain(VALUE self, VALUE wrapped_device) {
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  return INT2NUM(rtlsdr_get_tuner_gain(device));
}

.get_sample_rate(wrapped_device) ⇒ Object

getters and setters



161
162
163
164
165
# File 'ext/librtlsdr/librtlsdr.c', line 161

static VALUE turtleshell_get_sample_rate(VALUE self, VALUE wrapped_device) {
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  return UINT2NUM(rtlsdr_get_sample_rate(device));
}

.get_tuner_gains(wrapped_device) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/librtlsdr/librtlsdr.c', line 208

static VALUE turtleshell_get_gains(VALUE self, VALUE wrapped_device) {
  int *gains = NULL;
  int length, i;
  rtlsdr_dev_t *device;
  VALUE buffer = rb_ary_new();
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);

  length = rtlsdr_get_tuner_gains(device, NULL);
  gains = malloc(sizeof(int) * length);
  rtlsdr_get_tuner_gains(device, gains);

  for (i = 0; i < length; ++i) {
    rb_ary_push(buffer, INT2NUM(gains[i]));
  }

  free(gains);
  return buffer;
}

.get_tuner_type(wrapped_device) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'ext/librtlsdr/librtlsdr.c', line 227

static VALUE turtleshell_get_tuner_type(VALUE self, VALUE wrapped_device) {
  VALUE hash;
  int tuner_type;
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);

  hash = rb_const_get(m_rtlsdr, rb_intern("TunerTypes"));
  tuner_type = rtlsdr_get_tuner_type(device);
  return rb_hash_aref(hash, tuner_type);
}

.nth_device(n) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/librtlsdr/librtlsdr.c', line 43

static VALUE turtleshell_nth_device(VALUE self, VALUE n) {
  int open_success;
  uint32_t int_n = NUM2UINT(n);
  rtlsdr_dev_t *device = NULL;
  VALUE wrapped_device, hash = rb_hash_new();
  uint32_t device_count = (uint32_t)rtlsdr_get_device_count();

  if (int_n >= device_count) { return Qnil; }

  open_success = rtlsdr_open(&device, int_n);
  if (open_success != 0) {
    return Qnil;
  }

  wrapped_device = Data_Wrap_Struct(c_device, NULL, NULL, device);

  rb_hash_aset(hash, ID2SYM(rb_intern("name")), rb_str_new2(rtlsdr_get_device_name(0)));
  rb_hash_aset(hash, ID2SYM(rb_intern("device")), wrapped_device);

  return hash;
}

.read_async(device, bytes_to_read, callback) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/librtlsdr/librtlsdr.c', line 136

static VALUE turtleshell_read_asynchronous(VALUE self,
                                          VALUE device,
                                          VALUE bytes_to_read,
                                          VALUE callback) {
  int success, length = NUM2INT(bytes_to_read);
  rtlsdr_dev_t *p_device;
  struct turtleshell_context context;
  void *wrapped_context;

  Data_Get_Struct(device, rtlsdr_dev_t, p_device);

  context.device = p_device;
  context.callback = callback;
  wrapped_context = (void *)&context;

  rtlsdr_reset_buffer(p_device);
  success = rtlsdr_read_async(p_device, turtleshell_callback, wrapped_context, 0, length);

  if (success != 0) {
    printf("error reading async: return code %d\n", success);
  }

  return Qnil;
}

.read_sync(device, bytes_to_read) ⇒ Object

reading bytes



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
# File 'ext/librtlsdr/librtlsdr.c', line 72

static VALUE turtleshell_read_synchronous(VALUE self,
                                          VALUE device,
                                          VALUE bytes_to_read) {
  int success, i;
  int bytes_read;
  VALUE buffer = rb_ary_new();
  int length = NUM2INT(bytes_to_read);
  uint8_t *p_buffer = malloc(sizeof(uint8_t) * bytes_to_read);
  rtlsdr_dev_t *p_device;

  Data_Get_Struct(device, rtlsdr_dev_t, p_device);

  rtlsdr_reset_buffer(p_device);
  success = rtlsdr_read_sync(p_device, p_buffer, length, &bytes_read);
  if (success != 0) {
    printf("error reading bytes. read_sync returned %d\n", success);
    free(p_buffer);
    return buffer;
  }

  for (i = 0; i < bytes_read; ++i) {
    rb_ary_push(buffer, UINT2NUM((uint8_t)p_buffer[i]));
  }

  free(p_buffer);
  return buffer;
}

.set_center_freq(wrapped_device, value) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'ext/librtlsdr/librtlsdr.c', line 184

static VALUE turtleshell_set_center_frequency(VALUE self,
                                             VALUE wrapped_device,
                                             VALUE value) {
  uint32_t freq = NUM2UINT(value);
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  rtlsdr_set_center_freq(device, freq);
  return Qnil;
}

.set_gain(wrapped_device, value) ⇒ Object



200
201
202
203
204
205
206
# File 'ext/librtlsdr/librtlsdr.c', line 200

static VALUE turtleshell_set_gain(VALUE self, VALUE wrapped_device, VALUE value) {
  int gain = NUM2INT(value);
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  rtlsdr_set_tuner_gain(device, gain);
  return Qnil;
}

.set_manual_gain(wrapped_device, enabled) ⇒ Object



238
239
240
241
242
243
244
245
# File 'ext/librtlsdr/librtlsdr.c', line 238

static VALUE turtleshell_set_manual_gain(VALUE self, VALUE wrapped_device, VALUE enabled) {
  int result;
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);

  result = rtlsdr_set_tuner_gain_mode(device, RTEST(enabled));
  return INT2NUM(result);
}

.set_sample_rate(wrapped_device, value) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'ext/librtlsdr/librtlsdr.c', line 167

static VALUE turtleshell_set_sample_rate(VALUE self,
                                        VALUE wrapped_device,
                                        VALUE value) {
  uint32_t rate = NUM2UINT(value);
  rtlsdr_dev_t *device;
  Data_Get_Struct(wrapped_device, rtlsdr_dev_t, device);
  rtlsdr_set_sample_rate(device, rate);

  return Qnil;
}