Initial Commit
As of now, only the illuminance value itself is beeing updated, no calculations are done.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package eu.wa5p.lightmeter
|
||||
|
||||
data class LightmeterState(
|
||||
var illuminance: Double, // lux
|
||||
var aperture: Double, // f-stops
|
||||
var iso: Int,
|
||||
var shutterSpeed: Double // s
|
||||
)
|
||||
@@ -0,0 +1,171 @@
|
||||
package eu.wa5p.lightmeter
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.Sensor
|
||||
import android.hardware.SensorEvent
|
||||
import android.hardware.SensorEventListener
|
||||
import android.hardware.SensorManager
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import eu.wa5p.lightmeter.ui.theme.LightMeterTheme
|
||||
|
||||
const val TAG: String = "lightmeter MainActivity"
|
||||
|
||||
class MainActivity : ComponentActivity(), SensorEventListener {
|
||||
private lateinit var sensorManager: SensorManager
|
||||
private var illuminanceSensor: Sensor? = null
|
||||
private var lightmeterState = mutableStateOf(
|
||||
LightmeterState(
|
||||
illuminance = 0.0,
|
||||
aperture = 1.8,
|
||||
iso = 100,
|
||||
shutterSpeed = 0.01
|
||||
)
|
||||
)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
LightMeterTheme {
|
||||
LightmeterHomeScreen(state = lightmeterState )
|
||||
}
|
||||
}
|
||||
|
||||
// Get an instance of the sensor service, and use that to get an instance of
|
||||
// a particular sensor.
|
||||
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||
illuminanceSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
|
||||
}
|
||||
|
||||
override fun onSensorChanged(event: SensorEvent?) {
|
||||
event?.values?.let {
|
||||
for (v in it) {
|
||||
Log.d(TAG, "sensor changed: $v lux")
|
||||
lightmeterState.value = lightmeterState.value.copy(illuminance = v.toDouble())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
||||
Log.d(TAG, "accuracy changed: sensor: ${sensor?.name}. accuracy: $accuracy")
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
// Register a listener for the sensor.
|
||||
super.onResume()
|
||||
sensorManager.registerListener(this, illuminanceSensor, SensorManager.SENSOR_DELAY_NORMAL)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
// Be sure to unregister the sensor when the activity pauses.
|
||||
super.onPause()
|
||||
sensorManager.unregisterListener(this)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LightmeterListItem(
|
||||
label: String,
|
||||
value: String,
|
||||
unit: String
|
||||
) {
|
||||
Row(modifier = Modifier.padding(bottom = 8.dp)) {
|
||||
Text(
|
||||
modifier = Modifier.weight(0.4f).padding(all = 8.dp),
|
||||
text = label
|
||||
)
|
||||
Text(
|
||||
modifier = Modifier.weight(0.4f).padding(all = 8.dp),
|
||||
textAlign = TextAlign.Right,
|
||||
text = value
|
||||
)
|
||||
Text(
|
||||
modifier = Modifier.weight(0.2f).padding(all = 8.dp),
|
||||
text = unit
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun LightmeterListItemPreview() {
|
||||
LightMeterTheme {
|
||||
LightmeterListItem(
|
||||
label = "Illuminance",
|
||||
value = "1234",
|
||||
unit = "lux"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LightmeterHomeScreen(state: MutableState<LightmeterState>) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
LightmeterListItem(
|
||||
label = "Illuminance",
|
||||
value = "%.2f".format(state.value.illuminance),
|
||||
unit = "lux"
|
||||
)
|
||||
LightmeterListItem(
|
||||
label = "ISO",
|
||||
value = state.value.iso.toString(),
|
||||
unit = ""
|
||||
)
|
||||
LightmeterListItem(
|
||||
label = "Aperture",
|
||||
value = "%.2f".format(state.value.aperture),
|
||||
unit = "f"
|
||||
)
|
||||
LightmeterListItem(
|
||||
label = "Shutter Speed",
|
||||
value = "%.2f".format(state.value.shutterSpeed),
|
||||
unit = "s"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun LightmeterHomeScreenPreview() {
|
||||
LightMeterTheme {
|
||||
LightmeterHomeScreen(
|
||||
state = remember {
|
||||
mutableStateOf(
|
||||
LightmeterState(
|
||||
illuminance = 0.0,
|
||||
aperture = 1.8,
|
||||
iso = 100,
|
||||
shutterSpeed = 0.01
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package eu.wa5p.lightmeter.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val Purple80 = Color(0xFFD0BCFF)
|
||||
val PurpleGrey80 = Color(0xFFCCC2DC)
|
||||
val Pink80 = Color(0xFFEFB8C8)
|
||||
|
||||
val Purple40 = Color(0xFF6650a4)
|
||||
val PurpleGrey40 = Color(0xFF625b71)
|
||||
val Pink40 = Color(0xFF7D5260)
|
||||
@@ -0,0 +1,58 @@
|
||||
package eu.wa5p.lightmeter.ui.theme
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.dynamicDarkColorScheme
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
private val DarkColorScheme = darkColorScheme(
|
||||
primary = Purple80,
|
||||
secondary = PurpleGrey80,
|
||||
tertiary = Pink80
|
||||
)
|
||||
|
||||
private val LightColorScheme = lightColorScheme(
|
||||
primary = Purple40,
|
||||
secondary = PurpleGrey40,
|
||||
tertiary = Pink40
|
||||
|
||||
/* Other default colors to override
|
||||
background = Color(0xFFFFFBFE),
|
||||
surface = Color(0xFFFFFBFE),
|
||||
onPrimary = Color.White,
|
||||
onSecondary = Color.White,
|
||||
onTertiary = Color.White,
|
||||
onBackground = Color(0xFF1C1B1F),
|
||||
onSurface = Color(0xFF1C1B1F),
|
||||
*/
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun LightMeterTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
// Dynamic color is available on Android 12+
|
||||
dynamicColor: Boolean = true,
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val colorScheme = when {
|
||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
val context = LocalContext.current
|
||||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
||||
}
|
||||
|
||||
darkTheme -> DarkColorScheme
|
||||
else -> LightColorScheme
|
||||
}
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = Typography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package eu.wa5p.lightmeter.ui.theme
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
// Set of Material typography styles to start with
|
||||
val Typography = Typography(
|
||||
bodyLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
/* Other default text styles to override
|
||||
titleLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 22.sp,
|
||||
lineHeight = 28.sp,
|
||||
letterSpacing = 0.sp
|
||||
),
|
||||
labelSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 11.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
*/
|
||||
)
|
||||
Reference in New Issue
Block a user