Package Exports
- async-i2c-bus
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (async-i2c-bus) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
async-i2c-bus
Bus and Device classes for i2c-bus, with promised functions.
Bus and Device classes for i2c-bus, with promised functions.
You probably don't need this library anymore. The original purpose of the library was to have async methods instead of callbacks. Currently i2c-bus support promises.
Besides that it is still useful for me to have a class for Bus and Device, and an easy way to catch the errors produced in the bus, as well as typexcript typings. And it saves some time not having to write it for every library.
Table of Contents
Installation
yarn add async-i2c-bus
// or
npm i async-i2c-busUsage
The package exports the Bus, Device and BusError classes.
You can check the full documentation
Bus
The Bus wraps the original implementation with a typescript class.
To create a Bus use the static method create or createAndOpen (also opens the bus) with the following signatures:
Bus.create({ busNumber }): Bus
| Name | Type |
|---|---|
busNumber |
number |
Returns: Bus
Bus.createAndOpen({ busNumber }): Promise<Bus>
| Name | Type |
|---|---|
busNumber |
number |
Returns: Promise‹Bus›
Device
Device inherits all the device-oriented methods from Bus and calls them with the device's address.
Also you can extends / compose it to create your specific device class. Read further for an example of it.
To create a Device use the static method create with the following signature:
Device.create({ busNumber }): Device
| Name | Type |
|---|---|
bus |
bus |
address |
number |
Returns: Device
BusError
All the errors thrown by the bus are wrapped into BusError, which keeps the message and stack of the original error, but makes it easier to catch bus-specific errors.
Example
import { Bus, Device } from 'async-i2c-bus';
const WEATHER_SENSOR_ADDRESS = 0x77;
const main = async () => {
const bus = Bus.create();
await bus.open();
const devices = await bus.scan();
console.log(`Connected devices ${devices}`);
const weatherSensor = Device.create({ address: WEATHER_SENSOR_ADDRESS, bus });
// Configure Weather Sensor (BMP280)
await weatherSensor.writeByte(0xf4, 0b00100101);
await weatherSensor.writeByte(0xf5, 0b00100100);
// Read temperature
const temperatureBuffer = Buffer.alloc(3);
await weatherSensor.readI2cBlock(0xfa, 3, temperatureBuffer);
const temperature = temperatureBuffer.readUIntBE(0, 3) >>> 4;
console.log(`Temperature: ${temperature}`);
};Example using custom device
import { Device } from 'async-i2c-bus';
const WEATHER_SENSOR_ADDRESS = 0x77;
class WeatherSensor {
constructor({ bus }) {
this.device = Device.create({ bus, address: WEATHER_SENSOR_ADDRESS });
}
async readTemperature() {
const buffer = Buffer.alloc(3);
await this.bus.readI2cBlock(0xfa, 3, buffer);
return buffer.readUIntBE(0, 3) >>> 4;
}
}License
Licensed under MIT.
