From 45cbc790170941b48a867d87b667f506c70b00c2 Mon Sep 17 00:00:00 2001 From: Jasem Mutlaq Date: Tue, 10 Sep 2024 07:16:46 +0300 Subject: [PATCH] Clamp guide ms to [1,999] range. Fixes #2111 --- drivers/telescope/lx200ap_v2.cpp | 33 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/drivers/telescope/lx200ap_v2.cpp b/drivers/telescope/lx200ap_v2.cpp index 7b3ce6b882..c9564fa8e1 100644 --- a/drivers/telescope/lx200ap_v2.cpp +++ b/drivers/telescope/lx200ap_v2.cpp @@ -63,7 +63,7 @@ enum APPECRecordingState }; // maximum guide pulse request to send to controller -#define MAX_LX200AP_PULSE_LEN 999 +#define MAX_LX200AP_PULSE_LEN 999u // The workaround for long pulses doesn't work! // #define DONT_SIMULATE_LONG_PULSES true @@ -1267,11 +1267,8 @@ IPState LX200AstroPhysicsV2::GuideNorth(uint32_t ms) GuideNSTID = 0; } - if (ms > MAX_LX200AP_PULSE_LEN) - { - LOGF_DEBUG("GuideNorth truncating %dms pulse to %dms", ms, MAX_LX200AP_PULSE_LEN); - ms = MAX_LX200AP_PULSE_LEN; - } + ms = std::clamp(ms, 1u, MAX_LX200AP_PULSE_LEN); + if (usePulseCommand) { APSendPulseCmd(PortFD, LX200_NORTH, ms); @@ -1299,11 +1296,9 @@ IPState LX200AstroPhysicsV2::GuideSouth(uint32_t ms) GuideNSTID = 0; } - if (ms > MAX_LX200AP_PULSE_LEN) - { - LOGF_DEBUG("GuideSouth truncating %dms pulse to %dms", ms, MAX_LX200AP_PULSE_LEN); - ms = MAX_LX200AP_PULSE_LEN; - } + // Clamp to within 1 to 9999 + ms = std::clamp(ms, 1u, MAX_LX200AP_PULSE_LEN); + if (usePulseCommand) { APSendPulseCmd(PortFD, LX200_SOUTH, ms); @@ -1331,11 +1326,9 @@ IPState LX200AstroPhysicsV2::GuideEast(uint32_t ms) GuideWETID = 0; } - if (ms > MAX_LX200AP_PULSE_LEN) - { - LOGF_DEBUG("GuideEast truncating %dms pulse to %dms", ms, MAX_LX200AP_PULSE_LEN); - ms = MAX_LX200AP_PULSE_LEN; - } + // Clamp to within 1 to 9999 + ms = std::clamp(ms, 1u, MAX_LX200AP_PULSE_LEN); + if (usePulseCommand) { APSendPulseCmd(PortFD, LX200_EAST, ms); @@ -1363,11 +1356,9 @@ IPState LX200AstroPhysicsV2::GuideWest(uint32_t ms) GuideWETID = 0; } - if (ms > MAX_LX200AP_PULSE_LEN) - { - LOGF_DEBUG("GuideWest truncating %dms pulse to %dms", ms, MAX_LX200AP_PULSE_LEN); - ms = MAX_LX200AP_PULSE_LEN; - } + // Clamp to within 1 to 9999 + ms = std::clamp(ms, 1u, MAX_LX200AP_PULSE_LEN); + if (usePulseCommand) { APSendPulseCmd(PortFD, LX200_WEST, ms);