From 81b53a91d8a818ac7016725d33424ca269d72676 Mon Sep 17 00:00:00 2001 From: Kevin Webb Date: Wed, 17 Mar 2021 11:35:07 -0400 Subject: [PATCH 1/3] add SiWheel support for dual messages --- lib/service/bluetooth_service.dart | 62 ++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/service/bluetooth_service.dart b/lib/service/bluetooth_service.dart index 5da5d93..6a167b5 100644 --- a/lib/service/bluetooth_service.dart +++ b/lib/service/bluetooth_service.dart @@ -9,6 +9,7 @@ import 'package:shared_preferences/shared_preferences.dart'; final WHEEL_SERVICE_UUID = new Guid("1381f6e7-12f9-4ad7-aa87-1c5d50fe03f9"); final FORWARD_UUID = new Guid("4365ec89-253c-49f4-b8a4-3ffe6ad73673"); final REVERSE_UUID = new Guid("0d7f503f-78ed-4c24-a9f3-271264661448"); +final DUAL_UUID = new Guid("1fb110c0-3874-4c32-99c1-e1c8669c543f"); enum WheelStatus { CONNECTING, CONNECTED, DISCONNECTED } @@ -25,6 +26,7 @@ class BleWheel { BluetoothCharacteristic _forwardCharacteristic; BluetoothCharacteristic _reverseCharacteristic; + BluetoothCharacteristic _dualCharacteristic; bool _connectionFailed = false; @@ -92,24 +94,31 @@ class BleWheel { _forwardCharacteristic = characteristic; } else if (characteristic.uuid == REVERSE_UUID) { _reverseCharacteristic = characteristic; + } else if (characteristic.uuid == DUAL_UUID) { + _dualCharacteristic = characteristic; } } } } - if (_reverseCharacteristic == null) { - _connectionFailed = true; - await _device.disconnect(); - } - - if (_reverseCharacteristic == null) { - _connectionFailed = true; - await _device.disconnect(); - } +// try dual characteristic first + if (_dualCharacteristic != null) { + await subscribeDualCounter(); + } else { + // fall back to individual forward/reverse message streams if dual isn't avaialble + if (_reverseCharacteristic == null) { + _connectionFailed = true; + await _device.disconnect(); + } - await subscribeForwardCounter(); - await subscribeReverseCounter(); + if (_reverseCharacteristic == null) { + _connectionFailed = true; + await _device.disconnect(); + } + await subscribeForwardCounter(); + await subscribeReverseCounter(); + } _stateStreamController.add(WheelStatus.CONNECTED); } } @@ -120,9 +129,37 @@ class BleWheel { _reverseCharacteristic = null; } + subscribeDualCounter() async { + await _dualCharacteristic.setNotifyValue(true); + + // dual characterist packs the forward and reverse counts into a single 64bit message + // first four byte word is the forward count + // second four bytes word is reverse count + // both words are in little endian order + + _dualCharacteristic.value.listen((value) { + int forwardIntVal = + Uint8List.fromList(value.reversed.toList().sublist(4, 8)) + .buffer + .asByteData() + .getUint32(0); + _forwardStreamController.add(forwardIntVal); + + int reverseIntVal = + Uint8List.fromList(value.reversed.toList().sublist(0, 4)) + .buffer + .asByteData() + .getUint32(0); + _reverseStreamController.add(reverseIntVal); + }); + } + subscribeForwardCounter() async { await _forwardCharacteristic.setNotifyValue(true); + // forward characteristic reports counts as 32bit message + // in little endian order + _forwardCharacteristic.value.listen((value) { int intVal = Uint8List.fromList(value.reversed.toList()) .buffer @@ -135,6 +172,9 @@ class BleWheel { subscribeReverseCounter() async { await _reverseCharacteristic.setNotifyValue(true); + // reverse characteristic reports counts as 32bit message + // in little endian order + _reverseCharacteristic.value.listen((value) { int intVal = Uint8List.fromList(value.reversed.toList()) .buffer From 4c2fadaebbffa55b8a11e2e3454de62a64cad435 Mon Sep 17 00:00:00 2001 From: Kevin Webb Date: Wed, 17 Mar 2021 11:35:07 -0400 Subject: [PATCH 2/3] add SiWheel support for dual messages --- lib/service/bluetooth_service.dart | 62 ++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/service/bluetooth_service.dart b/lib/service/bluetooth_service.dart index 5da5d93..6a167b5 100644 --- a/lib/service/bluetooth_service.dart +++ b/lib/service/bluetooth_service.dart @@ -9,6 +9,7 @@ import 'package:shared_preferences/shared_preferences.dart'; final WHEEL_SERVICE_UUID = new Guid("1381f6e7-12f9-4ad7-aa87-1c5d50fe03f9"); final FORWARD_UUID = new Guid("4365ec89-253c-49f4-b8a4-3ffe6ad73673"); final REVERSE_UUID = new Guid("0d7f503f-78ed-4c24-a9f3-271264661448"); +final DUAL_UUID = new Guid("1fb110c0-3874-4c32-99c1-e1c8669c543f"); enum WheelStatus { CONNECTING, CONNECTED, DISCONNECTED } @@ -25,6 +26,7 @@ class BleWheel { BluetoothCharacteristic _forwardCharacteristic; BluetoothCharacteristic _reverseCharacteristic; + BluetoothCharacteristic _dualCharacteristic; bool _connectionFailed = false; @@ -92,24 +94,31 @@ class BleWheel { _forwardCharacteristic = characteristic; } else if (characteristic.uuid == REVERSE_UUID) { _reverseCharacteristic = characteristic; + } else if (characteristic.uuid == DUAL_UUID) { + _dualCharacteristic = characteristic; } } } } - if (_reverseCharacteristic == null) { - _connectionFailed = true; - await _device.disconnect(); - } - - if (_reverseCharacteristic == null) { - _connectionFailed = true; - await _device.disconnect(); - } +// try dual characteristic first + if (_dualCharacteristic != null) { + await subscribeDualCounter(); + } else { + // fall back to individual forward/reverse message streams if dual isn't avaialble + if (_reverseCharacteristic == null) { + _connectionFailed = true; + await _device.disconnect(); + } - await subscribeForwardCounter(); - await subscribeReverseCounter(); + if (_reverseCharacteristic == null) { + _connectionFailed = true; + await _device.disconnect(); + } + await subscribeForwardCounter(); + await subscribeReverseCounter(); + } _stateStreamController.add(WheelStatus.CONNECTED); } } @@ -120,9 +129,37 @@ class BleWheel { _reverseCharacteristic = null; } + subscribeDualCounter() async { + await _dualCharacteristic.setNotifyValue(true); + + // dual characterist packs the forward and reverse counts into a single 64bit message + // first four byte word is the forward count + // second four bytes word is reverse count + // both words are in little endian order + + _dualCharacteristic.value.listen((value) { + int forwardIntVal = + Uint8List.fromList(value.reversed.toList().sublist(4, 8)) + .buffer + .asByteData() + .getUint32(0); + _forwardStreamController.add(forwardIntVal); + + int reverseIntVal = + Uint8List.fromList(value.reversed.toList().sublist(0, 4)) + .buffer + .asByteData() + .getUint32(0); + _reverseStreamController.add(reverseIntVal); + }); + } + subscribeForwardCounter() async { await _forwardCharacteristic.setNotifyValue(true); + // forward characteristic reports counts as 32bit message + // in little endian order + _forwardCharacteristic.value.listen((value) { int intVal = Uint8List.fromList(value.reversed.toList()) .buffer @@ -135,6 +172,9 @@ class BleWheel { subscribeReverseCounter() async { await _reverseCharacteristic.setNotifyValue(true); + // reverse characteristic reports counts as 32bit message + // in little endian order + _reverseCharacteristic.value.listen((value) { int intVal = Uint8List.fromList(value.reversed.toList()) .buffer From 6ee237633041deebccad6add642475235cd63ef1 Mon Sep 17 00:00:00 2001 From: Kevin Webb Date: Wed, 17 Mar 2021 14:17:32 -0400 Subject: [PATCH 3/3] fix pubspec.lock regression --- pubspec.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.lock b/pubspec.lock index 25bdf69..73dcb1d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -620,7 +620,7 @@ packages: name: progresso url: "https://pub.dartlang.org" source: hosted - version: "0.2.0" + version: "0.1.1" protobuf: dependency: transitive description: