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

ledctl

v0.2.2

Published

Control your LEDs from JavaScript or using terminal

Downloads

4

Readme

ledctl

NPM Version Build Status Coverage Status Built with GNU Make

Control your LEDs from Node.js

This module allows you to take control of your LEDs on whatever board you are running it, as long as the board has some LEDs that can be controlled (and the OS is aware of them).

I wanted to push my limits a little so even though it seems a trivial task to turn a light on/off, I implemented this thing with almost scientific precision - floating-point timing accuracy, race condition prevention, filesystem operations queueing, excellent error handling etc. but not at the cost of usability and simplicity. Simply put, I had a blast writing this and I hope you enjoy it, too! :)

Currently you can:

  • [x] Discover available LEDs on your system
  • [x] Turn on/off the LEDs
  • [x] Change the LED's trigger settings
  • [x] Blink the LEDs
  • [x] Make your LEDs blink in morse code!
  • [ ] Monitor the progress of the blink events via event listeners (considering)
  • [ ] Control your LEDs from the terminal (planned)

Installation Instructions

Install the module via npm: npm install ledctl

Requirements

Currently only Linux-based operating systems are known to be supported. PRs are always welcome if you discover more operating systems where LEDs are exposed via the filesystem (usually in /sys/class/leds but this can be overriden if the OS uses a different path).

I am aware of Raspberry-Pi, Banana-Pi and BeagleBone having user-controllable LEDs, but I am quite sure there's many more.

Usage

Here's a crash course. I suggest you read the API docs afterwards - it contains all the information you may need to work with this library.

A quick hint - all callbacks are optional. However, if there's a problem writing to the LED's sysfs filesystem the function will throw on you.

Great benefit of this library is that you can simply chain calls to the LED however you wish and all the blinks will be queued and processed in the order you wanted - there's no need to introduce a callback hell just to blink out SOS in morse code.

var LEDController = require('ledctl')

// Which LEDs can be controlled?
console.log(LEDController.discover())

var led = new LEDController('green')
led.turnOn() // It should be on!
led.blink() // It should blink for 500ms by default
led.blink(
// Everything below is optional - these values represent defaults
// (except rate - rate defaults to 1)
  { for: 50 // percent of total blink time
    of: 1000 // milliseconds (total blink time)
    rate: 2 // Make it all twice as fast
  }
)

// Holy grail
led.morse('sos') // Watch the awesomeness!

Chaining methods and blinks

Methods are chainable. Also, my primary goal was to make an asynchronous implementation of LED control but without the usual callback hell associated with such implementation. As such, all the callbacks are optional (but they will throw on you if there's a problem, so be careful - better use a domain in critical production systems... Wait, what? A LED-controlling library in a production-critical system?)

// Let's control the green led...
var led = new LEDController('green')

// Let's make it blink out SOS in morse code, then turn
// it off for 5 seconds and then set its trigger to mmc0
// (so it will blink on SD card activity)
led
  .morse('sos')
  // Read: blink for zero percent of 5000 milliseconds
  .blink({ for: 0, of: 5000 })
  .trigger('mmc0')

Notice that the LED will process the events exactly in the order as you called the functions.

Emptying the blink queue

Sometimes it might be necessary to cancel any scheduled blink events and put the LED into an idle (off) state - to do that, simply call .reset():

led.morse('no one will see me :(').reset()

However, any callbacks associated with the cancelled blink events will be cancelled, too. Any blink events that you schedule afterwards will be processed as usual.

Extending the API

You may want to implement your own method that takes arbitrary input and generates (or not) photons via your LED! For example, you may want to take a Buffer and turn the LED on every time the bits change... Or take a string and blink when there's a vowel... Or whatever - you can do it all!

It's rather simple - first, you need to write your serialiser. A serialiser takes one argument - an input value (this is what should be converted to blink events - most likely some kind of string) and returns an array of Blink objects. A Blink object describes how long a LED should be on and how long it should be off during a single event.

I highly suggest that you take a look at how the morse code method is implemented in serialisers/morse.js.

Documentation

Documentation is available here.

To generate documentation locally, run make docs from the repository's root.

License

This software is licensed under the BSD-3-Clause License. See the LICENSE file for more information.