From 328e4480f6b35862cc0a903b494b17e75e4ee2d5 Mon Sep 17 00:00:00 2001 From: Valerie Young Date: Fri, 11 Jan 2019 10:56:00 -0500 Subject: [PATCH] Prevent reading non-existing sensors --- src/extensions/scratch3_gdx_for/index.js | 32 ++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 066e3d1ac..ea38f96b1 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -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;