Skip to content

Instantly share code, notes, and snippets.

@magnatronus
Last active March 14, 2024 03:49
Show Gist options
  • Save magnatronus/c2e5ef9f2ba022a2d6949be0730e6bb7 to your computer and use it in GitHub Desktop.
Save magnatronus/c2e5ef9f2ba022a2d6949be0730e6bb7 to your computer and use it in GitHub Desktop.
Extracting BBC Micro:bit accelerometer data using Flutter Blue
/// This gist assumes that Flutter Blue (https://github.com/pauldemarco/flutter_blue) is used to connect to a BBC Micro:bit
/// and is listening to the [device.onValueChanged] for the correct characteristic ("E95DCA4B-251D-470A-A062FA1922DFA9A8")
///
/// this will be something similar to
///
/// device.onValueChanged( (accChar).listen( (value){ convertRawData(value)}));
///
/// the 'raw' listen value is a List<int> of length 6 this needs to be converted into the X,Y and Z values from the
/// accelerometer and is done with the function below
///
/// The result is a List<double> with 3 values for X, Y & Z with values approx -1 to +1
convertRawData(data) {
Uint8List reading = Uint8List.fromList(value);
ByteData buffer = ByteData.view(reading.buffer);
double x = buffer.getInt16(0, Endian.little)/1000;
double y = buffer.getInt16(2, Endian.little)/1000;
double z = buffer.getInt16(4, Endian.little)/1000;
return [x, y, z];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment