{"id":16526176,"url":"https://github.com/alejandroherr/i2c-bus-promised","last_synced_at":"2025-07-16T06:40:14.050Z","repository":{"id":57270503,"uuid":"118325500","full_name":"AlejandroHerr/i2c-bus-promised","owner":"AlejandroHerr","description":"Bus and Device classes for i2c-bus, with promised functions.","archived":false,"fork":false,"pushed_at":"2019-04-03T08:28:11.000Z","size":507,"stargazers_count":1,"open_issues_count":14,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-13T12:51:23.809Z","etag":null,"topics":["i2c","i2c-bus","i2c-device","i2c-display","i2c-interface","i2c-master","i2c-sensors","promise-functions"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlejandroHerr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-21T10:39:34.000Z","updated_at":"2018-07-12T02:38:38.000Z","dependencies_parsed_at":"2022-09-02T01:02:57.435Z","dependency_job_id":null,"html_url":"https://github.com/AlejandroHerr/i2c-bus-promised","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/AlejandroHerr/i2c-bus-promised","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlejandroHerr%2Fi2c-bus-promised","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlejandroHerr%2Fi2c-bus-promised/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlejandroHerr%2Fi2c-bus-promised/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlejandroHerr%2Fi2c-bus-promised/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlejandroHerr","download_url":"https://codeload.github.com/AlejandroHerr/i2c-bus-promised/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlejandroHerr%2Fi2c-bus-promised/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265488936,"owners_count":23775212,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["i2c","i2c-bus","i2c-device","i2c-display","i2c-interface","i2c-master","i2c-sensors","promise-functions"],"created_at":"2024-10-11T17:26:23.882Z","updated_at":"2025-07-16T06:40:13.857Z","avatar_url":"https://github.com/AlejandroHerr.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# i2c-bus-promised [![CircleCI](https://circleci.com/gh/AlejandroHerr/i2c-bus-promised.svg?style=svg)](https://circleci.com/gh/AlejandroHerr/i2c-bus-promised) [![Greenkeeper badge](https://badges.greenkeeper.io/AlejandroHerr/i2c-bus-promised.svg)](https://greenkeeper.io/)\n\n\u003c!--@shields.flatSquare('deps','devDeps')--\u003e\n[![dependency status](https://img.shields.io/david/AlejandroHerr/i2c-bus-promised/master.svg?style=flat-square)](https://david-dm.org/AlejandroHerr/i2c-bus-promised/master) [![devDependency status](https://img.shields.io/david/dev/AlejandroHerr/i2c-bus-promised/master.svg?style=flat-square)](https://david-dm.org/AlejandroHerr/i2c-bus-promised/master#info=devDependencies)\n\u003c!--/@--\u003e\n\nBus and Device classes for [i2c-bus](https://github.com/fivdi/i2c-bus), with promised functions.\n\n\u003c!--@installation()--\u003e\n## Installation\n\n```sh\nnpm install --save i2c-bus-promised\n```\n\u003c!--/@--\u003e\n\n## Usage\n\nThere's two main class exports in this library: `Bus` and `Device`.\n\n`Bus` class wraps original `i2c-bus` methods and returns them promised, to make more comofrtable to work with them. The calls are queued to avoid blocking calls to the i2c bus.\n`Device` class abstracts an `i2c` device.The class is initialised with two arguments, `bus` and `device address`. Internally it will call the reciprocal methods on the bus object with it's address.\n\n\u003c!--@snippet('./examples/usage.js', { showSource: true })--\u003e\n```js\n// @flow\n/* eslint-disable no-console */\nimport { Bus, Device } from '../src';\nimport type { AddrType, WordType } from '../src/types';\n\nconst main = async () =\u003e {\n  const bus = new Bus();\n  await bus.open();\n\n  // Print bus info\n  await Promise.all([\n    bus.i2cFuncs().then((funcs: {[string]: number}) =\u003e console.log(JSON.stringify(funcs, null, 2))),\n    bus.scan().then((devices: Array\u003cAddrType\u003e) =\u003e console.log(JSON.stringify(devices, null, 2))),\n  ]);\n\n  // Initizalize devices\n  const weatherSensor = new Device(bus, 0x77);\n  const lightSensor = new Device(bus, 0x1d);\n\n  await weatherSensor.writeByte(0x23, 0b1 | 0b100);\n  await lightSensor.writeByte(0x25, 0x0f);\n\n  return Promise.all([\n    weatherSensor.readWord(0x50),\n    weatherSensor.readWord(0x52),\n    lightSensor.readWord(0x30),\n  ])\n    .then(([temperature, pressure, light]: Array\u003cWordType\u003e) =\u003e {\n      console.log(`The temperature is ${temperature}`);\n      console.log(`The pressure is ${pressure}`);\n      console.log(`The light is ${light}`);\n    });\n};\n\nmain()\n  .then(() =\u003e process.exit(0))\n  .catch((error: Error) =\u003e {\n    console.error(error.message);\n    process.exit(1);\n  });\n```\n\n\u003e File [./examples/usage.js](./examples/usage.js)\n\u003c!--/@--\u003e\n\nFor more information, consult the [API docs](https://github.com/AlejandroHerr/i2c-bus-promised/blob/master/API.md)\n\n### Extending Device\n\nHowever, you can also extend the `Device` to work with your i2c devices:\n\n\u003c!--@snippet('./examples/extendingDevice.js', { showSource: true })--\u003e\n```js\n// @flow\n/* eslint-disable no-console */\nimport { Bus, Device } from '../src';\nimport type { ByteType, WordType } from '../src/types';\n\nclass WeatherSensor extends Device {\n  constructor(bus: Bus) {\n    super(bus, 0x72);\n  }\n\n  writeConfig(config: ByteType) {\n    return this.writeByte(0x24, config);\n  }\n\n  readTemperature() {\n    return this.readWord(0x50);\n  }\n\n  readPressure() {\n    return this.readWord(0x52);\n  }\n}\n\nconst main = async () =\u003e {\n  const bus = new Bus();\n  const weatherSensor = new WeatherSensor(bus);\n\n  await bus.open();\n\n  await weatherSensor.writeConfig(0b1 | 0b100);\n\n  return Promise.all([\n    weatherSensor.readTemperature(),\n    weatherSensor.readPressure(),\n  ])\n    .then(([temperature, pressure]: Array\u003cWordType\u003e) =\u003e {\n      console.log(`The temperature is ${temperature}°C`);\n      console.log(`The pressure is ${pressure}Pa`);\n    });\n};\n\nmain()\n  .then(() =\u003e process.exit(0))\n  .catch((error: Error) =\u003e {\n    console.error(error.message);\n    process.exit(1);\n  });\n```\n\n\u003e File [./examples/extendingDevice.js](./examples/extendingDevice.js)\n\u003c!--/@--\u003e\n\nFor more information, consult the [API docs](https://github.com/AlejandroHerr/i2c-bus-promised/blob/master/API.md#device)\n\n## Tests\n\nTo run the tests just type:\n\n```bash\nyarn test\n```\n\nHowever it is recomended to run the `e2e` tests to ensure that everything works well.\n\n```bash\nBUS_NUMBER=1 yarn test:e2e\n```\n\n### mock/createI2cBus\n\n`createI2cBus` is a helper bus to be able to test your code using the library, and ensure that your code is doing what you want.\n\n\u003c!--@snippet('./examples/tests.js', { showSource: true })--\u003e\n```js\nimport { Bus } from '../src';\n\nconst BUS_NUMBER = 1;\n\njest.mock('i2c-bus', () =\u003e {\n  const createI2cBus = require('../src/mock/createI2cBus').default; // eslint-disable-line global-require\n\n  return createI2cBus({\n    busNumber: 1,\n    devices: {\n      0x0f: Buffer.from(Array.from(Array(0xff).keys()).reverse()),\n      0xf0: Buffer.from(Array.from(Array(0xff).keys())),\n    },\n    funcs: {\n      read: 0x01,\n      write: 0x02,\n    },\n  });\n});\n\nconst setup = async (busNumber) =\u003e {\n  const bus = new Bus(busNumber);\n\n  await bus.open();\n\n  return {\n    bus,\n    addToQueue: jest.spyOn(bus, 'addToQueue'),\n    physicalBus: bus.bus.physicalBus,\n  };\n};\n\ndescribe('Bus', () =\u003e {\n  describe('i2cFuncs', () =\u003e {\n    it('calls the i2cBus function through the promise queue', async () =\u003e {\n      const { bus, addToQueue, physicalBus } = await setup(BUS_NUMBER);\n\n      const result = await bus.i2cFuncs();\n\n      expect(addToQueue).toHaveBeenCalledWith('i2cFuncsAsync');\n      expect(result).toBe(physicalBus.funcs);\n    });\n  });\n  describe('scan', () =\u003e {\n    it('calls the i2cBus function through the promise queue', async () =\u003e {\n      const { bus, addToQueue, physicalBus } = await setup(BUS_NUMBER);\n\n      const result = await bus.scan();\n\n      expect(addToQueue).toHaveBeenCalledWith('scanAsync');\n      expect(result).toEqual(Object.keys(physicalBus.devices).map(addr =\u003e parseInt(addr, 10)));\n    });\n  });\n});\n```\n\n\u003e File [./examples/tests.js](./examples/tests.js)\n\u003c!--/@--\u003e\n\nBe aware that you're working with a real device, the tests won't be much meaningful. And many things can fail _IRL_. It's always recommendable to write `e2e` tests to run in a real device.\n\n\u003c!--@dependencies()--\u003e\n## \u003ca name=\"dependencies\"\u003eDependencies\u003c/a\u003e\n\n- [bluebird](https://github.com/petkaantonov/bluebird): Full featured Promises/A+ implementation with exceptionally good performance\n- [i2c-bus](https://github.com/fivdi/i2c-bus): I2C serial bus access with Node.js\n- [p-queue](undefined): Promise queue with concurrency control\n\n\u003c!--/@--\u003e\n\n## Documentation\n\n[Read the API](https://github.com/AlejandroHerr/i2c-bus-promised/blob/master/API.md)\n\n\u003c!--@license()--\u003e\n## License\n\nMIT © Alejandro Hernandez\n\u003c!--/@--\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandroherr%2Fi2c-bus-promised","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falejandroherr%2Fi2c-bus-promised","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandroherr%2Fi2c-bus-promised/lists"}