GoDice JavaScript API (with demo)

Overview

GoDice JavaScript API (with demo)

Overview

Use the GoDice JavaScript API to integrate GoDice functionality into your own JavaScript applications.

Here are some of the things that you can do with the GoDice JavaScript API:

  • Turn ON/OFF GoDice RGB Leds
  • Ask for the die Color (dots color)
  • Ask for the die battery level
  • Get differernt notifications reagrding the die state (Rolling or Stable and get the outcome number)

To run the demo (that use the API) just open the index.html file in Chrome or Microsoft Edge browser

Getting Started

  1. Create a web page and include the GoDice JavaScript API file (godice.js):

    <script src="godice.js"></script>
  2. In your main JavaScript file create a new instance of the GoDice class: goDice = new GoDice();

  3. To open the Bluetooth connection dialog and connect to your GoDice, call the following funtion: goDice.requestDevice();

  4. After successfull connection the function onDiceConnected will be called from the GoDice class

API Functions

There are few types of API calls:

  1. Message - a message to the die
  2. Request - a message to the die, that should follow by corresponding response.
  3. Response - an event that is expected after sending the request
  4. Events - a message sent by the die

Once you followed the "Getting Started" section and you have the goDice instance ready you can call to the following class functions:

Message:

/**
 * Turn On/Off RGB LEDs (the LEDs will turn off if both led1 and led2 are null or set to [0,0,0])
 * @param {Array} led1 - an array to control the 1st LED in the following format '[R,G,B]'
 *                       where R,G and B are numbers in the range of 0-255
 * @param {Array} led2 - an array to control the 2nd LED in the following format '[R,G,B]'
 *                       where R,G and B are numbers in the range of 0-255
 */                          
 setLed(led1, led2);

