diff --git a/open_wearable/docs/pages/sensor-pages.md b/open_wearable/docs/pages/sensor-pages.md index a759f87ef..b7d8957fd 100644 --- a/open_wearable/docs/pages/sensor-pages.md +++ b/open_wearable/docs/pages/sensor-pages.md @@ -43,6 +43,7 @@ - Does: - Builds sensor cards for all active sensors across wearables. - Supports no-graph mode and hide-empty-graphs mode. + - Can show expected and actual sampling rates on sensor charts. Labels are off by default; the preference is saved under Settings → General settings → Live data → Show sampling rates. - Uses merged provider listenables for efficient live refresh. - Provides: - Live chart/value surface for all connected sensors. diff --git a/open_wearable/lib/assets/fonts/OFL.txt b/open_wearable/lib/assets/fonts/OFL.txt new file mode 100644 index 000000000..c2a18415e --- /dev/null +++ b/open_wearable/lib/assets/fonts/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2015 The Roboto Mono Project Authors (https://github.com/googlefonts/robotomono) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/open_wearable/lib/assets/fonts/RobotoMono.ttf b/open_wearable/lib/assets/fonts/RobotoMono.ttf new file mode 100644 index 000000000..f21d1d716 Binary files /dev/null and b/open_wearable/lib/assets/fonts/RobotoMono.ttf differ diff --git a/open_wearable/lib/models/app_shutdown_settings.dart b/open_wearable/lib/models/app_shutdown_settings.dart index 3f64a1482..ff244d15f 100644 --- a/open_wearable/lib/models/app_shutdown_settings.dart +++ b/open_wearable/lib/models/app_shutdown_settings.dart @@ -19,6 +19,7 @@ class AppShutdownSettings { 'app_disable_live_data_graphs'; static const String _hideLiveDataGraphsWithoutDataKey = 'app_hide_live_data_graphs_without_data'; + static const String _showSamplingRatesKey = 'app_show_sampling_rates'; static const String _keepAppInForegroundKey = 'app_keep_in_foreground'; static final ValueNotifier _shutOffAllSensorsOnAppCloseNotifier = @@ -27,6 +28,8 @@ class AppShutdownSettings { ValueNotifier(false); static final ValueNotifier _hideLiveDataGraphsWithoutDataNotifier = ValueNotifier(false); + static final ValueNotifier _showSamplingRatesNotifier = + ValueNotifier(false); static final ValueNotifier _keepAppInForegroundNotifier = ValueNotifier(false); @@ -36,6 +39,8 @@ class AppShutdownSettings { _disableLiveDataGraphsNotifier; static ValueListenable get hideLiveDataGraphsWithoutDataListenable => _hideLiveDataGraphsWithoutDataNotifier; + static ValueListenable get showSamplingRatesListenable => + _showSamplingRatesNotifier; static ValueListenable get keepAppInForegroundListenable => _keepAppInForegroundNotifier; @@ -44,6 +49,7 @@ class AppShutdownSettings { static bool get disableLiveDataGraphs => _disableLiveDataGraphsNotifier.value; static bool get hideLiveDataGraphsWithoutData => _hideLiveDataGraphsWithoutDataNotifier.value; + static bool get showSamplingRates => _showSamplingRatesNotifier.value; static bool get keepAppInForeground => _keepAppInForegroundNotifier.value; static Future initialize() async { @@ -51,6 +57,7 @@ class AppShutdownSettings { loadShutOffAllSensorsOnAppClose(), loadDisableLiveDataGraphs(), loadHideLiveDataGraphsWithoutData(), + loadShowSamplingRates(), loadKeepAppInForeground(), ]); } @@ -97,6 +104,22 @@ class AppShutdownSettings { return enabled; } + /// Loads whether live graphs show their expected and actual sampling rates. + static Future loadShowSamplingRates() async { + final prefs = await SharedPreferences.getInstance(); + final enabled = prefs.getBool(_showSamplingRatesKey) ?? false; + _showSamplingRatesNotifier.value = enabled; + return enabled; + } + + /// Persists label visibility and updates all live graphs immediately. + static Future saveShowSamplingRates(bool enabled) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setBool(_showSamplingRatesKey, enabled); + _showSamplingRatesNotifier.value = enabled; + return enabled; + } + static Future loadKeepAppInForeground() async { final prefs = await SharedPreferences.getInstance(); final enabled = prefs.getBool(_keepAppInForegroundKey) ?? false; diff --git a/open_wearable/lib/widgets/sensors/values/sensor_chart.dart b/open_wearable/lib/widgets/sensors/values/sensor_chart.dart index 20a460b4e..5baedef1c 100644 --- a/open_wearable/lib/widgets/sensors/values/sensor_chart.dart +++ b/open_wearable/lib/widgets/sensors/values/sensor_chart.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'; import 'package:open_earable_flutter/open_earable_flutter.dart' hide logger; +import 'package:open_wearable/models/app_shutdown_settings.dart'; import 'package:open_wearable/view_models/sensor_configuration_provider.dart'; import 'package:open_wearable/view_models/sensor_data_provider.dart'; import 'package:open_wearable/view_models/wearables_provider.dart'; @@ -301,9 +302,40 @@ class _SensorChartState extends State { child: LiveDataGraphSurface( settings: widget.settings, onDisabledTap: widget.onDisabledTap, - child: LineChart( - chartData, - duration: const Duration(milliseconds: 0), + child: Stack( + fit: StackFit.expand, + children: [ + LineChart( + chartData, + duration: const Duration(milliseconds: 0), + ), + Positioned( + top: 4, + right: 4, + child: ValueListenableBuilder( + valueListenable: + AppShutdownSettings.showSamplingRatesListenable, + builder: (context, showSamplingRates, _) { + if (!showSamplingRates) return const SizedBox.shrink(); + return IgnorePointer( + child: Text( + _samplingRateLabel( + sensor, + sensorValues, + referenceTimestamp, + sensorConfigurationProvider, + ), + style: const TextStyle( + color: Colors.grey, + fontSize: 10, + fontFamily: 'RobotoMono', + ), + ), + ); + }, + ), + ), + ], ), ), ), @@ -587,6 +619,42 @@ class _SensorChartState extends State { } } + String _samplingRateLabel( + Sensor sensor, + Queue values, + int referenceTimestamp, + SensorConfigurationProvider? configurationProvider, + ) { + double? expectedHz; + for (final configuration in sensor.relatedConfigurations) { + final activeValue = _activeFrequencyValueForConfiguration( + configuration, + configurationProvider, + ); + if (activeValue == null) continue; + final rate = _isStreamingFrequencyValue(configuration, activeValue) + ? activeValue.frequencyHz + : 0.0; + expectedHz = max(expectedHz ?? 0, rate); + } + + // Use the existing rolling buffer, including silence since the last sample. + final elapsed = values.isEmpty + ? 0.0 + : (referenceTimestamp - values.first.timestamp) * + pow(10, sensor.timestampExponent); + final actualHz = + values.length < 2 || elapsed <= 0 ? 0.0 : (values.length - 1) / elapsed; + // Round up below the target, but only show an excess of at least 1 Hz. + final actual = expectedHz != null && actualHz < expectedHz + 1 + ? min(actualHz.ceil(), expectedHz.round()) + : actualHz.round(); + final expected = expectedHz?.round().toString() ?? '—'; + // Reserve only as many digits as the expected rate needs. + return 'Expected $expected Hz · ' + 'Actual ${actual.toString().padLeft(expected.length)} Hz'; + } + _FilterFrequencyBounds _frequencyBoundsForSensor( Sensor sensor, { SensorConfigurationProvider? sensorConfigurationProvider, diff --git a/open_wearable/lib/widgets/settings/general_settings_page.dart b/open_wearable/lib/widgets/settings/general_settings_page.dart index f2b9dc2fc..5a73bec4a 100644 --- a/open_wearable/lib/widgets/settings/general_settings_page.dart +++ b/open_wearable/lib/widgets/settings/general_settings_page.dart @@ -74,6 +74,26 @@ class _GeneralSettingsPageState extends State { } } + Future _setShowSamplingRates(bool enabled) async { + if (_isSaving) { + return; + } + + setState(() { + _isSaving = true; + }); + + try { + await AppShutdownSettings.saveShowSamplingRates(enabled); + } finally { + if (mounted) { + setState(() { + _isSaving = false; + }); + } + } + } + Future _setAutoConnectEnabled(bool enabled) async { if (_isSaving) { return; @@ -263,6 +283,25 @@ class _GeneralSettingsPageState extends State { 'Hides live data graphs in Sensors › Live Data until samples arrive', ), ), + ValueListenableBuilder( + valueListenable: AppShutdownSettings + .showSamplingRatesListenable, + builder: (context, showSamplingRates, _) => + SwitchListTile.adaptive( + value: showSamplingRates, + onChanged: _isSaving + ? null + : _setShowSamplingRates, + secondary: const Icon( + Icons.speed_rounded, + size: 18, + ), + title: const Text('Show sampling rates'), + subtitle: const Text( + 'Show expected and actual sampling rates on live graphs', + ), + ), + ), ], ), ], diff --git a/open_wearable/pubspec.yaml b/open_wearable/pubspec.yaml index fa0dee126..ea408c8cd 100644 --- a/open_wearable/pubspec.yaml +++ b/open_wearable/pubspec.yaml @@ -92,12 +92,18 @@ flutter: # the material Icons class. uses-material-design: true + fonts: + - family: RobotoMono + fonts: + - asset: lib/assets/fonts/RobotoMono.ttf + # To add assets to your application, add an assets section, like this: assets: - lib/apps/posture_tracker/assets/ - lib/apps/heart_tracker/assets/ - lib/apps/seal_check/assets/ - lib/assets/devices/phone-app.png + - lib/assets/fonts/OFL.txt - android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png # An image asset can refer to one or more resolution-specific "variants", seeq