-
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 #13 from matejdro/music
Music control packets
- Loading branch information
Showing
12 changed files
with
421 additions
and
15 deletions.
There are no files selected for viewing
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
4 changes: 2 additions & 2 deletions
4
src/commonMain/kotlin/io/rebble/libpebblecommon/exceptions/packet.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.rebble.libpebblecommon.exceptions | ||
|
||
class PacketEncodeException(message: String?): Exception(message) | ||
class PacketDecodeException(message: String?): Exception(message) | ||
class PacketEncodeException(message: String?) : Exception(message) | ||
class PacketDecodeException(message: String?, cause: Exception? = null) : Exception(message, cause) |
168 changes: 168 additions & 0 deletions
168
src/commonMain/kotlin/io/rebble/libpebblecommon/packets/Music.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,168 @@ | ||
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.* | ||
|
||
open class MusicControl(val message: Message) : PebblePacket(ProtocolEndpoint.MUSIC_CONTROL) { | ||
val command = SUByte(m, message.value) | ||
|
||
init { | ||
type = command.get() | ||
} | ||
|
||
enum class Message(val value: UByte) { | ||
PlayPause(0x01u), | ||
Pause(0x02u), | ||
Play(0x03u), | ||
NextTrack(0x04u), | ||
PreviousTrack(0x05u), | ||
VolumeUp(0x06u), | ||
VolumeDown(0x07u), | ||
GetCurrentTrack(0x08u), | ||
UpdateCurrentTrack(0x10u), | ||
UpdatePlayStateInfo(0x11u), | ||
UpdateVolumeInfo(0x12u), | ||
UpdatePlayerInfo(0x13u) | ||
} | ||
|
||
class UpdateCurrentTrack( | ||
artist: String = "", | ||
album: String = "", | ||
title: String = "", | ||
trackLength: Int? = null, | ||
trackCount: Int? = null, | ||
currentTrack: Int? = null | ||
) : MusicControl(Message.UpdateCurrentTrack) { | ||
val artist = SString(m, artist) | ||
val album = SString(m, album) | ||
val title = SString(m, title) | ||
val trackLength = SOptional( | ||
m, | ||
SUInt(StructMapper(), trackLength?.toUInt() ?: 0u, '<'), | ||
trackLength != null | ||
) | ||
val trackCount = SOptional( | ||
m, | ||
SUInt(StructMapper(), trackCount?.toUInt() ?: 0u, '<'), | ||
trackCount != null | ||
) | ||
val currentTrack = SOptional( | ||
m, | ||
SUInt(StructMapper(), currentTrack?.toUInt() ?: 0u, '<'), | ||
currentTrack != null | ||
) | ||
} | ||
|
||
class UpdatePlayStateInfo( | ||
playbackState: PlaybackState = PlaybackState.Unknown, | ||
trackPosition: UInt = 0u, | ||
playRate: UInt = 0u, | ||
shuffle: ShuffleState = ShuffleState.Unknown, | ||
repeat: RepeatState = RepeatState.Unknown | ||
) : MusicControl(Message.UpdatePlayStateInfo) { | ||
val state = SUByte(m, playbackState.value) | ||
val trackPosition = SUInt(m, trackPosition, '<') | ||
val playRate = SUInt(m, playRate, '<') | ||
val shuffle = SUByte(m, shuffle.value) | ||
val repeat = SUByte(m, repeat.value) | ||
} | ||
|
||
class UpdateVolumeInfo( | ||
volumePercent: UByte = 0u, | ||
) : MusicControl(Message.UpdateVolumeInfo) { | ||
val volumePercent = SUByte(m, volumePercent) | ||
} | ||
|
||
class UpdatePlayerInfo( | ||
pkg: String = "", | ||
name: String = "" | ||
) : MusicControl(Message.UpdatePlayerInfo) { | ||
val pkg = SString(m, pkg) | ||
val name = SString(m, name) | ||
} | ||
|
||
enum class PlaybackState(val value: UByte) { | ||
Paused(0x00u), | ||
Playing(0x01u), | ||
Rewinding(0x02u), | ||
FastForwarding(0x03u), | ||
Unknown(0x04u), | ||
} | ||
|
||
enum class ShuffleState(val value: UByte) { | ||
Unknown(0x00u), | ||
Off(0x01u), | ||
On(0x02u), | ||
} | ||
|
||
enum class RepeatState(val value: UByte) { | ||
Unknown(0x00u), | ||
Off(0x01u), | ||
One(0x02u), | ||
All(0x03u), | ||
} | ||
} | ||
|
||
fun musicPacketsRegister() { | ||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.PlayPause.value | ||
) { MusicControl(MusicControl.Message.PlayPause) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.Pause.value | ||
) { MusicControl(MusicControl.Message.Pause) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.Play.value | ||
) { MusicControl(MusicControl.Message.Play) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.NextTrack.value | ||
) { MusicControl(MusicControl.Message.NextTrack) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.PreviousTrack.value | ||
) { MusicControl(MusicControl.Message.PreviousTrack) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.VolumeUp.value | ||
) { MusicControl(MusicControl.Message.VolumeUp) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.VolumeDown.value | ||
) { MusicControl(MusicControl.Message.VolumeDown) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.GetCurrentTrack.value | ||
) { MusicControl(MusicControl.Message.GetCurrentTrack) } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.UpdateCurrentTrack.value | ||
) { MusicControl.UpdateCurrentTrack() } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.UpdatePlayStateInfo.value | ||
) { MusicControl.UpdatePlayStateInfo() } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.UpdateVolumeInfo.value | ||
) { MusicControl.UpdateVolumeInfo() } | ||
|
||
PacketRegistry.register( | ||
ProtocolEndpoint.MUSIC_CONTROL, | ||
MusicControl.Message.UpdatePlayerInfo.value | ||
) { MusicControl.UpdatePlayerInfo() } | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
src/commonMain/kotlin/io/rebble/libpebblecommon/services/MusicService.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,28 @@ | ||
package io.rebble.libpebblecommon.services | ||
|
||
import io.rebble.libpebblecommon.ProtocolHandler | ||
import io.rebble.libpebblecommon.packets.MusicControl | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
class MusicService(private val protocolHandler: ProtocolHandler) : ProtocolService { | ||
val receivedMessages = Channel<MusicControl>(Channel.BUFFERED) | ||
|
||
init { | ||
protocolHandler.registerReceiveCallback(ProtocolEndpoint.MUSIC_CONTROL, this::receive) | ||
} | ||
|
||
suspend fun send(packet: MusicControl) { | ||
protocolHandler.send(packet) | ||
} | ||
|
||
fun receive(packet: PebblePacket) { | ||
if (packet !is MusicControl) { | ||
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
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
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
Oops, something went wrong.