Skip to content

Commit

Permalink
deviceHasCapability should take a device.
Browse files Browse the repository at this point in the history
Basically, making the function resemble its name. I think this is an
improvement, even if it's more verbose.
  • Loading branch information
infovore committed Jan 10, 2025
1 parent 34b606e commit a66a0e6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 27 deletions.
10 changes: 6 additions & 4 deletions src/lib/components/DeviceOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import { configuration, editConfiguration } from "$lib/stores";
import CheckOption from "$lib/components/CheckOption.svelte";
import { deviceHasCapability } from "$lib/configuration";
import { deviceHasCapability, deviceForId } from "$lib/configuration";
let device = $derived(deviceForId($configuration?.deviceId));
const touchControl = () => {
editConfiguration.set($editConfiguration);
};
</script>

{#if $configuration && $editConfiguration}
{#if deviceHasCapability($configuration, "led")}
{#if deviceHasCapability(device, "led", $configuration.firmwareVersion)}
<CheckOption bind:checked={$editConfiguration.ledOn}
>LED permanently on when powered</CheckOption
>
Expand All @@ -32,7 +34,7 @@
</CheckOption>
{/if}

{#if deviceHasCapability($configuration, "faderCalibration")}
{#if deviceHasCapability(device, "faderCalibration", $configuration.firmwareVersion)}
<hr />
<h3>Fader Minimum/Maximum calibration</h3>
<div>
Expand Down Expand Up @@ -68,7 +70,7 @@
</p>
{/if}

{#if deviceHasCapability($configuration, "i2c")}
{#if deviceHasCapability(device, "i2c", $configuration.firmwareVersion)}
<hr />
<h3>I2C Leader/Follower</h3>
<select bind:value={$editConfiguration.i2cLeader} onchange={touchControl}>
Expand Down
18 changes: 16 additions & 2 deletions src/lib/components/EditControl.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { deviceHasCapability } from "$lib/configuration";
import { deviceHasCapability, deviceForId } from "$lib/configuration";
import { editConfiguration } from "$lib/stores";
import type { Control } from "$lib/types";
Expand All @@ -13,6 +13,20 @@
let maxCC = $derived(editControl.highResolution ? 127 - 32 : 127);
let device = $derived(
$editConfiguration ? deviceForId($editConfiguration.deviceId) : null,
);
let deviceSupportsHighResolution = $derived(
device &&
$editConfiguration &&
deviceHasCapability(
device,
"highResolution",
$editConfiguration.firmwareVersion,
),
);
const possibleChannels = Array.from({ length: 16 }, (_, i) => i + 1);
const touchChannel = () => {
Expand Down Expand Up @@ -68,7 +82,7 @@
max={maxCC}
/>
</dd>
{#if $editConfiguration && deviceHasCapability($editConfiguration, "highResolution")}
{#if deviceSupportsHighResolution}
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<dt class="hr-title" onclick={toggleHRMode}>High Resolution?</dt>
Expand Down
55 changes: 37 additions & 18 deletions src/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { gte } from "semver";
import { logger } from "$lib/logger";
import allKnownDevices from "./devices.json";

import type { Control, ControllerConfiguration } from "$lib/types";
import type {
Control,
ControllerConfiguration,
Device,
DeviceCapability,
} from "$lib/types";

export const isEquivalent = (
configA: ControllerConfiguration,
Expand Down Expand Up @@ -100,7 +105,13 @@ export const toSysexArray = (config: ControllerConfiguration) => {

// if you support high-res, we can actually append this to the end of an array;

if (deviceHasCapability(config, "highResolution")) {
if (
deviceHasCapability(
deviceForId(config.deviceId),
"highResolution",
versionArray.join("."),
)
) {
const usbHighResOffset = 84;
const trsHighResOffset = 87;

Expand Down Expand Up @@ -236,19 +247,29 @@ export const configFromSysexArray = (data: number[]) => {

usbControls.forEach((c) => (c.val = 0));

const usbHiresConfig = data.slice(81 + offset, 84 + offset);
const usbHighResolution = unpackHiresConfig(usbHiresConfig);
const device = deviceForId(deviceId);

usbControls.forEach((control, i) => {
control.highResolution = usbHighResolution[i];
});
let usbHighResolution = Array.from(Array(16)).map(() => false);
let trsHighResolution = Array.from(Array(16)).map(() => false);

const trsHiresConfig = data.slice(84 + offset, 87 + offset);
const trsHighResolution = unpackHiresConfig(trsHiresConfig);
if (
device &&
deviceHasCapability(device, "highResolution", firmwareVersion)
) {
const usbHiresConfig = data.slice(81 + offset, 84 + offset);
usbHighResolution = unpackHiresConfig(usbHiresConfig);

trsControls.forEach((control, i) => {
control.highResolution = trsHighResolution[i];
});
usbControls.forEach((control, i) => {
control.highResolution = usbHighResolution[i];
});

const trsHiresConfig = data.slice(84 + offset, 87 + offset);
trsHighResolution = unpackHiresConfig(trsHiresConfig);

trsControls.forEach((control, i) => {
control.highResolution = trsHighResolution[i];
});
}

return {
ledOn,
Expand Down Expand Up @@ -295,13 +316,11 @@ const packHiresConfig = (controls: Control[]) => {

export const deviceForId = (id: number) => allKnownDevices[id];

type Capability = "led" | "i2c" | "faderCalibration" | "highResolution";

export const deviceHasCapability = (
config: ControllerConfiguration,
capability: Capability,
device: Device,
capability: DeviceCapability,
firmwareVersion: string,
) => {
const device = deviceForId(config.deviceId);
if (!device) return;
if (!device.capabilities[capability]) {
return false;
Expand All @@ -311,5 +330,5 @@ export const deviceHasCapability = (
return true;
}

return gte(config.firmwareVersion, device.capabilities[capability]);
return gte(firmwareVersion, device.capabilities[capability]);
};
15 changes: 15 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ export type ControllerConfiguration = {
usbHighResolution: boolean[];
trsHighResolution: boolean[];
};

export type DeviceCapability =
| "led"
| "i2c"
| "faderCalibration"
| "highResolution";

export type Device = {
name: string;
controlCount: number;
capabilities: Partial<Record<DeviceCapability, string | boolean>>;
sendShortMessages?: boolean;
latestFirmwareVersion?: string;
firmwareUrl?: string;
};
21 changes: 18 additions & 3 deletions src/routes/Editing.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import Subhead from "$lib/components/Subhead.svelte";
import { Tab, TabList, TabPanel, Tabs } from "$lib/components/tabs";
import { deviceForId, isEquivalent, toSysexArray } from "$lib/configuration";
import {
deviceForId,
deviceHasCapability,
isEquivalent,
toSysexArray,
} from "$lib/configuration";
import { importConfig } from "$lib/import_export";
import { requestConfig, sendConfiguration } from "$lib/midi/sysex";
import {
Expand Down Expand Up @@ -62,6 +67,16 @@
let device = $derived(
$configuration ? deviceForId($configuration.deviceId) : null,
);
let deviceSupportsHighResolution = $derived(
$editConfiguration &&
device &&
deviceHasCapability(
device,
"highResolution",
$editConfiguration.firmwareVersion,
),
);
</script>

<Subhead title="Edit Configuration">
Expand Down Expand Up @@ -94,7 +109,7 @@
{/if}
{/each}
</div>
{#if device?.capabilities.highResolution}
{#if deviceSupportsHighResolution}
<ExplainHiResMode />
{/if}
</TabPanel>
Expand All @@ -107,7 +122,7 @@
{/if}
{/each}
</div>
{#if device?.capabilities.highResolution}
{#if deviceSupportsHighResolution}
<ExplainHiResMode />
{/if}
</TabPanel>
Expand Down

0 comments on commit a66a0e6

Please sign in to comment.