update for new rust implementation of the microcontroller
Some checks failed
xiao_pet_tracker / semantic-pull-request (push) Failing after 0s
xiao_pet_tracker / build (push) Failing after 0s
xiao_pet_tracker / spell-check (push) Failing after 0s

This commit is contained in:
2025-06-27 21:38:09 +02:00
parent a16145e48c
commit 7aa7f7f5f6
2 changed files with 77 additions and 65 deletions

View File

@@ -27,10 +27,6 @@ class XiaoConnectorCubit extends Cubit<XiaoConnectorState> {
late final Box<CapturePoint> _capturePointsBox;
late final Box<CaptureBox> _captureBoxes;
bool gotRotation = false;
bool gotAcceleration = false;
bool gotTimeStamp = false;
int _rotX = 0;
int _rotY = 0;
int _rotZ = 0;
@@ -111,31 +107,52 @@ class XiaoConnectorCubit extends Cubit<XiaoConnectorState> {
.where(
(c) =>
c.characteristicUuid ==
Guid('19B10000-E8F2-537E-4F6C-D104768A1217'),
Guid('19B10000-E8F2-537E-4F6C-D104768A1216'),
)
.first;
final data = await accelService.read();
print(data);
// accelService.onValueReceived((data) {});
final gyroX = fromBytesToInt32(data[0], data[1], data[2], data[3]);
final gyroY = fromBytesToInt32(data[4], data[5], data[6], data[7]);
final gyroZ = fromBytesToInt32(data[8], data[9], data[10], data[11]);
final accelX = fromBytesToInt32(data[12], data[13], data[14], data[15]);
final accelY = fromBytesToInt32(data[16], data[17], data[18], data[19]);
final accelZ = fromBytesToInt32(data[20], data[21], data[22], data[23]);
final timeStamp = fromBytesToInt64(
data[24],
data[25],
data[26],
data[27],
data[28],
data[29],
data[30],
data[31],
);
print('GYRO x:$gyroX, y:$gyroY, z:$gyroZ');
print('ACCEL x:$accelX, y:$accelY, z:$accelZ');
print('TIMESTAMP: ${DateTime.fromMicrosecondsSinceEpoch(timeStamp)}');
}
Future<void> setCapturingOn() async {
Future<void> setCapturing({required bool on}) async {
final senseService = _services
.where(
(s) => s.serviceUuid == Guid('4c534d36-4453-3354-5253-657276696365'),
(s) => s.serviceUuid == Guid('19B10000-E8F2-537E-4F6C-D104768A1214'),
)
.first
.characteristics
.where(
(c) =>
c.characteristicUuid ==
Guid('63617074-7572-696E-6753-657276696365'),
Guid('19B10000-E8F2-537E-4F6C-D104768A1215'),
)
.first;
await senseService.write([1]);
await senseService.write([if (on) 1 else 0]);
}
Uint8List int64BigEndianBytes(int value) =>
@@ -144,7 +161,7 @@ class XiaoConnectorCubit extends Cubit<XiaoConnectorState> {
Future<void> setTime(int millisSinceEpoch) async {
final senseService = _services
.where(
(s) => s.serviceUuid == Guid('4c534d36-4453-3354-5253-657276696365'),
(s) => s.serviceUuid == Guid('19B10000-E8F2-537E-4F6C-D104768A1214'),
)
.first
.characteristics
@@ -162,77 +179,61 @@ class XiaoConnectorCubit extends Cubit<XiaoConnectorState> {
Future<void> startCapturing() async {
_senseService = _services
.where(
(s) => s.serviceUuid == Guid('4c534d36-4453-3354-5253-657276696365'),
(s) => s.serviceUuid == Guid('19B10000-E8F2-537E-4F6C-D104768A1214'),
)
.first
.characteristics
.where(
(c) =>
c.characteristicUuid ==
Guid('61636365-6c65-7261-7469-6f6e44617461'),
Guid('19B10000-E8F2-537E-4F6C-D104768A1216'),
)
.first;
dataCapturingSubscription = _senseService.onValueReceived.listen((value) {
dataCapturingSubscription = _senseService.onValueReceived.listen((data) {
// rotation
if (value.last == 1) {
_rotX = fromBytesToInt32(value[0], value[1], value[2], value[3]);
_rotY = fromBytesToInt32(value[4], value[5], value[6], value[7]);
_rotZ = fromBytesToInt32(value[8], value[9], value[10], value[11]);
gotRotation = true;
}
_rotX = fromBytesToInt32(data[0], data[1], data[2], data[3]);
_rotY = fromBytesToInt32(data[4], data[5], data[6], data[7]);
_rotZ = fromBytesToInt32(data[8], data[9], data[10], data[11]);
// acceleration
if (value.last == 2) {
_accX = fromBytesToInt32(value[0], value[1], value[2], value[3]);
_accY = fromBytesToInt32(value[4], value[5], value[6], value[7]);
_accZ = fromBytesToInt32(value[8], value[9], value[10], value[11]);
gotAcceleration = true;
}
_accX = fromBytesToInt32(data[12], data[13], data[14], data[15]);
_accY = fromBytesToInt32(data[16], data[17], data[18], data[19]);
_accZ = fromBytesToInt32(data[20], data[21], data[22], data[23]);
if (value.last == 3) {
final timeStamp = fromBytesToInt64(
value[0],
value[1],
value[2],
value[3],
value[4],
value[5],
value[6],
value[7],
);
_millisecondsSinceEpochSend = timeStamp;
gotTimeStamp = true;
}
final timeStamp = fromBytesToInt64(
data[24],
data[25],
data[26],
data[27],
data[28],
data[29],
data[30],
data[31],
);
_millisecondsSinceEpochSend = timeStamp;
if (gotAcceleration && gotRotation && gotTimeStamp) {
final capturePoint = CapturePoint(
type: _captureType,
uuid: _uuid,
rotationX: _rotX,
rotationY: _rotY,
rotationZ: _rotZ,
accelerationX: _accX,
accelerationY: _accY,
accelerationZ: _accZ,
millisecondsSinceEpochReceived:
DateTime.now().toUtc().millisecondsSinceEpoch,
millisecondsSinceEpochSend: _millisecondsSinceEpochSend,
);
emit(state.copyWith(lastCapturedPoint: capturePoint));
final capturePoint = CapturePoint(
type: _captureType,
uuid: _uuid,
rotationX: _rotX,
rotationY: _rotY,
rotationZ: _rotZ,
accelerationX: _accX,
accelerationY: _accY,
accelerationZ: _accZ,
millisecondsSinceEpochReceived:
DateTime.now().toUtc().millisecondsSinceEpoch,
millisecondsSinceEpochSend: _millisecondsSinceEpochSend,
);
emit(state.copyWith(lastCapturedPoint: capturePoint));
if (isRecording) {
_writeToObjectBox(capturePoint: capturePoint);
}
gotAcceleration = false;
gotRotation = false;
gotTimeStamp = false;
if (isRecording) {
_writeToObjectBox(capturePoint: capturePoint);
}
});
await setCapturingOn();
await setCapturing(on: true);
await setTime(DateTime.now().millisecondsSinceEpoch);
// await setTime(DateTime.now().millisecondsSinceEpoch);
device.cancelWhenDisconnected(dataCapturingSubscription);
@@ -246,6 +247,8 @@ class XiaoConnectorCubit extends Cubit<XiaoConnectorState> {
await _senseService.setNotifyValue(false);
await setCapturing(on: false);
emit(state.copyWith(status: XiaoConnectorStatus.connected));
}