-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from matejdro/screenshots
add screenshot packets
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/commonMain/kotlin/io/rebble/libpebblecommon/packets/Screenshot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.rebble.libpebblecommon.packets | ||
|
||
import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import io.rebble.libpebblecommon.structmapper.SUByte | ||
import io.rebble.libpebblecommon.structmapper.SUInt | ||
import io.rebble.libpebblecommon.structmapper.SUnboundBytes | ||
import io.rebble.libpebblecommon.structmapper.StructMappable | ||
|
||
class ScreenshotRequest : PebblePacket(ProtocolEndpoint.SCREENSHOT) { | ||
/** | ||
* Command. Only one command (take screenshot, value 0) is supported for now | ||
*/ | ||
val command = SUByte(m, 0u) | ||
} | ||
|
||
class ScreenshotResponse : PebblePacket(ProtocolEndpoint.SCREENSHOT) { | ||
val data = SUnboundBytes(m) | ||
} | ||
|
||
class ScreenshotHeader : StructMappable() { | ||
/** | ||
* @see ScreenshotResponseCode | ||
*/ | ||
val responseCode = SUByte(m) | ||
|
||
/** | ||
* @see ScreenshotVersion | ||
*/ | ||
val version = SUInt(m) | ||
|
||
val width = SUInt(m) | ||
val height = SUInt(m) | ||
val data = SUnboundBytes(m) | ||
} | ||
|
||
enum class ScreenshotResponseCode(val rawCode: UByte) { | ||
OK(0u), | ||
MalformedCommand(0u), | ||
OutOfMemory(0u), | ||
AlreadyInProgress(0u); | ||
|
||
companion object { | ||
fun fromRawCode(rawCode: UByte): ScreenshotResponseCode { | ||
return values().firstOrNull { it.rawCode == rawCode } | ||
?: error("Unknown screenshot response code: $rawCode") | ||
} | ||
} | ||
} | ||
|
||
enum class ScreenshotVersion(val rawCode: UInt) { | ||
BLACK_WHITE_1_BIT(1u), | ||
COLOR_8_BIT(2u); | ||
|
||
companion object { | ||
fun fromRawCode(rawCode: UInt): ScreenshotVersion { | ||
return values().firstOrNull { it.rawCode == rawCode } | ||
?: error("Unknown screenshots version: $rawCode") | ||
} | ||
} | ||
} | ||
|
||
fun screenshotPacketsRegister() { | ||
PacketRegistry.register(ProtocolEndpoint.SCREENSHOT) { | ||
ScreenshotResponse() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/commonMain/kotlin/io/rebble/libpebblecommon/services/ScreenshotService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.rebble.libpebblecommon.services | ||
|
||
import io.rebble.libpebblecommon.ProtocolHandler | ||
import io.rebble.libpebblecommon.packets.ScreenshotRequest | ||
import io.rebble.libpebblecommon.packets.ScreenshotResponse | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
class ScreenshotService(private val protocolHandler: ProtocolHandler) : ProtocolService { | ||
val receivedMessages = Channel<ScreenshotResponse>(Channel.BUFFERED) | ||
|
||
init { | ||
protocolHandler.registerReceiveCallback(ProtocolEndpoint.SCREENSHOT, this::receive) | ||
} | ||
|
||
suspend fun send(packet: ScreenshotRequest) { | ||
protocolHandler.send(packet) | ||
} | ||
|
||
fun receive(packet: PebblePacket) { | ||
if (packet !is ScreenshotResponse) { | ||
throw IllegalStateException("Received invalid packet type: $packet") | ||
} | ||
|
||
receivedMessages.offer(packet) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters