From 59f6df4260f47b6f254241deb0c0050f9953191c Mon Sep 17 00:00:00 2001 From: Janez T Date: Tue, 17 Mar 2026 09:30:13 +0100 Subject: [PATCH] feat: GPS module toggle in device settings --- lib/providers/connection_provider.dart | 24 +++++++++++ lib/screens/device_config_screen.dart | 60 ++++++++++++++++++++++++++ pubspec.yaml | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/providers/connection_provider.dart b/lib/providers/connection_provider.dart index 527ebad..9a58c22 100644 --- a/lib/providers/connection_provider.dart +++ b/lib/providers/connection_provider.dart @@ -2202,6 +2202,30 @@ class ConnectionProvider with ChangeNotifier { } /// Request fresh device info (triggers SelfInfo response) + /// Read custom variables from device (GPS mode, sensor settings, etc.) + Future> getCustomVars() async { + if (!_activeService.isConnected) { + return {}; + } + try { + return await _activeService.getCustomVars(); + } catch (e) { + debugPrint('⚠️ [Provider] getCustomVars failed: $e'); + return {}; + } + } + + /// Set a custom variable on the device + Future setCustomVar(String key, String value) async { + if (!_activeService.isConnected) return; + try { + await _activeService.setCustomVar(key, value); + } catch (e) { + _error = 'Failed to set $key: $e'; + notifyListeners(); + } + } + Future refreshDeviceInfo() async { if (_isSpectrumScanActive) return; if (!_activeService.isConnected) { diff --git a/lib/screens/device_config_screen.dart b/lib/screens/device_config_screen.dart index a4c4bf3..a43f326 100644 --- a/lib/screens/device_config_screen.dart +++ b/lib/screens/device_config_screen.dart @@ -182,6 +182,8 @@ class _DeviceConfigScreenState extends State { bool _telemetryEnabled = false; bool _repeatEnabled = false; + bool? _gpsEnabled; // null = not supported by hardware + bool _gpsLoading = false; bool _autoAddDiscoveredContactsEnabled = true; bool _autoAddUsersEnabled = true; bool _autoAddRepeatersEnabled = true; @@ -294,6 +296,7 @@ class _DeviceConfigScreenState extends State { WidgetsBinding.instance.addPostFrameCallback((_) { _connectionProvider.getBatteryAndStorage(); unawaited(_connectionProvider.getAutoaddConfig()); + unawaited(_loadGpsMode()); }); } @@ -701,6 +704,37 @@ class _DeviceConfigScreenState extends State { } } + Future _loadGpsMode() async { + try { + final vars = await _connectionProvider.getCustomVars(); + if (!mounted) return; + final gpsValue = vars['gps']; + setState(() { + _gpsEnabled = gpsValue != null ? gpsValue == '1' : null; + }); + } catch (_) { + // Device may not support custom vars (old firmware / no GPS hardware) + } + } + + Future _setGpsMode(bool enabled) async { + setState(() => _gpsLoading = true); + try { + await _connectionProvider.setCustomVar('gps', enabled ? '1' : '0'); + if (!mounted) return; + setState(() { + _gpsEnabled = enabled; + _gpsLoading = false; + }); + } catch (e) { + if (!mounted) return; + setState(() => _gpsLoading = false); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Failed to set GPS mode: $e')), + ); + } + } + Future _useCurrentLocation() async { try { bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); @@ -1331,6 +1365,32 @@ class _DeviceConfigScreenState extends State { }, ), ), + if (_gpsEnabled != null) ...[ + const SizedBox(height: 12), + _SettingHighlightCard( + icon: _gpsEnabled! + ? Icons.gps_fixed + : Icons.gps_off, + title: 'GPS Module', + description: + 'Enable or disable the onboard GPS hardware.', + accentColor: _gpsEnabled! + ? colorScheme.primary + : colorScheme.onSurfaceVariant, + trailing: _gpsLoading + ? const SizedBox( + width: 24, + height: 24, + child: CircularProgressIndicator( + strokeWidth: 2, + ), + ) + : Switch( + value: _gpsEnabled!, + onChanged: _setGpsMode, + ), + ), + ], const SizedBox(height: 18), TextField( controller: _nameController, diff --git a/pubspec.yaml b/pubspec.yaml index fb3c01b..47cf7ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,7 +44,7 @@ dependencies: meshcore_client: git: url: https://github.com/dz0ny/meshcore_client.git - ref: "9b3d63c34ed0243becb76e306ee35be24760847d" + ref: "77c1f81f24b4839cf70439a478ff077c026c120e" # Codec2 ultra-low-bitrate speech codec (FFI plugin) codec2_flutter: