Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alejandroherr/async-i2c-bus
Bus and Device classes for i2c-bus, with promised functions.
https://github.com/alejandroherr/async-i2c-bus
i2c i2c-bus i2c-device i2c-display i2c-interface i2c-protocol i2c-sensors i2c-slave raspberry-pi raspberrypi
Last synced: about 2 months ago
JSON representation
Bus and Device classes for i2c-bus, with promised functions.
- Host: GitHub
- URL: https://github.com/alejandroherr/async-i2c-bus
- Owner: AlejandroHerr
- License: mit
- Created: 2018-12-09T19:08:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-28T07:48:57.000Z (7 months ago)
- Last Synced: 2024-05-29T19:59:52.236Z (7 months ago)
- Topics: i2c, i2c-bus, i2c-device, i2c-display, i2c-interface, i2c-protocol, i2c-sensors, i2c-slave, raspberry-pi, raspberrypi
- Language: TypeScript
- Homepage: https://async-i2c-bus.alejandroherr.io
- Size: 958 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 40
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
async-i2c-bus
Bus and Device classes for i2c-bus, with promised functions.
Bus and Device classes for [i2c-bus](https://www.npmjs.com/package/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.
[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/dark.png)](#table-of-contents)
## Table of Contents
* [Installation](#installation)
* [Usage](#usage)
* [Bus](#bus)
* [Device](#device)
* [BusError](#buserror)
* [Example](#example)
* [Example using custom device](#example-using-custom-device)
* [License](#license)[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/dark.png)](#installation)
## Installation
```javascript
yarn add async-i2c-bus// or
npm i async-i2c-bus
```[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/dark.png)](#usage)
## Usage
The package exports the `Bus`, `Device` and `BusError` classes.
You can check the [full documentation](https://async-i2c-bus.alejandroherr.io/)
### 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`
| 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
```javascript
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
```javascript
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;
}
}
```[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/dark.png)](#license)
## License
Licensed under [MIT](https://opensource.org/licenses/MIT).