4 Commits

3 changed files with 48 additions and 75 deletions

View File

@@ -304,7 +304,7 @@ fun HomeScreen(
pulseType = pulseType,
handPieceType = handPiece.type,
type = LaserStatusType.REPETITION,
angle = if (pulseType == 0 || repetitionList.size < 2) 0f else repetitionAngle,
angle = if (pulseType == 0 || repetitionList.isEmpty()) 0f else repetitionAngle,
onChange = { angle ->
if (handPiece.type == 0) {
Toast.makeText(
@@ -315,7 +315,7 @@ fun HomeScreen(
return@LaserControlView
}
if (pulseType == 0 || repetitionList.size < 2) {
if (pulseType == 0 || repetitionList.isEmpty()) {
//Toast.makeText(context, "Single pulse type (0 Hz)", Toast.LENGTH_SHORT).show()
} else {
mainViewModel.onChangeRepetition(angle)
@@ -332,7 +332,7 @@ fun HomeScreen(
return@LaserControlView
}
if (pulseType == 0 || repetitionList.size < 2) {
if (pulseType == 0 || repetitionList.isEmpty()) {
//Toast.makeText(context, "Single pulse type (0 Hz)", Toast.LENGTH_SHORT).show()
} else {
//mainViewModel.onClickRepetition(state)

View File

@@ -1106,7 +1106,7 @@ class MainViewModel @Inject constructor(
CMD.LASER_STATUS -> {
val l = packet.data.toLaserStatus()
setLaserStatus(l.copy())
// on receuve laser on (or interval) increase count and save to pref.
// on receive laser on (or interval) increase count and save to pref.
// Laser On 패킷만 counting 하도록 변경 (2개 패킷 모두 수신 됨)
if (_laserStatus.value.laserStatus == LASER_STATUS.LASER_ON /* || l.laserStatus == LASER_STATUS.INTERVAL */) {
playBeepSound()
@@ -1185,58 +1185,19 @@ class MainViewModel @Inject constructor(
setEnergyControl(EnergyControl(0x03, 0x00))
setEnergyMeasured(EnergyMeasured(0x04, 0x00, 0x00))
// hand piece 별 count 증가
when (h.type) {
1 -> { // hp 5x5
setLifeTime(
_lifeTime.value.copy(
hp5x5 = _lifeTime.value.hp5x5 + 1
)
)
saveLifeTimeToPreference()
Timber.d("_lifeTime (hp5x5): ${_lifeTime}")
}
2 -> {
setLifeTime(
_lifeTime.value.copy(
hp7x7 = _lifeTime.value.hp7x7 + 1
)
)
saveLifeTimeToPreference()
Timber.d("_lifeTime (hp7x7): ${_lifeTime}")
}
3 -> {
setLifeTime(
_lifeTime.value.copy(
hp10x10 = _lifeTime.value.hp10x10 + 1
)
)
saveLifeTimeToPreference()
Timber.d("_lifeTime (hp10x10): ${_lifeTime}")
}
4 -> {
setLifeTime(
_lifeTime.value.copy(
hp12x12 = _lifeTime.value.hp12x12 + 1
)
)
saveLifeTimeToPreference()
Timber.d("_lifeTime (hp12x12): ${_lifeTime}")
}
5 -> {
setLifeTime(
_lifeTime.value.copy(
hp3x15 = _lifeTime.value.hp3x15 + 1
)
)
saveLifeTimeToPreference()
Timber.d("_lifeTime (hp3x15): ${_lifeTime}")
}
val connectedHandPieceLifeTime = when (h.type) {
1 -> lifeTime.value.hp5x5
2 -> lifeTime.value.hp7x7
3 -> lifeTime.value.hp10x10
4 -> lifeTime.value.hp12x12
5 -> lifeTime.value.hp3x15
else -> {
Timber.d("unknwon hand piece type: ${h.type}")
Timber.d("Unknown Hand-piece type: ${h.type}")
return@launch
}
}
Timber.d("HAND_PIECE lifetime: type=${h.type}, value=${connectedHandPieceLifeTime}, hpCount=${hpCount.value}")
// hand piece가 변경 시점 에서, pref 테이블 값을 -> energyTable 로 로딩
loadFluenceTable(handPiece.value.type)
@@ -2130,11 +2091,10 @@ class MainViewModel @Inject constructor(
} else {
0f
}
val repetitionAngle = if (newRepetitionList.isNotEmpty()) {
repetitionStep.stepToDegree(totalSteps = newRepetitionList.size)
} else {
0f
}
val repetitionAngle = repetitionIndexToAngle(
index = repetitionStep,
totalSteps = newRepetitionList.size,
)
Timber.d("pulseStep: ${pulseStep} fluenceStep: ${fluenceStep} repetitionStep: ${repetitionStep}")
@@ -2178,23 +2138,21 @@ class MainViewModel @Inject constructor(
setRepetitionList(newRepetitionList)
}
// Smartly preserve Repetition Angle
// 4. Check if the old repetition value exists in the new list.
val oldRepetitionIndex = if (currentRepetitionValue != null) {
newRepetitionList.indexOf(currentRepetitionValue)
} else {
-1
}
// 4. Resolve and apply repetition by value (not by old angle/index).
val targetRepetition = currentRepetitionValue ?: 0f
val resolvedRepetition = newRepetitionList.minByOrNull { abs(it - targetRepetition) } ?: 0f
val resolvedRepetitionIndex = newRepetitionList.indexOf(resolvedRepetition).takeIf { it >= 0 } ?: 0
if (oldRepetitionIndex != -1) {
// If the old value exists, set the slider to that position.
val preservedAngle = oldRepetitionIndex.stepToDegree(totalSteps = newRepetitionList.size)
setRepetitionAngle(preservedAngle)
Timber.d("Repetition value $currentRepetitionValue preserved at new angle $preservedAngle.")
if (newRepetitionList.isNotEmpty()) {
val resolvedRepetitionAngle = repetitionIndexToAngle(
index = resolvedRepetitionIndex,
totalSteps = newRepetitionList.size,
)
setRepetitionAngle(resolvedRepetitionAngle)
Timber.d("Resolved repetition by value: old=$currentRepetitionValue, new=$resolvedRepetition, angle=$resolvedRepetitionAngle")
} else {
// If it doesn't exist, THEN reset the slider to the beginning.
setRepetitionAngle(0f)
Timber.d("Repetition value $currentRepetitionValue not supported in new list. Resetting angle.")
Timber.d("Repetition list is empty. Resetting repetition angle to 0f.")
}
// 5. Conditionally reset the fluence angle slider if needed
@@ -2301,8 +2259,12 @@ class MainViewModel @Inject constructor(
fun onChangeRepetition(angle: Float) {
// This logic is simple and has no complex dependencies, so it can remain as is.
if (angle != repetitionAngle.value) {
setRepetitionAngle(angle)
val normalizedAngle = repetitionIndexToAngle(
index = angle.degreeToStep(totalSteps = repetitionList.value.size),
totalSteps = repetitionList.value.size,
)
if (normalizedAngle != repetitionAngle.value) {
setRepetitionAngle(normalizedAngle)
}
setSelectedPresetIndex(0)
}
@@ -2313,12 +2275,23 @@ class MainViewModel @Inject constructor(
val newStep = if (state == UpDownState.Up) currentStep + 1 else currentStep - 1
if (newStep in repetitionList.value.indices) {
val newRepetitionAngle = newStep.stepToDegree(totalSteps = repetitionList.value.size)
val newRepetitionAngle = repetitionIndexToAngle(
index = newStep,
totalSteps = repetitionList.value.size,
)
setRepetitionAngle(newRepetitionAngle)
setSelectedPresetIndex(0)
}
}
private fun repetitionIndexToAngle(index: Int, totalSteps: Int): Float {
return when {
totalSteps <= 0 -> 0f
totalSteps == 1 -> 270f
else -> index.stepToDegree(totalSteps = totalSteps)
}
}
/**
* Perform all heavy I/O in a single background block.
* This prevents the "Skipped frames" caused by 30+ sequential bridge calls