Class: RubyI2C::Adapter::Device

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-i2c/adapter/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_file: nil, mutex: nil, force: false) ⇒ Device

Returns a new instance of Device.



10
11
12
13
14
# File 'lib/ruby-i2c/adapter/device.rb', line 10

def initialize(device_file: nil, mutex: nil, force: false)
  super force: force
  @device_file = device_file || self.class.default_device_file
  @mutex = mutex || self.class.default_mutex
end

Instance Attribute Details

#device_fileObject (readonly)

Returns the value of attribute device_file.



8
9
10
# File 'lib/ruby-i2c/adapter/device.rb', line 8

def device_file
  @device_file
end

#mutexObject (readonly)

Returns the value of attribute mutex.



8
9
10
# File 'lib/ruby-i2c/adapter/device.rb', line 8

def mutex
  @mutex
end

Class Method Details

.default_device_fileObject



5
# File 'lib/ruby-i2c/adapter/device.rb', line 5

def self.default_device_file; Dir.glob('/dev/i2c-*').sort.first; end

.default_mutexObject



6
# File 'lib/ruby-i2c/adapter/device.rb', line 6

def self.default_mutex; Mutex.new; end

Instance Method Details

#command(address, cmd, length = 1, rest = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby-i2c/adapter/device.rb', line 17

def command(address, cmd, length=1, rest=nil)
  ret = nil
  @mutex.synchronize do
    operate(address) do
      @handle.syswrite encode(cmd)
      sleep rest if rest
      ret = @handle.sysread length
    end
  end
  return ret
rescue SystemCallError => e
  handle e
end

#read(address, length = 1) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-i2c/adapter/device.rb', line 32

def read(address, length=1)
  ret = nil
  @mutex.synchronize do
    operate(address) do
      ret = @handle.sysread length
    end
  end
  return ret
rescue SystemCallError => e
  handle e
end

#write(address, *data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby-i2c/adapter/device.rb', line 45

def write(address, *data)
  ret = nil
  @mutex.synchronize do
    operate(address) do
      ret = @handle.syswrite encode(*data)
    end
  end
  return ret
rescue SystemCallError => e
  handle e
end