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

sense-hat-web-emu

v2.1.0

Published

Web emulator for Sense HAT applications

Downloads

3

Readme

Sense HAT Web Emulator

npm version

This is a web emulator for Sense HAT applications. You may use it to preview your Sense HAT applications locally, thus testing they work correctly before you push them to your Raspberry Pi.

The web emulator runs your application using your local Node.js but if you have nodemon installed, it will use nodemon instead; this way you can have the emulator running and reloading the browser whenever you save your code files.

When you start the web emulator, it will automatically open a browser tab for you. The SPA running in the browser communicates with your application via sockets.

Installation

Add sense-hat-web-emu as a development dependency of your project with

npm install sense-hat-web-emu --save-dev

or

yarn add sense-hat-web-emu --dev

Usage

The web emulator basically provides a replacement for the sense-joystick and sense-hat-leds npm packages.

Most probably your Sense HAT application looks something like this:

const senseJoystick = require('sense-joystick')
const senseLeds = require('sense-hat-led')

// your application code which uses senseJoystick and senseLeds

Ideally, in order to use the web emulator you would modularize your code so that your application's entry point receives senseJoytick and senseLeds as parameters. This not only makes it easier to use the web emulator, it also makes your application easier to test.

Your entry file would look something like this:

module.exports = (senseJoystick, senseLeds) => {
  // your application code here
}

Here's an example of what an application's entry file could look like:

module.exports = (senseJoystick, senseLeds) => {
  senseJoystick.getJoystick().then((joystick) => {
    const _ = [0, 0, 0] // black color
    const R = [255, 0, 0] // red color
    
    joystick.on('press', val => {
      if (val === 'click') {
        senseLeds.setPixels([
          _, _, _, _, _, _, _, _,
          _, R, R, _, _, R, R, _,
          R, R, R, R, R, R, R, R,
          R, R, R, R, R, R, R, R,
          _, R, R, R, R, R, R, _,
          _, _, R, R, R, R, _, _,
          _, _, _, R, R, _, _, _,
          _, _, _, _, _, _, _, _
        ])
      }      
    })
  })
}

When running the Sense HAT web emulator, it will automatically inject the emulator versions of senseJoystick and senseLeds into your applications entry point.

Now you can add a script in your package.json file

"scripts": {
  "webemu": "webemu start --file ./path/to/file.js"
}

And you'd either run it with:

npm run webemu

or

yarn webemu

Alternatively, if you don't have a package.json file or don't want to add a script to it, you can also run:

./node_modules/.bin/webemu start --file ./src/test-app.js

Options

These are the option flags you can provide to the web emulator:

| Flag | Description | Default value | Required | |--------|----------------------------------------------------------------------------|---------------|----------| | file | Relative path to your application's entry file | - | Yes | | launch | Specify whether to launch the browser when the web emulator starts | False | No | | port | Specify the port where the web emulator will start | 3000 | No | | watch | Specify whether to start the web emulator in watch mode (requires nodemon) | False | No |

Examples:

./node_modules/.bin/webemu start --file ./src/test-app.js --port 4000

./node_modules/.bin/webemu start --file ./src/test-app.js --launch --watch

Controls

Once your Sense HAT application is running, use your keyboard to control the Sense HAT joystick. Controls are as follows:

  • UP: Up arrow ↑
  • RIGHT: Right arrow →
  • DOWN: Down arrow ↓
  • LEFT: Left arrow ←
  • CLICK: Enter key ↩

Demo

Check out this running demo!

The commands for the demo application are the following:

  • Pressing Up arrow ↑ displays the message hello world! one letter at a time
  • Pressing Right arrow → scrolls the message hello world!
  • Pressing Down arrow ↓ flips the 8x8 LED matrix vertically
  • Pressing Left arrow ← loads an 8x8 pixel image of a space invader
  • Pressing Enter key displays the next shape in the collection (heart️ ❤️ / smiley 😀 / earth 🌎 / star ⭐️ / check ✔️)

Here's the source code for this Sense HAT application.

Disclaimer

The web emulator runs your Sense HAT Node application on the server side and communicates with the browser through sockets. Because of the asynchronous nature of this communication, an application written using the synchronous version of senseLeds methods will not work correctly. If you're willing to use the web emulator and it's not imperative for you to use synchronous methods, I'd recommend using their async version. Otherwise, I'm truly sorry and also accepting contributions to work around this limitation.

Documentation

Thanks

Thank you for trying the Sense HAT web emulator!

Your feedback is very welcome, and your contributions even more so!