From 8effc58d9050456ae4affca723a3aeb0d186a2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Sat, 21 Mar 2026 13:36:01 +0100 Subject: [PATCH 1/2] Clamp prediction values to 39-400 mg/dL Fixes #495 --- LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift | 8 +++----- .../Controllers/Nightscout/DeviceStatusOpenAPS.swift | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift index fe10b62b9..a8d538e0c 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift @@ -72,11 +72,9 @@ extension MainViewController { while i <= toLoad { if i < prediction.count { let sgvValue = Int(round(prediction[i])) - // Skip values higher than 600 - if sgvValue <= 600 { - let prediction = ShareGlucoseData(sgv: sgvValue, date: predictionTime, direction: "flat") - predictionData.append(prediction) - } + let clampedValue = min(max(sgvValue, 39), 400) + let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat") + predictionData.append(prediction) predictionTime += 300 } i += 1 diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift index 57a940695..977f75987 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift @@ -182,7 +182,8 @@ extension MainViewController { minPredBG = min(minPredBG, predictionValue) maxPredBG = max(maxPredBG, predictionValue) - let prediction = ShareGlucoseData(sgv: Int(round(predictionValue)), date: predictionTime, direction: "flat") + let clampedValue = min(max(Int(round(predictionValue)), 39), 400) + let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat") predictionData.append(prediction) predictionTime += 300 } From 1d4fe4614fa35a1157ca80a009f8ab2e40aa3b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Thu, 9 Apr 2026 11:45:03 +0200 Subject: [PATCH 2/2] Extract prediction clamp range to named constants Replace hard-coded 39/400 mg/dL clamp bounds with globalVariables.minDisplayGlucose and maxDisplayGlucose, shared with the main-screen HIGH/LOW display logic so the range has a single source of truth. --- LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift | 2 +- .../Controllers/Nightscout/DeviceStatusOpenAPS.swift | 2 +- LoopFollow/Helpers/Globals.swift | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift index a8d538e0c..9eb5f6c43 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift @@ -72,7 +72,7 @@ extension MainViewController { while i <= toLoad { if i < prediction.count { let sgvValue = Int(round(prediction[i])) - let clampedValue = min(max(sgvValue, 39), 400) + let clampedValue = min(max(sgvValue, globalVariables.minDisplayGlucose), globalVariables.maxDisplayGlucose) let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat") predictionData.append(prediction) predictionTime += 300 diff --git a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift index 977f75987..407613301 100644 --- a/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift +++ b/LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift @@ -182,7 +182,7 @@ extension MainViewController { minPredBG = min(minPredBG, predictionValue) maxPredBG = max(maxPredBG, predictionValue) - let clampedValue = min(max(Int(round(predictionValue)), 39), 400) + let clampedValue = min(max(Int(round(predictionValue)), globalVariables.minDisplayGlucose), globalVariables.maxDisplayGlucose) let prediction = ShareGlucoseData(sgv: clampedValue, date: predictionTime, direction: "flat") predictionData.append(prediction) predictionTime += 300 diff --git a/LoopFollow/Helpers/Globals.swift b/LoopFollow/Helpers/Globals.swift index a93d5fa36..770ad7f70 100644 --- a/LoopFollow/Helpers/Globals.swift +++ b/LoopFollow/Helpers/Globals.swift @@ -11,4 +11,11 @@ enum globalVariables { static let dotCarb: Float = 5 static let dotBolus: Float = 5 static let dotOther: Float = 5 + + // Glucose display range (mg/dL) + // Values at or below the min are shown as "LOW" on the main display; + // values at or above the max are shown as "HIGH". Also used to clamp + // prediction values on the graph. + static let minDisplayGlucose: Int = 39 + static let maxDisplayGlucose: Int = 400 }