Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions scratch-vm/src/extensions/scratch3_legoble/lib/ble-base-blocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ArgumentType = require('../../../extension-support/argument-type');
const BlockType = require('../../../extension-support/block-type');
const Cast = require('../../../util/cast');
const UtilColor = require('../../../util/color');

const Color = require('./color');

Expand Down Expand Up @@ -251,6 +252,50 @@ class BleBaseBlocks {
defaultValue: Color.BLUE
}
}
},
{
opcode: 'setHubLEDColorRGB',
text: formatMessage({
id: 'legobluetooth.setHubLEDColorRGB',
default: 'set hub LED color to R[RED] G[GREEN] B[BLUE]',
}),
blockType: BlockType.COMMAND,
arguments: {
RED: {
type: ArgumentType.NUMBER,
defaultValue: 255
},
GREEN: {
type: ArgumentType.NUMBER,
defaultValue: 255
},
BLUE: {
type: ArgumentType.NUMBER,
defaultValue: 255
}
}
},
{
opcode: 'setHubLEDColorHSV',
text: formatMessage({
id: 'legobluetooth.setHubLEDColorHSV',
default: 'set hub LED color to H[HUE] S[SATURATION] V[VALUE]',
}),
blockType: BlockType.COMMAND,
arguments: {
HUE: {
type: ArgumentType.NUMBER,
defaultValue: 360
},
SATURATION: {
type: ArgumentType.NUMBER,
defaultValue: 100
},
VALUE: {
type: ArgumentType.NUMBER,
defaultValue: 100
}
}
}
];

Expand Down Expand Up @@ -598,6 +643,22 @@ class BleBaseBlocks {
return this._peripheral.setLEDColor(color).then(waitPromise);
}

setHubLEDColorRGB(args) {
const red = Cast.toNumber(args.RED);
const green = Cast.toNumber(args.GREEN);
const blue = Cast.toNumber(args.BLUE);
return this._peripheral.setLEDColorRGB(red, green, blue).then(waitPromise);
}

setHubLEDColorHSV(args) {
const rgb = UtilColor.hsvToRgb({
h: Cast.toNumber(args.HUE),
s: Cast.toNumber(args.SATURATION) / 100,
v: Cast.toNumber(args.VALUE) / 100
});
return this._peripheral.setLEDColorRGB(rgb.r, rgb.g, rgb.b).then(waitPromise);
}

getHubTilt(args) {
const key = 'tilt' + args.XYZ.toUpperCase();
const value = this._peripheral.internalInputValue(key);
Expand Down
11 changes: 11 additions & 0 deletions scratch-vm/src/extensions/scratch3_legoble/lib/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,23 @@ class Hub {

const portId = this._devices.findIndex(device => device && device.ioType == IOType.RGB_LIGHT);
if (portId != -1) {
this.sendMessage(MessageType.PORT_INPUT_FORMAT_SETUP, [portId, 0x00], false);
return this.sendOutputCommand(portId, 0x51, [0x00, color]);
} else {
return Promise.resolve();
}
}

setLEDColorRGB(red, green, blue) {
const portId = this._devices.findIndex(device => device && device.ioType == IOType.RGB_LIGHT);
if (portId != -1) {
this.sendMessage(MessageType.PORT_INPUT_FORMAT_SETUP, [portId, 0x01], false);
return this.sendOutputCommand(portId, 0x51, [0x01, MathUtil.clamp(red, 0, 255), MathUtil.clamp(green, 0, 255), MathUtil.clamp(blue, 0, 255)]);
} else {
return Promise.resolve();
}
}

setVolume(volume) {
volume = MathUtil.clamp(volume, 0, 100);
return this.sendMessage(MessageType.HUB_PROPERTIES, [HubPropertyReference.SPEAKER_VOLUME, HubPropertyOperation.SET, volume]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const setupTranslations = function (formatMessage, extTranslations = {}) {
'legobluetooth.getForce': '[PORT] force',
'legobluetooth.getTilt': '[PORT] tilt [XY]',
'legobluetooth.setHubLEDColor': 'set hub LED color to [COLOR]',
'legobluetooth.setHubLEDColorRGB': 'set hub LED color to R[RED] G[GREEN] B[BLUE]',
'legobluetooth.setHubLEDColorHSV': 'set hub LED color to H[HUE] S[SATURATION] V[VALUE]',
'legobluetooth.getHubTilt': 'hub tilt [XYZ]',
'legobluetooth.getAngle': '[AXIS] angle',

Expand Down