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

sainsmart-16-hid

v0.9.1

Published

Control the Sainsmart 16-relay USB device from node.

Downloads

8

Readme

sainsmart-16-hid

Control the Sainsmart 16 relay USB HID device from node.js.

Alpha

I'll be using this module to create an irrigation controller. Until I'm satisfied with its meatspace operation, this should be considered alpha software!

Usage

$ npm install sainsmart-16-hid


const Relay = require('sainsmart-16-hid');

let relay = new Relay();

// enable relays 0 and 4
relay.write(0b0000000000010001);
// enable relays 14 and 15
relay.write(Math.pow(2, 14) | Math.pow(2, 15));
// leave 14 & 15 enabled, but enable relay 5
relay.set(5, true);
// check to see if relay 14 was enabled
(Math.pow(2, 14) & ~relay.state) === 0;
// or
relay.stateArray[14] === true;
// turn off relay 14
relay.set(14, false);

Keep in mind that constructing a Relay object will send a reset command to the module and set all relays to zero. This is so that your control module can begin in a known state in the event of a crash or restart. If you need to access the state of the usb device when set by another piece of software, consider another library.

API

new Relay([path])

Path is optional, and can be used to specify a specific device for node-hid. If not specified, the first Sainsmart 16-relay USB HID device found will be used.

relay.state

16-bit integer representation of the state of the relays. 0b0000000000000001 means relay 0 is enabled, 0b1000000000000000 means relay 15 is enabled, etc.

relay.stateArray

Another representation of the relay states, an array of booleans.

relay.stateArray // relays 0, 4, and 15 are enabled
> [true, false, false, false, true, false, false, false, true, false, false, false, false, false, false, true];

relay.read(callback)

Sends a read command to the USB device. callback(err, data) (required) will include any error and data will be the 16-bit integer representation of the state of the relays. relay.state and relay.stateArray will be updated when this function completes. Note that you shouldn't need this function. Each time write or set is called this information can be accessed from relay.state or relay.stateArray.

relay.write(state)

Used to change the state of all the relays on the device. For example:

// turn on relays 0, 3, and 14. everything else off.
relay.write(0b0100000000001001);
// turn everything off except relays 5 & 6
relay.write(Math.pow(2, 5) | Math.pow(2, 6));

relay.set(relay, state)

Turn a specific relay on or off, without affecting the state of other relays.

relay.write(Math.pow(2, 1) | Math.pow(2, 15));
relay.set(15, false);
relay.set(14, true);
relay.stateArray
> [false, true, false, false, false, false, false, false, false, false, false, false, false, true, false]

Credit

All credit goes to mvines/relay for documenting the protocol for communicating with this device. I just made an API that fits my needs.