GithubHelp home page GithubHelp logo

alejandroherr / async-i2c-bus Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 981 KB

Bus and Device classes for i2c-bus, with promised functions.

Home Page: https://async-i2c-bus.alejandroherr.io

License: MIT License

JavaScript 3.00% TypeScript 96.59% Shell 0.40%
i2c i2c-sensors i2c-bus i2c-device i2c-interface i2c-display i2c-slave i2c-protocol raspberrypi raspberry-pi

async-i2c-bus's Introduction

async-i2c-bus

Downloads per month NPM Version Dependencies Contributors CircleCI codecov Commitizen friendly semantic-release

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-bus

-----------------------------------------------------

Usage

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.

async-i2c-bus's People

Contributors

alejandroherr avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

reactual

async-i2c-bus's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm codecov Unavailable
npm npm-run-all Available

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency npm-run-all with npm-run-all2 5.0.0
  • fix(deps): update dependency @types/node to v12.20.55
  • fix(deps): update dependency i2c-bus to v5.2.3
  • fix(deps): update dependency typescript to v3.9.10
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-config-airbnb-typescript to v18
  • chore(deps): update dependency eslint-config-prettier to v9
  • chore(deps): update dependency eslint-plugin-prettier to v5
  • chore(deps): update dependency node to v20 (node, @types/node)
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency typedoc-plugin-markdown to v4
  • fix(deps): update dependency typescript to v5
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @types/node ^12.12.8
  • i2c-bus ^5.1.0
  • typescript ^3.7.2
  • @appnest/readme 1.2.5
  • @types/jest 24.9.1
  • @typescript-eslint/eslint-plugin 2.23.0
  • @typescript-eslint/parser 2.23.0
  • codecov 3.6.5
  • cz-conventional-changelog 3.0.2
  • eslint 6.8.0
  • eslint-config-airbnb-typescript 6.3.1
  • eslint-config-prettier 6.7.0
  • eslint-plugin-import 2.19.1
  • eslint-plugin-prettier 3.1.2
  • jest 24.9.0
  • npm-run-all 4.1.5
  • prettier 1.19.1
  • semantic-release 15.14.0
  • ts-jest 24.2.0
  • typedoc 0.15.5
  • typedoc-plugin-markdown 2.2.14
  • node >=8.10.0
nvm
.nvmrc
  • node 10.18.0

  • Check this box to trigger a request for Renovate to run again on this repository

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.