pixels-library

Connect to Pixels dice using Web Bluetooth.

Usage no npm install needed!

<script type="module">
  import pixelsLibrary from 'https://cdn.skypack.dev/pixels-library';
</script>

README

Pixels Web Package

This is the Pixels web package for front end web developers. It enables communications between Pixels dice and a web browser using Bluetooth Low Energy.

This package relies on the Web Bluetooth API for accessing Bluetooth. At the time of writing, only Chrome, Edge and Opera browsers have support for these APIs.

Currently all testing is done with Chrome on Windows 10. You may need to first set this flag to enable the Bluetooth API: chrome://flags/#enable-experimental-web-platform-features.

Please open a ticket in GitHub if you're having any issue.

This package is based on https://github.com/tomchen/example-typescript-package.

Getting Started

To install the package:

npm i pixels-library

Start by calling the requestPixel function to request the user to select a Pixel die to connect to.

This function returns a Pixel object. Call the connect asynchronous method to initiate a Bluetooth connection with the die.

The Pixel class has a number of methods to retrieve information about the die state. It also let you add a listener for any Pixel message (as defined in MessageTypeValues).

Here is simple code example:

// Ask user to select a Pixel
const pixel = await requestPixel();
log('Connecting...')
await pixel.connect();

// Get some info
const rollState = await pixel.getRollState();
log(`=> roll state: ${rollState.state}, face ${rollState.face}`);
const battery = await pixel.getBatteryLevel();
log(`=> battery: ${battery.level}, ${battery.voltage}v`);
const rssi = await pixel.getRssi();

// Add listener to get notified when the Pixel roll state changes
pixel.addMessageListener(MessageTypeValues.RollState, (evt: Event) => {
    const msg = (evt as CustomEvent).detail as RollState
    log(`=> roll state: ${msg.state}, face ${msg.face}`)

Module documentation

See the module's export documentation here.

Documentation is generated with TypeDoc.