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

NCD-776: Limit voltage presets to fit the boards range #73

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## NEXT - unreleased

EXPERIMENTAL RELEASE

### Changed

- Showing only the voltage presets that are in range

## 0.1.10 - 2024-03-06

EXPERIMENTAL RELEASE
Expand Down
108 changes: 61 additions & 47 deletions src/features/VoltageConfiguration/VoltageConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ const VoltageConfiguration = ({
pmicPortDescription ??
`Set voltage for PMIC port ${pmicPort} (${label})`;

// Voltage presets in lieu of presets in board definition files
const voltagePresetValues = [1200, 1800, 2000, 3300, 1600, 2400]
.filter(
filterVoltage =>
filterVoltage >= voltageMin && filterVoltage <= voltageMax
)
.slice(0, 3)
.sort((a, b) => a - b);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not that it really matters, but why have unsorted presets only to explicitly sort them after?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does, in fact, matter :) The reason is to pick relevant voltage levels fro the different voltage ranges. I slice of the tree first elements to pick the most relevant fitting the range, and finally sort them to make the UI nice.

Good catch, though! 👍


return (
<Card
title={
Expand All @@ -53,53 +62,12 @@ const VoltageConfiguration = ({
<p>{description}</p>
</div>
)}
<div className="tw-flex tw-gap-1">
<Button
variant="secondary"
className="tw-w-full"
onClick={() => {
// Set voltage to 1800
dispatch(
setPmicConfigValue({
pmicConfigPort: pmicPort,
configPinState: 1800,
})
);
}}
>
1.8V
</Button>
<Button
variant="secondary"
className="tw-w-full"
onClick={() => {
// Set voltage to 1800
dispatch(
setPmicConfigValue({
pmicConfigPort: pmicPort,
configPinState: 2000,
})
);
}}
>
2.0V
</Button>
<Button
variant="secondary"
className="tw-w-full"
onClick={() => {
// Set voltage to 3000
dispatch(
setPmicConfigValue({
pmicConfigPort: pmicPort,
configPinState: 3000,
})
);
}}
>
3.0V
</Button>
</div>

<VoltagePresetButtons
voltages={voltagePresetValues}
pmicPort={pmicPort}
/>

<div className="tw-flex tw-flex-col">
<NumberInput
showSlider
Expand All @@ -121,4 +89,50 @@ const VoltageConfiguration = ({
);
};

interface VoltagePresetButtonsProps {
pmicPort: number;
voltages: number[];
}

const VoltagePresetButtons = ({
pmicPort,
voltages,
}: VoltagePresetButtonsProps) => (
<div id="preset-buttons" className="tw-flex tw-gap-1">
{voltages.map(voltage => (
<PresetButton
key={`voltage-preset-${pmicPort}-${voltage}`}
pmicPort={pmicPort}
voltage={voltage}
/>
))}
</div>
);

interface PresetButtonProps {
pmicPort: number;
voltage: number;
}

const PresetButton = ({ pmicPort, voltage }: PresetButtonProps) => {
const dispatch = useDispatch();

return (
<Button
variant="secondary"
className="tw-w-full"
onClick={() => {
dispatch(
setPmicConfigValue({
pmicConfigPort: pmicPort,
configPinState: voltage,
})
);
}}
>
{(voltage / 1000).toFixed(1)}V
</Button>
);
};

export default VoltageConfiguration;