Class: I2C::Dev

Inherits:
Object
  • Object
show all
Defined in:
lib/i2c/backends/i2c-dev.rb

Constant Summary collapse

I2C_SLAVE =

see i2c-dev.h

0x0703

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comsMutexObject (readonly)

Returns the value of attribute comsMutex.



21
22
23
# File 'lib/i2c/backends/i2c-dev.rb', line 21

def comsMutex
  @comsMutex
end

Class Method Details

.create(device_path) ⇒ Object

Raises:

  • (Errno::ENOENT)


14
15
16
17
18
19
# File 'lib/i2c/backends/i2c-dev.rb', line 14

def self.create(device_path)
	raise Errno::ENOENT, "Device #{device_path} not found." unless File.exists?(device_path)
	@instances ||= Hash.new
	@instances[device_path] = Dev.new(device_path) unless @instances.has_key?(device_path)
	@instances[device_path]
end

Instance Method Details

#read(address, size, *params) ⇒ Object

this tries to lock the coms mutex (unless already held), then sends *params, if given, and then tries to read size bytes. The result is a String which can be treated with String#unpack afterwards



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/i2c/backends/i2c-dev.rb', line 49

def read(address, size, *params)
	if(@comsMutex.owned?)
		keepLock = true;
	else
		@comsMutex.lock;
	end

	begin
		setup_device(address);
		raw_write(params) unless params.empty?
		result = raw_read(size);
	ensure
		@comsMutex.unlock() unless keepLock;
		return result;
	end
end

#read_byte(address) ⇒ Object

Read a byte from the current address. Return a one char String which can be treated with String#unpack



68
69
70
# File 'lib/i2c/backends/i2c-dev.rb', line 68

def read_byte(address)
	read(address, 1);
end

#write(address, *params) ⇒ Object

this tries to lock the coms mutex, unless already held, then sends every param, begining with params[0] If the current param is a Fixnum, it is treated as one byte. If the param is a String, this string will be sent byte by byte. You can use Array#pack to create a string from an array For Fixnum there is a convenient function to_short which transforms the number to a string this way: 12345.to_short == [12345].pack(ā€œsā€)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/i2c/backends/i2c-dev.rb', line 30

def write(address, *params)
	if(@comsMutex.owned?)
		keepLock = true;
	else
		@comsMutex.lock;
	end

	begin
		setup_device(address);
		raw_write(params);
	ensure
		@comsMutex.unlock() unless keepLock;
	end
end