Skip to content

Commit 79b9795

Browse files
committed
feat(protocol): ✨ add lighting effects and zero-based layers
1 parent 84c364d commit 79b9795

7 files changed

Lines changed: 254 additions & 70 deletions

File tree

app/src/main/java/com/patchself/codexmacro/bluetooth/CodexMicroService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CodexMicroService : Service() {
9494
private val rpcEngine by lazy {
9595
CodexRpcEngine(
9696
statusProvider = { DeviceStatus(_state.value.battery, _state.value.isCharging) },
97-
layerProvider = { _settings.value.activeLayer + 1 },
97+
layerProvider = { _settings.value.activeLayer },
9898
threadLightProvider = { _state.value.threads[it] },
9999
ambientProvider = { _state.value.ambient },
100100
keysProvider = { _state.value.keys },

app/src/main/java/com/patchself/codexmacro/protocol/CodexModels.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package com.patchself.codexmacro.protocol
33
data class ThreadLight(
44
val color: Long = 0,
55
val brightness: Float = 0f,
6-
val effect: String = "off",
6+
val effect: Int = 0,
77
val speed: Float = 0f,
8+
val syncKeysLighting: Boolean = false,
9+
val syncAmbientLighting: Boolean = false,
810
)
911

1012
data class LightingSide(
1113
val color: Long = 0,
1214
val brightness: Float = 0f,
13-
val effect: String = "off",
15+
val effect: Int = 0,
1416
val speed: Float = 0f,
17+
val magic: Float = 0f,
1518
)
1619

1720
data class DeviceStatus(

app/src/main/java/com/patchself/codexmacro/protocol/CodexProtocol.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ object CodexProtocol {
1010
const val payloadSize = 61
1111
const val messageType = 2
1212
const val maxRpcSize = 4096
13+
const val effectOff = 0
14+
const val effectSolid = 1
15+
const val effectSnake = 2
16+
const val effectRainbow = 3
17+
const val effectBreath = 4
18+
const val effectGradient = 5
19+
const val effectShallowBreath = 6
1320

1421
val reportMap = byteArrayOf(
1522
0x06, 0x00, 0xFF.toByte(),

app/src/main/java/com/patchself/codexmacro/protocol/CodexRpcEngine.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CodexRpcEngine(
3333
buildJsonObject {
3434
put("version", firmwareVersion)
3535
put("profile_index", 0)
36-
put("layer_index", layerProvider().coerceIn(1, 6))
36+
put("layer_index", layerProvider().coerceIn(0, 5))
3737
put("battery", status.battery)
3838
put("is_charging", status.isCharging)
3939
},
@@ -64,8 +64,10 @@ class CodexRpcEngine(
6464
id to ThreadLight(
6565
color = value.longOr("c", current.color),
6666
brightness = value.floatOr("b", current.brightness),
67-
effect = value.stringOr("e", current.effect),
67+
effect = value.intOr("e", current.effect),
6868
speed = value.floatOr("s", current.speed),
69+
syncKeysLighting = value.booleanFlagOr("sk", current.syncKeysLighting),
70+
syncAmbientLighting = value.booleanFlagOr("sa", current.syncAmbientLighting),
6971
),
7072
)
7173
}
@@ -84,8 +86,9 @@ class CodexRpcEngine(
8486
private fun JsonObject.toLightingSide(current: LightingSide) = LightingSide(
8587
color = longOr("c", current.color),
8688
brightness = floatOr("b", current.brightness),
87-
effect = stringOr("e", current.effect),
89+
effect = intOr("e", current.effect),
8890
speed = floatOr("s", current.speed),
91+
magic = floatOr("m", current.magic),
8992
)
9093

9194
private fun JsonObject.longOr(key: String, fallback: Long): Long =
@@ -94,8 +97,11 @@ class CodexRpcEngine(
9497
private fun JsonObject.floatOr(key: String, fallback: Float): Float =
9598
(this[key] as? JsonPrimitive)?.floatOrNull ?: fallback
9699

97-
private fun JsonObject.stringOr(key: String, fallback: String): String =
98-
(this[key] as? JsonPrimitive)?.contentOrNull ?: fallback
100+
private fun JsonObject.intOr(key: String, fallback: Int): Int =
101+
(this[key] as? JsonPrimitive)?.intOrNull ?: fallback
102+
103+
private fun JsonObject.booleanFlagOr(key: String, fallback: Boolean): Boolean =
104+
(this[key] as? JsonPrimitive)?.intOrNull?.let { it != 0 } ?: fallback
99105

100106
private fun success(id: JsonElement) = response(id, buildJsonObject { put("ok", true) })
101107

0 commit comments

Comments
 (0)