From b858d29ce8e0b90edc451d5d22c81feadc055c1f Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:21:26 +0200 Subject: [PATCH] Make read bulk fan speed read correctly from the array --- prosjekt.X/command-handler.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index 96bc956..7ae96f5 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -245,9 +245,23 @@ uint8_t route_command(int pos) { break; case READ_BULK_FAN_SPEED: if (context.fan == FAN1) { - return fan1_history[pos]; + // Validate that pos is valid, if not return 0 + if (pos >= 512) { return 0; } + + // Calculate the index position + int16_t index = fan1_history_index - pos; + if (index < 0) { index = 511-pos; } + + return fan1_history[index]; } else if (context.fan == FAN2) { - return fan2_history[pos]; + // Validate that pos is valid, if not return 0 + if (pos >= 512) { return 0; } + + // Calculate the index position + int16_t index = fan2_history_index - pos; + if (index < 0) { index = 511-pos; } + + return fan2_history[index]; } else { return 0; }