mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Adding interval to check for battery level.
This commit is contained in:
parent
b57b6c0a84
commit
a177b4eeb7
1 changed files with 36 additions and 0 deletions
|
@ -40,11 +40,18 @@ const BLEService = {
|
|||
*/
|
||||
const BLECharacteristic = {
|
||||
ATTACHED_IO: '00001527-1212-efde-1523-785feabcd123',
|
||||
LOW_VOLTAGE_ALERT: '00001528-1212-efde-1523-785feabcd123',
|
||||
INPUT_VALUES: '00001560-1212-efde-1523-785feabcd123',
|
||||
INPUT_COMMAND: '00001563-1212-efde-1523-785feabcd123',
|
||||
OUTPUT_COMMAND: '00001565-1212-efde-1523-785feabcd123'
|
||||
};
|
||||
|
||||
/**
|
||||
* A time interval to wait (in milliseconds) in between battery check calls.
|
||||
* @type {number}
|
||||
*/
|
||||
const BLEBatteryCheckInterval = 5000;
|
||||
|
||||
/**
|
||||
* A time interval to wait (in milliseconds) while a block that sends a BLE message is running.
|
||||
* @type {number}
|
||||
|
@ -421,8 +428,16 @@ class WeDo2 {
|
|||
*/
|
||||
this._rateLimiter = new RateLimiter(BLESendRateMax);
|
||||
|
||||
/**
|
||||
* An interval id for the battery check interval.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this._batteryLevelIntervalId = null;
|
||||
|
||||
this._onConnect = this._onConnect.bind(this);
|
||||
this._onMessage = this._onMessage.bind(this);
|
||||
this._checkBatteryLevel = this._checkBatteryLevel.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -606,6 +621,12 @@ class WeDo2 {
|
|||
if (this._ble) {
|
||||
this._ble.disconnect();
|
||||
}
|
||||
|
||||
if (this._batteryLevelIntervalId) {
|
||||
console.log('clearing batterylevelintervalid');
|
||||
window.clearInterval(this._batteryLevelIntervalId);
|
||||
this._batteryLevelIntervalId = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -711,6 +732,7 @@ class WeDo2 {
|
|||
BLECharacteristic.ATTACHED_IO,
|
||||
this._onMessage
|
||||
);
|
||||
this._batteryLevelIntervalId = window.setInterval(this._checkBatteryLevel, BLEBatteryCheckInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -757,6 +779,20 @@ class WeDo2 {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
_checkBatteryLevel () {
|
||||
this._ble.read(
|
||||
BLEService.DEVICE_SERVICE,
|
||||
BLECharacteristic.LOW_VOLTAGE_ALERT,
|
||||
false,
|
||||
this._onMessage // will ignore result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
|
Loading…
Reference in a new issue