Skip to content
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

Feature/additional analog inputs #87

Merged
merged 7 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 69 additions & 7 deletions packages/sample/src/command/analog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,92 @@ export async function analog(hub: ExpansionHub, channel: number, continuous: boo
while (true) {
let value = await hub.getAnalogInput(channel);
console.log(`ADC: ${value} mV`);
if (!continuous) break;
if (!continuous) {
break;
}
}
}

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

export async function battery(hub: ExpansionHub, continuous: boolean) {
export async function batteryVoltage(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getTemperature();
console.log(`Temperature: ${value} C`);
if (!continuous) break;
let value = await hub.getBatteryVoltage();
console.log(`Battery Voltage: ${value} mV`);
if (!continuous) {
break;
}
}
}

export async function batteryCurrent(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getBatteryCurrent();
console.log(`Battery Current: ${value} mA`);
if (!continuous) {
break;
}
}
}

export async function voltageRail(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.get5VBusVoltage();
console.log(`5V rail: ${value} mV`);
if (!continuous) break;
if (!continuous) {
break;
}
}
}

export async function i2cCurrent(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getI2CCurrent();
console.log(`I2C Current: ${value} mA`);
if (!continuous) {
break;
}
}
}

export async function servoCurrent(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getServoCurrent();
console.log(`Servo Current: ${value} mA`);
if (!continuous) {
break;
}
}
}

export async function motorCurrent(
hub: ExpansionHub,
channel: number,
continuous: boolean,
) {
while (true) {
let value = await hub.getMotorCurrent(channel);
console.log(`Motor ${channel} Current: ${value} mA`);
if (!continuous) {
break;
}
}
}

export async function digitalBusCurrent(hub: ExpansionHub, continuous: boolean) {
while (true) {
let value = await hub.getBatteryCurrent();
console.log(`Digital Bus Current: ${value} mA`);
if (!continuous) {
break;
}
}
}
150 changes: 132 additions & 18 deletions packages/sample/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Command } from "commander";
import { analog, battery, temperature, voltageRail } from "./command/analog.js";
import {
analog,
batteryCurrent,
batteryVoltage,
digitalBusCurrent,
i2cCurrent,
motorCurrent,
servoCurrent,
temperature,
voltageRail,
} from "./command/analog.js";
import { error } from "./command/error.js";
import { list } from "./command/list.js";
import { led } from "./command/led.js";
Expand Down Expand Up @@ -38,13 +48,36 @@ program
.command("led")
.description("Run LED steps")
.action(async () => {
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];

runOnSigint(() => {
hub.close();
});

await led(hub);
});

let motorCommand = program.command("motor");

motorCommand
.command("current <channel>")
.option("--continuous", "Run continuously")
.description(
"Read the current through a motor. Specify --continuous to run continuously",
)
.action(async (channel, options) => {
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await led(hub);
let isContinuous = options.continuous !== undefined;
let channelNumber = Number(channel);

runOnSigint(() => {
hub.close();
});

await motorCurrent(hub, channelNumber, isContinuous);
hub.close();
});

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

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

runOnSigint(() => {
hub.close();
});

await analog(hub, portNumber, isContinuous);
hub.close();
});

program
Expand All @@ -74,67 +109,146 @@ program
"Specify --continuous to run continuously",
)
.action(async (options) => {
let isContinuous = options.continuous !== undefined;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];

runOnSigint(() => {
hub.close();
});

await temperature(hub, isContinuous);
});

program
.command("5vRailVoltage")
.option("--continuous", "Run continuously")
.description(
"Read the current 5V rail voltage. Specify --continuous to run continuously",
)
.action(async (options) => {
let isContinuous = options.continuous !== undefined;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await temperature(hub, isContinuous);

runOnSigint(() => {
hub.close();
});

await voltageRail(hub, isContinuous);
hub.close();
});

program
let batteryCommand = program
.command("battery")
.description("Get information about the battery");

batteryCommand
.command("voltage")
.option("--continuous", "Run continuously")
.description(
"Read the current 5V rail voltage. Specify --continuous to run continuously",
"Read the current battery voltage. Specify --continuous to run continuously",
)
.action(async (options) => {
let isContinuous = options.continuous !== undefined;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];

runOnSigint(() => {
hub.close();
});

await batteryVoltage(hub, isContinuous);
hub.close();
});

batteryCommand
.command("current")
.option("--continuous", "Run continuously")
.description("Read the battery current. Specify --continuous to run continuously")
.action(async (options) => {
let isContinuous = options.continuous !== undefined;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await voltageRail(hub, isContinuous);

runOnSigint(() => {
hub.close();
});

await batteryCurrent(hub, isContinuous);
hub.close();
});

program
.command("battery")
.command("i2c-current")
.option("--continuous", "Run continuously")
.description(
"Read the current battery Voltage. Specify --continuous to run continuously",
"Read the I2C sub-system current. Specify --continuous to run continuously",
)
.action(async (options) => {
let isContinuous = options.continuous !== undefined;
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];

runOnSigint(() => {
hub.close();
});

await i2cCurrent(hub, isContinuous);
hub.close();
});

program
.command("digital-current")
.option("--continuous", "Run continuously")
.description("Read the digital bus current. Specify --continuous to run continuously")
.action(async (options) => {
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
let isContinuous = options.continuous !== undefined;

runOnSigint(() => {
hub.close();
});

await digitalBusCurrent(hub, isContinuous);
hub.close();
});

program
.command("servo-current")
.option("--continuous", "Run continuously")
.description(
"Read the total current through all servos. Specify --continuous to run continuously",
)
.action(async (options) => {
let hubs = await openConnectedExpansionHubs();
let hub = hubs[0];
await battery(hub, isContinuous);
let isContinuous = options.continuous !== undefined;

runOnSigint(() => {
hub.close();
});

await servoCurrent(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 () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these because the hub wasn't semantically declared before this. It's very unlikely to cause any issues (unless we get a sigint before it finishes opening the hub).

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];

runOnSigint(async () => {
await hub.setServoEnable(channelValue, false);
hub.close();
});

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

Expand Down