Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #273 from MXCzkEVM/blueberry_ring
Browse files Browse the repository at this point in the history
Blueberry ring
  • Loading branch information
reasje authored Aug 1, 2024
2 parents 9d03c7d + a4b242b commit 3d988d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BlueberryRingUseCase extends ReactiveUseCase {
}

Future<T> readData<T>(Uint8List Function() getCommandFunc, String dataName,
T Function(Uint8List) resolveData) async {
T Function(Uint8List) resolveData, bool isFrag) async {
return checkEstablishment<T>(
() async {
final command = getCommandFunc();
Expand All @@ -171,13 +171,29 @@ class BlueberryRingUseCase extends ReactiveUseCase {
// Create a completer to handle the response
final Completer<List<int>> completer = Completer<List<int>>();

Timer? timer;
List<int> data = [];

// Subscribe to the stream and filter for the specific command
final StreamSubscription<List<int>> subscription =
stream.listen((element) {
if (element.isNotEmpty && element.first == command.first) {
timer?.cancel();
collectLog('read$dataName:value $element');
if (!completer.isCompleted) {
completer.complete(element);
data.addAll(element);
collectLog('read$dataName:value $data');

if (!completer.isCompleted && !isFrag) {
completer.complete(data);
} else if (!completer.isCompleted &&
isFrag &&
element.last == 255) {
completer.complete(data);
} else {
timer = Timer(
const Duration(milliseconds: 5000),
() => completer.complete(data),
);
}
}
});
Expand All @@ -196,29 +212,44 @@ class BlueberryRingUseCase extends ReactiveUseCase {
);
}

Future<int> readLevel() async => readData<int>(
BlueberryCommands.readLevel, 'Level', BlueberryResolves.readLevel);
Future<int> readLevel() async => readData<int>(BlueberryCommands.readLevel,
'Level', BlueberryResolves.readLevel, BlueberryMethods.readLevel.frag);

Future<String> readVersion() async => readData<String>(
BlueberryCommands.readVersion, 'Version', BlueberryResolves.readVersion);
BlueberryCommands.readVersion,
'Version',
BlueberryResolves.readVersion,
BlueberryMethods.readVersion.frag);

Future<Uint8List> readTime() async => readData<Uint8List>(
BlueberryCommands.readTime, 'Time', BlueberryResolves.readTime);
BlueberryCommands.readTime,
'Time',
BlueberryResolves.readTime,
BlueberryMethods.readTime.frag);

Future<List<PeriodicSleepData>> readSleep() async =>
readData<List<PeriodicSleepData>>(
BlueberryCommands.readSleep, 'Sleep', BlueberryResolves.readSleep);
readData<List<PeriodicSleepData>>(BlueberryCommands.readSleep, 'Sleep',
BlueberryResolves.readSleep, BlueberryMethods.readSleep.frag);

Future<List<BloodOxygensData>> readBloodOxygens() async =>
readData<List<BloodOxygensData>>(BlueberryCommands.readBloodOxygens,
'BloodOxygens', BlueberryResolves.readBloodOxygens);
readData<List<BloodOxygensData>>(
BlueberryCommands.readBloodOxygens,
'BloodOxygens',
BlueberryResolves.readBloodOxygens,
BlueberryMethods.readBloodOxygens.frag);

Future<List<StepsData>> readSteps() async => readData<List<StepsData>>(
BlueberryCommands.readSteps, 'Steps', BlueberryResolves.readSteps);
BlueberryCommands.readSteps,
'Steps',
BlueberryResolves.readSteps,
BlueberryMethods.readSteps.frag);

Future<List<HeartRateData>> readHeartRate() async =>
readData<List<HeartRateData>>(BlueberryCommands.readHeartRates,
'HeartRate', BlueberryResolves.readHeartRates);
readData<List<HeartRateData>>(
BlueberryCommands.readHeartRates,
'HeartRate',
BlueberryResolves.readHeartRates,
BlueberryMethods.readHeartRates.frag);

Future<BluetoothCharacteristic> getBlueberryRingCharacteristic() async {
final service = await getBlueberryRingBluetoothService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'dart:typed_data';
class Method {
final int uid;
final List<int>? arg;
final bool frag;

Method({required this.uid, this.arg});
Method({required this.uid, this.arg, this.frag = false});
}

class BlueberryCommandsUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class GetMappings {
class BlueberryMethods {
static final readLevel = Method(uid: 0x13, arg: [1]);
static final readVersion = Method(uid: 0x27);
static final readSteps = Method(uid: 0x52, arg: [1]);
static final readTime = Method(uid: 0x41);
static final readSleep = Method(uid: 0x53, arg: [1, 2, 3]);
static final readHeartRates = Method(uid: 0x55, arg: [1, 2, 3]);
static final readBloodOxygens = Method(uid: 0x66, arg: [1, 2, 3]);
static final readSteps = Method(uid: 0x52, arg: [1], frag: true);
static final readSleep = Method(uid: 0x53, arg: [1, 2, 3], frag: true);
static final readHeartRates = Method(uid: 0x55, arg: [1, 2, 3], frag: true);
static final readBloodOxygens = Method(uid: 0x66, arg: [1, 2, 3], frag: true);
static final writeTime = Method(uid: 0x01, arg: [1, 2, 3, 4, 5, 6]);
static final writeRestore = Method(uid: 0x12, arg: []);
}
Expand Down

0 comments on commit 3d988d0

Please sign in to comment.