Skip to content

Commit 46086c6

Browse files
AsCressmarcnause
authored andcommitted
feat: added USB communication handler for desktop
1 parent 566f9b5 commit 46086c6

16 files changed

+1354
-13
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:flutter_libserialport/flutter_libserialport.dart';
4+
import 'package:pslab/communication/handler/base.dart';
5+
import 'package:pslab/others/logger_service.dart';
6+
7+
class DesktopUSBCommunicationHandler implements CommunicationHandler {
8+
static const int pslabVendorIdV5 = 1240;
9+
static const int pslabProductIdV5 = 223;
10+
static const int pslabVendorIdV6 = 0x10C4;
11+
static const int pslabProductIdV6 = 0xEA60;
12+
SerialPort? mPort;
13+
14+
@override
15+
bool connected = false;
16+
17+
@override
18+
bool deviceFound = false;
19+
20+
@override
21+
void close() {
22+
if (!connected || mPort == null) return;
23+
mPort?.close();
24+
connected = false;
25+
}
26+
27+
@override
28+
Future<void> initialize() async {
29+
List<String> addresses = SerialPort.availablePorts;
30+
for (final address in addresses) {
31+
final port = SerialPort(address);
32+
if ((port.vendorId == pslabVendorIdV5 &&
33+
port.productId == pslabProductIdV5) ||
34+
(port.vendorId == pslabVendorIdV6 &&
35+
port.productId == pslabProductIdV6)) {
36+
deviceFound = true;
37+
mPort = port;
38+
break;
39+
}
40+
}
41+
if (deviceFound) {
42+
logger.d("Found PSLab device");
43+
} else {
44+
logger.d("No drivers found");
45+
}
46+
}
47+
48+
@override
49+
bool isConnected() {
50+
return connected;
51+
}
52+
53+
@override
54+
bool isDeviceFound() {
55+
return deviceFound;
56+
}
57+
58+
@override
59+
Future<void> open() async {
60+
if (!deviceFound) {
61+
throw Exception("Device not connected");
62+
}
63+
mPort?.openReadWrite();
64+
mPort?.config.baudRate = 1000000;
65+
mPort?.config.bits = 8;
66+
mPort?.config.stopBits = 1;
67+
mPort?.config.parity = SerialPortParity.none;
68+
connected = true;
69+
}
70+
71+
@override
72+
Future<int> read(Uint8List dest, int bytesToRead, int timeoutMillis) async {
73+
int numBytesRead = 0;
74+
int bytesToBeReadTemp = bytesToRead;
75+
try {
76+
while (numBytesRead < bytesToRead) {
77+
Uint8List receivedData = mPort!.read(bytesToBeReadTemp, timeout: 0);
78+
int readNow = receivedData.length;
79+
if (readNow == 0) {
80+
logger.e("Read Error: $bytesToBeReadTemp");
81+
return numBytesRead;
82+
} else {
83+
int readLength = readNow.clamp(0, bytesToBeReadTemp);
84+
dest.setRange(numBytesRead, numBytesRead + readLength, receivedData);
85+
numBytesRead += readLength;
86+
bytesToBeReadTemp -= readLength;
87+
}
88+
}
89+
} catch (e) {
90+
logger.e("Exception during read: $e");
91+
}
92+
93+
logger.d("Bytes Read: $numBytesRead");
94+
return numBytesRead;
95+
}
96+
97+
@override
98+
void write(Uint8List src, int timeoutMillis) {
99+
mPort?.write(src, timeout: 0);
100+
mPort?.flush();
101+
}
102+
}

