From 7b7003d3f53d5d68d815a12d122abbeac6ceacff Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Tue, 3 Feb 2026 19:31:21 -0400 Subject: [PATCH 1/9] Add drafty human friendly current metrics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, the metircs are always shown with the in milliampere (e.i, using the "mA" unit). A nicer way would be to have thoses metrics shown in more friendly way (e.g, 1A -> 1A, 1000A -> 1kA, 0.000001A -> 1μA...etc). At this point, this is just a draft of how it would be implemented. --- .../core/model/util/UnitConversions.kt | 24 +++++++++++++++++++ .../node/metrics/EnvironmentMetrics.kt | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index b4c0f83423..a8019195c9 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -16,7 +16,10 @@ */ package org.meshtastic.core.model.util +import kotlin.math.floor import kotlin.math.ln +import kotlin.math.log10 +import kotlin.math.pow import kotlin.math.roundToInt object UnitConversions { @@ -53,4 +56,25 @@ object UnitConversions { val alpha = (a * tempCelsius) / (b + tempCelsius) + ln(humidity / 100f) return (b * alpha) / (a - alpha) } + + fun numberToHuman(number: Flaot): String { + val unitsMap = mapOf("Nano" to -9, "Micro" to -6, "Milli" to -3 , "Unit" to 0, + "Thousand" to 3, "Million" to 6, "Billion" to 9) + var exponent = floor(log10(number)).toInt() + + if (exponent.mod(3) != 0) { + if ((-9..-7).contains(exponent)) exponent = -9 + if ((-6..-4).contains(exponent)) exponent = -6 + if ((-3..-1).contains(exponent)) exponent = -3 + if ((0..2).contains(exponent)) exponent = 0 + if ((3..5).contains(exponent)) exponent = 3 + if ((6..0).contains(exponent)) exponent = 6 + if ((9..12).contains(exponent)) exponent = 9 + } + + val unit = unitsMap.entries.associate{(k,v)-> v to k}.get(exponent) + val value = (number/10.0.pow(exponent)) + + return "$value $unit" + } } diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt index ec14998cc1..ed7ebb06e9 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt @@ -374,7 +374,8 @@ private fun VoltageCurrentDisplay(envMetrics: TelemetryProtos.EnvironmentMetrics if (hasCurrent) { val current = envMetrics.current!! Text( - text = "%s %.2f mA".format(stringResource(Res.string.current), current), + text = "%s %s".format(stringResource(Res.string.current), + numberToHuman(current)), color = MaterialTheme.colorScheme.onSurface, fontSize = MaterialTheme.typography.labelLarge.fontSize, ) From c7ab3f7ece8110e5daf859caa26f28688de62c3d Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Thu, 5 Feb 2026 15:00:20 -0400 Subject: [PATCH 2/9] Fix ranges and add custom units The ranges were wrong and therefore gave wrong result so they've been fixed. Also, there's possiblity of the function using custom units instead of the default ones. This new parameter is optional as one may be just fine the default units. --- .../core/model/util/UnitConversions.kt | 18 ++++++++++++------ .../feature/node/metrics/EnvironmentMetrics.kt | 5 ++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index a8019195c9..b91308117b 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -57,21 +57,27 @@ object UnitConversions { return (b * alpha) / (a - alpha) } - fun numberToHuman(number: Flaot): String { + fun numberToHuman(number: Flaot, units: Map = emptyMap()): String { val unitsMap = mapOf("Nano" to -9, "Micro" to -6, "Milli" to -3 , "Unit" to 0, "Thousand" to 3, "Million" to 6, "Billion" to 9) var exponent = floor(log10(number)).toInt() if (exponent.mod(3) != 0) { - if ((-9..-7).contains(exponent)) exponent = -9 - if ((-6..-4).contains(exponent)) exponent = -6 - if ((-3..-1).contains(exponent)) exponent = -3 - if ((0..2).contains(exponent)) exponent = 0 + if ((-12..-9).contains(exponent)) exponent = -9 + if ((-8..-6).contains(exponent)) exponent = -6 + if ((-5..-3).contains(exponent)) exponent = -3 + if ((-2..2).contains(exponent)) exponent = 0 if ((3..5).contains(exponent)) exponent = 3 - if ((6..0).contains(exponent)) exponent = 6 + if ((6..8).contains(exponent)) exponent = 6 if ((9..12).contains(exponent)) exponent = 9 } + var exponentsMap = unitsMap.entries.associate { (k, v) -> v to k }.toMutableMap() + units.forEach { (unit_key, custom_v) -> + val lookup_key = exponentsMap.filterValues { it == unit_key }.keys; + if (lookup_key.iterator().hasNext()) exponentsMap.put(lookup_key.iterator().next(), custom_v) + } + val unit = unitsMap.entries.associate{(k,v)-> v to k}.get(exponent) val value = (number/10.0.pow(exponent)) diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt index ed7ebb06e9..07c8e573c0 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt @@ -373,9 +373,12 @@ private fun VoltageCurrentDisplay(envMetrics: TelemetryProtos.EnvironmentMetrics } if (hasCurrent) { val current = envMetrics.current!! + val ampereUnits = mapOf("Nano" to "nA", "Micro" to "μA", "Milli" to "mA" , + "Unit" to "A", "Thousand" to "kA", "Million" to "MA", + "Billion" to "GA") Text( text = "%s %s".format(stringResource(Res.string.current), - numberToHuman(current)), + numberToHuman(current, ampereUnits)), color = MaterialTheme.colorScheme.onSurface, fontSize = MaterialTheme.typography.labelLarge.fontSize, ) From 9bd9c9474adff3dfc56ae016c14096b72f51e13c Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Thu, 5 Feb 2026 17:31:04 -0400 Subject: [PATCH 3/9] Apply spotless Spotless has been apply to fix formatting and linting errors. --- .../core/model/util/UnitConversions.kt | 24 ++++++++++++------- .../node/metrics/EnvironmentMetrics.kt | 16 +++++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index b91308117b..5decd11c55 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -57,9 +57,17 @@ object UnitConversions { return (b * alpha) / (a - alpha) } - fun numberToHuman(number: Flaot, units: Map = emptyMap()): String { - val unitsMap = mapOf("Nano" to -9, "Micro" to -6, "Milli" to -3 , "Unit" to 0, - "Thousand" to 3, "Million" to 6, "Billion" to 9) + fun numberToHuman(number: Float, units: Map = emptyMap()): String { + val unitsMap = + mapOf( + "Nano" to -9, + "Micro" to -6, + "Milli" to -3, + "Unit" to 0, + "Thousand" to 3, + "Million" to 6, + "Billion" to 9, + ) var exponent = floor(log10(number)).toInt() if (exponent.mod(3) != 0) { @@ -73,13 +81,13 @@ object UnitConversions { } var exponentsMap = unitsMap.entries.associate { (k, v) -> v to k }.toMutableMap() - units.forEach { (unit_key, custom_v) -> - val lookup_key = exponentsMap.filterValues { it == unit_key }.keys; - if (lookup_key.iterator().hasNext()) exponentsMap.put(lookup_key.iterator().next(), custom_v) + units.forEach { (unitKey, customUnit) -> + val lookupKey = exponentsMap.filterValues { it == unitKey }.keys + if (lookupKey.iterator().hasNext()) exponentsMap.put(lookupKey.iterator().next(), customUnit) } - val unit = unitsMap.entries.associate{(k,v)-> v to k}.get(exponent) - val value = (number/10.0.pow(exponent)) + val unit = unitsMap.entries.associate { (k, v) -> v to k }.get(exponent) + val value = (number / 10.0.pow(exponent)) return "$value $unit" } diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt index 07c8e573c0..6c90994577 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt @@ -373,12 +373,18 @@ private fun VoltageCurrentDisplay(envMetrics: TelemetryProtos.EnvironmentMetrics } if (hasCurrent) { val current = envMetrics.current!! - val ampereUnits = mapOf("Nano" to "nA", "Micro" to "μA", "Milli" to "mA" , - "Unit" to "A", "Thousand" to "kA", "Million" to "MA", - "Billion" to "GA") + val ampereUnits = + mapOf( + "Nano" to "nA", + "Micro" to "μA", + "Milli" to "mA", + "Unit" to "A", + "Thousand" to "kA", + "Million" to "MA", + "Billion" to "GA", + ) Text( - text = "%s %s".format(stringResource(Res.string.current), - numberToHuman(current, ampereUnits)), + text = "%s %s".format(stringResource(Res.string.current), numberToHuman(current, ampereUnits)), color = MaterialTheme.colorScheme.onSurface, fontSize = MaterialTheme.typography.labelLarge.fontSize, ) From a59691742ffa2ab499e5eb684d0d6a1aaf84c4a5 Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Fri, 6 Feb 2026 09:43:47 -0400 Subject: [PATCH 4/9] Make constants out of Units Both base and ampere units have been change into constants as gradle suggested it and also because it makes more sense. --- .../core/model/util/UnitConversions.kt | 28 +++++++++++-------- .../node/metrics/EnvironmentMetrics.kt | 14 ++-------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index 5decd11c55..caf48b2d2d 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -24,6 +24,19 @@ import kotlin.math.roundToInt object UnitConversions { + val BASE_UNITS = + mapOf("Nano" to -9, "Micro" to -6, "Milli" to -3, "Unit" to 0, "Thousand" to 3, "Million" to 6, "Billion" to 9) + val AMPERE_UNITS = + mapOf( + "Nano" to "nA", + "Micro" to "μA", + "Milli" to "mA", + "Unit" to "A", + "Thousand" to "kA", + "Million" to "MA", + "Billion" to "GA", + ) + @Suppress("MagicNumber") fun celsiusToFahrenheit(celsius: Float): Float = (celsius * 1.8F) + 32 @@ -57,17 +70,8 @@ object UnitConversions { return (b * alpha) / (a - alpha) } + @Suppress("MagicNumber") fun numberToHuman(number: Float, units: Map = emptyMap()): String { - val unitsMap = - mapOf( - "Nano" to -9, - "Micro" to -6, - "Milli" to -3, - "Unit" to 0, - "Thousand" to 3, - "Million" to 6, - "Billion" to 9, - ) var exponent = floor(log10(number)).toInt() if (exponent.mod(3) != 0) { @@ -80,13 +84,13 @@ object UnitConversions { if ((9..12).contains(exponent)) exponent = 9 } - var exponentsMap = unitsMap.entries.associate { (k, v) -> v to k }.toMutableMap() + var exponentsMap = BASE_UNITS.entries.associate { (k, v) -> v to k }.toMutableMap() units.forEach { (unitKey, customUnit) -> val lookupKey = exponentsMap.filterValues { it == unitKey }.keys if (lookupKey.iterator().hasNext()) exponentsMap.put(lookupKey.iterator().next(), customUnit) } - val unit = unitsMap.entries.associate { (k, v) -> v to k }.get(exponent) + val unit = BASE_UNITS.entries.associate { (k, v) -> v to k }.get(exponent) val value = (number / 10.0.pow(exponent)) return "$value $unit" diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt index 6c90994577..c43be370d3 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt @@ -64,7 +64,9 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoScrollState import kotlinx.coroutines.launch import org.jetbrains.compose.resources.stringResource import org.meshtastic.core.model.TelemetryType +import org.meshtastic.core.model.util.UnitConversions.AMPERE_UNITS import org.meshtastic.core.model.util.UnitConversions.celsiusToFahrenheit +import org.meshtastic.core.model.util.UnitConversions.numberToHuman import org.meshtastic.core.strings.Res import org.meshtastic.core.strings.current import org.meshtastic.core.strings.env_metrics_log @@ -373,18 +375,8 @@ private fun VoltageCurrentDisplay(envMetrics: TelemetryProtos.EnvironmentMetrics } if (hasCurrent) { val current = envMetrics.current!! - val ampereUnits = - mapOf( - "Nano" to "nA", - "Micro" to "μA", - "Milli" to "mA", - "Unit" to "A", - "Thousand" to "kA", - "Million" to "MA", - "Billion" to "GA", - ) Text( - text = "%s %s".format(stringResource(Res.string.current), numberToHuman(current, ampereUnits)), + text = "%s %s".format(stringResource(Res.string.current), numberToHuman(current, AMPERE_UNITS)), color = MaterialTheme.colorScheme.onSurface, fontSize = MaterialTheme.typography.labelLarge.fontSize, ) From 9207994dc23e4849e6583fc9cc8ce8fb07132a8c Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Sat, 7 Feb 2026 17:30:59 -0400 Subject: [PATCH 5/9] Fix formatting, exponent logic and duplication The original formatting was the number with two decimal places (e.i, "% 2f") so the returned value is formatted that way now. The exponent logic wasn't straightforward and complex? It's been replaced the following formulae `(exponent / 3) * 3` which will round down to the nearest multiple of 3 in the exponent calculation is not a multiple of 3. The base unit map was inverted a second time to assign the unit so it's been updated. --- .../core/model/util/UnitConversions.kt | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index caf48b2d2d..fa32231d3e 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -16,6 +16,7 @@ */ package org.meshtastic.core.model.util +import java.lang.String.format import kotlin.math.floor import kotlin.math.ln import kotlin.math.log10 @@ -73,26 +74,17 @@ object UnitConversions { @Suppress("MagicNumber") fun numberToHuman(number: Float, units: Map = emptyMap()): String { var exponent = floor(log10(number)).toInt() - - if (exponent.mod(3) != 0) { - if ((-12..-9).contains(exponent)) exponent = -9 - if ((-8..-6).contains(exponent)) exponent = -6 - if ((-5..-3).contains(exponent)) exponent = -3 - if ((-2..2).contains(exponent)) exponent = 0 - if ((3..5).contains(exponent)) exponent = 3 - if ((6..8).contains(exponent)) exponent = 6 - if ((9..12).contains(exponent)) exponent = 9 - } + if (exponent.mod(3) != 0 && exponent in -11..11) exponent = (exponent / 3) * 3 var exponentsMap = BASE_UNITS.entries.associate { (k, v) -> v to k }.toMutableMap() units.forEach { (unitKey, customUnit) -> val lookupKey = exponentsMap.filterValues { it == unitKey }.keys - if (lookupKey.iterator().hasNext()) exponentsMap.put(lookupKey.iterator().next(), customUnit) + if (lookupKey.iterator().hasNext()) exponentsMap[lookupKey.iterator().next()] = customUnit } - val unit = BASE_UNITS.entries.associate { (k, v) -> v to k }.get(exponent) + val unit = exponentsMap[exponent] val value = (number / 10.0.pow(exponent)) - return "$value $unit" + return "${"%.2f".format(value)} $unit" } } From af0d8923e47663b000056569d94ace065484a00f Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Sun, 8 Feb 2026 18:28:08 -0400 Subject: [PATCH 6/9] Validate input and transform number into base unit This makes sure that number less than or equal to 0 are handle before attempting calculations and "N/A" will be returned in that case. Metrics like current and distance are stored respectively in milliampere (mA) and millimeters (mm) so before doing calculations and determining the units, the metrics have to be converted in base unit (e.i, divided by 1000) first. --- .../kotlin/org/meshtastic/core/model/util/UnitConversions.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index fa32231d3e..e9b319f87d 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -73,7 +73,9 @@ object UnitConversions { @Suppress("MagicNumber") fun numberToHuman(number: Float, units: Map = emptyMap()): String { - var exponent = floor(log10(number)).toInt() + if (number <= 0) return "N/A" + + var exponent = floor(log10(number / 1000.0)).toInt() if (exponent.mod(3) != 0 && exponent in -11..11) exponent = (exponent / 3) * 3 var exponentsMap = BASE_UNITS.entries.associate { (k, v) -> v to k }.toMutableMap() From ac13f4c32f322b9241084a6a7a1191cc672d6b02 Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Sun, 8 Feb 2026 19:25:58 -0400 Subject: [PATCH 7/9] Remove custom unit part We eventually decided to display metrics in base unit to make things simpler so therefore mapping exponent to unit is no longer needed. --- .../core/model/util/UnitConversions.kt | 30 ++----------------- .../node/metrics/EnvironmentMetrics.kt | 7 ++--- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index e9b319f87d..b1aa88cf9c 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -16,7 +16,6 @@ */ package org.meshtastic.core.model.util -import java.lang.String.format import kotlin.math.floor import kotlin.math.ln import kotlin.math.log10 @@ -24,20 +23,6 @@ import kotlin.math.pow import kotlin.math.roundToInt object UnitConversions { - - val BASE_UNITS = - mapOf("Nano" to -9, "Micro" to -6, "Milli" to -3, "Unit" to 0, "Thousand" to 3, "Million" to 6, "Billion" to 9) - val AMPERE_UNITS = - mapOf( - "Nano" to "nA", - "Micro" to "μA", - "Milli" to "mA", - "Unit" to "A", - "Thousand" to "kA", - "Million" to "MA", - "Billion" to "GA", - ) - @Suppress("MagicNumber") fun celsiusToFahrenheit(celsius: Float): Float = (celsius * 1.8F) + 32 @@ -72,21 +57,12 @@ object UnitConversions { } @Suppress("MagicNumber") - fun numberToHuman(number: Float, units: Map = emptyMap()): String { - if (number <= 0) return "N/A" + fun convertToBaseUnit(number: Float): Float { + if (number <= 0) return 0f var exponent = floor(log10(number / 1000.0)).toInt() if (exponent.mod(3) != 0 && exponent in -11..11) exponent = (exponent / 3) * 3 - var exponentsMap = BASE_UNITS.entries.associate { (k, v) -> v to k }.toMutableMap() - units.forEach { (unitKey, customUnit) -> - val lookupKey = exponentsMap.filterValues { it == unitKey }.keys - if (lookupKey.iterator().hasNext()) exponentsMap[lookupKey.iterator().next()] = customUnit - } - - val unit = exponentsMap[exponent] - val value = (number / 10.0.pow(exponent)) - - return "${"%.2f".format(value)} $unit" + return number / 10f.pow(exponent) } } diff --git a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt index c43be370d3..980cf34018 100644 --- a/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt +++ b/feature/node/src/main/kotlin/org/meshtastic/feature/node/metrics/EnvironmentMetrics.kt @@ -64,9 +64,8 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoScrollState import kotlinx.coroutines.launch import org.jetbrains.compose.resources.stringResource import org.meshtastic.core.model.TelemetryType -import org.meshtastic.core.model.util.UnitConversions.AMPERE_UNITS import org.meshtastic.core.model.util.UnitConversions.celsiusToFahrenheit -import org.meshtastic.core.model.util.UnitConversions.numberToHuman +import org.meshtastic.core.model.util.UnitConversions.convertToBaseUnit import org.meshtastic.core.strings.Res import org.meshtastic.core.strings.current import org.meshtastic.core.strings.env_metrics_log @@ -374,9 +373,9 @@ private fun VoltageCurrentDisplay(envMetrics: TelemetryProtos.EnvironmentMetrics ) } if (hasCurrent) { - val current = envMetrics.current!! + val current = convertToBaseUnit(envMetrics.current!!) Text( - text = "%s %s".format(stringResource(Res.string.current), numberToHuman(current, AMPERE_UNITS)), + text = "%s %.2f A".format(stringResource(Res.string.current), current), color = MaterialTheme.colorScheme.onSurface, fontSize = MaterialTheme.typography.labelLarge.fontSize, ) From ba614f0ebc4750fb1432d1cf1bcc58390002f7bf Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Sun, 8 Feb 2026 21:43:50 -0400 Subject: [PATCH 8/9] Add tests for `convertToBaseUnit` function Tests cases have been added to make sure the fonction works as expected. --- .../meshtastic/core/model/util/UnitConversions.kt | 8 +++++++- .../core/model/util/UnitConversionsTest.kt | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index b1aa88cf9c..468093eb74 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -46,7 +46,7 @@ object UnitConversions { } /** - * Calculated the dew point based on the Magnus-Tetens approximation which is a widely used formula for calculating + * Calculates the dew point based on the Magnus-Tetens approximation which is a widely used formula for calculating * dew point temperature. */ @Suppress("MagicNumber") @@ -56,6 +56,12 @@ object UnitConversions { return (b * alpha) / (a - alpha) } + /** + * Converts numbers from milli to unit. + * examples: + * - 1000 milliamperes will be converted into 1 ampere, + * - 100 millimeters to 0.1 meters + */ @Suppress("MagicNumber") fun convertToBaseUnit(number: Float): Float { if (number <= 0) return 0f diff --git a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt index 07832a9036..110891303c 100644 --- a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt +++ b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt @@ -115,4 +115,16 @@ class UnitConversionsTest { val zeroHumidity = UnitConversions.calculateDewPoint(20.0f, 0.0f) assertTrue("Expected NaN for 0% humidity", zeroHumidity.isNaN()) } + + @Test + fun `convertToBaseUnit converts correctly`() { + mapOf( + 18200f to 18.2f, + -4f to 0.004f, + 0f to 0f, + -9f to 0f, + ).forEach { (number, expectedvalue) -> + assertEquals(expectedvalue, UnitConversions.convertToBaseUnit(number)) + } + } } From 2f3eeba556425b0f0ec0bf14c06b1a5e7ee575b0 Mon Sep 17 00:00:00 2001 From: NeimadTL Date: Mon, 9 Feb 2026 13:44:31 -0400 Subject: [PATCH 9/9] Apply spotless Spotless have been applied sucessfully but not sure issues are resolved (attempt#1). --- .../org/meshtastic/core/model/util/UnitConversions.kt | 7 +++---- .../org/meshtastic/core/model/util/UnitConversionsTest.kt | 7 +------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt index 468093eb74..cded16e7ae 100644 --- a/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt +++ b/core/model/src/main/kotlin/org/meshtastic/core/model/util/UnitConversions.kt @@ -57,10 +57,9 @@ object UnitConversions { } /** - * Converts numbers from milli to unit. - * examples: - * - 1000 milliamperes will be converted into 1 ampere, - * - 100 millimeters to 0.1 meters + * Converts numbers from milli to unit. examples: + * - 1000 milliamperes will be converted into 1 ampere, + * - 100 millimeters to 0.1 meters */ @Suppress("MagicNumber") fun convertToBaseUnit(number: Float): Float { diff --git a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt index 110891303c..5cbe637df4 100644 --- a/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt +++ b/core/model/src/test/kotlin/org/meshtastic/core/model/util/UnitConversionsTest.kt @@ -118,12 +118,7 @@ class UnitConversionsTest { @Test fun `convertToBaseUnit converts correctly`() { - mapOf( - 18200f to 18.2f, - -4f to 0.004f, - 0f to 0f, - -9f to 0f, - ).forEach { (number, expectedvalue) -> + mapOf(18200f to 18.2f, -4f to 0.004f, 0f to 0f, -9f to 0f).forEach { (number, expectedvalue) -> assertEquals(expectedvalue, UnitConversions.convertToBaseUnit(number)) } }