@robotical/sensors-dashboard
Version:
A dashboard tool for depicting Marty data by Robotical
71 lines (70 loc) • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.MICROBIT_SIGNALS = void 0;
var _Addon = _interopRequireDefault(require("../models/addons/Addon"));
var _AddonSub = _interopRequireDefault(require("../models/addons/AddonSub"));
var _RaftInterface = _interopRequireDefault(require("./RaftInterface"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const MICROBIT_SIGNALS = exports.MICROBIT_SIGNALS = {
tilt: {
group: "Tilt",
x: "X (°)",
y: "Y (°)"
},
buttons: {
group: "Buttons",
a: "A",
b: "B"
},
touch: {
group: "Touch pins",
p0: "P0",
p1: "P1",
p2: "P2"
},
gestures: {
group: "Gestures",
moved: "Moved",
shaken: "Shaken"
}
};
const bitValue = (value, bit) => value >> bit & 1;
const binaryValue = value => Number(value) === 0 ? 0 : 1;
class MicroBitInterface extends _RaftInterface.default {
constructor(microBit) {
super();
this.microBit = microBit;
this.unsubscribeSensorListener = microBit.addSensorListener(sensors => this.publishSensors(sensors));
}
getAvailableAddons() {
const sensors = this.microBit.sensors;
return [new _Addon.default(MICROBIT_SIGNALS.tilt.group, [new _AddonSub.default(MICROBIT_SIGNALS.tilt.x, sensors.tiltX / 10), new _AddonSub.default(MICROBIT_SIGNALS.tilt.y, sensors.tiltY / 10)]), new _Addon.default(MICROBIT_SIGNALS.buttons.group, [new _AddonSub.default(MICROBIT_SIGNALS.buttons.a, binaryValue(sensors.buttonA)), new _AddonSub.default(MICROBIT_SIGNALS.buttons.b, binaryValue(sensors.buttonB))]), new _Addon.default(MICROBIT_SIGNALS.touch.group, [new _AddonSub.default(MICROBIT_SIGNALS.touch.p0, binaryValue(sensors.touchPins[0])), new _AddonSub.default(MICROBIT_SIGNALS.touch.p1, binaryValue(sensors.touchPins[1])), new _AddonSub.default(MICROBIT_SIGNALS.touch.p2, binaryValue(sensors.touchPins[2]))]), new _Addon.default(MICROBIT_SIGNALS.gestures.group, [new _AddonSub.default(MICROBIT_SIGNALS.gestures.moved, bitValue(sensors.gestureState, 2)), new _AddonSub.default(MICROBIT_SIGNALS.gestures.shaken, bitValue(sensors.gestureState, 0))])];
}
unsubscribeFromPublishedData() {
var _this$unsubscribeSens;
(_this$unsubscribeSens = this.unsubscribeSensorListener) === null || _this$unsubscribeSens === void 0 ? void 0 : _this$unsubscribeSens.call(this);
this.unsubscribeSensorListener = null;
}
publishSensors(sensors) {
this.publish(MICROBIT_SIGNALS.tilt.group, MICROBIT_SIGNALS.tilt.x, sensors.tiltX / 10);
this.publish(MICROBIT_SIGNALS.tilt.group, MICROBIT_SIGNALS.tilt.y, sensors.tiltY / 10);
this.publish(MICROBIT_SIGNALS.buttons.group, MICROBIT_SIGNALS.buttons.a, binaryValue(sensors.buttonA));
this.publish(MICROBIT_SIGNALS.buttons.group, MICROBIT_SIGNALS.buttons.b, binaryValue(sensors.buttonB));
this.publish(MICROBIT_SIGNALS.touch.group, MICROBIT_SIGNALS.touch.p0, binaryValue(sensors.touchPins[0]));
this.publish(MICROBIT_SIGNALS.touch.group, MICROBIT_SIGNALS.touch.p1, binaryValue(sensors.touchPins[1]));
this.publish(MICROBIT_SIGNALS.touch.group, MICROBIT_SIGNALS.touch.p2, binaryValue(sensors.touchPins[2]));
this.publish(MICROBIT_SIGNALS.gestures.group, MICROBIT_SIGNALS.gestures.moved, bitValue(sensors.gestureState, 2));
this.publish(MICROBIT_SIGNALS.gestures.group, MICROBIT_SIGNALS.gestures.shaken, bitValue(sensors.gestureState, 0));
}
publish(whoAmI, addonInput, value) {
this.dispatchEvent({
type: "on".concat(whoAmI, "=>").concat(addonInput, "Change"),
value,
whoAmI,
addonInput
});
}
}
exports.default = MicroBitInterface;