lib/l10n/app_localizations_de.dart

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ class AppLocalizationsDe extends AppLocalizations {
884884
String get builtIn => 'Built-In';
885885

886886
@override
887-
String get lx => 'Lx';
887+
String get lx => 'lx';
888888

889889
@override
890890
String get maxScaleError => 'Max Scale';
@@ -1445,6 +1445,101 @@ class AppLocalizationsDe extends AppLocalizations {
14451445
@override
14461446
String get estimated => 'Estimated';
14471447

1448+
@override
1449+
String get experiments => 'Experiments';
1450+
1451+
@override
1452+
String get startExperiment => 'Start Experiment';
1453+
1454+
@override
1455+
String get lightIntensityVsDistance => 'Light Intensity vs Distance';
1456+
1457+
@override
1458+
String get lightIntensityVsDistanceDesc =>
1459+
'Measure how light intensity changes with distance from the source';
1460+
1461+
@override
1462+
String get stepCompleted => 'Step Completed!';
1463+
1464+
@override
1465+
String get endExperiment => 'End Experiment';
1466+
1467+
@override
1468+
String get next => 'Next';
1469+
1470+
@override
1471+
String get previous => 'Previous';
1472+
1473+
@override
1474+
String get step => 'Step';
1475+
1476+
@override
1477+
String get experimentCompleted => 'Experiment Completed';
1478+
1479+
@override
1480+
String get setUp => 'Setup';
1481+
1482+
@override
1483+
String get lightExperimentSetUpContent =>
1484+
'Place your device near a light source (lamp, window, or flashlight).';
1485+
1486+
@override
1487+
String get preparation => 'Preparation';
1488+
1489+
@override
1490+
String get lightExperimentPreparationContent =>
1491+
'Make sure you have space to move towards the light source gradually.';
1492+
1493+
@override
1494+
String get instructions => 'Instructions';
1495+
1496+
@override
1497+
String get lightExperimentInstructionContent =>
1498+
'You will measure light intensity at different distances. Follow the on-screen prompts to move closer or farther from the light source.';
1499+
1500+
@override
1501+
String get moveTowardsLight => 'Move towards the light source';
1502+
1503+
@override
1504+
String get moveAwayFromLight => 'Move away from the light source';
1505+
1506+
@override
1507+
String get holdPosition => 'Hold your position and let the reading stabilize';
1508+
1509+
@override
1510+
String get followInstructions =>
1511+
'Follow the on-screen instructions to set up your experiment.';
1512+
1513+
@override
1514+
String get gesture => 'Gesture';
1515+
1516+
@override
1517+
String get blueLabel => 'Blue';
1518+
1519+
@override
1520+
String get greenLabel => 'Green';
1521+
1522+
@override
1523+
String get proxLabel => 'Prox';
1524+
1525+
@override
1526+
String get redLabel => 'Red';
1527+
1528+
@override
1529+
String get mode => 'Mode';
1530+
1531+
@override
1532+
String get configure => 'Configure';
1533+
1534+
@override
1535+
String get proximity => 'Proximity';
1536+
1537+
@override
1538+
String get light => 'Light';
1539+
1540+
@override
1541+
String get lux => 'Lux';
1542+
14481543
@override
14491544
String get distance => 'Distance';
14501545

lib/l10n/app_localizations_es.dart

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ class AppLocalizationsEs extends AppLocalizations {
884884
String get builtIn => 'Built-In';
885885

886886
@override
887-
String get lx => 'Lx';
887+
String get lx => 'lx';
888888

889889
@override
890890
String get maxScaleError => 'Max Scale';
@@ -1445,6 +1445,101 @@ class AppLocalizationsEs extends AppLocalizations {
14451445
@override
14461446
String get estimated => 'Estimated';
14471447

1448+
@override
1449+
String get experiments => 'Experiments';
1450+
1451+
@override
1452+
String get startExperiment => 'Start Experiment';
1453+
1454+
@override
1455+
String get lightIntensityVsDistance => 'Light Intensity vs Distance';
1456+
1457+
@override
1458+
String get lightIntensityVsDistanceDesc =>
1459+
'Measure how light intensity changes with distance from the source';
1460+
1461+
@override
1462+
String get stepCompleted => 'Step Completed!';
1463+
1464+
@override
1465+
String get endExperiment => 'End Experiment';
1466+
1467+
@override
1468+
String get next => 'Next';
1469+
1470+
@override
1471+
String get previous => 'Previous';
1472+
1473+
@override
1474+
String get step => 'Step';
1475+
1476+
@override
1477+
String get experimentCompleted => 'Experiment Completed';
1478+
1479+
@override
1480+
String get setUp => 'Setup';
1481+
1482+
@override
1483+
String get lightExperimentSetUpContent =>
1484+
'Place your device near a light source (lamp, window, or flashlight).';
1485+
1486+
@override
1487+
String get preparation => 'Preparation';
1488+
1489+
@override
1490+
String get lightExperimentPreparationContent =>
1491+
'Make sure you have space to move towards the light source gradually.';
1492+
1493+
@override
1494+
String get instructions => 'Instructions';
1495+
1496+
@override
1497+
String get lightExperimentInstructionContent =>
1498+
'You will measure light intensity at different distances. Follow the on-screen prompts to move closer or farther from the light source.';
1499+
1500+
@override
1501+
String get moveTowardsLight => 'Move towards the light source';
1502+
1503+
@override
1504+
String get moveAwayFromLight => 'Move away from the light source';
1505+
1506+
@override
1507+
String get holdPosition => 'Hold your position and let the reading stabilize';
1508+
1509+
@override
1510+
String get followInstructions =>
1511+
'Follow the on-screen instructions to set up your experiment.';
1512+
1513+
@override
1514+
String get gesture => 'Gesture';
1515+
1516+
@override
1517+
String get blueLabel => 'Blue';
1518+
1519+
@override
1520+
String get greenLabel => 'Green';
1521+
1522+
@override
1523+
String get proxLabel => 'Prox';
1524+
1525+
@override
1526+
String get redLabel => 'Red';
1527+
1528+
@override
1529+
String get mode => 'Mode';
1530+
1531+
@override
1532+
String get configure => 'Configure';
1533+
1534+
@override
1535+
String get proximity => 'Proximity';
1536+
1537+
@override
1538+
String get light => 'Light';
1539+
1540+
@override
1541+
String get lux => 'Lux';
1542+
14481543
@override
14491544
String get distance => 'Distance';
14501545

0 commit comments

Comments
 (0)