Class: TurtleShell::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/turtleshell/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = 0) ⇒ Device

Returns a new instance of Device.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/turtleshell/device.rb', line 8

def initialize(n = 0)
  raise ArgumentError.new('TurtleShell::Device.new expects a number') unless n.is_a? Fixnum

  unless raw_device_attrs = TurtleShell::RTLSDR.nth_device(n)
    raise TurtleShell::DeviceNotFoundError
  end

  @name = raw_device_attrs[:name]
  @device = raw_device_attrs[:device]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/turtleshell/device.rb', line 6

def name
  @name
end

Class Method Details

.device_with_name(name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/turtleshell/device.rb', line 19

def self.device_with_name(name)
  unless index = TurtleShell.all_devices.index(name)
    raise ArgumentError.new('No device with that name')
  end

  new(index)
end

Instance Method Details

#center_frequencyObject



43
44
45
# File 'lib/turtleshell/device.rb', line 43

def center_frequency
  TurtleShell::RTLSDR.get_center_freq(@device)
end

#center_frequency=(freq) ⇒ Object



47
48
49
# File 'lib/turtleshell/device.rb', line 47

def center_frequency=(freq)
  TurtleShell::RTLSDR.set_center_freq(@device, freq)
end

#close_deviceObject



103
104
105
106
107
# File 'lib/turtleshell/device.rb', line 103

def close_device
  unless @device.nil?
    TurtleShell::RTLSDR.close_device(@device)
  end
end

#disable_manual_gain_modeObject



72
73
74
# File 'lib/turtleshell/device.rb', line 72

def disable_manual_gain_mode
  TurtleShell::RTLSDR.set_manual_gain(@device, false)
end

#freq_correctionObject



27
28
29
# File 'lib/turtleshell/device.rb', line 27

def freq_correction
  TurtleShell::RTLSDR.get_freq_correction(@device)
end

#freq_correction=(v) ⇒ Object



31
32
33
# File 'lib/turtleshell/device.rb', line 31

def freq_correction=(v)
  TurtleShell::RTLSDR.set_freq_correction(@device, v)
end

#gainObject



51
52
53
# File 'lib/turtleshell/device.rb', line 51

def gain
  TurtleShell::RTLSDR.get_gain(@device)
end

#gain=(gain) ⇒ Object



55
56
57
# File 'lib/turtleshell/device.rb', line 55

def gain=(gain)
  TurtleShell::RTLSDR.set_gain(@device, gain)
end

#get_available_gainsObject

most devices only support a finite number of gain values



60
61
62
# File 'lib/turtleshell/device.rb', line 60

def get_available_gains
  TurtleShell::RTLSDR.get_tuner_gains(@device)
end

#get_tuner_typeObject



64
65
66
# File 'lib/turtleshell/device.rb', line 64

def get_tuner_type
  TurtleShell::RTLSDR.get_tuner_type(@device)
end

#read_samples(number_of_samples = 1024) ⇒ Object

read specified number of complex samples from tuner real and imaginary parts are normalized between [-1, 1]



78
79
80
81
82
# File 'lib/turtleshell/device.rb', line 78

def read_samples(number_of_samples = 1024)
  number_of_bytes = 2 * number_of_samples
  raw_data = read_bytes(number_of_bytes)
  packed_bytes_to_complex(raw_data)
end

#read_samples_async(number_of_samples = 1024, &block) ⇒ Object

read specified number of complex samples from tuner into a block real and imaginary parts are normalized between [-1, 1]



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/turtleshell/device.rb', line 86

def read_samples_async(number_of_samples = 1024, &block)
  block = proc { } unless block_given?
  wrapped_block = proc do |raw_data|
    block.call(packed_bytes_to_complex(raw_data))
  end

  number_of_bytes_to_read = 2 * number_of_samples

  begin
      TurtleShell::RTLSDR.read_async(@device, number_of_bytes_to_read, wrapped_block)
  rescue SignalException => e
    puts 'Caught signal, cancelling async callback.'
    TurtleShell::RTLSDR.end_async(@device)
    raise e
  end
end

#sample_rateObject



35
36
37
# File 'lib/turtleshell/device.rb', line 35

def sample_rate
  TurtleShell::RTLSDR.get_sample_rate(@device)
end

#sample_rate=(rate) ⇒ Object



39
40
41
# File 'lib/turtleshell/device.rb', line 39

def sample_rate=(rate)
  TurtleShell::RTLSDR.set_sample_rate(@device, rate)
end

#set_manual_gain_modeObject



68
69
70
# File 'lib/turtleshell/device.rb', line 68

def set_manual_gain_mode
  TurtleShell::RTLSDR.set_manual_gain(@device, true)
end