Skip to content

Commit 32f61e4

Browse files
committed
Ability to set RGB color
1 parent 3ad7de2 commit 32f61e4

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

scratch-vm/src/extensions/scratch3_legoble/lib/ble-base-blocks.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,28 @@ class BleBaseBlocks {
251251
defaultValue: Color.BLUE
252252
}
253253
}
254+
},
255+
{
256+
opcode: 'setHubLEDColorRGB',
257+
text: formatMessage({
258+
id: 'legobluetooth.setHubLEDColorRGB',
259+
default: 'set hub LED color to R[RED] G[GREEN] B[BLUE]',
260+
}),
261+
blockType: BlockType.COMMAND,
262+
arguments: {
263+
RED: {
264+
type: ArgumentType.NUMBER,
265+
defaultValue: 255
266+
},
267+
GREEN: {
268+
type: ArgumentType.NUMBER,
269+
defaultValue: 255
270+
},
271+
BLUE: {
272+
type: ArgumentType.NUMBER,
273+
defaultValue: 255
274+
}
275+
}
254276
}
255277
];
256278

@@ -598,6 +620,13 @@ class BleBaseBlocks {
598620
return this._peripheral.setLEDColor(color).then(waitPromise);
599621
}
600622

623+
setHubLEDColorRGB(args) {
624+
const red = Cast.toNumber(args.RED);
625+
const green = Cast.toNumber(args.GREEN);
626+
const blue = Cast.toNumber(args.BLUE);
627+
return this._peripheral.setLEDColorRGB(red, green, blue).then(waitPromise);
628+
}
629+
601630
getHubTilt(args) {
602631
const key = 'tilt' + args.XYZ.toUpperCase();
603632
const value = this._peripheral.internalInputValue(key);

scratch-vm/src/extensions/scratch3_legoble/lib/hub.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,23 @@ class Hub {
510510

511511
const portId = this._devices.findIndex(device => device && device.ioType == IOType.RGB_LIGHT);
512512
if (portId != -1) {
513+
this.sendMessage(MessageType.PORT_INPUT_FORMAT_SETUP, [portId, 0x00], false);
513514
return this.sendOutputCommand(portId, 0x51, [0x00, color]);
514515
} else {
515516
return Promise.resolve();
516517
}
517518
}
518519

520+
setLEDColorRGB(red, green, blue) {
521+
const portId = this._devices.findIndex(device => device && device.ioType == IOType.RGB_LIGHT);
522+
if (portId != -1) {
523+
this.sendMessage(MessageType.PORT_INPUT_FORMAT_SETUP, [portId, 0x01], false);
524+
return this.sendOutputCommand(portId, 0x51, [0x01, MathUtil.clamp(red, 0, 255), MathUtil.clamp(green, 0, 255), MathUtil.clamp(blue, 0, 255)]);
525+
} else {
526+
return Promise.resolve();
527+
}
528+
}
529+
519530
setVolume(volume) {
520531
volume = MathUtil.clamp(volume, 0, 100);
521532
return this.sendMessage(MessageType.HUB_PROPERTIES, [HubPropertyReference.SPEAKER_VOLUME, HubPropertyOperation.SET, volume]);

scratch-vm/src/extensions/scratch3_legoble/lib/setup-translations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const setupTranslations = function (formatMessage, extTranslations = {}) {
2727
'legobluetooth.getForce': '[PORT] force',
2828
'legobluetooth.getTilt': '[PORT] tilt [XY]',
2929
'legobluetooth.setHubLEDColor': 'set hub LED color to [COLOR]',
30+
'legobluetooth.setHubLEDColorRGB': 'set hub LED color to R[RED] G[GREEN] B[BLUE]',
3031
'legobluetooth.getHubTilt': 'hub tilt [XYZ]',
3132
'legobluetooth.getAngle': '[AXIS] angle',
3233

0 commit comments

Comments
 (0)