From 279473a512adaf15bd7a0479e923b33654ff2072 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 18 Oct 2023 18:40:15 +0000 Subject: [PATCH] do not use default values for rendering servo; rely on min/max angle --- jacdac-dotnet | 2 +- jacdac-python | 2 +- jacdac-ts | 2 +- pxt-jacdac | 2 +- src/components/dashboard/DashboardServo.tsx | 23 ++++++++++++++++----- src/components/hooks/useThrottledValue.ts | 5 +++++ 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/jacdac-dotnet b/jacdac-dotnet index 1da49cff0df3..d880b659d8f4 160000 --- a/jacdac-dotnet +++ b/jacdac-dotnet @@ -1 +1 @@ -Subproject commit 1da49cff0df3c680bbd4e034e610cba2767b5401 +Subproject commit d880b659d8f4d74fd8ddc60c07b35cb66a57e97a diff --git a/jacdac-python b/jacdac-python index 07f897f4528e..3e018387083a 160000 --- a/jacdac-python +++ b/jacdac-python @@ -1 +1 @@ -Subproject commit 07f897f4528e3656bb867a48ead42ae225d88bf5 +Subproject commit 3e018387083abbf8e388ded8930169343726e05c diff --git a/jacdac-ts b/jacdac-ts index ef3f96defc61..3b80af72d58f 160000 --- a/jacdac-ts +++ b/jacdac-ts @@ -1 +1 @@ -Subproject commit ef3f96defc6125f5e6e3d98507498f0fa194152b +Subproject commit 3b80af72d58fabcaddc54fa2b51f44b9ccf58cc3 diff --git a/pxt-jacdac b/pxt-jacdac index 1483cf6003fb..d8afba90caba 160000 --- a/pxt-jacdac +++ b/pxt-jacdac @@ -1 +1 @@ -Subproject commit 1483cf6003fb9068c0a66255e78ccfae6a9fbb8c +Subproject commit d8afba90caba7f5f0c5c0306a3ee1849083de814 diff --git a/src/components/dashboard/DashboardServo.tsx b/src/components/dashboard/DashboardServo.tsx index 02c0c147bea1..5cf1bd08e2b2 100644 --- a/src/components/dashboard/DashboardServo.tsx +++ b/src/components/dashboard/DashboardServo.tsx @@ -13,10 +13,11 @@ import { ServoServer } from "../../../jacdac-ts/src/servers/servoserver" import { JDService } from "../../../jacdac-ts/src/jdom/service" import ServoWidget from "../widgets/ServoWidget" import useRegister from "../hooks/useRegister" +import DashboardRegisterValueFallback from "./DashboardRegisterValueFallback" function useActualAngle(service: JDService, visible: boolean) { const angleRegister = useRegister(service, ServoReg.Angle) - const [angle = 90] = useRegisterUnpackedValue<[number]>(angleRegister, { + const [angle] = useRegisterUnpackedValue<[number]>(angleRegister, { visible, }) const actualAngleRegister = useRegister(service, ServoReg.ActualAngle) @@ -52,12 +53,12 @@ export default function DashboardServo(props: DashboardServiceProps) { props ) const minAngleRegister = useRegister(service, ServoReg.MinAngle) - const [minAngle = -90] = useRegisterUnpackedValue<[number]>( + const [minAngle] = useRegisterUnpackedValue<[number]>( minAngleRegister, props ) const maxAngleRegister = useRegister(service, ServoReg.MaxAngle) - const [maxAngle = 90] = useRegisterUnpackedValue<[number]>( + const [maxAngle] = useRegisterUnpackedValue<[number]>( maxAngleRegister, props ) @@ -65,12 +66,20 @@ export default function DashboardServo(props: DashboardServiceProps) { const [responseSpeed = SG90_RESPONSE_SPEED] = useRegisterUnpackedValue< [number] >(responseSpeedRegister, { visible }) + const server = useServiceServer(service) + + if (angle === undefined) + return + if (minAngle === undefined) + return + if (maxAngle === undefined) + return + const rotationalSpeed = 60 / responseSpeed const continuous = /cont=1/.test(clientVariant) const throttle = ((angle - minAngle) / (maxAngle - minAngle)) * 200 - 100 - const server = useServiceServer(service) const color = server ? "secondary" : "primary" const toggleOff = () => enabledRegister.sendSetBoolAsync(!enabled, true) @@ -104,7 +113,11 @@ export default function DashboardServo(props: DashboardServiceProps) { enabled={enabled} toggleOff={toggleOff} widgetSize={widgetSize} - rotationRate={continuous ? (throttle * rotationalSpeed) / 100 : undefined} + rotationRate={ + continuous + ? (throttle * rotationalSpeed) / 100 + : undefined + } visible={visible} /> diff --git a/src/components/hooks/useThrottledValue.ts b/src/components/hooks/useThrottledValue.ts index b45e14d00a44..b3240404bd92 100644 --- a/src/components/hooks/useThrottledValue.ts +++ b/src/components/hooks/useThrottledValue.ts @@ -17,6 +17,11 @@ export default function useThrottledValue( return false } + // not a value yet + if (isNaN(value)) { + return false + } + // increment towards value const dt = time / 1000 // s const error = value - animated