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



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

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

#center_frequency=(freq) ⇒ Object



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

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

#close_deviceObject



95
96
97
98
99
# File 'lib/turtleshell/device.rb', line 95

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

#disable_manual_gain_modeObject



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

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

#gainObject



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

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

#gain=(gain) ⇒ Object



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

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

#get_available_gainsObject

most devices only support a finite number of gain values



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

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

#get_tuner_typeObject



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

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]



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

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]



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/turtleshell/device.rb', line 78

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



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

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

#sample_rate=(rate) ⇒ Object



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

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

#set_manual_gain_modeObject



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

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