Class: I2C::LinuxI2Cdev

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

Overview

I2C bus driver

only implement high-level methods.

Constant Summary collapse

I2C_SLAVE =

see /usr/include/linux/i2c.h i2c-dev.h

0x0703
I2C_RDWR =
0x0707
I2C_M_RD =
0x0001

Instance Method Summary collapse

Constructor Details

#initialize(node, *params) ⇒ LinuxI2Cdev

constructor

Parameters:

  • node (String)

    device node of I2C

  • params (nil)

    dummy.

See Also:



49
50
51
# File 'lib/mruby/i2c.rb', line 49

def initialize(node, *params)
  @device = File.open(node, "r+:ASCII-8BIT")
end

Instance Method Details

#read_ioctl(i2c_adrs_7, read_bytes, *param) ⇒ String Also known as: read

Reads data of read_bytes bytes from the device with the address i2c_adrs_7. using ioctl

Parameters:

  • i2c_adrs_7 (Integer)

    I2C slave address (7bit address)

  • read_bytes (Integer)

    read bytes.

  • param (Integer, String, Array<Integer>)

    output data before reading.

Returns:

  • (String)

    reading datas.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mruby/i2c.rb', line 82

def read_ioctl( i2c_adrs_7, read_bytes, *param )
  out_data = _rebuild_output_data( param )
  recv_data = "\x00".b * read_bytes

  # prepare the struct i2c_msg[2]
  # struct i2c_msg {
  #        __u16 addr;
  #        __u16 flags;
  #        __u16 len;         << hidden padding _u16
  #        __u8 *buf;
  # };
  i2c_msg_s = [ i2c_adrs_7, 0, out_data.bytesize, 0,
                [out_data].pack('p').unpack1('J') ].pack('SSSSJ')
  i2c_msg_r = [ i2c_adrs_7, I2C_M_RD, read_bytes, 0,
                [recv_data].pack('p').unpack1('J') ].pack('SSSSJ')

  # prepare the struct i2c_rdwr_ioctl_data
  # struct i2c_rdwr_ioctl_data {
  #         struct i2c_msg *msgs;   /* pointers to i2c_msgs */
  #         __u32 nmsgs;            /* number of i2c_msgs */
  # };
  if out_data.empty?
    arg = [ [i2c_msg_r            ].pack('P').unpack1('J'), 1 ].pack('JL')
  else
    arg = [ [i2c_msg_s + i2c_msg_r].pack('P').unpack1('J'), 2 ].pack('JL')
  end

  @device.ioctl( I2C_RDWR, arg )
  return recv_data
end

#read_sysread(i2c_adrs_7, read_bytes, *param) ⇒ Object

Reads data of read_bytes bytes from the device with the address i2c_adrs_7. using sysread

See Also:



64
65
66
67
68
69
70
# File 'lib/mruby/i2c.rb', line 64

def read_sysread( i2c_adrs_7, read_bytes, *param )
  out_data = _rebuild_output_data( param )

  _use_slave_adrs( i2c_adrs_7 )
  @device.syswrite( out_data )  if !out_data.empty?
  @device.sysread( read_bytes )
end

#write(i2c_adrs_7, *outputs) ⇒ Integer

Writes data specified in outputs to the device with the address i2c_adrs_7.

Parameters:

  • i2c_adrs_7 (Integer)

    I2C slave address (7bit address)

  • outputs (Integer, String, Array<Integer>)

    output data.

Returns:

  • (Integer)

    number of bytes actually write.



123
124
125
126
127
128
# File 'lib/mruby/i2c.rb', line 123

def write( i2c_adrs_7 , *outputs )
  out_data = _rebuild_output_data( outputs )

  _use_slave_adrs( i2c_adrs_7 )
  @device.syswrite( out_data )
end