From 7aa7f7f5f6d7f5decf8a308bef07588a26a08866 Mon Sep 17 00:00:00 2001 From: baldeau Date: Fri, 27 Jun 2025 21:38:09 +0200 Subject: [PATCH] update for new rust implementation of the microcontroller --- .../cubit/xiao_connector_cubit.dart | 133 +++++++++--------- lib/xiao_connector/view/connected_view.dart | 9 ++ 2 files changed, 77 insertions(+), 65 deletions(-) diff --git a/lib/xiao_connector/cubit/xiao_connector_cubit.dart b/lib/xiao_connector/cubit/xiao_connector_cubit.dart index 9215705..5f66068 100644 --- a/lib/xiao_connector/cubit/xiao_connector_cubit.dart +++ b/lib/xiao_connector/cubit/xiao_connector_cubit.dart @@ -27,10 +27,6 @@ class XiaoConnectorCubit extends Cubit { late final Box _capturePointsBox; late final Box _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 { .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 setCapturingOn() async { + Future 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 { Future 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 { Future 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 { await _senseService.setNotifyValue(false); + await setCapturing(on: false); + emit(state.copyWith(status: XiaoConnectorStatus.connected)); } diff --git a/lib/xiao_connector/view/connected_view.dart b/lib/xiao_connector/view/connected_view.dart index 233cac6..35c57c9 100644 --- a/lib/xiao_connector/view/connected_view.dart +++ b/lib/xiao_connector/view/connected_view.dart @@ -51,6 +51,15 @@ class ConnectedView extends StatelessWidget { }, child: const Text('DEBUG'), ), + const SizedBox( + height: 48, + ), + ElevatedButton( + onPressed: () { + context.read().setCapturing(on: true); + }, + child: const Text('CAPTURE'), + ), ], ), ),