@johntalton/i2c-bus-soft
v1.0.1
Published
Library to wrap [`I2CBus`](https://github.com/johntalton/and-other-delights) over top of GPIO abstraction.
Readme
I2CBus Software over GPIO
Library to wrap I2CBus over top of GPIO abstraction.
Motivation
Because 🤷🏻♂️, but also:
To further understanding and testing of I²C bus interactions and expose unique capabilities.
And to provide a I2CBus abstraction that is capable of being the source bus for a wider range of sensors implemented overtop of this API.
Primary testing of the has been done over the following configuration:
SerialPort -> Excamera I2CDriver -> i2c-bus-excamera -> pcf8574 -> i2c-bus-soft -> device*
* where devices include EEPROM / AHT20 / DS1841 and others
The pcf8574 acts as the primary GPIO interface as it naturally floats HIGH and does not have the concept of input/output direction. This lends itself to I²C control. However it is itself a I2CBus device, that is running overtop of the Excamera Serial i2cDriver.
Example
import { I2CBusGPIO } from '@johntalton/i2c-bus-soft'
/*
using some custom GPIO device implement the set/get interface
(note true maps to Floating/HIGH while false is LOW)
*/
const gpioBus = new I2CBusGPIO({
/**
* @param {boolean} scl
* @param {boolean} sda
*/
async set(scl, sda) {
// set scl and sda according to parameters
},
async get() {
// return scl and sda as boolean values
return { scl, sda }
}
})
// now the bus can be used as normal I2CBus
// such as to scan
const devices = await gpioBus.scan()
for (const device of devices) {
console.log('found device at', '0x' + device.toString(16).toUpperCase().padStart(2, '0'))
}
// or using a attached device
const adt = new ADT7410(new I2CAddressedBus(gpioBus, 0x48))
const id = await adt.getId()
const temp = await adt.getTemperature()
Using the PCF8574
Using pin 6/7 used for SDA/SCL respectively
import { I2CBusGPIO } from '@johntalton/i2c-bus-soft'
import { PCF8574 } from '@johntalton/pcf8574'
const device = new PCF8574(/* some other bus */)
const gpioBus = new I2CBusGPIO({
async set(scl, sda) {
await device.writePort({ p6: sda, p7: scl })
},
async get() {
const { p6, p7 } = await device.readPort()
return { scl, sda }
}
})