mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Prevent reading non-existing sensors
This commit is contained in:
parent
f6a9dd95b0
commit
328e4480f6
1 changed files with 22 additions and 10 deletions
|
@ -68,6 +68,7 @@ class GdxFor {
|
|||
|
||||
this.disconnect = this.disconnect.bind(this);
|
||||
this._onConnect = this._onConnect.bind(this);
|
||||
this._sensorsEnabled = null;
|
||||
}
|
||||
|
||||
|
||||
|
@ -100,12 +101,13 @@ class GdxFor {
|
|||
}
|
||||
|
||||
/**
|
||||
* Called by the runtime when a use exits the connection popup.
|
||||
* Called by the runtime when a user exits the connection popup.
|
||||
* Disconnect from the GDX FOR.
|
||||
*/
|
||||
disconnect () {
|
||||
if (this._device) {
|
||||
this._device.close();
|
||||
this._sensorsEnabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,12 +153,22 @@ class GdxFor {
|
|||
}
|
||||
});
|
||||
});
|
||||
this._device.on('measurements-started', () => {
|
||||
this._sensorsEnabled = true;
|
||||
})
|
||||
this._device.start(10); // Set the period to 10 milliseconds
|
||||
}
|
||||
|
||||
/**
|
||||
* Device is connected and measurements enabled
|
||||
* @private
|
||||
*/
|
||||
_canReadSensors () {
|
||||
return this.isConnected() && this._sensorsEnabled;
|
||||
}
|
||||
|
||||
getForce () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
let force = this._device.getSensor(1).value;
|
||||
// Normalize the force, which can be measured between -50 and 50 N,
|
||||
// to be a value between -100 and 100.
|
||||
|
@ -167,7 +179,7 @@ class GdxFor {
|
|||
}
|
||||
|
||||
getTiltX () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
let x = this.getAccelerationX();
|
||||
let y = this.getAccelerationY();
|
||||
let z = this.getAccelerationZ();
|
||||
|
@ -217,7 +229,7 @@ class GdxFor {
|
|||
}
|
||||
|
||||
getTiltY () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
let x = this.getAccelerationX();
|
||||
let y = this.getAccelerationY();
|
||||
let z = this.getAccelerationZ();
|
||||
|
@ -268,42 +280,42 @@ class GdxFor {
|
|||
|
||||
|
||||
getAccelerationX () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(2).value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getAccelerationY () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(3).value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getAccelerationZ () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(4).value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getSpinSpeedX () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(5).value * (180 / Math.PI);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getSpinSpeedY () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(6).value * (180 / Math.PI);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
getSpinSpeedZ () {
|
||||
if (this.isConnected()) {
|
||||
if (this._canReadSensors()) {
|
||||
return this._device.getSensor(7).value * (180 / Math.PI);
|
||||
}
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue