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