🐛 Fixes undefined I2C behavior
This commit is contained in:
@@ -1,29 +1,40 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import SettingsCard from "$lib/components/SettingsCard.svelte";
|
import SettingsCard from '$lib/components/SettingsCard.svelte';
|
||||||
import { onMount } from "svelte";
|
import { onMount } from 'svelte';
|
||||||
import { socket } from "$lib/stores";
|
import { socket } from '$lib/stores';
|
||||||
import type { I2CDevice } from "$lib/types/models";
|
import type { I2CDevice } from '$lib/types/models';
|
||||||
import { Connection } from "$lib/components/icons";
|
import { Connection } from '$lib/components/icons';
|
||||||
|
|
||||||
const i2cDevices = [
|
const i2cDevices = [
|
||||||
{address:30, part_number: "HMC5883", name: "3-Axis Digital Compass/Magnetometer IC"},
|
{ address: 30, part_number: 'HMC5883', name: '3-Axis Digital Compass/Magnetometer IC' },
|
||||||
{address:64, part_number: "PCA9685", name: "16-channel PWM driver default address"},
|
{ address: 64, part_number: 'PCA9685', name: '16-channel PWM driver default address' },
|
||||||
{address:72, part_number: "ADS1115", name: "4-channel 16-bit ADC"},
|
{ address: 72, part_number: 'ADS1115', name: '4-channel 16-bit ADC' },
|
||||||
{address:104, part_number: "MPU6050", name: "Six-Axis (Gyro + Accelerometer) MEMS MotionTracking™ Devices"},
|
{
|
||||||
{address:119, part_number: "BMP085", name: "Temp/Barometric"},
|
address: 104,
|
||||||
|
part_number: 'MPU6050',
|
||||||
|
name: 'Six-Axis (Gyro + Accelerometer) MEMS MotionTracking™ Devices'
|
||||||
|
},
|
||||||
|
{ address: 119, part_number: 'BMP085', name: 'Temp/Barometric' }
|
||||||
];
|
];
|
||||||
|
|
||||||
let active_devices:I2CDevice[] = [];
|
let active_devices: I2CDevice[] = [];
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
socket.on('i2cScan', handleScan);
|
socket.on('i2cScan', handleScan);
|
||||||
socket.sendEvent('i2cScan', "");
|
socket.sendEvent('i2cScan', '');
|
||||||
return () => socket.off('i2cScan', handleScan);
|
return () => socket.off('i2cScan', handleScan);
|
||||||
})
|
});
|
||||||
|
|
||||||
const handleScan = (data: any) => {
|
const handleScan = (data: any) => {
|
||||||
active_devices = data.addresses.map((address:number) => i2cDevices.find(device => device.address === address))
|
active_devices = data.addresses.map(
|
||||||
}
|
(address: number) =>
|
||||||
|
i2cDevices.find(device => device.address === address) || {
|
||||||
|
address,
|
||||||
|
part_number: 'Unknown',
|
||||||
|
name: 'Unknown'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SettingsCard collapsible={false}>
|
<SettingsCard collapsible={false}>
|
||||||
@@ -31,8 +42,12 @@
|
|||||||
<span slot="title">I<sup>2</sup>C</span>
|
<span slot="title">I<sup>2</sup>C</span>
|
||||||
|
|
||||||
<div class="grid">
|
<div class="grid">
|
||||||
{#each active_devices as device }
|
{#if active_devices.length === 0}
|
||||||
<div>[{device.address.toString(16)}] {device.part_number} - {device.name}</div>
|
<div>No I2C devices found</div>
|
||||||
{/each}
|
{:else}
|
||||||
|
{#each active_devices as device}
|
||||||
|
<div>[{device.address.toString(16)}] {device.part_number} - {device.name}</div>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</SettingsCard>
|
</SettingsCard>
|
||||||
|
|||||||
Reference in New Issue
Block a user