Skip to content

Commit 5d8d1e4

Browse files
authored
fix: initialization of scienceLabCommon and In-Built MIC (#2629)
1 parent a939c40 commit 5d8d1e4

File tree

5 files changed

+283
-276
lines changed

5 files changed

+283
-276
lines changed

lib/others/audio_jack.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import 'package:pslab/others/logger_service.dart';
33

44
class AudioJack {
55
static const int samplingRate = 44100;
6+
bool _isListening = false;
67
final FlutterAudioCapture flutterAudioCapture = FlutterAudioCapture();
78

89
List<double> _audioBuffer = [];
910

1011
AudioJack();
1112

12-
Future<bool> configure() async {
13+
Future<void> initialize() async {
1314
await flutterAudioCapture.init();
15+
}
16+
17+
Future<void> start() async {
1418
await flutterAudioCapture.start(_listener, _onError,
1519
sampleRate: samplingRate);
16-
return true;
20+
_isListening = true;
1721
}
1822

1923
List<double> read() {
@@ -28,7 +32,12 @@ class AudioJack {
2832
logger.e(e);
2933
}
3034

31-
void close() async {
35+
Future<void> close() async {
3236
await flutterAudioCapture.stop();
37+
_isListening = false;
38+
}
39+
40+
bool isListening() {
41+
return _isListening;
3342
}
3443
}

lib/others/science_lab_common.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class ScienceLabCommon {
2828
return communicationHandler.initialize();
2929
}
3030

31-
void setConnected() {
32-
communicationHandler.connected = true;
31+
void setConnected(bool connected) {
32+
communicationHandler.connected = connected;
3333
}
3434

3535
bool isConnected() {

lib/providers/board_state_provider.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ class BoardStateProvider extends ChangeNotifier {
2121
await scienceLabCommon.initialize();
2222
pslabIsConnected = await scienceLabCommon.openDevice();
2323
setPSLabVersionIDs();
24-
UsbSerial.usbEventStream?.listen((UsbEvent usbEvent) async {
25-
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
26-
if (await attemptToConnectPSLab()) {
27-
pslabIsConnected = await scienceLabCommon.openDevice();
28-
setPSLabVersionIDs();
24+
UsbSerial.usbEventStream?.listen(
25+
(UsbEvent usbEvent) async {
26+
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
27+
if (await attemptToConnectPSLab()) {
28+
pslabIsConnected = await scienceLabCommon.openDevice();
29+
setPSLabVersionIDs();
30+
}
31+
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
32+
scienceLabCommon.setConnected(false);
33+
pslabIsConnected = false;
34+
pslabVersionID = 'Not Connected';
35+
notifyListeners();
2936
}
30-
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
31-
scienceLabCommon.setConnected();
32-
pslabIsConnected = false;
33-
pslabVersionID = 'Not Connected';
34-
notifyListeners();
35-
}
36-
});
37+
},
38+
);
3739
}
3840

3941
Future<void> setPSLabVersionIDs() async {

lib/providers/oscilloscope_state_provider.dart

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ enum ChannelMeasurements {
2424
negativePeak
2525
}
2626

27-
AudioJack? _audioJack;
28-
2927
class OscilloscopeStateProvider extends ChangeNotifier {
28+
late AudioJack _audioJack;
3029
late int _selectedIndex;
3130

3231
int get selectedIndex => _selectedIndex;
@@ -88,6 +87,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
8887
late Timer _timer;
8988

9089
OscilloscopeStateProvider() {
90+
_audioJack = AudioJack();
9191
_selectedIndex = 0;
9292

9393
isCH1Selected = false;
@@ -160,9 +160,9 @@ class OscilloscopeStateProvider extends ChangeNotifier {
160160
_isProcessing = true;
161161

162162
if (_isRunning) {
163-
if (isInBuiltMICSelected && _audioJack == null) {
164-
_audioJack = AudioJack();
165-
await _audioJack?.configure();
163+
if (isInBuiltMICSelected && !_audioJack.isListening()) {
164+
await _audioJack.initialize();
165+
await _audioJack.start();
166166
}
167167

168168
List<String> channels = [];
@@ -191,6 +191,9 @@ class OscilloscopeStateProvider extends ChangeNotifier {
191191
dataEntries = [];
192192
}
193193
}
194+
if (!isInBuiltMICSelected && _audioJack.isListening()) {
195+
await _audioJack.close();
196+
}
194197
}
195198
_isProcessing = false;
196199
},
@@ -348,8 +351,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
348351
noOfChannels++;
349352
isTriggered = false;
350353
entries.add([]);
351-
_audioJack ??= AudioJack();
352-
List<double> buffer = _audioJack!.read();
354+
List<double> buffer = _audioJack.read();
353355
xDataString = List.filled(buffer.length, '');
354356
yDataString.add(List.filled(buffer.length, ''));
355357

@@ -513,17 +515,20 @@ class OscilloscopeStateProvider extends ChangeNotifier {
513515
Colors.white,
514516
Colors.deepPurple
515517
];
516-
return List<LineChartBarData>.generate(dataEntries.length, (index) {
517-
return LineChartBarData(
518-
spots: dataEntries[index],
519-
isCurved: true,
520-
color: colors[index % colors.length],
521-
barWidth: 1,
522-
dotData: const FlDotData(
523-
show: false,
524-
),
525-
);
526-
});
518+
return List<LineChartBarData>.generate(
519+
dataEntries.length,
520+
(index) {
521+
return LineChartBarData(
522+
spots: dataEntries[index],
523+
isCurved: true,
524+
color: colors[index % colors.length],
525+
barWidth: 1,
526+
dotData: const FlDotData(
527+
show: false,
528+
),
529+
);
530+
},
531+
);
527532
}
528533

529534
@override
@@ -532,6 +537,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
532537
if (_timer.isActive) {
533538
_timer.cancel();
534539
}
540+
_audioJack.close();
535541
super.dispose();
536542
}
537543
}

0 commit comments

Comments
 (0)