Requests:

 /**
  * Open a browser connection dialog to connect a single GoDice, after successfull connection it will followed 
  * by corresponding "onDiceConnected" callback function (response) from the GoDice class (instance).
  */	
  requestDevice();
 /**
  * Request for the die color, that should follow by corresponding "onDiceColor" callback function (response) 
  * from the GoDice class (instance).
  */
  getDiceColor(){
 /**
  * Request for the die battery, that should follow by corresponding "onBatteryLevel" callback function (response)
  * from the GoDice class (instance).
  */
  getBatteryLevel()

Responses:

/**
  * To recognize connected die, override the function "onDiceConnected" in the GoDice class, with the following parameter:
  * @param {string} diceID - the die unique identifier	 
  * @param {GoDice class} diceInstance - the die class instance	 
  */

// example:
  
GoDice.prototype.onDiceConnected = (diceId, diceInstance) => {  
  // die unique identifier
  let dieIdentifier = diceID;
	
  // die unique identifier
  let dieClass = diceInstance;		
};
/**
 * To recognize the battery level, override the function "onBatteryLevel" in the GoDice class, with the following parameter:
 * @param {string} diceID - the die unique identifier	 
 * @param {string} level - the battery level of this diceID 
 */
 
 //example:
 
 GoDice.prototype.onBatteryLevel = (diceId, batteryLevel) => {
   // die unique identifier
     let dieIdentifier = diceID;
     
   // battery level
   let dieBatLevel = batteryLevel; 
 };
 /**
  * To recognize the color of the die, override the function "onDiceColor" in the GoDice class, with the following parameter:
  * @param {string} diceID - the die unique identifier	 
  * @param {string} color - the color of the die (corresponding to the diceID). 
  * COLOR_BLACK		0
  * COLOR_RED		1
  * COLOR_GREEN		2
  * COLOR_BLUE		3
  * COLOR_YELLOW	4
  * COLOR_ORANGE	5
  */
  
  // example:
  
  GoDice.prototype.onDiceColor = (diceId, color) => {
    // die unique identifier
    let dieIdentifier = diceID;
	  
    // die color
    let dieColor = color; 
  };

Events:

 /**
  * When the die is stable after a legit roll the function "onStable" will be called from the GoDice class with the following parameter:
  * @param {string} diceId - the die unique identifier	 
  * @param {string} value - the D6 outcome value (1-6)
  * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z 
  */
  
  // example:
  
  GoDice.prototype.onStable = (diceId, value, xyzAccRaw) => {
      // die unique identifier
      let dieIdentifier = diceID;
	  
      // the D6 outcome value (1-6)
      let dieValue = value;
	  
      // the acc raw values x,y,z
      let accX = xyzAccRaw[0];
      let accY = xyzAccRaw[1];
      let accZ = xyzAccRaw[2];
  };
 /**
  * When the die is stable (but not flat) after a legit roll the function "onTiltStable" will be called from the GoDice class with the following parameter:
  * @param {string} diceId - the die unique identifier	 
  * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z 
  */
  
  // example:
  
  GoDice.prototype.onTiltStable = (diceId, xyzAccRaw) => {
      // die unique identifier
      let dieIdentifier = diceID;

      // the acc raw values x,y,z
      let accX = xyzAccRaw[0];
      let accY = xyzAccRaw[1];
      let accZ = xyzAccRaw[2];
  };
 /**
  * When the die is stable after a "fake" roll the function "onFakeStable" will be called from the GoDice class with the following parameter:
  * @param {string} diceId - the die unique identifier	 
  * @param {string} value - the D6 outcome value (1-6)
  * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z 
  */
  
  // example:
  
  GoDice.prototype.onFakeStable = (diceId, value, xyzAccRaw) => {
      // die unique identifier
      let dieIdentifier = diceID;
	  
      // the D6 outcome value (1-6)
      let dieValue = value;
	  
      // the acc raw values x,y,z
      let accX = xyzAccRaw[0];
      let accY = xyzAccRaw[1];
      let accZ = xyzAccRaw[2];
  };
/**
 * When the die is stable after a small movement (rotating from one face to different face) the function "onMoveStable" will be called from the GoDice class with the   * following parameter:
 * @param {string} diceId - the die unique identifier	 
 * @param {string} value - the D6 outcome value (1-6)
 * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z 
 */
 
 // example:
 
 GoDice.prototype.onMoveStable = (diceId, value, xyzAccRaw) => {
     // die unique identifier
     let dieIdentifier = diceID;
     
     // the D6 outcome value (1-6)
     let dieValue = value;
     
     // the acc raw values x,y,z
     let accX = xyzAccRaw[0];
     let accY = xyzAccRaw[1];
     let accZ = xyzAccRaw[2];
 };
You might also like...

Demo repo used in crash course for students learning web development.

Web Development Crash Course Hosted by Rubberdøk Preparation Following these steps prepares you to join the interactive React demo of the crash course

Sep 20, 2022

A demo to show how to re-use Eleventy Image’s disk cache across Netlify builds.

Re-use Eleventy Image Disk Cache across Netlify Builds Live Demo This repository takes all of the high resolution browser logos and processes them thr

Apr 5, 2022

A repository of a Cypress testing demo

adeoweb-cypress-demo This is a repository of a Cypress testing demo. Based on adeoweb.biz website case study. What is this talk We will 👀 live-code a

Mar 24, 2022

Minze JS framework demo for codrops article.

Minze JS framework demo for codrops article.

Minze Demo Demo of a fictive Smart Home dashboard application. Article on Codrops Demo Running The application only needs a webserver to run. Install

Dec 31, 2022

Elven Tools Dapp - Elrond blockckchain frontend dapp demo. Primarily for NFT minting, but it can be used for other purposes.

Elven Tools Dapp Docs: elven.tools/docs/landing-page.html Demo: dapp-demo.elven.tools Sneak peek: youtu.be/ATSxD3mD4dc The Dapp is built using Nextjs

Jan 1, 2023

B-OS Demo Application

B-OS Demo Application Technology stack: Node.js, Metarhia Database management system: PostgreSQL Frontend stack: vanilla.js Concept Feature list Serve

Sep 9, 2022

The Space Pirates game is a demo made to celebrate the Babylon.js 5.0 Release.

The Space Pirates game is a demo made to celebrate the Babylon.js 5.0 Release.

Space Pirates This is the repository for the Space Pirates game demo for Babylon.js 5.0 Release. It contains assets and code so you can clone, learn a

Dec 26, 2022

TS & JS Library for adaptive precision cursor for the web. Releases will come out soon! Meanwhile, check out the demo site:

Haha, cool cursor go brrrr... Table of Content What is this? Installation & Setup Installation Setup Usage Cursor controls Element settings Known issu

Nov 24, 2022

A demo app showingcasing laravel-livewire-wizard

A demo app showingcasing laravel-livewire-wizard

A demo of spatie/laravel-livewire-wizard This repo contains a Laravel app to showcase the spatie/laravel-livewizard package. Usage Clone the repo Copy

Dec 29, 2022
Comments
  • DnD Shells

    DnD Shells

    Hi, can we get also example for handling dnd shells? I did not checked unity example yet but it seems there is no mention about those here (if its same there it would be nice to add it there too).

    opened by astromediaonly 2
  • Fixing duplicate dice-wrapper elements on same die connect

    Fixing duplicate dice-wrapper elements on same die connect

    Could fix the issue by making onDiceConnected a no-op if the die already has a dice-wrapper, or by clearing all of the children of the dice-wrapper. Since there is already logic in getDiceHtmlEl to check if the dice-wrapper element exists I thought it would be better to clear the elements instead of duplicating/refactoring the logic.

    opened by W0NDERBREAD 0
  • Unable to detect dices on macOS with Chrome

    Unable to detect dices on macOS with Chrome

    Hello,

    I just received my GoDice package today and already started enjoying the app!

    Now I'd like to dig into this package to play with the API and build awesome things.

    But I'm facing an issue, when trying to connect dices, nothing is detected, can you help me?

    CleanShot 2022-05-17 at 10 26 54@2x

    Using macOS 12.3.1 with Chrome 101.0.4951.64.

    Thanks!

    opened by HapLifeMan 5
Owner
Particula
Particula
A demo of LaunchDarkly, React, and Vite, using the Pokémon API!

Pokémon Feature Flags demo Here's a demo for integrating feature flags into a React project! Built with React, Vite, the PokeAPI, and LaunchDarkly! Wh

Cassidy Williams 15 Jan 5, 2022
Api for playing around with dummy data. Feel free to use it in your demo projects, tutorials, or testing tasks.

dummy-api Api similar to dummy-api Api for playing around with dummy data. Feel free to use it in your demo projects, tutorials, or testing tasks. All

Donald Wu 4 Jan 7, 2023
A small, lightweight JavaScript plugin for placing items in straight rows (jQuery and vanilla JS version) – Demo:

rowGrid.js rowGrid.js is a small, lightweight (~1000 bytes gzipped) jQuery plugin for placing images (or other items) in straight rows. The grid is si

Bruno Joseph 669 Jul 22, 2022
Distributed real-time chat demo on Blueboat in 15 lines of JavaScript

minichat Distributed real-time chat demo on Blueboat in 15 lines of JavaScript, Live Demo The code Router.get("/", () => new Response(` <script>const

Heyang Zhou 10 Aug 3, 2022
科技风智慧城市 Demo: http://stonerao.com/public/city/

Three.js Setup Download Node.js. Run this followed commands: # Install dependencies (only the first time) npm install # Run the local server at local

rao_yan 120 Dec 2, 2022
A demo for E2E build piplelines in Design Systems using monorepo's and automation :zap:.

Design System Pipelines demo What is it? A working demonstration for end-to-end build piplelines in Design Systems using Primer Primitives, Primer CSS

Rez 7 Oct 20, 2022
The app helps you to add todo items to your list, mark completed ones and also delete finished items. Its a handy tool for your day today activies. Check out the live demo.

Todo List App The app helps you to add todo items to your list, mark completed ones and also delete finished items. Its a handy tool for your day toda

Atugonza ( Billions ) Joel 14 Apr 22, 2022
Demo showcasing information leaks resulting from an IndexedDB same-origin policy violation in WebKit.

Safari 15 IndexedDB Leaks Description This demo showcases information leaks resulting from an IndexedDB same-origin policy violation in WebKit (a brow

FingerprintJS 101 Nov 5, 2022
LunaSec - Open Source Security Software built by Security Engineers. Scan your dependencies for Log4Shell, or add Data Tokenization to prevent data leaks. Try our live Tokenizer demo: https://app.lunasec.dev

Our Software We're a team of Security Engineers on a mission to make awesome Open Source Application Security tooling. It all lives in this repo. Here

LunaSec 1.2k Jan 7, 2023
Example TodoMVC app(s) to demo and learn Muban

Muban Todo MVC This repo contains multiple projects that showcase different ways of creating a Todo MVC app with Muban. It serves as an example, but a

Muban.js 3 Jul 9, 2022