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; }