Coder Social home page Coder Social 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.

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.