Skip to content

Commit

Permalink
wip: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Feb 7, 2024
1 parent 1fc093a commit 255ecf2
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions src/commands/CameraControlCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,9 @@ export class CameraControlCommand extends BasicWritableCommand<CameraControlPack
buffer.writeUint16BE(this.properties.numberData.length, header16BitPos)

let offset = headerSize
for (let i = 0; i < this.properties.numberData.length; i++) {
let rawValue = this.properties.numberData[i]

// This is encoding the 5.11 fixed point floats
// These use Two's complement to handle negative values

const invert = rawValue < 0
if (invert) rawValue = -rawValue

const decimal = Math.floor((rawValue % 1) * 0x800) // TODO - or round?
const real = Math.floor(rawValue)

let encodedValue = (real << 11) + decimal
if (invert) encodedValue = 65536 - encodedValue

buffer.writeUInt16BE(encodedValue, offset)
for (const value of this.properties.numberData) {
// Values are encoded as 5.11 fixed point floats
buffer.writeInt16BE(value * 2048, offset)
offset += 2
}

Expand Down Expand Up @@ -252,20 +239,10 @@ export class CameraControlUpdateCommand extends DeserializedCommand<CameraContro
}
case CameraControlDataType.FLOAT: {
for (let i = 0; i < count16Bit; i++) {
let decodedValue = rawCommand.readUInt16BE(offset) // 0x7ff

// This is decoding the 5.11 fixed point floats
// These use Two's complement to handle negative values

const invert = decodedValue & 0x8000
if (invert) decodedValue = 0x10000 - decodedValue

const decimal = (decodedValue & 0x7ff) / 0x800
let real = (decodedValue >> 11) + decimal

if (invert) real = -real
const decodedValue = rawCommand.readInt16BE(offset)

props.numberData.push(real)
// Values are encoded as 5.11 fixed point floats
props.numberData.push(decodedValue / 2048)
offset += 2
}
break
Expand Down

0 comments on commit 255ecf2

Please sign in to comment.