C# library for interacting with Pixels dice
Find a file
2025-05-19 18:09:20 -07:00
.github/workflows Update github actions 2025-05-19 18:09:20 -07:00
.idea Add JSON schema for import and add missed "Cycle" animation 2024-03-13 23:09:02 -07:00
.out/packages Add .gitkeep to preserve folder 2024-01-16 15:31:55 -08:00
BasicDiceMonitor Update to btleplug 2024-02-28 22:42:12 -08:00
CmdLine Create "anim-import" program 2024-03-13 16:45:52 -07:00
VaettirNet.PixelsDice.AnimationImport Update Readme with sample code 2025-05-19 15:02:01 -07:00
VaettirNet.PixelsDice.Net Create "anim-import" program 2024-03-13 16:45:52 -07:00
VaettirNet.PixelsDice.Net.Tests Allow setting animation profiles 2024-02-11 00:18:46 -08:00
WpfUIDice Update to btleplug 2024-02-28 22:42:12 -08:00
.gitignore Fix local/ci building 2024-01-16 15:24:54 -08:00
Directory.Build.props Disconnect die on dispose 2024-01-16 16:12:01 -08:00
LICENSE Create LICENSE 2024-01-15 11:16:00 -08:00
nuget.config Forwarded POST messages 2024-01-29 02:43:49 -08:00
Pixels.Net.sln Create "anim-import" program 2024-03-13 16:45:52 -07:00
Pixels.Net.sln.DotSettings <EnableWindowsTargeting>true</EnableWindowsTargeting> 2024-01-15 22:01:04 -08:00
Pixels.Net.sln.DotSettings.user Update Readme with sample code 2025-05-19 15:02:01 -07:00
README.md Update Readme with sample code 2025-05-19 15:02:01 -07:00

Pixels.Net

Build Status NuGet

A .NET library for connecting to and interacting with Pixels electronic dice.

Overview

Pixels.Net is a C# wrapper that allows you to connect to, monitor, and control Pixels electronic dice using Bluetooth Low Energy (BLE). The library provides an interface to communicate with Pixels dice, including reading sensor data, monitoring dice state changes, and playing animations.

Features

  • Connect to Pixels dice via Bluetooth Low Energy
  • Monitor dice state (rolling, face up, etc.)
  • Receive real-time roll results
  • Play and control LED animations
  • Import animations from Pixels Studio

Projects

The solution contains several projects:

  • VaettirNet.PixelsDice.Net: The core library for interacting with Pixels dice
  • VaettirNet.PixelsDice.AnimationImport: Tools for importing animations from Pixels Studio
  • BasicDiceMonitor: Simple console application demonstrating basic dice monitoring
  • WpfUIDice: WPF example application with UI for interacting with dice
  • CmdLine: Command-line tool for interacting with Pixels dice

Getting Started

Installation

Add the VaettirNet.PixelsDice.Net NuGet package to your project:

dotnet add package VaettirNet.PixelsDice.Net

Basic Usage

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using VaettirNet.PixelsDice.Net;
// Set log level if needed
Logger.SetLogLevel(PixelsLogLevel.Info);
// Create a PixelsManager
var mgr = await PixelsManager.CreateAsync();
// Create cancellation token for scan control
CancellationTokenSource exit = new();
Console.CancelKeyPress += (_, e) => 
{
    if (!exit.IsCancellationRequested) e.Cancel = true;
    exit.Cancel(true);
};
// Scan for dice
Console.WriteLine("Searching for dice (can help to pick up and handle them)"); List found = new(); 
// Start scanning for dice
await foreach (PixelsDie die in mgr.ScanAsync(true, cancellationToken: exit.Token))
{
    found.Add(die);
    
    // Subscribe to roll state changes
    die.RollStateChanged += (die, state, value, face) => {
        if (state == RollState.OnFace)
            Console.WriteLine($"Die {die.PixelId}: {state} face {value} (index: {face})");
    };
    
    // Subscribe to remote actions
    die.RemoteAction += (die, actionId) => {
        Console.WriteLine($"Die {die.PixelId}: remote action {actionId}");
    };
    
    // Connect to the die if not already connected
    if (!die.IsConnected)
    {
        Console.WriteLine($"ID to reconnect later: {die.GetPersistentIdentifier()}");
        Console.WriteLine("Connecting to die...");
        await die.ConnectAsync();
    }
    
    Console.WriteLine(
        $"Connected to die {die.PixelId} (color:{die.Colorway}, type:{die.Type},
    }
}
Console.WriteLine($"Found {found.Count} dice!");

// Wait for cancellation
try
{
    await Task.Delay(Timeout.Infinite, exit.Token);
} catch (OperationCanceledException) when (exit.IsCancellationRequested)
{
    Console.WriteLine("Exiting");
}
finally
{
    // Disconnect from all dice
    foreach (var die in found)
    {
        Console.WriteLine($"Disconnecting {die.PixelId}");
        await die.DisposeAsync();
    }
}

Reconnecting to Specific Dice

You can reconnect to previously discovered dice using their persistent identifiers:

// List of saved die identifiers
List<string> savedDiceIds = ["12AB34CD"]; 
// Reconnect to specific dice
await foreach (PixelsDie die in mgr.ReattachAsync(savedDiceIds, cancellationToken: exit.Token))
{
    // Handle reconnected die
    Console.WriteLine($"Reconnected to die {die.PixelId}");
}

Creating and Playing Animations

// Create simple animations
var animations = new InstantAnimationSet(
    [
        new SimpleAnimation(
            TimeSpan.FromSeconds(2),
            1,
            Color.Purple,
            1,
            0),
        new GradientAnimation(
            TimeSpan.FromSeconds(1),
            FaceMask.All,
            new RgbTrack(
                [
                    new RgbKeyFrame(Color.Red, TimeSpan.Zero),
                    new RgbKeyFrame(Color.Blue, TimeSpan.FromSeconds(0.5)),
                    new RgbKeyFrame(Color.Green, TimeSpan.FromSeconds(1)),
                ],
                FaceMask.All))]);
// Send animations to the die
await die.SendInstantAnimations(animations);
// Play an animation with index 0
await die.PlayInstantAnimationAsync(0, 1, 0);

Examples

Check the example projects for more detailed usage:

  • BasicDiceMonitor: Simple console application for monitoring dice rolls
  • WpfUIDice: Interactive WPF application with UI controls for dice

Dependencies

Documentation

For more detailed documentation, see the comments in the source code and the example applications.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements