Class: Pedalog::Device

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#serialObject

Returns the value of attribute serial.



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

def serial
  @serial
end

Class Method Details

.find_allObject

Finds all the connected Pedalog devices, and returns an array of Device instances to describe them.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pedalog.rb', line 38

def self.find_all
  max_devices = Interface::pedalog_get_max_devices

  device = FFI::MemoryPointer.new(Interface::PedalogDevice, max_devices, false)
  devices = max_devices.times.collect do |i|
    Interface::PedalogDevice.new(device + i * Interface::PedalogDevice.size)
  end
  
  device_count = Interface::pedalog_find_devices(device)

  result = device_count.times.collect do |i|
    devices[i].to_native
  end

  device.free

  result
end

.get_error_message(error) ⇒ Object

Gets a string describing an error code.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pedalog.rb', line 58

def self.get_error_message(error)
  max_error_message = Interface::pedalog_get_max_error_message

  ptr = FFI::MemoryPointer.new(:char, max_error_message, false)
  
  Interface::pedalog_get_error_message(error, ptr)

  message = ptr.read_string

  ptr.free
  message
end

Instance Method Details

#read_dataObject

Returns a Data instance with this device’s current values, or nil if the device has been disconnected. If nil is returned, Device.find_all should be called again to update the list of connected devices.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pedalog.rb', line 73

def read_data
  device = Interface::PedalogDevice.new
  device.from_native(self)
  
  data = Interface::PedalogData.new
  
  result = Interface::pedalog_read_data(device, data)

  return nil if result == PEDALOG_ERROR_NO_DEVICE_FOUND
  throw Device.get_error_message(result) unless result == PEDALOG_OK

  data.to_native
end