@@ -143,6 +143,7 @@ class HIDCommon {
143143 var temp: Int? = null
144144 var brd_id: Int? = null
145145 var mcu_id: Int? = null
146+ var button: Int? = null
146147 // var imu_id: Int? = null
147148 // var mag_id: Int? = null
148149 var fw_date: Int? = null
@@ -220,6 +221,28 @@ class HIDCommon {
220221 }
221222 }
222223
224+ 6 -> { // data
225+ button = dataReceived[i + 2 ].toUByte().toInt()
226+ rssi = dataReceived[i + 15 ].toUByte().toInt()
227+ }
228+
229+ 7 -> { // reduced precision quat and accel with data
230+ button = dataReceived[i + 2 ].toUByte().toInt()
231+ // quaternion is quantized as exponential map
232+ // X = 10 bits, Y/Z = 11 bits
233+ val buffer = ByteBuffer .wrap(dataReceived, i + 5 , 4 )
234+ buffer.order(java.nio.ByteOrder .LITTLE_ENDIAN )
235+ val q_buf = buffer.getInt().toUInt()
236+ q[0 ] = (q_buf and 1023u ).toInt()
237+ q[1 ] = (q_buf shr 10 and 2047u ).toInt()
238+ q[2 ] = (q_buf shr 21 and 2047u ).toInt()
239+ for (j in 0 .. 2 ) { // accel received as fixed 7, in m/s^2
240+ // Q7 as short little endian
241+ a[j] = dataReceived[i + 9 + j * 2 + 1 ].toInt() shl 8 or dataReceived[i + 9 + j * 2 ].toUByte().toInt()
242+ }
243+ rssi = dataReceived[i + 15 ].toUByte().toInt()
244+ }
245+
223246 else -> {
224247 }
225248 }
@@ -248,6 +271,15 @@ class HIDCommon {
248271 device.mcuType = mcuType!!
249272 }
250273 }
274+ if (button != null ) {
275+ if (tracker.button == null ) {
276+ tracker.button = 0
277+ }
278+ if (button != tracker.button) {
279+ button = button and tracker.button!! .inv ()
280+ // Nothing to do now..
281+ }
282+ }
251283 if (fw_date != null && fw_major != null && fw_minor != null && fw_patch != null ) {
252284 val firmwareYear = 2020 + (fw_date shr 9 and 127 )
253285 val firmwareMonth = fw_date shr 5 and 15
@@ -274,7 +306,7 @@ class HIDCommon {
274306 rot = AXES_OFFSET .times(scaleRot).times(rot) // no division
275307 tracker.setRotation(rot)
276308 }
277- if (packetType == 2 ) {
309+ if (packetType == 2 || packetType == 7 ) {
278310 val v = floatArrayOf(q[0 ].toFloat(), q[1 ].toFloat(), q[2 ].toFloat()) // used q array for quantized data
279311 v[0 ] / = (1 shl 10 ).toFloat()
280312 v[1 ] / = (1 shl 11 ).toFloat()
@@ -293,7 +325,7 @@ class HIDCommon {
293325 rot = AXES_OFFSET .times(rot) // no division
294326 tracker.setRotation(rot)
295327 }
296- if (packetType == 1 || packetType == 2 ) {
328+ if (packetType == 1 || packetType == 2 || packetType == 7 ) {
297329 // Acceleration is in local device frame
298330 // On flat surface / face up:
299331 // Right side of the device is +X
@@ -317,7 +349,7 @@ class HIDCommon {
317349 val magnetometer = Vector3 (m[0 ].toFloat(), m[1 ].toFloat(), m[2 ].toFloat()).times(scaleMag) // no division
318350 tracker.setMagVector(magnetometer)
319351 }
320- if (packetType == 1 || packetType == 2 || packetType == 4 ) {
352+ if (packetType == 1 || packetType == 2 || packetType == 4 || packetType == 7 ) {
321353 tracker.dataTick() // only data tick if there is rotation data
322354 }
323355 }
0 commit comments