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

View File

@@ -51,6 +51,15 @@ class ConnectedView extends StatelessWidget {
}, },
child: const Text('DEBUG'), child: const Text('DEBUG'),
), ),
const SizedBox(
height: 48,
),
ElevatedButton(
onPressed: () {
context.read<XiaoConnectorCubit>().setCapturing(on: true);
},
child: const Text('CAPTURE'),
),
], ],
), ),
), ),