Skip to content

Commit

Permalink
fix: initialization of scienceLabCommon and In-Built MIC (#2629)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsCress authored Feb 11, 2025
1 parent a939c40 commit 5d8d1e4
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 276 deletions.
15 changes: 12 additions & 3 deletions lib/others/audio_jack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import 'package:pslab/others/logger_service.dart';

class AudioJack {
static const int samplingRate = 44100;
bool _isListening = false;
final FlutterAudioCapture flutterAudioCapture = FlutterAudioCapture();

List<double> _audioBuffer = [];

AudioJack();

Future<bool> configure() async {
Future<void> initialize() async {
await flutterAudioCapture.init();
}

Future<void> start() async {
await flutterAudioCapture.start(_listener, _onError,
sampleRate: samplingRate);
return true;
_isListening = true;
}

List<double> read() {
Expand All @@ -28,7 +32,12 @@ class AudioJack {
logger.e(e);
}

void close() async {
Future<void> close() async {
await flutterAudioCapture.stop();
_isListening = false;
}

bool isListening() {
return _isListening;
}
}
4 changes: 2 additions & 2 deletions lib/others/science_lab_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ScienceLabCommon {
return communicationHandler.initialize();
}

void setConnected() {
communicationHandler.connected = true;
void setConnected(bool connected) {
communicationHandler.connected = connected;
}

bool isConnected() {
Expand Down
26 changes: 14 additions & 12 deletions lib/providers/board_state_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ class BoardStateProvider extends ChangeNotifier {
await scienceLabCommon.initialize();
pslabIsConnected = await scienceLabCommon.openDevice();
setPSLabVersionIDs();
UsbSerial.usbEventStream?.listen((UsbEvent usbEvent) async {
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
if (await attemptToConnectPSLab()) {
pslabIsConnected = await scienceLabCommon.openDevice();
setPSLabVersionIDs();
UsbSerial.usbEventStream?.listen(
(UsbEvent usbEvent) async {
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
if (await attemptToConnectPSLab()) {
pslabIsConnected = await scienceLabCommon.openDevice();
setPSLabVersionIDs();
}
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
scienceLabCommon.setConnected(false);
pslabIsConnected = false;
pslabVersionID = 'Not Connected';
notifyListeners();
}
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
scienceLabCommon.setConnected();
pslabIsConnected = false;
pslabVersionID = 'Not Connected';
notifyListeners();
}
});
},
);
}

Future<void> setPSLabVersionIDs() async {
Expand Down
42 changes: 24 additions & 18 deletions lib/providers/oscilloscope_state_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ enum ChannelMeasurements {
negativePeak
}

AudioJack? _audioJack;

class OscilloscopeStateProvider extends ChangeNotifier {
late AudioJack _audioJack;
late int _selectedIndex;

int get selectedIndex => _selectedIndex;
Expand Down Expand Up @@ -88,6 +87,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
late Timer _timer;

OscilloscopeStateProvider() {
_audioJack = AudioJack();
_selectedIndex = 0;

isCH1Selected = false;
Expand Down Expand Up @@ -160,9 +160,9 @@ class OscilloscopeStateProvider extends ChangeNotifier {
_isProcessing = true;

if (_isRunning) {
if (isInBuiltMICSelected && _audioJack == null) {
_audioJack = AudioJack();
await _audioJack?.configure();
if (isInBuiltMICSelected && !_audioJack.isListening()) {
await _audioJack.initialize();
await _audioJack.start();
}

List<String> channels = [];
Expand Down Expand Up @@ -191,6 +191,9 @@ class OscilloscopeStateProvider extends ChangeNotifier {
dataEntries = [];
}
}
if (!isInBuiltMICSelected && _audioJack.isListening()) {
await _audioJack.close();
}
}
_isProcessing = false;
},
Expand Down Expand Up @@ -348,8 +351,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
noOfChannels++;
isTriggered = false;
entries.add([]);
_audioJack ??= AudioJack();
List<double> buffer = _audioJack!.read();
List<double> buffer = _audioJack.read();
xDataString = List.filled(buffer.length, '');
yDataString.add(List.filled(buffer.length, ''));

Expand Down Expand Up @@ -513,17 +515,20 @@ class OscilloscopeStateProvider extends ChangeNotifier {
Colors.white,
Colors.deepPurple
];
return List<LineChartBarData>.generate(dataEntries.length, (index) {
return LineChartBarData(
spots: dataEntries[index],
isCurved: true,
color: colors[index % colors.length],
barWidth: 1,
dotData: const FlDotData(
show: false,
),
);
});
return List<LineChartBarData>.generate(
dataEntries.length,
(index) {
return LineChartBarData(
spots: dataEntries[index],
isCurved: true,
color: colors[index % colors.length],
barWidth: 1,
dotData: const FlDotData(
show: false,
),
);
},
);
}

@override
Expand All @@ -532,6 +537,7 @@ class OscilloscopeStateProvider extends ChangeNotifier {
if (_timer.isActive) {
_timer.cancel();
}
_audioJack.close();
super.dispose();
}
}
Loading

0 comments on commit 5d8d1e4

Please sign in to comment.