Skip to content

Commit

Permalink
Improved camera trigger and ui thread
Browse files Browse the repository at this point in the history
  • Loading branch information
NiroDeveloper committed Jun 17, 2024
1 parent 9e8ed29 commit 509f22d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
48 changes: 26 additions & 22 deletions app/src/main/java/dev/niro/cameraremote/ui/UserInputController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package dev.niro.cameraremote.ui
import android.content.Context
import dev.niro.cameraremote.bluetooth.BluetoothController
import dev.niro.cameraremote.interfaces.IUserInterfaceTimerCallback
import dev.niro.cameraremote.utils.Clock
import dev.niro.cameraremote.utils.Vibrator
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

object UserInputController {
Expand Down Expand Up @@ -35,36 +35,40 @@ object UserInputController {
}

triggerCoroutine = CoroutineScope(Dispatchers.Default).launch {
var firstPicture = true
Vibrator.tick(context)

do {
val configuredTimerDelay = timerDelay
runTimerProcess(context)
} while (autoTriggerEnabled)
}
}

for (waitCounter in 0..<configuredTimerDelay) {
if (firstPicture || waitCounter > 0) {
Vibrator.tick(context)
}
private suspend fun runTimerProcess(context: Context) {
val configuredTimerDelay = timerDelay

val tickFPS = if (uiCallback?.isAmbientModeActive() == true) { 1 } else { 40 }
for (waitCounter in 0..<configuredTimerDelay) {
if (waitCounter > 0) {
Vibrator.tick(context)
}

for (subTick in 1..tickFPS) {
delay(1000L / tickFPS)
val uiFPS = if (uiCallback?.isAmbientModeActive() == false) { 40 } else { 1 }

val subTickProgress = subTick / tickFPS.toFloat()
val progress = (waitCounter + subTickProgress) / configuredTimerDelay
for (subTick in 1..uiFPS) {
Clock.sleep(1000L / uiFPS)

uiCallback?.changeProgressIndicatorState(progress)
}
}
val subTickProgress = subTick / uiFPS.toFloat()
val progress = (waitCounter + subTickProgress) / configuredTimerDelay

if (configuredTimerDelay == 0) {
uiCallback?.changeProgressIndicatorState(1f)
}
uiCallback?.changeProgressIndicatorState(progress)
}
}

Vibrator.shoot(context)
BluetoothController.takePicture()
firstPicture = false
} while (autoTriggerEnabled)
if (configuredTimerDelay == 0) {
uiCallback?.changeProgressIndicatorState(1f)
}

Vibrator.shoot(context)
BluetoothController.takePicture()
}

fun toggleTimer() {
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/dev/niro/cameraremote/utils/Clock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.niro.cameraremote.utils

import kotlinx.coroutines.delay

object Clock {

suspend fun sleep(milliseconds: Long) {
val startTime = System.currentTimeMillis()

while (true) {
val elapsedTime = System.currentTimeMillis() - startTime
val remainingTime = milliseconds - elapsedTime

if (remainingTime <= 0) {
break
}

if (remainingTime < 10) {
delay(remainingTime)
} else {
delay(remainingTime / 2)
}
}
}

}

0 comments on commit 509f22d

Please sign in to comment.