'get 16-bit value from bytes B(I)..B(I+1)
Sub GetShortFromWithinByteArray(B() As Byte, I As Int) As Short
If B.Length < I + 2 Then 'pessimists of the world, unite!
Log("wtf " & " " & B.Length & " " & I)
End If
Dim bc As ByteConverter
bc.LittleEndian = True 'BLE GATT generally does things the right way ;-)
Dim ByteArrayIn() As Byte = Array As Byte(B(I), B(I+1))
Dim ShortArrayOut() As Short = bc.ShortsFromBytes(ByteArrayIn)
Return ShortArrayOut(0)
'if your eyesight is bulletproof, then you could condense the above three lines to:
'Return bc.ShortsFromBytes(Array As Byte(B(I), B(I+1)))(0)
End Sub
'get 32-bit value from bytes B(I)..B(I+3)
Sub GetIntFromWithinByteArray(B() As Byte, I As Int) As Int
If B.Length < I + 4 Then 'pessimists of the world, unite!
Log("wtf " & " " & B.Length & " " & I)
End If
Dim bc As ByteConverter
bc.LittleEndian = True
Dim ByteArrayIn() As Byte = Array As Byte(B(I), B(I+1), B(I+2), B(I+3))
Dim IntArrayOut() As Int = bc.IntsFromBytes(ByteArrayIn)
Return IntArrayOut(0)
End Sub
Sub HandleWheelPacket(B() As Byte)
If B.Length = 0 Then
'empty packet = no data
Return
End If
Dim GetDataFrom As Int = 0 'start at beginning of packet
Dim DataFlags As Byte = B(GetDataFrom)
GetDataFrom = GetDataFrom + 1 'advance to next field
Dim WheelDataFlag As Boolean = Bit.And(DataFlags, Bit.ShiftLeft(1,0)) <> 0 'bit 0
Dim CrankDataFlag As Boolean = Bit.And(DataFlags, Bit.ShiftLeft(1,1)) <> 0 'bit 1
'check that we actually have that data
Dim NumDataBytesExpected As Int = 0
If WheelDataFlag Then NumDataBytesExpected = NumDataBytesExpected + 6
If CrankDataFlag Then NumDataBytesExpected = NumDataBytesExpected + 4
If (B.Length - GetDataFrom) < NumDataBytesExpected Then 'if numbytesremaining < numbytesexpected
Log("wtf missing data")
Return
End If
If WheelDataFlag Then
Dim CumulativeWheelRevolutions As Int = GetIntFromWithinByteArray(B, GetDataFrom)
GetDataFrom = GetDataFrom + 4 'advance to next field
Dim LastWheelEventTime As Short = GetShortFromWithinByteArray(B, GetDataFrom)
GetDataFrom = GetDataFrom + 2 'advance to next field
End If
If CrankDataFlag Then
Dim CumulativeCrankRevolutions As Short = GetShortFromWithinByteArray(B, GetDataFrom)
GetDataFrom = GetDataFrom + 2 'next field
Dim LastCrankEventTime As Short = GetShortFromWithinByteArray(B, GetDataFrom)
GetDataFrom = GetDataFrom + 2 'next field
End If
'process above-extracted data
Dim bc As ByteConverter
Log("Packet is " & bc.HexFromBytes(B))
If WheelDataFlag Then
Log("Wheel " & CumulativeWheelRevolutions & " " & LastWheelEventTime)
End If
If CrankDataFlag Then
Log("Crank " & CumulativeCrankRevolutions & " " & LastCrankEventTime)
End If
End Sub
Sub AppStart (Args() As String)
Log("Test full packet")
Dim B() As Byte = Array As Byte(0x03, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x33, 0x00, 0x44, 0x00)
HandleWheelPacket(B)
Log("Test wheel packet with superfluous data")
Dim B() As Byte = Array As Byte(0x01, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x33, 0x00, 0x44, 0x00)
HandleWheelPacket(B)
Log("Test crank packet")
Dim B() As Byte = Array As Byte(0x02, 0x33, 0x00, 0x44, 0x00)
HandleWheelPacket(B)
Log("Test empty packet (!) with superfluous data")
Dim B() As Byte = Array As Byte(0x00, 0x33, 0x00, 0x44, 0x00)
HandleWheelPacket(B)
Log("Test empty packet (!)")
Dim B() As Byte = Array As Byte(0x00)
HandleWheelPacket(B)
Log("Test short packet")
Dim B() As Byte = Array As Byte(0x02, 0x33, 0x00, 0x44)
HandleWheelPacket(B)
Log("Finito")
End Sub