Giter Site home page Giter Site logo

ruby-i2c-devices's Introduction

ruby-i2c-devices

i2c-devices is a library for using I2C devices.

SYNOPSYS

Usage of I2CDevice class directly:

require "i2c"
require "i2c/driver/i2c-dev"
device = I2CDevice.new(address: 0x60, driver: I2CDevice::Driver::I2CDev.new("/dev/i2c-1"))

# like i2c-tools's i2cget command
length = 3
device.i2cget(0x01, length)

# like i2c-tools's i2cset command
device.i2cset(0x01, 0x11, 0x12 ... )

or pre-defiend device driver class:

require "i2c/device/acm1602ni"

lcd = I2CDevice::ACM1602NI.new

lcd.put_line(0, "0123456789ABCDEF")

with driver class

require "i2c/device/mpl115a2"
require "i2c/driver/i2c-dev"

mpl = I2CDevice::MPL115A2.new(driver: I2CDevice::Driver::I2CDev.new("/dev/i2c-0"))
p mpl.calculate_hPa

or GPIO backend driver (this is very slow)

require "i2c/device/mpl115a2"
require "i2c/driver/gpio"

mpl = I2CDevice::MPL115A2.new(driver: I2CDevice::Driver::GPIO.new(
	sda: 23, # pin 16 in raspberry pi
	scl: 24, # pin 18 in raspberry pi
))

p mpl.calculate_hPa

Class

I2CDevice

Generic class for manipulating I2C device.

I2CDevice.new(address: address, driver: driver)

  • address : Integer : 7-bit slave address without r/w bit. MSB is always 0.
  • driver : I2CDevice::Driver : backend driver class. (default: I2CDevice::Driver::I2CDev)

I2CDevice#i2cset(*data) #=> Integer

Write data to slave.

Returns Integer which is bytes length wrote.

I2CDevice#i2cget(param, length=1) #=> String

This method read data from slave with following process:

  1. Write param to slave
  2. re-start
  3. Read data until NACK or length

Returns String.

I2CDevice::Driver::I2CDev

This depends on /dev/i2c-* (i2c-dev) feature on Linux. You may load i2c-dev kernel module.

I2CDevice::Driver::I2CDev.new(path)

  • path : String : Path to /dev/i2c-*

I2CDevice::Driver::GPIO

This depends on /sys/class/gpio feature on Linux and implements by bit-banging.

I2CDevice::Driver::I2CDev.new(sda: sda, scl: scl, speed: speed)

  • sda : Integer : Pin number of SDA.
  • scl : Integer : Pin number of SCL.
  • speed : Integer : I2C clock speed in kHz. (default: 1)

Pin number of sda and scl is not real pin number but logical pin number. Eg. In Raspberry Pi, specifing sda: 23, scl: 24 means using 16 and 18 pin.

You can specify 100 kHz or 400 kHz (or more) to speed but speed is rate-limited by host CPU speed. Typically, this module fall short of requirements of I2C spec (in Raspberry Pi, clock speed is about 1.3kHz). But most slave devices support DC~100kHz clock speed.

REQUIREMENTS

Currently this library depends on Linux's i2c-dev or sysfs with GPIO feature.

  • I2CDevice::Driver::I2CDev /dev/i2c-0 (i2c-dev), default
  • I2CDevice::Driver::GPIO /sys/class/gpio (GPIO)

TODO

  • More supported devices

ruby-i2c-devices's People

Contributors

brundage avatar cho45 avatar hotchpotch avatar phg0x7b avatar willia4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ruby-i2c-devices's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.