i2cdriver

1.2.2 • Public • Published

i2cdriver

An i2c development board that works on Mac, Windows, and Linux?
... and has a graphic display?
... and for under $30?
Yes please! Buy one now!


tfmini-plus tfmini-plus

This library contains functions for:

  • i2c-bus API
  • i2cdriver device API

Swap in place of the i2c-bus module

// const i2c = require('i2c-bus')
// const bus_id = 1
 
const i2c = require('i2cdriver/i2c-bus')
const bus_id = '/dev/tty.usbserial-DO01INSW'
 
const tfminiplus = require('tfmini-plus')
 
i2c.openPromisified(bus_id)
.then(async (bus) => {
  const tfmp = await tfminiplus.I2C(bus, 0x10)
  const distance = await tfmp.distance()
  console.log('distance:', distance)
})
.catch(console.error);

i2c-bus is a popular module for communicating with i2c devices. This module provides a compatible/interchangable API so you can develop your i2c code on your development computer and deploy it to a device later- with only swapping out the module reference and bus id.

i2cdriver API

The API functions try to match the API provided by i2cdriver as close as possible.

The driver API just issues the special device commands. It does not enforce any specific (valid) command order. You will likely want to use i2c-bus compatible APIs which coordinates the device calls properly.

setspeed(100|400)

Sets the i2c bus speed (in KHz).

Supported values are:

  • 100 = 100KHz
  • 400 = 400KHz

setpullups(mask)

Sets the SDA and CLK pull-up resister values. See the i2cdriver documentation for more information.

Valid values: 0b000000 to 0b111111

scan()

Scans the i2c bus (0x08-0x77) for connected devices.

Returns: An array of address numbers of detected devices.

reset()

Resets the i2c bus.

start(addr, rw)

Starts a read or write session to a device on the bus.

addr - The i2c device address
rw - Indicates whether to start a read or write session. Valid values:
    • "write", 0, false
    • "read", 1, true

read(length)

Issues a read request for length bytes. You should call start(addr, 'read') first. The i2cdriver does not restrict you from sending a read/write request out of sequence- but the result will be unknown (probably not good).

NOTE1: The i2c-bus APIs coordinate these calls properly.

NOTE2: The i2cdriver device is limited to 64 bytes in a single read. This function will read 64 byte chunks at a time- but the device must support an internal read cursor (it remembers where it left off).

Returns:: A Buffer of the bytes read.

write(buff)

Writes the Buffer to the i2c bus. You should call start(addr, 'write') first. The i2cdriver does not restrict you from sending a read/write request out of sequence- but the result will be unknown (probably not good).

buff - A Buffer of the bytes you want to send

Note: The i2c-bus APIs coordinate these calls properly.

stop()

Concludes a read or write session.

regrd(addr, register, length = 1)

A helper function that reads from a register on a device.

addr - The i2c device address
register - (byte) The register to read from.
length - The number of bytes to read. (e.g.: 2 for a 16 bit value)

NOTE: The i2cdriver device is limited to 64 bytes in a single read. This function will read 64 byte chunks at a time- however, this deviates from how other i2c implementations operate and may cause problems.

Returns:: A Buffer of the bytes read.

regwr(addr, register, value)

A helper function that writes to a register on a device.

addr - The i2c device address
register - (byte) The register to write to.
value - The byte(s) to write. Vaid values:
    • byte (Number)
    • [byte1, byte2] (Array of bytes) for 16bit values

status()

Gets the device status. See the i2cdriver documentation for more information.

Returns:: An object with the status properties.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i i2cdriver

Weekly Downloads

0

Version

1.2.2

License

MIT

Unpacked Size

10.2 MB

Total Files

8

Last publish

Collaborators

  • williamkapke