Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
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 {

@Suppress("MagicNumber")
fun celsiusToFahrenheit(celsius: Float): Float = (celsius * 1.8F) + 32

Expand All @@ -44,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")
Expand All @@ -53,4 +55,19 @@ object UnitConversions {
val alpha = (a * tempCelsius) / (b + tempCelsius) + ln(humidity / 100f)
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

var exponent = floor(log10(number / 1000.0)).toInt()
if (exponent.mod(3) != 0 && exponent in -11..11) exponent = (exponent / 3) * 3

return number / 10f.pow(exponent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ 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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.stringResource
import org.meshtastic.core.model.TelemetryType
import org.meshtastic.core.model.util.UnitConversions.celsiusToFahrenheit
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
Expand Down Expand Up @@ -374,9 +375,9 @@ private fun VoltageCurrentDisplay(envMetrics: org.meshtastic.proto.EnvironmentMe
)
}
if (hasCurrent) {
val current = envMetrics.current!!
val current = convertToBaseUnit(envMetrics.current!!)
Text(
text = "%s %.2f mA".format(stringResource(Res.string.current), current),
text = "%s %.2f A".format(stringResource(Res.string.current), current),
color = MaterialTheme.colorScheme.onSurface,
fontSize = MaterialTheme.typography.labelLarge.fontSize,
)
Expand Down
Loading