Skip to content

Commit ad0b6c5

Browse files
authored
Merge branch 'dev' into dev
2 parents 3cd55f6 + ccf9a5f commit ad0b6c5

4 files changed

Lines changed: 60 additions & 12 deletions

File tree

plugins/eversense/src/main/kotlin/com/nightscout/eversense/models/EversenseState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class EversenseState {
2121

2222
var recentGlucoseDatetime: Long = 0
2323
var recentGlucoseValue: Int = 0
24+
var lastGlucoseRaw: Int = 0
2425

2526
var settings = EversenseTransmitterSettings()
2627
}

plugins/eversense/src/main/kotlin/com/nightscout/eversense/packets/Eversense365Communicator.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.nightscout.eversense.packets.e365.GetGlucoseDataPacket
1515
import com.nightscout.eversense.packets.e365.GetPatientSettingsPacket
1616
import com.nightscout.eversense.packets.e365.GetSensorInformationPacket
1717
import com.nightscout.eversense.packets.e365.SetCurrentDateTimePacket
18+
import com.nightscout.eversense.util.EselSmoothing
1819
import com.nightscout.eversense.util.EversenseLogger
1920
import com.nightscout.eversense.util.StorageKeys
2021
import kotlinx.serialization.json.Json
@@ -43,11 +44,17 @@ class Eversense365Communicator {
4344
return
4445
}
4546

47+
var currentGlucose = glucoseData.glucoseInMgDl
48+
if (state.useSmoothing && state.recentGlucoseValue > 0 && state.lastGlucoseRaw > 0) {
49+
currentGlucose = EselSmoothing.smooth(currentGlucose, state.recentGlucoseValue, state.lastGlucoseRaw)
50+
}
51+
4652
val result = mutableListOf<EversenseCGMResult>()
4753
state.recentGlucoseDatetime = glucoseData.datetime
48-
state.recentGlucoseValue = glucoseData.glucoseInMgDl
54+
state.recentGlucoseValue = currentGlucose
55+
state.lastGlucoseRaw = glucoseData.glucoseInMgDl
4956
result += EversenseCGMResult(
50-
glucoseInMgDl = glucoseData.glucoseInMgDl,
57+
glucoseInMgDl = currentGlucose,
5158
datetime = glucoseData.datetime,
5259
trend = glucoseData.trend
5360
)

plugins/eversense/src/main/kotlin/com/nightscout/eversense/packets/EversenseE3Communicator.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import com.nightscout.eversense.packets.e3.SetSettingRateFallingThresholdPacket
5252
import com.nightscout.eversense.packets.e3.SetSettingRateRisingEnabledPacket
5353
import com.nightscout.eversense.packets.e3.SetSettingRateRisingThresholdPacket
5454
import com.nightscout.eversense.packets.e3.SetSettingVibratePacket
55+
import com.nightscout.eversense.util.EselSmoothing
5556
import com.nightscout.eversense.util.EversenseLogger
5657
import com.nightscout.eversense.util.StorageKeys
5758
import kotlinx.serialization.json.Json
@@ -75,24 +76,30 @@ class EversenseE3Communicator {
7576

7677
try {
7778
EversenseLogger.debug(TAG, "Reading current glucose...")
78-
val currentGlucose = gatt.writePacket<GetCurrentGlucosePacket.Response>(GetCurrentGlucosePacket())
79-
if (currentGlucose.datetime <= state.recentGlucoseDatetime) {
80-
EversenseLogger.warning(TAG, "Glucose data is still recent after reading - currentReading: ${currentGlucose.datetime}, lastReading: ${state.recentGlucoseDatetime}")
79+
val glucoseData = gatt.writePacket<GetCurrentGlucosePacket.Response>(GetCurrentGlucosePacket())
80+
if (glucoseData.datetime <= state.recentGlucoseDatetime) {
81+
EversenseLogger.warning(TAG, "Glucose data is still recent after reading - currentReading: ${glucoseData.datetime}, lastReading: ${state.recentGlucoseDatetime}")
8182
return
8283
}
8384

84-
if (currentGlucose.glucoseInMgDl > 1000) {
85-
EversenseLogger.error(TAG, "recentGlucose exceeds range - received: ${currentGlucose.glucoseInMgDl}")
85+
if (glucoseData.glucoseInMgDl > 1000) {
86+
EversenseLogger.error(TAG, "recentGlucose exceeds range - received: ${glucoseData.glucoseInMgDl}")
8687
return
8788
}
8889

90+
var currentGlucose = glucoseData.glucoseInMgDl
91+
if (state.useSmoothing && state.recentGlucoseValue > 0 && state.lastGlucoseRaw > 0) {
92+
currentGlucose = EselSmoothing.smooth(currentGlucose, state.recentGlucoseValue, state.lastGlucoseRaw)
93+
}
94+
8995
val result = mutableListOf<EversenseCGMResult>()
90-
state.recentGlucoseDatetime = currentGlucose.datetime
91-
state.recentGlucoseValue = currentGlucose.glucoseInMgDl
96+
state.recentGlucoseDatetime = glucoseData.datetime
97+
state.recentGlucoseValue = currentGlucose
98+
state.lastGlucoseRaw = glucoseData.glucoseInMgDl
9299
result += EversenseCGMResult(
93-
glucoseInMgDl = currentGlucose.glucoseInMgDl,
94-
datetime = currentGlucose.datetime,
95-
trend = currentGlucose.trend
100+
glucoseInMgDl = currentGlucose,
101+
datetime = glucoseData.datetime,
102+
trend = glucoseData.trend
96103
)
97104

98105
// TODO: read history for backfill
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.nightscout.eversense.util
2+
3+
import kotlin.math.min
4+
import kotlin.math.round
5+
6+
class EselSmoothing {
7+
companion object {
8+
private const val factor: Double = 0.3
9+
private const val correction: Double = 0.5
10+
private const val descent_factor: Double = 0.0
11+
12+
fun smooth(currentRaw: Int, lastSmooth: Int, lastRaw: Int): Int {
13+
val value = currentRaw.toDouble()
14+
15+
// exponential smoothing, see https://en.wikipedia.org/wiki/Exponential_smoothing
16+
// y'[t]=y'[t-1] + (a*(y-y'[t-1])) = a*y+(1-a)*y'[t-1]
17+
// factor is a, value is y, lastSmooth y'[t-1], smooth y'
18+
// factor between 0 and 1, default 0.3
19+
// factor = 0: always last smooth (constant)
20+
// factor = 1: no smoothing
21+
var smooth: Double = lastSmooth + (factor * (value - lastSmooth))
22+
23+
// correction: average of delta between raw and smooth value, added to smooth with correction factor
24+
// correction between 0 and 1, default 0.5
25+
// correction = 0: no correction, full smoothing
26+
// correction > 0: less smoothing
27+
smooth += (correction * ((lastRaw - lastSmooth) + (value - smooth)) / 2.0)
28+
smooth -= descent_factor * (smooth - min(value, smooth))
29+
30+
return round(smooth).toInt()
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)