-
Notifications
You must be signed in to change notification settings - Fork 64
New paradigm for handling attached devices #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
d453fe5
Basic implementation of BasicMotor types
4af1d3d
Added autosubscribe when adding eventlisteners
8ffc60b
attachedDevices now a map
93a1369
Basic code for passing messages through to the device
d382c3d
Basic ColorDistanceSensor and TachoMotor handling
a02249f
Implemented basic lights, device busy-ness
f9cea9a
Yay, port object is no longer a thing
7fdc754
Start of hub refactor
5c03e5c
More refactoring
1793eb0
Removing commented code
6beeda5
Implemented methods for retrieving hubs and devices
6a7c489
Added more motor types
nathankellenicki 198d637
Renamed devices
nathankellenicki 81cbdac
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
31ac932
Motion and tilt sensor
2a67242
Removed some commented code from hubs
023b141
Better handling of device initialisation, implemented more modes
37be7f8
Rename
406e9e9
Rename again
225f331
Better directory structure
df70d8a
Renamed devices
nathankellenicki 04b79ad
Renamed devices
nathankellenicki 388bce1
Mapping of events to modes
36c34a9
Exported device ModeMap
ffd3cce
Exported hub PortMap
364089b
Power to speed when rotating by angle
de4feb0
Added functions to wait for port attachments
c1b8697
VoltageSensor
2e06a17
CurrentSensor
6d23179
PUPRemoteButton
195c0d5
HubLED and MoveHubTiltSensor
6e77d69
Control+ (Technic Medium Hub) sensors
a5a9b2e
Control+ (Technic Medium Hub) devices, renamed hubs to be more inline…
705fbd3
writeDirect function on device
bc644c8
Duplo motors and sensors
0350176
Bug fixes, hubs now wait for hub properties before being connected
2dabedd
More WeDo 2.0 devices
579f823
Device events now emit on hub too
nathankellenicki 9113d22
Named internal ports on hubs
nathankellenicki c5146bd
Renamed button to remoteButton on RC
nathankellenicki 290f469
Device events emit value objects
nathankellenicki 077a737
WeDo 2.0 unsubscribing
9f087e3
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
nathankellenicki 673f5ff
Start of port combining
afa7c74
Merge branch 'feature/devices' of github.com:nathankellenicki/node-po…
875293a
Fixed linting errors and removed namespaces
bf721ed
Virtual port attachment and detachment
b43b59e
Virtual ports tested
f7e86d3
Added setSpeed, virtual port multiparam options
74be8a7
Initial absolute motor support
56eaa7c
Fixed parts of absolute angle
nathankellenicki 63f1c10
Servo calibration and reset
5fde49c
Adding power and brightness ramping back in
ea20b1b
Can not retrieve human readable device type from devices. Updated web…
dfd22b1
Can not retrieve human readable device type from devices. Updated web…
0c3ff8b
Devices now cache notified values for easy retrieval without events
225b60f
Devices now cache notified values for easy retrieval without events
d329ce6
Added support for Spike Prime motors
ef31567
Added support for Technic Color Sensor and Technic Distance Sensor (S…
407acce
Added Technic Force Sensor (Spike Prime)
c6f8015
Added example of slow speed train control with a Medium Linear Motor …
e34eea9
Removed examples with old API
c0009f8
Updated vernie sample, added braking style
e721e0f
Updated docs, readme, version
nathankellenicki a34c422
Added announement to readme
nathankellenicki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * | ||
| * This demonstrates connecting multiple hubs to your laptop. Once connected, all the hubs LED lights will cycle through the same colors simultaneously. | ||
| * | ||
| */ | ||
|
|
||
| const PoweredUP = require(".."); | ||
|
|
||
| const poweredUP = new PoweredUP.PoweredUP(); | ||
| poweredUP.scan(); // Start scanning for hubs | ||
|
|
||
| console.log("Looking for Hubs..."); | ||
|
|
||
| poweredUP.on("discover", async (hub) => { // Wait to discover hubs | ||
|
|
||
| await hub.connect(); // Connect to hub | ||
| console.log(`Connected to ${hub.name}!`); | ||
|
|
||
| hub.on("attach", (device) => { | ||
|
|
||
| if (device instanceof PoweredUP.ControlPlusLargeMotor) { | ||
| const motor = device; | ||
| motor.setSpeed(30); | ||
| } | ||
|
|
||
| if (device instanceof PoweredUP.ColorDistanceSensor) { | ||
| const sensor = device; | ||
| sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications | ||
| console.log(`Distance ${distance}`); | ||
| }) | ||
| } | ||
|
|
||
| device.on("detach", () => { | ||
| console.log(device.connected); | ||
| }) | ||
|
|
||
| }); | ||
|
|
||
| hub.on("disconnect", () => { | ||
| console.log("Hub disconnected"); | ||
| }) | ||
|
|
||
| }); |
This file contains hidden or 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,41 @@ | ||
| import { Device } from "./device"; | ||
| import { Hub } from "./hub"; | ||
|
|
||
| import * as Consts from "./consts"; | ||
|
|
||
| import { mapSpeed } from "./utils"; | ||
|
|
||
| export class BasicMotor extends Device { | ||
|
|
||
|
|
||
| constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { | ||
| super(hub, portId, type); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Set the motor speed. | ||
| * @method BasicMotor#setSpeed | ||
| * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. | ||
| * @returns {Promise} Resolved upon successful completion of command. | ||
| */ | ||
| public setSpeed (speed: number) { | ||
| return new Promise((resolve) => { | ||
| const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(speed)]); | ||
| this.send(data); | ||
| return resolve(); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Fully (hard) stop the motor. | ||
| * @method BasicMotor#brake | ||
| * @returns {Promise} Resolved upon successful completion of command. | ||
| */ | ||
| public brake () { | ||
| return this.setSpeed(127); | ||
| } | ||
|
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since basic motors don't have a tachometer, this is actually
setPower. I guess this makes it easier for the API, but would you then havesetSpeedandsetPowerfor tacho motors or justsetSpeed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I guess I haven't liked
poweras I think for most usersspeedis a more appropriate term for what they want, even if it isn't exactly the same. I don't this library should attempt to exactly emulate LWP functionally, instead adding a user friendly layer on top of it.Perhaps we could have
setSpeed(50)set power by default, however with an options object (such assetSpeed(50, { maxPower: 50 })to switch to maintaining the speed with power?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nutki I've gone with
setPowerand done a 180 on this. :) Will implementsetSpeedappropriately later.