main
1import 'dart:math';
2
3/// Whether the bit at offset (0-7) is set.
4///
5/// Masks the byte with a 1 that has [offset] to the right and moves the
6/// remaining bit to the first position and checks if it's equal to 1.
7bool isBitIntByteSet(int byte, int offset) =>
8 (((byte & (1 << offset)) >> offset) == 1);
9
10/// Attempts to read an IEEE-11073 16bit SFloat starting at data[offset].
11double? readSFloat(List<int> data, int offset) {
12 if (data.length < offset + 2) return null;
13 // TODO: special values (NaN, Infinity)
14 // If this ever stops working: https://github.com/NordicSemiconductor/Kotlin-BLE-Library/blob/6b565e59de21dfa53ef80ff8351ac4a4550e8d58/core/src/main/java/no/nordicsemi/android/kotlin/ble/core/data/util/DataByteArray.kt#L392
15 final mantissa = data[offset] + ((data[offset + 1] & 0x0F) << 8);
16 final exponent = data[offset + 1] >> 4;
17 return (mantissa * (pow(10, exponent))).toDouble();
18}
19
20int? readUInt8(List<int> data, int offset) {
21 if (data.length < offset + 1) return null;
22 return data[offset];
23}
24
25int? readUInt16Le(List<int> data, int offset) {
26 if (data.length < offset + 2) return null;
27 return data[offset] + (data[offset+1] << 8);
28}
29
30int? readUInt16Be(List<int> data, int offset) {
31 if (data.length < offset + 2) return null;
32 return (data[offset] << 8) + data[offset + 1];
33}