Class: I2CDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/templates/grove_pi/i2c/i2c.rb

Overview

Generic abstract class for I2C manipulation.

Direct Known Subclasses

ADT7410, Bmp180, D6T44L, HD44780, HDC1000, MPL115A2

Defined Under Namespace

Modules: Driver Classes: ACM1602NI, ADT7410, AQM0802A, Bmp180, D6T44L, HD44780, HDC1000, I2CBUSBusy, I2CException, I2CIOError, MPL115A2

Constant Summary collapse

VERSION =
"0.0.6"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ I2CDevice

args[:address]
Integer

7-bit slave address without r/w bit. MSB is always 0.

args[:driver]
I2CDevice::Driver::I2CDev

Instance of driver class



15
16
17
18
19
20
21
22
23
# File 'lib/templates/grove_pi/i2c/i2c.rb', line 15

def initialize(args={})
	if args[:driver].nil?
		require "i2c/driver/i2c-dev"
		args[:driver] = I2CDevice::Driver::I2CDev.new
	end

	@driver  = args[:driver]
	@address = args[:address] or raise I2CException, "args[:address] required"
end

Instance Attribute Details

#addressObject

Slave address



11
12
13
# File 'lib/templates/grove_pi/i2c/i2c.rb', line 11

def address
  @address
end

Instance Method Details

#i2cget(param, length = 1) ⇒ Object

This method read data from slave with following process:

  1. Write ‘param` to slave

  2. re-start

  3. Read data until NACK or ‘length`

param
Integer

First writing byte. Typically, this is slave memory address.

length=1
Integer

Read bytes length

Returns
String

Bytes



34
35
36
# File 'lib/templates/grove_pi/i2c/i2c.rb', line 34

def i2cget(param, length=1)
	@driver.i2cget(@address, param, length)
end

#i2cset(*data) ⇒ Object

Write data to slave.

data
Array

Writing bytes array.

Returns
String

Wrote bytes



41
42
43
# File 'lib/templates/grove_pi/i2c/i2c.rb', line 41

def i2cset(*data)
	@driver.i2cset(@address, *data)
end