WeDo2

WeDo2

Manage communication with a WeDo 2.0 peripheral over a Bluetooth Low Energy client socket.

Constructor

new WeDo2()

Members

(private) _batteryLevelIntervalId :number

An interval id for the battery check interval.

Type:
  • number

(private) _ble :BLE

The Bluetooth connection socket for reading/writing peripheral data.

Type:

_extensionId

The id of the extension this peripheral belongs to.

(private) _motors :Array.<WeDo2Motor>

The motors which this WeDo 2.0 could possibly have.

Type:

(private) _ports :Array.<string>

A list of the ids of the motors or sensors in ports 1 and 2.

Type:
  • Array.<string>

(private) _rateLimiter :RateLimiter

A rate limiter utility, to help limit the rate at which we send BLE messages over the socket to Scratch Link to a maximum number of sends per second.

Type:

(private) _runtime :Runtime

The Scratch 3.0 runtime used to trigger the green flag button.

Type:

(private) _sensors :Object.<string, number>

The most recently received value for each sensor.

Type:
  • Object.<string, number>

distance

tiltX

tiltY

Methods

_checkBatteryLevel()

Check the battery level on the WeDo 2.0. If the WeDo 2.0 has disconnected for some reason, the BLE socket will get an error back and automatically close the socket.

(private) _clearPort(connectID)

Clear the sensor or motor present at port 1 or 2.

Parameters:
Name Type Description
connectID number

the port to clear.

(private) _onConnect()

Sets LED mode and initial color and starts reading data from peripheral after BLE has connected.

(private) _onMessage(base64)

Process the sensor data from the incoming BLE characteristic.

Parameters:
Name Type Description
base64 object

the incoming BLE data.

(private) _registerSensorOrMotor(connectID, type)

Register a new sensor or motor connected at a port. Store the type of sensor or motor internally, and then register for notifications on input values if it is a sensor.

Parameters:
Name Type Description
connectID number

the port to register a sensor or motor on.

type number

the type ID of the sensor or motor

connect(id)

Called by the runtime when user wants to connect to a certain WeDo 2.0 peripheral.

Parameters:
Name Type Description
id number

the id of the peripheral to connect to.

disconnect()

Disconnects from the current BLE socket.

generateInputCommand(connectID, type, mode, delta, units, enableNotifications) → {array}

Generate a WeDo 2.0 'Input Command' in the byte array format (COMMAND ID, COMMAND TYPE, CONNECT ID, TYPE ID, MODE, DELTA INTERVAL (4 BYTES), UNIT, NOTIFICATIONS ENABLED).

This sends a command to the WeDo 2.0 that sets that input format of the specified inputs and sets value change notifications.

Parameters:
Name Type Description
connectID number

the port (Connect ID) to send a command to.

type number

the type of input sensor.

mode number

the mode of the input sensor.

delta number

the delta change needed to trigger notification.

units array

the unit of the input sensor value.

enableNotifications boolean

whether to enable notifications.

Returns:
  • a generated input command.
Type
array

generateOutputCommand(connectID, commandID, values) → {array}

Generate a WeDo 2.0 'Output Command' in the byte array format (CONNECT ID, COMMAND ID, NUMBER OF BYTES, VALUES ...).

This sends a command to the WeDo 2.0 to actuate the specified outputs.

Parameters:
Name Type Default Description
connectID number

the port (Connect ID) to send a command to.

commandID number

the id of the byte command.

values array null

the list of values to write to the command.

Returns:
  • a generated output command.
Type
array

isConnected() → {boolean}

Called by the runtime to detect whether the WeDo 2.0 peripheral is connected.

Returns:
  • the connected state.
Type
boolean

motor(index) → {WeDo2Motor}

Access a particular motor on this peripheral.

Parameters:
Name Type Description
index int

the zero-based index of the desired motor.

Returns:
  • the WeDo2Motor instance, if any, at that index.
Type
WeDo2Motor

playTone(tone, milliseconds) → {Promise}

Play a tone from the WeDo 2.0 peripheral for a specific amount of time.

Parameters:
Name Type Description
tone int

the pitch of the tone, in Hz.

milliseconds int

the duration of the note, in milliseconds.

Returns:
  • a promise of the completion of the play tone send operation.
Type
Promise

reset()

Reset all the state and timeout/interval ids.

scan()

Called by the runtime when user wants to scan for a WeDo 2.0 peripheral.

send(uuid, message, useLimiteropt) → {Promise}

Write a message to the WeDo 2.0 peripheral BLE socket.

Parameters:
Name Type Attributes Default Description
uuid number

the UUID of the characteristic to write to

message Array

the message to write.

useLimiter boolean <optional>
true

if true, use the rate limiter

Returns:
  • a promise result of the write operation
Type
Promise

setLED(inputRGB) → {Promise}

Set the WeDo 2.0 peripheral's LED to a specific color.

Parameters:
Name Type Description
inputRGB int

a 24-bit RGB color in 0xRRGGBB format.

Returns:
  • a promise of the completion of the set led send operation.
Type
Promise

setLEDMode() → {Promise}

Sets the input mode of the LED to RGB.

Returns:
  • a promise returned by the send operation.
Type
Promise

stopAll()

Stop the tone playing and motors on the WeDo 2.0 peripheral.

stopAllMotors()

Stop all the motors that are currently running.

stopLED() → {Promise}

Switch off the LED on the WeDo 2.0.

Returns:
  • a promise of the completion of the stop led send operation.
Type
Promise

stopTone() → {Promise}

Stop the tone playing from the WeDo 2.0 peripheral, if any.

Returns:
  • a promise that the command sent.
Type
Promise