Skip to content

Commit 14cf179

Browse files
committed
Add another special LED function to the API
1 parent e9a2cc9 commit 14cf179

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ Message:
5555
*/
5656
setLed(led1, led2);
5757
```
58+
```javascript
59+
/**
60+
* Pulses LEDs for a defined time and color
61+
* @param {number} pulseCount - an integer of how many times the pulse will repeat (max 255)
62+
* @param {number} onTime - how much time should the LED be ON each pulse (units of 10ms, max 255)
63+
* @param {number} offTime - how much time should the LED be OFF each pulse (units of 10ms, max 255)
64+
* @param {Array} RGB - an array to control both LEDs color's in the following format '[R, G, B]'
65+
* where R, G and B are number in the range of 0-255
66+
*/
67+
pulseLed(pulseCount, onTime, offTime, RGB)
68+
```
5869

5970
Requests:
6071
---------
@@ -96,7 +107,7 @@ Requests:
96107
Responses:
97108
----------
98109
```javascript
99-
/**
110+
/**
100111
* To recognize connected die, override the function "onDiceConnected" in the GoDice class, with the following parameter:
101112
* @param {string} diceID - the die unique identifier
102113
* @param {GoDice class} diceInstance - the die class instance

godice.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class GoDice {
99
BATTERY_LEVEL: 3,
1010
DICE_COLOUR: 23,
1111
SET_LED: 8,
12+
SET_LED_TOGGLE: 16,
1213
}
1314

1415
diceColour = {
@@ -278,10 +279,11 @@ class GoDice {
278279

279280
/**
280281
* Turn On/Off RGB LEDs, will turn off if led1 and led2 are null
281-
* @param {Array} led1 - an array to control the 1st LED in the following format '[R,G,B]'
282-
* where R,G and B are numbers in the range of 0-255
283-
* @param {Array} led2 - an array to control the 2nd LED in the following format '[R,G,B]'
284-
* where R,G and B are numbers in the range of 0-255
282+
* @param {Array} led1 - an array to control the 1st LED in the following format '[R, G, B]'
283+
* where R,G and B are numbers in the range of 0-255
284+
*
285+
* @param {Array} led2 - an array to control the 2nd LED in the following format '[R, G, B]'
286+
* where R,G and B are numbers in the ran ge of 0-255
285287
*/
286288
setLed(led1, led2) {
287289
console.log(led1, led2)
@@ -296,6 +298,22 @@ class GoDice {
296298
console.debug("messageArray", messageArray);
297299
this.sendMessage(messageArray);
298300
}
301+
302+
/**
303+
* Pulses LEDs for set time and color
304+
* @param {number} pulseCount - an integer of how many times the pulse will repeat (max 255)
305+
* @param {number} onTime - how much time should the LED be ON each pulse (units of 10ms, max 255)
306+
* @param {number} offTime - how much time should the LED be OFF each pulse (units of 10ms, max 255)
307+
* @param {Array} RGB - an array to control both LEDs color's in the following format '[R, G, B]'
308+
* where R, G and B are number in the range of 0-255
309+
*/
310+
pulseLed(pulseCount, onTime, offTime, RGB) {
311+
if (RGB.length === 3) {
312+
let rgbColor = RGB.map((i) => Math.max(Math.min(i, 255), 0));
313+
const messageArray = [this.messageIdentifiers.SET_LED_TOGGLE, pulseCount, onTime, offTime, ...rgbColor, 1, 0]
314+
this.sendMessage(messageArray);
315+
}
316+
}
299317

300318
/******* Internal Helper Functions *******/
301319

@@ -442,7 +460,7 @@ class GoDice {
442460
const diceCurrentNumber = this.getDieValue(data, 1);
443461
const xyzArray = this.getXyzFromBytes(data, 1)
444462
this.rolledValue = diceCurrentNumber;
445-
this.onStable(deviceId, diceCurrentNumber, xyzArray);
463+
this.onStable(deviceId, diceCurrentNumber, xyzArray);
446464
}
447465

448466
if (firstByte === 70 && secondByte === 83) {

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h3 id="gocube-message"></h3>
3434
.dice-wrapper{
3535
display: inline-block;
3636
width: 215px;
37-
height: 420px;
37+
height: 490px;
3838
border: 1px solid gray;
3939
border-radius: 5px;
4040
margin-right: 5px;

main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ GoDice.prototype.onDiceConnected = (diceId, diceInstance) => {
7171
ledOffButton.onclick = diceInstance.setLed.bind(diceInstance, [0], [0])
7272
ledOffButton.textContent = 'Switch Off Led';
7373
diceHtmlEl.append(ledOffButton)
74-
74+
75+
// Pulse Led
76+
const ledPulseButton = document.createElement('button');
77+
ledPulseButton.className = 'btn btn-outline-primary';
78+
ledPulseButton.onclick = diceInstance.pulseLed.bind(diceInstance, 5, 30, 20, [0, 0, 255])
79+
ledPulseButton.textContent = "Pulse"
80+
diceHtmlEl.append(ledPulseButton)
81+
7582
// get Dice color to use goDice.getDiceColor(diceID) function
7683
const getDiceColorButton = document.createElement('button');
7784
getDiceColorButton.className = 'btn btn-outline-primary';

0 commit comments

Comments
 (0)