summaryrefslogtreecommitdiff
path: root/lib/utils/esense_input.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils/esense_input.dart')
-rw-r--r--lib/utils/esense_input.dart60
1 files changed, 28 insertions, 32 deletions
diff --git a/lib/utils/esense_input.dart b/lib/utils/esense_input.dart
index 14cf7de..08875d5 100644
--- a/lib/utils/esense_input.dart
+++ b/lib/utils/esense_input.dart
@@ -15,18 +15,17 @@ class ESenseInput {
ESenseManager eSenseManager = ESenseManager('unknown');
// valuenotifier allows widgets to rerender when the value changes
ValueNotifier<String> deviceStatus = ValueNotifier('Disconnected');
- StreamSubscription? subscription;
+ StreamSubscription? _subscription;
String eSenseDeviceName = '';
bool connected = false;
- bool sampling = false;
- int sampleRate = 20;
+ final int _sampleRate = 20;
- InputDirection inputDirection = InputDirection();
- int x = 0;
- int y = 0;
- int z = 0;
+ final InputDirection _inputDirection = InputDirection();
+ int _x = 0;
+ int _y = 0;
+ int _z = 0;
ESenseInput._() {
_listenToESense();
@@ -83,7 +82,6 @@ class ESenseInput {
break;
case ConnectionType.disconnected:
deviceStatus.value = 'Disconnected';
- sampling = false;
_pauseListenToSensorEvents();
break;
case ConnectionType.device_found:
@@ -107,7 +105,7 @@ class ESenseInput {
void _startListenToSensorEvents() async {
// // any changes to the sampling frequency must be done BEFORE listening to sensor events
print('setting sampling frequency...');
- bool successs = await eSenseManager.setSamplingRate(sampleRate);
+ bool successs = await eSenseManager.setSamplingRate(_sampleRate);
if (successs) {
print('setSamplingRate success');
} else {
@@ -115,19 +113,17 @@ class ESenseInput {
}
// subscribe to sensor event from the eSense device
- subscription = eSenseManager.sensorEvents.listen((event) {
+ _subscription = eSenseManager.sensorEvents.listen((event) {
// print('SENSOR event: $event');
if (event.gyro != null) {
_parseGyroData(event.gyro!);
}
});
- sampling = true;
}
/// cancels the sensorEvents listening
void _pauseListenToSensorEvents() async {
- subscription?.cancel();
- sampling = false;
+ _subscription?.cancel();
}
/// add up all new gyro [data] in the form of deg/s multiplied by scaling factor
@@ -135,42 +131,42 @@ class ESenseInput {
void _parseGyroData(List<int> data) {
// Float value in deg/s = Gyro value / Gyro scale factor
// The default configuration is +- 500deg/s for the gyroscope.
- x = (x + (15 * data[0] ~/ (500 * sampleRate))) % 360;
- y = (y + (15 * data[1] ~/ (500 * sampleRate))) % 360;
- z = (z + (15 * data[2] ~/ (500 * sampleRate))) % 360;
- print('$x, $y, $z');
+ _x = (_x + (15 * data[0] ~/ (500 * _sampleRate))) % 360;
+ _y = (_y + (15 * data[1] ~/ (500 * _sampleRate))) % 360;
+ _z = (_z + (15 * data[2] ~/ (500 * _sampleRate))) % 360;
+ print('$_x, $_y, $_z');
// print('${(z.toDouble() / 500.0 * (1.0 / sampleRate.toDouble())) * 7.5}');
// print('${z.toDouble() / 500.0 * (1.0 / 10.0)}');
}
/// nulls all angles and reset inputDirection
void resetAngles() {
- inputDirection.reset();
- x = 0;
- y = 0;
- z = 0;
+ _inputDirection.reset();
+ _x = 0;
+ _y = 0;
+ _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;
+ _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;
+ if (expect == ArrowDirection.up && _inputDirection.up ||
+ expect == ArrowDirection.down && _inputDirection.down) {
+ _y = 0;
}
- if (expect == ArrowDirection.left && inputDirection.left ||
- expect == ArrowDirection.right && inputDirection.right) {
- z = 0;
+ if (expect == ArrowDirection.left && _inputDirection.left ||
+ expect == ArrowDirection.right && _inputDirection.right) {
+ _z = 0;
}
- return inputDirection;
+ return _inputDirection;
}
/// connect to ESense with [deviceName] by first asking for permissions