diff options
Diffstat (limited to 'lib/utils/esense_input.dart')
-rw-r--r-- | lib/utils/esense_input.dart | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/utils/esense_input.dart b/lib/utils/esense_input.dart index b738664..14cf7de 100644 --- a/lib/utils/esense_input.dart +++ b/lib/utils/esense_input.dart @@ -8,9 +8,12 @@ import 'package:sense_the_rhythm/models/arrow_direction.dart'; import 'package:sense_the_rhythm/models/input_direction.dart'; class ESenseInput { + // create singleton that is available on all widgets so it does not have to be + // carried down in the widget tree static final instance = ESenseInput._(); ESenseManager eSenseManager = ESenseManager('unknown'); + // valuenotifier allows widgets to rerender when the value changes ValueNotifier<String> deviceStatus = ValueNotifier('Disconnected'); StreamSubscription? subscription; @@ -29,8 +32,11 @@ class ESenseInput { _listenToESense(); } + /// ask and check if permissions are enabled and granted Future<bool> _askForPermissions() async { + // is desktop if (!Platform.isAndroid && !Platform.isIOS) return false; + // is bluetooth even enabled? if (!await Permission.bluetooth.serviceStatus.isEnabled) { deviceStatus.value = "Bluetooth is disabled!"; return false; @@ -55,6 +61,7 @@ class ESenseInput { return true; } + /// listen to connectionEvents and set deviceStatus void _listenToESense() { // if you want to get the connection events when connecting, // set up the listener BEFORE connecting... @@ -89,12 +96,14 @@ class ESenseInput { }); } + /// get eSenseEvent stream only containung button events Stream<ButtonEventChanged> buttonEvents() { return eSenseManager.eSenseEvents .where((event) => event.runtimeType == ButtonEventChanged) .cast(); } + /// sets sampling rate and listens to sensorEvents void _startListenToSensorEvents() async { // // any changes to the sampling frequency must be done BEFORE listening to sensor events print('setting sampling frequency...'); @@ -115,11 +124,14 @@ class ESenseInput { sampling = true; } + /// cancels the sensorEvents listening void _pauseListenToSensorEvents() async { subscription?.cancel(); sampling = false; } + /// add up all new gyro [data] in the form of deg/s multiplied by scaling factor + /// to get real angles void _parseGyroData(List<int> data) { // Float value in deg/s = Gyro value / Gyro scale factor // The default configuration is +- 500deg/s for the gyroscope. @@ -131,6 +143,7 @@ class ESenseInput { // print('${z.toDouble() / 500.0 * (1.0 / 10.0)}'); } + /// nulls all angles and reset inputDirection void resetAngles() { inputDirection.reset(); x = 0; @@ -138,26 +151,29 @@ class ESenseInput { z = 0; } + /// get InputDirection by checking if angels are in defined ranges and + /// calibrating based on the [expect]ed direction from ArrowDirection InputDirection getInputDirection(ArrowDirection expect) { + // check if angle is in range inputDirection.up = z > 180 && z < 340; inputDirection.down = z > 20 && z < 180; inputDirection.left = y > 0 && y < 180; inputDirection.right = y > 180 && y < 360; + // calibrate based on expected directoin from ArrowDirection if (expect == ArrowDirection.up && inputDirection.up || expect == ArrowDirection.down && inputDirection.down) { y = 0; - print("ehit"); } if (expect == ArrowDirection.left && inputDirection.left || expect == ArrowDirection.right && inputDirection.right) { z = 0; - print("ehit"); } return inputDirection; } + /// connect to ESense with [deviceName] by first asking for permissions Future<void> connectToESense(String deviceName) async { if (!connected) { bool permissionSuccessfull = await _askForPermissions(); |