Class: RubyI2C::Adapter::UnixDevice

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_i2c/adapter/unix_device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of UnixDevice.



12
13
14
15
16
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 12

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.



10
11
12
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 10

def device_file
  @device_file
end

#mutexObject (readonly)

Returns the value of attribute mutex.



10
11
12
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 10

def mutex
  @mutex
end

Class Method Details

.default_device_fileObject



7
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 7

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

.default_mutexObject



8
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 8

def self.default_mutex; Mutex.new; end

Instance Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 19

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 34

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_i2c/adapter/unix_device.rb', line 47

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