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

led

v1.0.1

Published

LED control on Linux boards

Downloads

19

Readme

led

This module leverages the LED specific functionality provided by the Linux operating system to control LEDs using JavaScript. The advantage of this technique is speed as the heavy work is off-loaded to the operating system.

Linux boards often have LEDs that can be controlled from userspace. Out of the box, the Raspberry Pi 1 has one such LED labeled ACT or OK, the Raspberry Pi 3 has two such LEDs labeled ACT and PWR. The BeagleBone has four, user led 0 through 3. Some systems allow additional off-board LEDs to be added at runtime using device tree overlays.

The features supported by these LEDs varies from system to system. One system will allow the LEDs to be turned on and off while the next system will support additional fetaures such as heartbeat or hardware accelerated blinking.

Contents

Installation

npm install led

Usage

BeagleBone Black

Blink all user LEDs on the BeagleBone Black five times a second

var Led = require('led');

['usr0', 'usr1', 'usr2', 'usr3'].forEach(function (name) {
  new Led('beaglebone:green:' + name).blink(100, 100);
});

Heartbeat all user LEDs leds on the BeagleBone Black

var Led = require('led');

['usr0', 'usr1', 'usr2', 'usr3'].forEach(function (name) {
  new Led('beaglebone:green:' + name).heartbeat();
});

Blip all user LEDs on the BeagleBone Black once every two seconds

var Led = require('led');

['usr0', 'usr1', 'usr2', 'usr3'].forEach(function (name) {
  new Led('beaglebone:green:' + name).blink(1, 1999);
});

Raspberry Pi

Turn the ACT LED on the Raspberry Pi on for one second

var Led = require('led'),
  led = new Led('ACT');

led.on();

setTimeout(function () {
  led.off();
}, 1000);

Blink ACT and PWR LEDs on the Raspberry Pi five times a second

var Led = require('led');

['ACT', 'PWR'].forEach(function (name) {
          new Led(name).blink(100, 100);
});

Heartbeat ACT and PWR LEDs on the Raspberry Pi

var Led = require('led');

['ACT', 'PWR'].forEach(function (name) {
          new Led(name).heartbeat();
});

Notes

Although it may not be immediately obvious, the LEDs on the BeagleBone Black or Raspberry Pi will continue to blink, heartbeat, and blip after the corresponding programs have terminated. All the heavy work involved in controlling the LEDs has been off-loaded to the operating system and the number of CPU cycles required to control the LEDs is minimized.

API

Led(name) Returns a new Led object which can be used to control the LED with the specified name. The name to use for a particular LED is the name of the corresponding directory in /sys/class/leds. Examples are ACT on the Raspberry Pi and beaglebone:green:usr0, beaglebone:green:usr1, beaglebone:green:usr2, and beaglebone:green:usr3 on the BeagleBone or BeagleBone Black.

on() Turn the LED on.

off() Turn the LED off.

heartbeat() Heartbeat the LED.

blink(delayOn, delayOff) Blink the LED. delayOn and delayOff specify the on and off time in milliseconds.

delayOn(val) Modify the on time for a blinking LED to the specified value in milliseconds.

delayOff(val) Modify the off time for a blinking LED to the specified value in milliseconds.

How Does It Work?

Linux systems often have files representing LEDs that appear in /sys/class/leds. Such LEDs can be controlled by writing the appropriate values to the appropriate files. More information can be found here

Example - Device Tree Overlay

The example directory contains a device tree overlay called myled.dto which can configure P9_12 on the BeagleBone Black as a pin for controlling an LED from userspace. The script myled performs all the necessray setup and after everything has been setup correctly, there should be an LED called 'my:red:led' in /sys/class/leds.

The following program can be used to let 'my:red:led' glow dimly for a second and then brighlty.

var Led = require('led'),
  led = new Led('my:red:led'),
  dim = true;

led.blink(1, 9);

setInterval(function () {
  if (dim) {
    led.delayOn(9);
    led.delayOff(1);
  } else {
    led.delayOn(1);
    led.delayOff(9);
  }

  dim = !dim;
}, 1000);