|
|
|
|
@@ -134,6 +134,7 @@ import kotlinx.coroutines.joinAll
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
import timber.log.Timber
|
|
|
|
|
import kotlin.math.abs
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import kotlin.experimental.or
|
|
|
|
|
import kotlin.time.Duration.Companion.milliseconds
|
|
|
|
|
@@ -2081,28 +2082,59 @@ class MainViewModel @Inject constructor(
|
|
|
|
|
Timber.d("preset: ${preset}")
|
|
|
|
|
|
|
|
|
|
val newPreset = if (preset == null) {
|
|
|
|
|
val defaultPulseWidth = PulseDurations.first()
|
|
|
|
|
Preset(
|
|
|
|
|
handPieceType = handPiece.value.type,
|
|
|
|
|
priority = priority,
|
|
|
|
|
fluence = energyTable.value.getKey2ListForKey1(0.5f).first(),
|
|
|
|
|
fluence = energyTable.value.getKey2ListForKey1(defaultPulseWidth).firstOrNull() ?: 0f,
|
|
|
|
|
repetition = repetitionList.value.first(),
|
|
|
|
|
pulseWidth = PulseDurations.first(),
|
|
|
|
|
pulseWidth = defaultPulseWidth,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
preset
|
|
|
|
|
}
|
|
|
|
|
applyPreset(newPreset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val fluenceList = energyTable.value.getKey2ListForKey1(newPreset.pulseWidth)
|
|
|
|
|
fun applyPreset(preset: Preset) {
|
|
|
|
|
val newPreset = preset.copy()
|
|
|
|
|
|
|
|
|
|
val pulseStep = PulseDurations.indexOf(newPreset.pulseWidth).takeIf {it != -1} ?: 0
|
|
|
|
|
val fluenceStep = fluenceList.indexOf(newPreset.fluence).takeIf {it != -1} ?: 0
|
|
|
|
|
val repetitionStep = repetitionList.value.indexOf(newPreset.repetition).takeIf {it != -1} ?: 0
|
|
|
|
|
val resolvedPulseWidth = PulseDurations[pulseStep]
|
|
|
|
|
|
|
|
|
|
// 프리셋의 pulseWidth 기준으로 실제 사용 가능한 fluence 목록을 먼저 동기화한다.
|
|
|
|
|
val newFluenceList = energyTable.value.getKey2ListForKey1(resolvedPulseWidth)
|
|
|
|
|
if (newFluenceList != fluenceList.value) {
|
|
|
|
|
setFluenceList(newFluenceList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 프리셋 fluence가 정확히 없을 수 있으므로, 가장 가까운 값으로 보정한다.
|
|
|
|
|
val resolvedFluence = newFluenceList.minByOrNull { abs(it - newPreset.fluence) }
|
|
|
|
|
?: newFluenceList.firstOrNull()
|
|
|
|
|
?: 0f
|
|
|
|
|
val fluenceStep = newFluenceList.indexOf(resolvedFluence).takeIf { it != -1 } ?: 0
|
|
|
|
|
|
|
|
|
|
// 보정된 (pulseWidth, fluence) 조합으로 repetition 테이블을 다시 계산한다.
|
|
|
|
|
val hzType = hzTable.value.getValue(resolvedPulseWidth, resolvedFluence)
|
|
|
|
|
val newRepetitionList = RepetitionsByColorKey[hzType] ?: RepetitionsByColorKey[KEY_YELLOW]!!
|
|
|
|
|
if (newRepetitionList != repetitionList.value) {
|
|
|
|
|
setRepetitionList(newRepetitionList)
|
|
|
|
|
}
|
|
|
|
|
val repetitionStep = newRepetitionList.indexOf(newPreset.repetition).takeIf { it != -1 } ?: 0
|
|
|
|
|
|
|
|
|
|
Timber.d("pulseStep: ${pulseStep} fluenceStep: ${fluenceStep} repetitionStep: ${repetitionStep}")
|
|
|
|
|
|
|
|
|
|
val pulseAngle = pulseStep.stepToDegree(totalSteps = PulseDurations.size)
|
|
|
|
|
val fluenceAngle = fluenceStep.stepToDegree(totalSteps = fluenceList.size)
|
|
|
|
|
val repetitionAngle = repetitionStep.stepToDegree(totalSteps = repetitionList.value.size)
|
|
|
|
|
val fluenceAngle = if (newFluenceList.isNotEmpty()) {
|
|
|
|
|
fluenceStep.stepToDegree(totalSteps = newFluenceList.size)
|
|
|
|
|
} else {
|
|
|
|
|
0f
|
|
|
|
|
}
|
|
|
|
|
val repetitionAngle = if (newRepetitionList.isNotEmpty()) {
|
|
|
|
|
repetitionStep.stepToDegree(totalSteps = newRepetitionList.size)
|
|
|
|
|
} else {
|
|
|
|
|
0f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Timber.d("pulseStep: ${pulseStep} fluenceStep: ${fluenceStep} repetitionStep: ${repetitionStep}")
|
|
|
|
|
|
|
|
|
|
@@ -2132,8 +2164,15 @@ class MainViewModel @Inject constructor(
|
|
|
|
|
setFluenceList(newFluenceList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2-1. Resolve and apply fluence by value (not by old angle/index).
|
|
|
|
|
val resolvedFluence = newFluenceList.minByOrNull { abs(it - newFluence) } ?: 0f
|
|
|
|
|
val resolvedFluenceIndex = newFluenceList.indexOf(resolvedFluence).takeIf { it >= 0 } ?: 0
|
|
|
|
|
if (newFluenceList.isNotEmpty()) {
|
|
|
|
|
setFluenceAngle(resolvedFluenceIndex.stepToDegree(totalSteps = newFluenceList.size))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Safely Update Repetition List (Prevents NullPointerException)
|
|
|
|
|
val newHzType = hzTable.value.getValue(newPulseDuration, newFluence)
|
|
|
|
|
val newHzType = hzTable.value.getValue(newPulseDuration, resolvedFluence)
|
|
|
|
|
val newRepetitionList = RepetitionsByColorKey[newHzType] ?: RepetitionsByColorKey[KEY_YELLOW]!!
|
|
|
|
|
if (newRepetitionList != repetitionList.value) {
|
|
|
|
|
setRepetitionList(newRepetitionList)
|
|
|
|
|
@@ -2168,7 +2207,7 @@ class MainViewModel @Inject constructor(
|
|
|
|
|
// 6. Any change invalidates the current preset selection.
|
|
|
|
|
setSelectedPresetIndex(0)
|
|
|
|
|
|
|
|
|
|
Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$newFluence -> newRepListSize=${newRepetitionList.size}")
|
|
|
|
|
Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$resolvedFluence -> newRepListSize=${newRepetitionList.size}")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -2188,13 +2227,16 @@ class MainViewModel @Inject constructor(
|
|
|
|
|
setPulseAngle(newPulseStep.stepToDegree(totalSteps = PulseDurations.size))
|
|
|
|
|
|
|
|
|
|
val newPulseDuration = PulseDurations[newPulseStep]
|
|
|
|
|
// When pulse duration changes via slider, we use the first available fluence for the new list.
|
|
|
|
|
val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f
|
|
|
|
|
// Keep the current fluence value when pulse duration changes via slider.
|
|
|
|
|
val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size)
|
|
|
|
|
val currentFluence = fluenceList.value.getOrNull(currentFluenceStep)
|
|
|
|
|
?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull()
|
|
|
|
|
?: 0f
|
|
|
|
|
|
|
|
|
|
// Call the centralized helper, resetting the fluence slider.
|
|
|
|
|
// Call the centralized helper using the preserved fluence value.
|
|
|
|
|
updateLaserParameters(
|
|
|
|
|
newPulseDuration = newPulseDuration,
|
|
|
|
|
newFluence = firstFluence,
|
|
|
|
|
newFluence = currentFluence,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -2206,12 +2248,17 @@ class MainViewModel @Inject constructor(
|
|
|
|
|
setPulseAngle(newStep.stepToDegree(totalSteps = PulseDurations.size))
|
|
|
|
|
|
|
|
|
|
val newPulseDuration = PulseDurations[newStep]
|
|
|
|
|
val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f
|
|
|
|
|
// Keep the current fluence value when pulse duration changes via slider.
|
|
|
|
|
val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size)
|
|
|
|
|
val currentFluence = fluenceList.value.getOrNull(currentFluenceStep)
|
|
|
|
|
?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull()
|
|
|
|
|
?: 0f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Call the centralized helper, resetting the fluence slider.
|
|
|
|
|
updateLaserParameters(
|
|
|
|
|
newPulseDuration = newPulseDuration,
|
|
|
|
|
newFluence = firstFluence,
|
|
|
|
|
newFluence = currentFluence,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|