Skip to content

Commit

Permalink
fix: keyboard key pressed on idle, swapping device selection is blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
ShapeLayer committed Oct 21, 2024
1 parent 7d28618 commit b18a7c9
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 173 deletions.
6 changes: 5 additions & 1 deletion configurator/lib/components/device_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class _DeviceSelectorState extends State<DeviceSelector> {

void _loadAvailableDevice() {
setState(() {
_serialDescriptors = [];
_serialDescriptors = _getAvailableDevices();
});
}
Expand All @@ -48,6 +47,11 @@ class _DeviceSelectorState extends State<DeviceSelector> {
style: FilledButton.styleFrom(
backgroundColor:
SelectedDeviceStateUtils.getColor(_selectedDeviceState)),
onOpenMenu: () {
setState(() {
_loadAvailableDevice();
});
},
onSelected: (selected) async {
Globals.instance.currentSerialDevicePort = selected.toValue();
setState(() {
Expand Down
13 changes: 13 additions & 0 deletions configurator/lib/models/error_serial_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ class SerialPortIsNotOpened implements Exception {
String cause;
SerialPortIsNotOpened(this.cause);
}

class SerialPortIsNotClosed implements Exception {
String cause;
SerialPortIsNotClosed(this.cause);
}

class SerialPortCannotOpen implements Exception {
String cause;
SerialPortCannotOpen(this.cause);
}

class SerialPortCannotClose implements Exception {
String cause;
SerialPortCannotClose(this.cause);
}

class SerialPortIsAlreadyUsed implements Exception {
String cause;
SerialPortIsAlreadyUsed(this.cause);
}

class StreamControllerNotInstantiatedWell implements Exception {
String cause;
StreamControllerNotInstantiatedWell(this.cause);
}
61 changes: 43 additions & 18 deletions configurator/lib/models/serial_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import 'package:configurator/utilities/libserialport_utils.dart';
import 'package:configurator/utilities/uint8list_extension.dart';

class SerialDevice {
bool openedByThis = false;
late SerialDescriptor? serialDescriptor;
late SerialPort? serialPort;
late SerialPortConfig serialPortConfig;
late SerialPortReader? serialPortReader;
late StreamSubscription streamSubscription;
final StreamController streamController = StreamController<Uint8List>();
late StreamController<Uint8List>? streamController;

void _initSerialPortConfig() {
serialPortConfig = SerialPortConfig()
Expand All @@ -32,14 +33,14 @@ class SerialDevice {

// SerialPort
serialPort ??= SerialPort(port);
print(serialPort);
// SerialDescriptor
serialDescriptor = SerialDescriptor(port, '${serialPort?.description}');
}

void _initWithNull() {
serialPort = null;
serialPortReader = null;
streamController = null;
}

void _initSerialPortAndDescriptor(String port) =>
Expand Down Expand Up @@ -71,35 +72,55 @@ class SerialDevice {
}

void beginCommunication() {
if (serialPort == null) return;
if (serialPort == null) {
throw Exception('Serial port is null');
}
if (serialPort!.isOpen) {
// throw SerialPortIsNotClosed;
return;
if (openedByThis) {
print('opened by this.');
closeCommunication();
} else {
throw SerialPortIsAlreadyUsed(
'Serial port is already open by another process');
}
} else {
if (!serialPort!.openReadWrite()) {
throw SerialPortCannotOpen('Failed to open port ${serialPort!.name}');
}
openedByThis = true;
}
if (!serialPort!.openReadWrite()) {
throw SerialPortCannotOpen('Failed to open port ${serialPort!.name}');

// Initialize the reader and controller
serialPortReader = SerialPortReader(serialPort!);
streamController = StreamController<Uint8List>();

if (streamController == null) {
throw StreamControllerNotInstantiatedWell(
'Failed to instantiate StreamController');
}

serialPortReader ??= SerialPortReader(serialPort!);
streamSubscription = serialPortReader!.stream.listen((data) {
streamController.add(data);
streamController?.add(data);
}, onDone: () {
streamController.close();
streamController?.close();
});
}

void closeCommunication() {
if (serialPort == null) return;

if (serialPort!.isOpen) {
// throw SerialPortIsNotOpened;
return;
}
if (!serialPort!.close()) {
throw SerialPortCannotOpen('Failed to close port ${serialPort!.name}');
if (!serialPort!.close()) {
throw SerialPortCannotOpen('Failed to close port ${serialPort!.name}');
}
}

serialPortReader?.close();
streamSubscription.cancel();
serialPortReader?.close();
serialPortReader = null;
streamController?.close();
streamController = null;
openedByThis = false;
}

Future<bool> checkDeviceIsValid() async => await requestHandshake();
Expand All @@ -110,11 +131,13 @@ class SerialDevice {
beginCommunication();
} on SerialPortCannotOpen catch (e) {
return false;
} on SerialPortIsAlreadyUsed catch (e) {
return false;
}

serialPort!.write(magic.handshakeRequest);
try {
Uint8List response = await streamController.stream.first
Uint8List response = await streamController!.stream.first
.timeout(const Duration(seconds: 5));
print('Received response: $response');
closeCommunication();
Expand All @@ -131,11 +154,13 @@ class SerialDevice {
beginCommunication();
} on SerialPortCannotOpen {
return false;
} on StreamControllerNotInstantiatedWell {
return false;
}

serialPort!.write(magic.loadKeyConfigurationRequest);
try {
Uint8List response = await streamController.stream.first
Uint8List response = await streamController!.stream.first
.timeout(const Duration(seconds: 5));
print('Received response: $response');
closeCommunication();
Expand Down
8 changes: 6 additions & 2 deletions configurator/lib/widgets/list_dropdown_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class ListDropdownButton extends StatefulWidget {
required this.items,
this.placeholder,
this.style,
this.onOpenMenu,
this.onSelected});
List<IListDropdownButtonItem> items;
Widget? placeholder;
ButtonStyle? style;
final Function()? onOpenMenu;
final Function(IListDropdownButtonItem selected)? onSelected;

@override
Expand All @@ -21,8 +23,8 @@ class _ListDropdownButtonState extends State<ListDropdownButton> {
String? _selected;
ButtonStyle _thisStyle = FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 12.0),
minimumSize: const Size(0, 0), // Remove minimum size constraints
tapTargetSize: MaterialTapTargetSize.shrinkWrap, // Shrink to fit
minimumSize: const Size(0, 0),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
);

@override
Expand All @@ -32,6 +34,8 @@ class _ListDropdownButtonState extends State<ListDropdownButton> {
style:
(widget.style == null ? _thisStyle : widget.style!.merge(_thisStyle)),
onPressed: () async {
widget.onOpenMenu?.call();

final RenderBox button = context.findRenderObject() as RenderBox;
final RenderBox overlay =
Overlay.of(context).context.findRenderObject() as RenderBox;
Expand Down
3 changes: 1 addition & 2 deletions controller/controller.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "config.h"
#include "handlers.h"
#include "utils.h"
#include "magic.h"
#include "global.h"
Expand Down Expand Up @@ -57,7 +56,7 @@ void loop() {
// Reset the buffer for the next message
if (Global::getInstance()->serialCommState & need_clear == need_clear) {
bytesReceived = 0;
Global::getInstance()->serialCommState &= ~4;
Global::getInstance()->serialCommState &= ~need_clear;
}
}
}
Loading

0 comments on commit b18a7c9

Please sign in to comment.