forked from nathankellenicki/node-poweredup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathan Kellenicki
committed
Oct 2, 2020
1 parent
f64b141
commit 7bd7d3c
Showing
9 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Device } from "./device"; | ||
|
||
import { IDeviceInterface } from "../interfaces"; | ||
|
||
import * as Consts from "../consts"; | ||
|
||
/** | ||
* @class MarioBarcodeSensor | ||
* @extends Device | ||
*/ | ||
export class MarioBarcodeSensor extends Device { | ||
|
||
constructor (hub: IDeviceInterface, portId: number) { | ||
super(hub, portId, ModeMap, Consts.DeviceType.MARIO_BARCODE_SENSOR); | ||
} | ||
|
||
public receive (message: Buffer) { | ||
const mode = this._mode; | ||
|
||
switch (mode) { | ||
case Mode.BARCODE: | ||
/** | ||
* Emits when the barcode sensor sees a barcode. | ||
* @event MarioBarcodeSensor#barcode | ||
* @type {object} | ||
* @param {number} id | ||
*/ | ||
const barcode = message.readUInt16LE(4); | ||
const color = message.readUInt16LE(6); | ||
if (color === 0xffff) { | ||
// This is a barcode | ||
this.notify("barcode", { barcode }); | ||
} else if (barcode === 0xffff) { | ||
// This is a color | ||
this.notify("barcode", { color }); | ||
} | ||
break; | ||
case Mode.RGB: | ||
/** | ||
* Emits when the barcode sensor sees a RGB color. | ||
* @event MarioBarcodeSensor#rgb | ||
* @type {object} | ||
* @param {number} r | ||
* @param {number} g | ||
* @param {number} b | ||
*/ | ||
const r = message[4]; | ||
const g = message[5]; | ||
const b = message[6]; | ||
this.notify("rgb", { r, g, b }); | ||
break; | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
export enum Mode { | ||
BARCODE = 0x00, | ||
RGB = 0x01, | ||
} | ||
|
||
export const ModeMap: {[event: string]: number} = { | ||
"barcode": Mode.BARCODE, | ||
"rgb": Mode.RGB, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Device } from "./device"; | ||
|
||
import { IDeviceInterface } from "../interfaces"; | ||
|
||
import * as Consts from "../consts"; | ||
|
||
/** | ||
* @class MarioPantsSensor | ||
* @extends Device | ||
*/ | ||
export class MarioPantsSensor extends Device { | ||
|
||
constructor (hub: IDeviceInterface, portId: number) { | ||
super(hub, portId, ModeMap, Consts.DeviceType.MARIO_PANTS_SENSOR); | ||
} | ||
|
||
public receive (message: Buffer) { | ||
const mode = this._mode; | ||
|
||
switch (mode) { | ||
case Mode.PANTS: | ||
/** | ||
* Emits when the user changes the pants on Mario. | ||
* @event MarioPantsSensor#pants | ||
* @type {object} | ||
* @param {number} pants | ||
*/ | ||
const pants = message[4]; | ||
this.notify("pants", { pants }); | ||
break; | ||
} | ||
} | ||
|
||
} | ||
|
||
export enum Mode { | ||
PANTS = 0x00, | ||
} | ||
|
||
export const ModeMap: {[event: string]: number} = { | ||
"pants": Mode.PANTS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Peripheral } from "@abandonware/noble"; | ||
import compareVersion from "compare-versions"; | ||
|
||
import { IBLEAbstraction } from "../interfaces"; | ||
|
||
import { LPF2Hub } from "./lpf2hub"; | ||
|
||
import * as Consts from "../consts"; | ||
|
||
import Debug = require("debug"); | ||
const debug = Debug("movehub"); | ||
|
||
|
||
/** | ||
* Mario is emitted if the discovered device is a LEGO Super Mario brick. | ||
* @class Mario | ||
* @extends LPF2Hub | ||
* @extends BaseHub | ||
*/ | ||
export class Mario extends LPF2Hub { | ||
|
||
|
||
public static IsMario (peripheral: Peripheral) { | ||
return ( | ||
peripheral.advertisement && | ||
peripheral.advertisement.serviceUuids && | ||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && | ||
peripheral.advertisement.manufacturerData && | ||
peripheral.advertisement.manufacturerData.length > 3 && | ||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.MARIO_ID | ||
); | ||
} | ||
|
||
constructor (device: IBLEAbstraction) { | ||
super(device, PortMap, Consts.HubType.MOVE_HUB); | ||
debug("Discovered Mario"); | ||
} | ||
|
||
|
||
public async connect () { | ||
debug("Connecting to Mario"); | ||
await super.connect(); | ||
debug("Connect completed"); | ||
} | ||
|
||
|
||
} | ||
|
||
export const PortMap: {[portName: string]: number} = { | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters