From 46b643e58f4465b4021497d0c851664530c82212 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 23 Dec 2020 20:33:54 -0800 Subject: [PATCH] First pass at Mario events --- src/consts.ts | 1 + src/devices/marioevents.ts | 74 ++++++++++++++++++++++++++++++++++++++ src/hubs/basehub.ts | 2 ++ webble_test.html | 73 ------------------------------------- 4 files changed, 77 insertions(+), 73 deletions(-) create mode 100644 src/devices/marioevents.ts delete mode 100644 webble_test.html diff --git a/src/consts.ts b/src/consts.ts index 31c67f8..992db07 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -85,6 +85,7 @@ export enum DeviceType { TECHNIC_COLOR_SENSOR = 61, // Spike Prime TECHNIC_DISTANCE_SENSOR = 62, // Spike Prime TECHNIC_FORCE_SENSOR = 63, // Spike Prime + MARIO_EVENTS = 70, MARIO_ACCELEROMETER = 71, MARIO_BARCODE_SENSOR = 73, MARIO_PANTS_SENSOR = 74, diff --git a/src/devices/marioevents.ts b/src/devices/marioevents.ts new file mode 100644 index 0000000..ed9e3f5 --- /dev/null +++ b/src/devices/marioevents.ts @@ -0,0 +1,74 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +/** + * @class MarioEvents + * @extends Device + */ +export class MarioEvents extends Device { + + private _events: number[] = []; + private _coins: { [enemy: number]: number } = {}; + private _started: boolean = false; + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, ModeMap, Consts.DeviceType.MARIO_EVENTS); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case Mode.EVENT: + const type = message[5]; + const value = message[6]; + if (type === 0x13) { + const event = value; + if (event === 0xb8 && !this._started) { + this._started = true; + this._events = []; + this._coins = {}; + } + this._events.push(value); + if (this._started) { + this.notify("event", Object.assign( + { event }, + (event === 0xb7) ? { history: this._events, coins: this._coins } : {} + )); + } + if (event === 0xb7) { + this._started = false; + } + } else if (type === 0x20) { + if (!this._started) { + break; + } + const enemy = message[4]; + let coins = value; + if (coins === 0) { + break; + } + if (this._coins[enemy]) { + coins -= this._coins[enemy]; + this._coins[enemy] += coins; + } else { + this._coins[enemy] = coins; + } + this.notify("event", { enemy, coins }); + } + break; + } + } + +} + +export enum Mode { + EVENT = 0x02, +} + +export const ModeMap: {[event: string]: number} = { + "event": Mode.EVENT, +}; diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index c53d881..fa7f4e7 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -13,6 +13,7 @@ import { DuploTrainBaseSpeedometer } from "../devices/duplotrainbasespeedometer" import { HubLED } from "../devices/hubled"; import { Light } from "../devices/light"; +import { MarioEvents } from "../devices/marioevents"; import { MarioAccelerometer } from "../devices/marioaccelerometer"; import { MarioBarcodeSensor } from "../devices/mariobarcodesensor"; import { MarioPantsSensor } from "../devices/mariopantssensor"; @@ -420,6 +421,7 @@ export class BaseHub extends EventEmitter { [Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR]: DuploTrainBaseMotor, [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER]: DuploTrainBaseSpeaker, [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER]: DuploTrainBaseSpeedometer, + [Consts.DeviceType.MARIO_EVENTS]: MarioEvents, [Consts.DeviceType.MARIO_ACCELEROMETER]: MarioAccelerometer, [Consts.DeviceType.MARIO_BARCODE_SENSOR]: MarioBarcodeSensor, [Consts.DeviceType.MARIO_PANTS_SENSOR]: MarioPantsSensor, diff --git a/webble_test.html b/webble_test.html deleted file mode 100644 index 4e13fb3..0000000 --- a/webble_test.html +++ /dev/null @@ -1,73 +0,0 @@ - - - - -node-poweredup Web Bluetooth Test - - - - -
- -
- - - \ No newline at end of file