|
1 |
| -# GoDiceJavaScriptAPI |
| 1 | +# GoDice JavaScript API (with demo) |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Use the GoDice JavaScript API to integrate GoDice functionality into your own JavaScript applications. |
| 6 | + |
| 7 | +Here are some of the things that you can do with the GoDice JavaScript API: |
| 8 | + |
| 9 | +* Turn ON/OFF GoDice RGB Leds |
| 10 | +* Ask for the die Color (dots color) |
| 11 | +* Ask for the die battery level |
| 12 | +* Get differernt notifications reagrding the die state (Rolling or Stabe and get the outcome number) |
| 13 | + |
| 14 | +To run the demo (that use the API) just open the index.html file in Chrome or Microsoft Edge browser |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Getting Started |
| 19 | + |
| 20 | +1. Create a web page and include the GoDice JavaScript API file (godice.js): |
| 21 | + <script src="godice.js"></script> |
| 22 | + |
| 23 | +2. In your main JavaScript file create a new instance of the GoDice class: |
| 24 | + goDice = new GoDice(); |
| 25 | + |
| 26 | +3. To open the Bluetooth connection dialog and connect to your GoDice, call the following funtion: |
| 27 | + goDice.requestDevice(); |
| 28 | + |
| 29 | +4. After successfull connection the function onDiceConnected will be called from the GoDice class |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +## API Functions |
| 35 | + |
| 36 | +There are few types of API calls: |
| 37 | +1. Message - a message to the die |
| 38 | +2. Request - a message to the die, that should follow by corresponding response. |
| 39 | +3. Response - an event that is expected after sending the request |
| 40 | +4. Events - a message sent by the die |
| 41 | + |
| 42 | +Once you followed the "Getting Started" section and you have the goDice instance ready you can call to the following class functions: |
| 43 | + |
| 44 | + |
| 45 | +Message: |
| 46 | +--------- |
| 47 | +```javascript |
| 48 | +/** |
| 49 | + * Turn On/Off RGB LEDs (the LEDs will turn off if both led1 and led2 are null or set to [0,0,0]) |
| 50 | + * @param {Array} led1 - an array to control the 1st LED in the following format '[R,G,B]' |
| 51 | + * where R,G and B are numbers in the range of 0-255 |
| 52 | + * @param {Array} led2 - an array to control the 2nd LED in the following format '[R,G,B]' |
| 53 | + * where R,G and B are numbers in the range of 0-255 |
| 54 | + */ |
| 55 | + setLed(led1, led2); |
| 56 | +``` |
| 57 | + |
| 58 | +Requests: |
| 59 | +--------- |
| 60 | +```javascript |
| 61 | + /** |
| 62 | + * Open a browser connection dialog to connect a single GoDice, after successfull connection it will followed |
| 63 | + * by corresponding "onDiceConnected" callback function (response) from the GoDice class (instance). |
| 64 | + */ |
| 65 | + requestDevice(); |
| 66 | +``` |
| 67 | + |
| 68 | +```javascript |
| 69 | + /** |
| 70 | + * Request for the die color, that should follow by corresponding "onDiceColor" callback function (response) |
| 71 | + * from the GoDice class (instance). |
| 72 | + */ |
| 73 | + getDiceColor(){ |
| 74 | +``` |
| 75 | +
|
| 76 | +```javascript |
| 77 | + /** |
| 78 | + * Request for the die battery, that should follow by corresponding "onBatteryLevel" callback function (response) |
| 79 | + * from the GoDice class (instance). |
| 80 | + */ |
| 81 | + getBatteryLevel() |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +Responses: |
| 86 | +---------- |
| 87 | +```javascript |
| 88 | +/** |
| 89 | + * To recognize connected die, override the function "onDiceConnected" in the GoDice class, with the following parameter: |
| 90 | + * @param {string} diceID - the die unique identifier |
| 91 | + * @param {GoDice class} diceInstance - the die class instance |
| 92 | + */ |
| 93 | + |
| 94 | +// example: |
| 95 | + |
| 96 | +GoDice.prototype.onDiceConnected = (diceId, diceInstance) => { |
| 97 | + // die unique identifier |
| 98 | + let dieIdentifier = diceID; |
| 99 | + |
| 100 | + // die unique identifier |
| 101 | + let dieClass = diceInstance; |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | + ```javascript |
| 106 | + /** |
| 107 | + * To recognize the battery level, override the function "onBatteryLevel" in the GoDice class, with the following parameter: |
| 108 | + * @param {string} diceID - the die unique identifier |
| 109 | + * @param {string} level - the battery level of this diceID |
| 110 | + */ |
| 111 | + |
| 112 | + //example: |
| 113 | + |
| 114 | + GoDice.prototype.onBatteryLevel = (diceId, batteryLevel) => { |
| 115 | + // die unique identifier |
| 116 | + let dieIdentifier = diceID; |
| 117 | + |
| 118 | + // battery level |
| 119 | + let dieBatLevel = batteryLevel; |
| 120 | + }; |
| 121 | +``` |
| 122 | + |
| 123 | +```javascript |
| 124 | + /** |
| 125 | + * To recognize the color of the die, override the function "onDiceColor" in the GoDice class, with the following parameter: |
| 126 | + * @param {string} diceID - the die unique identifier |
| 127 | + * @param {string} color - the color of the die (corresponding to the diceID). |
| 128 | + * COLOR_BLACK 0 |
| 129 | + * COLOR_RED 1 |
| 130 | + * COLOR_GREEN 2 |
| 131 | + * COLOR_BLUE 3 |
| 132 | + * COLOR_YELLOW 4 |
| 133 | + * COLOR_ORANGE 5 |
| 134 | + */ |
| 135 | + |
| 136 | + // example: |
| 137 | + |
| 138 | + GoDice.prototype.onDiceColor = (diceId, color) => { |
| 139 | + // die unique identifier |
| 140 | + let dieIdentifier = diceID; |
| 141 | + |
| 142 | + // die color |
| 143 | + let dieColor = color; |
| 144 | + }; |
| 145 | +``` |
| 146 | + |
| 147 | +Events: |
| 148 | +------- |
| 149 | +```javascript |
| 150 | + /** |
| 151 | + * When the die is stable after a legit roll the function "onStable" will be called from the GoDice class with the following parameter: |
| 152 | + * @param {string} diceId - the die unique identifier |
| 153 | + * @param {string} value - the D6 outcome value (1-6) |
| 154 | + * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z |
| 155 | + */ |
| 156 | + |
| 157 | + // example: |
| 158 | + |
| 159 | + GoDice.prototype.onStable = (diceId, value, xyzAccRaw) => { |
| 160 | + // die unique identifier |
| 161 | + let dieIdentifier = diceID; |
| 162 | + |
| 163 | + // the D6 outcome value (1-6) |
| 164 | + let dieValue = value; |
| 165 | + |
| 166 | + // the acc raw values x,y,z |
| 167 | + let accX = xyzAccRaw[0]; |
| 168 | + let accY = xyzAccRaw[1]; |
| 169 | + let accZ = xyzAccRaw[2]; |
| 170 | + }; |
| 171 | +``` |
| 172 | + |
| 173 | + |
| 174 | +```javascript |
| 175 | + /** |
| 176 | + * 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: |
| 177 | + * @param {string} diceId - the die unique identifier |
| 178 | + * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z |
| 179 | + */ |
| 180 | + |
| 181 | + // example: |
| 182 | + |
| 183 | + GoDice.prototype.onTiltStable = (diceId, xyzAccRaw) => { |
| 184 | + // die unique identifier |
| 185 | + let dieIdentifier = diceID; |
| 186 | + |
| 187 | + // the acc raw values x,y,z |
| 188 | + let accX = xyzAccRaw[0]; |
| 189 | + let accY = xyzAccRaw[1]; |
| 190 | + let accZ = xyzAccRaw[2]; |
| 191 | + }; |
| 192 | +``` |
| 193 | +
|
| 194 | +
|
| 195 | +```javascript |
| 196 | + /** |
| 197 | + * When the die is stable after a "fake" roll the function "onFakeStable" will be called from the GoDice class with the following parameter: |
| 198 | + * @param {string} diceId - the die unique identifier |
| 199 | + * @param {string} value - the D6 outcome value (1-6) |
| 200 | + * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z |
| 201 | + */ |
| 202 | + |
| 203 | + // example: |
| 204 | + |
| 205 | + GoDice.prototype.onFakeStable = (diceId, value, xyzAccRaw) => { |
| 206 | + // die unique identifier |
| 207 | + let dieIdentifier = diceID; |
| 208 | + |
| 209 | + // the D6 outcome value (1-6) |
| 210 | + let dieValue = value; |
| 211 | + |
| 212 | + // the acc raw values x,y,z |
| 213 | + let accX = xyzAccRaw[0]; |
| 214 | + let accY = xyzAccRaw[1]; |
| 215 | + let accZ = xyzAccRaw[2]; |
| 216 | + }; |
| 217 | +``` |
| 218 | +
|
| 219 | +
|
| 220 | + ```javascript |
| 221 | + /** |
| 222 | + * 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: |
| 223 | + * @param {string} diceId - the die unique identifier |
| 224 | + * @param {string} value - the D6 outcome value (1-6) |
| 225 | + * @param {array} xyzAccRaw [x, y, z] - the acc raw data x, y, z |
| 226 | + */ |
| 227 | + |
| 228 | + // example: |
| 229 | + |
| 230 | + GoDice.prototype.onMoveStable = (diceId, value, xyzAccRaw) => { |
| 231 | + // die unique identifier |
| 232 | + let dieIdentifier = diceID; |
| 233 | + |
| 234 | + // the D6 outcome value (1-6) |
| 235 | + let dieValue = value; |
| 236 | + |
| 237 | + // the acc raw values x,y,z |
| 238 | + let accX = xyzAccRaw[0]; |
| 239 | + let accY = xyzAccRaw[1]; |
| 240 | + let accZ = xyzAccRaw[2]; |
| 241 | + }; |
| 242 | +``` |
0 commit comments