Skip to content

Commit

Permalink
Merge pull request #84 from REVrobotics/refactor/pass-hubs
Browse files Browse the repository at this point in the history
Pass hubs in to commands
  • Loading branch information
LandryNorris authored Jun 26, 2023
2 parents 5e93efe + 2bee66f commit bc07479
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 72 deletions.
72 changes: 18 additions & 54 deletions packages/sample/src/command/analog.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,33 @@
import { openConnectedExpansionHubs } from "@rev-robotics/expansion-hub";
import { ExpansionHub, openConnectedExpansionHubs } from "@rev-robotics/expansion-hub";

export async function analog(channel: number, continuous: boolean) {
const hubs = await openConnectedExpansionHubs();

if (continuous) {
while (true) {
let value = await hubs[0].getAnalogInput(channel);
console.log(`ADC: ${value} mV`);
}
} else {
let value = await hubs[0].getAnalogInput(channel);
export async function analog(hub: ExpansionHub, channel: number, continuous: boolean) {
while (true) {
let value = await hub.getAnalogInput(channel);
console.log(`ADC: ${value} mV`);
hubs.forEach((hub) => {
hub.close();
});
if (!continuous) break;
}
}

export async function temperature(continuous: boolean) {
const hubs = await openConnectedExpansionHubs();

if (continuous) {
while (true) {
let value = await hubs[0].getTemperature();
console.log(`Temperature: ${value} C`);
}
} else {
let value = await hubs[0].getTemperature();
export async function temperature(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getTemperature();
console.log(`Temperature: ${value} C`);
hubs.forEach((hub) => {
hub.close();
});
if (!continuous) break;
}
}

export async function battery(continuous: boolean) {
const hubs = await openConnectedExpansionHubs();

if (continuous) {
while (true) {
let value = await hubs[0].getBatteryVoltage();
console.log(`Battery: ${value} mV`);
}
} else {
let value = await hubs[0].getBatteryVoltage();
console.log(`Battery: ${value} mV`);
hubs.forEach((hub) => {
hub.close();
});
export async function battery(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getTemperature();
console.log(`Temperature: ${value} C`);
if (!continuous) break;
}
}

export async function voltageRail(continuous: boolean) {
const hubs = await openConnectedExpansionHubs();

if (continuous) {
while (true) {
let value = await hubs[0].get5VBusVoltage();
console.log(`5V rail: ${value} mV`);
}
} else {
let value = await hubs[0].get5VBusVoltage();
export async function voltageRail(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.get5VBusVoltage();
console.log(`5V rail: ${value} mV`);
hubs.forEach((hub) => {
hub.close();
});
if (!continuous) break;
}
}
8 changes: 3 additions & 5 deletions packages/sample/src/command/led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ import {
createLedPattern,
ExpansionHub,
LedPatternStep,
openConnectedExpansionHubs,
} from "@rev-robotics/expansion-hub";

export async function led() {
const hubs: ExpansionHub[] = await openConnectedExpansionHubs();
export async function led(hub: ExpansionHub) {
const steps = [
new LedPatternStep(1, 0, 255, 0), //green
new LedPatternStep(1, 255, 0, 0), //red
new LedPatternStep(1, 0, 0, 255), //blue
new LedPatternStep(1, 255, 0, 255), //magenta
new LedPatternStep(1, 255, 255, 0), //yellow
];
await hubs[0].sendKeepAlive();
await hub.sendKeepAlive();
const pattern = createLedPattern(steps);
await hubs[0].setModuleLedPattern(pattern);
await hub.setModuleLedPattern(pattern);
}
17 changes: 10 additions & 7 deletions packages/sample/src/command/servo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { openConnectedExpansionHubs } from "@rev-robotics/expansion-hub";
import { ExpansionHub } from "@rev-robotics/expansion-hub";

export async function runServo(channel: number, pulseWidth: number, framePeriod: number) {
const hubs = await openConnectedExpansionHubs();

await hubs[0].setServoConfiguration(channel, framePeriod);
await hubs[0].setServoPulseWidth(channel, pulseWidth);
await hubs[0].setServoEnable(channel, true);
export async function runServo(
hub: ExpansionHub,
channel: number,
pulseWidth: number,
framePeriod: number,
) {
await hub.setServoConfiguration(channel, framePeriod);
await hub.setServoPulseWidth(channel, pulseWidth);
await hub.setServoEnable(channel, true);
}
59 changes: 53 additions & 6 deletions packages/sample/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import { error } from "./command/error.js";
import { list } from "./command/list.js";
import { led } from "./command/led.js";
import { runServo } from "./command/servo.js";
import { openConnectedExpansionHubs } from "@rev-robotics/expansion-hub";

function runOnSigint(block: () => void) {
process.on("SIGINT", () => {
block();
process.exit();
});
}

const program = new Command();

Expand All @@ -30,7 +38,13 @@ program
.command("led")
.description("Run LED steps")
.action(async () => {
await led();
runOnSigint(() => {
hub.close();
});

let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await led(hub);
});

program
Expand All @@ -41,9 +55,15 @@ program
"--continuous to run continuously.",
)
.action(async (port, options) => {
runOnSigint(() => {
hub.close();
});

let isContinuous = options.continuous !== undefined;
let portNumber = Number(port);
await analog(portNumber, isContinuous);
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await analog(hub, portNumber, isContinuous);
});

program
Expand All @@ -54,8 +74,14 @@ program
"Specify --continuous to run continuously",
)
.action(async (options) => {
runOnSigint(() => {
hub.close();
});

let isContinuous = options.continuous !== undefined;
await temperature(isContinuous);
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await temperature(hub, isContinuous);
});

program
Expand All @@ -65,8 +91,15 @@ program
"Read the current 5V rail voltage. Specify --continuous to run continuously",
)
.action(async (options) => {
runOnSigint(() => {
hub.close();
});

let isContinuous = options.continuous !== undefined;
await voltageRail(isContinuous);
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await voltageRail(hub, isContinuous);
hub.close();
});

program
Expand All @@ -76,19 +109,33 @@ program
"Read the current battery Voltage. Specify --continuous to run continuously",
)
.action(async (options) => {
runOnSigint(() => {
hub.close();
});

let isContinuous = options.continuous !== undefined;
await battery(isContinuous);
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await battery(hub, isContinuous);
hub.close();
});

program
.command("servo <channel> <pulseWidth> [frameWidth]")
.description("Run a servo with pulse width and optional frame width")
.action(async (channel, pulseWidth, frameWidth) => {
runOnSigint(async () => {
await hub.setServoEnable(channelValue, false);
hub.close();
});

let channelValue = Number(channel);
let pulseWidthValue = Number(pulseWidth);
let frameWidthValue = frameWidth ? Number(frameWidth) : 4000;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];

await runServo(channelValue, pulseWidthValue, frameWidthValue);
await runServo(hub, channelValue, pulseWidthValue, frameWidthValue);
});

program.parse(process.argv);

0 comments on commit bc07479

Please sign in to comment.