npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

faux-i2c

v1.0.0

Published

Mock I2C interface for unit testing devices that communicate over I2C

Downloads

4

Readme

Faux I2C

Build Status Coverage Status

This module provides a mock i2c interface for unit testing. It allows intercepting and testing of device i/o.

It isn't blackmagic. It just lets you emulate I2C manually in tests, so you can confirm that the data being sent and received by your device library is working as expected.

FauxI2C is modeled after Raspi-I2C.

Installation

NPM:

npm install --save-dev faux-i2c

Yarn:

yarn add --dev faux-i2c

Usage

Replace the I2C interface in your device library with an instance of FauxI2C.

Then, you can set the onRead and onWrite handlers to intercept those processes.

For example:

"use strict";

/**
 * Example I2C-enabled device class that reads and writes something over I2C
 */
class Device {

    constructor(options) {
        this.i2c = options.i2c;
        this.address = 0x42;
    }

    readSomething(callback) {
        const register = 0x01;
        const bytes = 2;
        this.i2c.read(this.address, register, bytes, (err, buffer) => {
            // Do something with the returned bytes
            callback(err, buffer);
        });
    }

    writeSomething(bytes, callback) {
        const register = 0x01;
        const buffer = Buffer.from(bytes);
        this.i2c.write(this.address, register, buffer, (err) => {
            // Do something after the write
            callback(err);
        });
    }
}

describe('Device', () => {

    const FauxI2C = require('faux-i2c');

    it('should do some reading', (done) => {
        let readCalled = false;
        const onRead = (address, register, byteCount, callback) => {
            readCalled = true;
            
            // Here, you can confirm your device is requesting what it should be
            // then send back emulated data, letting you test every scenario
            
            address.should.be.exactly(0x42);
            register.should.be.exactly(0x01);
            byteCount.should.be.exactly(2);
            callback(null, Buffer.from([0x01, 0x02]));
        };

        const i2c = new FauxI2C({ onRead });
        const device = new Device({ i2c });

        device.readSomething((err, buffer) => {
            should(err).not.be.ok();
            should(buffer).be.ok();
            buffer.length.should.be.exactly(2);
            buffer[0].should.be.exactly(0x01);
            buffer[1].should.be.exactly(0x02);
            readCalled.should.be.exactly(true);
            done();
        });
    });

    it('should do some writing', (done) => {
        let writeCalled = false;
        const onWrite = (address, register, buffer, callback) => {
            writeCalled = true;
            
            // Here, you can confirm your device is sending what it should be
            
            address.should.be.exactly(0x42);
            register.should.be.exactly(0x01);
            buffer.length.should.be.exactly(2);
            buffer[0].should.be.exactly(0x01);
            buffer[1].should.be.exactly(0x02);
            callback(null, Buffer.from([0x01, 0x02]));
        };

        const i2c = new FauxI2C({ onWrite });
        const device = new Device({ i2c });

        device.writeSomething([0x01, 0x02], (err) => {
            should(err).not.be.ok();
            writeCalled.should.be.exactly(true);
            done();
        });
    });

});

Contributing

If you find a bug or have suggestions, please open a pull request. Contributions are greatly appreciated.

To maintain standards, please ensure that contributions pass unit tests and has complete code coverage.

To run the tests:

npm run report

This script will run the unit tests, code quality and show code coverage. If you are lacking coverage, please see coverage/index.html to narrow down where your coverage is lacking.

License

TL;DR? see: http://www.tldrlegal.com/license/mit-license

The MIT License (MIT)
Copyright (c) 2018 Kevin M. Fitzgerald

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.