Skip to content

Commit

Permalink
Merge pull request #93 from sharedstreets/ble-dual
Browse files Browse the repository at this point in the history
add SiWheel support for dual messages
  • Loading branch information
russbiggs authored Mar 17, 2021
2 parents f4d1f3e + 6ee2376 commit 4cf6143
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
62 changes: 51 additions & 11 deletions lib/service/bluetooth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -25,6 +26,7 @@ class BleWheel {

BluetoothCharacteristic _forwardCharacteristic;
BluetoothCharacteristic _reverseCharacteristic;
BluetoothCharacteristic _dualCharacteristic;

bool _connectionFailed = false;

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4cf6143

Please sign in to comment.