Factoring out a _onSensorValueChanged function.

This commit is contained in:
Evelyn Eastmond 2019-02-04 21:41:57 -05:00
parent e87dd01629
commit da3fc930b1

View file

@ -200,27 +200,36 @@ class GdxFor {
_onConnect () { _onConnect () {
const adapter = new ScratchLinkDeviceAdapter(this._scratchLinkSocket, BLEUUID); const adapter = new ScratchLinkDeviceAdapter(this._scratchLinkSocket, BLEUUID);
godirect.createDevice(adapter, {open: true, startMeasurements: false}).then(device => { godirect.createDevice(adapter, {open: true, startMeasurements: false}).then(device => {
// Setup device
this._device = device; this._device = device;
this._device.keepValues = false; // todo: possibly remove after updating Vernier godirect module this._device.keepValues = false; // todo: possibly remove after updating Vernier godirect module
this._startMeasurements();
});
}
/** // Enable sensors
* Enable and begin reading measurements
* @private
*/
_startMeasurements () {
this._device.sensors.forEach(sensor => { this._device.sensors.forEach(sensor => {
sensor.setEnabled(true); sensor.setEnabled(true);
}); });
// Set sensor value-update behavior
this._device.on('measurements-started', () => { this._device.on('measurements-started', () => {
const enabledSensors = this._device.sensors.filter(s => s.enabled); const enabledSensors = this._device.sensors.filter(s => s.enabled);
enabledSensors.forEach(sensor => { enabledSensors.forEach(sensor => {
sensor.on('value-changed', s => { sensor.on('value-changed', s => {
let val = s.value; // TODO: rename/replace this._onSensorValueChanged(s);
});
});
});
// Start device
this._device.start(10); // Set the period to 10 milliseconds
});
}
// TODO: JSDoc
_onSensorValueChanged (sensor) {
let val = sensor.value;
const framesPerSec = 1000 / this._runtime.currentStepTime; const framesPerSec = 1000 / this._runtime.currentStepTime;
switch (s.number) {
switch (sensor.number) {
case GDXFOR_SENSOR.FORCE: case GDXFOR_SENSOR.FORCE:
// Normalize the force, which can be measured between -50 and 50 N, // Normalize the force, which can be measured between -50 and 50 N,
// to be a value between -100 and 100. // to be a value between -100 and 100.
@ -228,13 +237,13 @@ class GdxFor {
this._sensors.force = val; this._sensors.force = val;
break; break;
case GDXFOR_SENSOR.ACCELERATION_X: case GDXFOR_SENSOR.ACCELERATION_X:
this._sensors.accelerationX = s.value; this._sensors.accelerationX = sensor.value;
break; break;
case GDXFOR_SENSOR.ACCELERATION_Y: case GDXFOR_SENSOR.ACCELERATION_Y:
this._sensors.accelerationY = s.value; this._sensors.accelerationY = sensor.value;
break; break;
case GDXFOR_SENSOR.ACCELERATION_Z: case GDXFOR_SENSOR.ACCELERATION_Z:
this._sensors.accelerationZ = s.value; this._sensors.accelerationZ = sensor.value;
break; break;
case GDXFOR_SENSOR.SPIN_SPEED_X: case GDXFOR_SENSOR.SPIN_SPEED_X:
val = MathUtil.radToDeg(val); val = MathUtil.radToDeg(val);
@ -255,10 +264,6 @@ class GdxFor {
this._sensors.spinSpeedZ = val; this._sensors.spinSpeedZ = val;
break; break;
} }
});
});
});
this._device.start(10); // Set the period to 10 milliseconds
} }
getForce () { getForce () {