Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,27 @@ import com.gopro.open_gopro.domain.communicator.bleCommunicator.ResponseId
import com.gopro.open_gopro.entity.communicator.GpStatus
import com.gopro.open_gopro.entity.communicator.QueryId
import com.gopro.open_gopro.operations.IUByteArrayCompanion
import com.gopro.open_gopro.operations.IValuedEnum
import com.gopro.open_gopro.operations.SettingId
import com.gopro.open_gopro.util.extensions.toUByteArray
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

@OptIn(ExperimentalUnsignedTypes::class)
internal class UByteArrayEnumTransformer<T>(private val companion: IUByteArrayCompanion<T>) where
T : Enum<T>,
T : IValuedEnum<*> {
fun toUByteArray(value: T): UByteArray =
if (value.value is UByte) {
ubyteArrayOf(value.value as UByte)
} else if (value.value is ULong) {
(value.value as ULong).toUByteArray()
} else {
throw Exception("Only Enums of value type UByte and ULong can be converted to UByteArray")
}

fun fromUByteArray(data: UByteArray): T = companion.fromUByteArray(data)
}

/**
* A per-setting ID wrapper to perform all setting queries
*
* @param T Setting data type
* @property settingId Setting ID
* @property marshaller operation marshaller to marshal the queries
* @param enum Enum data type
* @param byteTransformer Enum data type
* @see [Open GoPro Spec](https://gopro.github.io/OpenGoPro/ble/features/settings.html)
*/
@OptIn(ExperimentalUnsignedTypes::class)
class Setting<T>
class Setting<T : Any>
internal constructor(
private val settingId: SettingId,
enum: IUByteArrayCompanion<T>,
private val byteTransformer: IUByteArrayCompanion<T>,
private val marshaller: IOperationMarshaller,
) where T : Enum<T>, T : IValuedEnum<*> {
private val byteTransformer = UByteArrayEnumTransformer(enum)

) {
private inner class SetSettingValue(private val value: T) :
BaseOperation<Unit>("Set Setting Value::${settingId.name}") {
override suspend fun execute(communicator: BleCommunicator): Result<Unit> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* cohn.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* live_streaming.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* media.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* network_management.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* preset_status.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* request_get_preset_status.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* response_generic.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* set_camera_control_status.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* turbo_transfer.kt/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
/* This copyright was auto-generated on Tue Mar 25 16:44:30 UTC 2025 */

@file:OptIn(pbandk.PublicForGeneratedCode::class)

package com.gopro.open_gopro.operations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.gopro.open_gopro.entity.queries

import com.gopro.open_gopro.operations.IUByteArrayCompanion
import com.gopro.open_gopro.util.extensions.toBoolean
import com.gopro.open_gopro.util.extensions.toInt
import com.gopro.open_gopro.util.extensions.toUByte

object ComplexQueryEntity {
data class ScheduledCapture(
val hour: Int,
val minute: Int,
val is24hour: Boolean,
val isEnabled: Boolean
) {
companion object : IUByteArrayCompanion<ScheduledCapture> {
private val hourMask = (0x1FU).toUByte()
private val minuteMask = (0xFCU).toUByte()
private val is24HourMask = (0x02U).toUByte()
private val isEnabledMask = (0x01U).toUByte()

@OptIn(ExperimentalUnsignedTypes::class)
override fun fromUByteArray(value: UByteArray) =
// Payload is the last 2 bytes
value.takeLast(2).let {
val hour = it[0] and hourMask
val minute = (it[1] and minuteMask).toLong() shr 2
val is24Hour = (it[1] and is24HourMask).toLong() shr 1
val isEnabled = it[1] and isEnabledMask
ScheduledCapture(
hour = hour.toInt(),
minute = minute.toInt(),
is24hour = is24Hour.toInt().toBoolean(),
isEnabled = isEnabled.toInt().toBoolean())
}

@OptIn(ExperimentalUnsignedTypes::class)
override fun toUByteArray(value: ScheduledCapture): UByteArray {
val hourField = value.hour.toLong()
val minuteField = value.minute.toLong() shl 2
val is24HourField = value.is24hour.toInt().toLong() shl 1
val isEnabledField = value.isEnabled.toInt().toLong()
return ubyteArrayOf(
0U, 0U, hourField.toUByte(), (minuteField or is24HourField or isEnabledField).toUByte())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ interface IValuedEnum<T> {
}

@OptIn(ExperimentalUnsignedTypes::class)
interface IUByteArrayCompanion<T> where T : Enum<T>, T : IValuedEnum<*> {
interface IUByteArrayCompanion<T> {
fun fromUByteArray(value: UByteArray): T
}

enum class BooleanEnum(override val value: UByte) : IValuedEnum<UByte> {
FALSE(0U),
TRUE(1U);

fun toBoolean() = this == TRUE

@OptIn(ExperimentalUnsignedTypes::class)
companion object : IUByteArrayCompanion<BooleanEnum> {
override fun fromUByteArray(value: UByteArray) =
BooleanEnum.entries.first { it.value == value.last() }
}
fun toUByteArray(value: T): UByteArray
}
Loading