mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-12 17:27:43 +03:00
[ESP32 TTGO] My hardware key to operate my app (Ep.2/2 - Completing App)
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:usb_serial/transaction.dart';
|
||||
import 'package:usb_serial/usb_serial.dart';
|
||||
|
||||
class SerialController extends GetxController {
|
||||
UsbPort _port;
|
||||
StreamSubscription<String> _subscription;
|
||||
Transaction<String> _transaction;
|
||||
int _deviceId;
|
||||
TextEditingController textController;
|
||||
String _keyFromDevice;
|
||||
String logMessage = "";
|
||||
final hardwareKeyConnected = false.obs;
|
||||
|
||||
Future<bool> _connectTo(device) async {
|
||||
if (_subscription != null) {
|
||||
_subscription.cancel();
|
||||
_subscription = null;
|
||||
}
|
||||
|
||||
if (_transaction != null) {
|
||||
_transaction.dispose();
|
||||
_transaction = null;
|
||||
}
|
||||
|
||||
if (_port != null) {
|
||||
_port.close();
|
||||
_port = null;
|
||||
}
|
||||
|
||||
if (device == null) {
|
||||
_deviceId = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
_port = await device.create();
|
||||
if (!await _port.open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_deviceId = device.deviceId;
|
||||
await _port.setDTR(true);
|
||||
await _port.setRTS(true);
|
||||
await _port.setPortParameters(
|
||||
115200, UsbPort.DATABITS_8, UsbPort.STOPBITS_1, UsbPort.PARITY_NONE);
|
||||
|
||||
_transaction = Transaction.stringTerminated(
|
||||
_port.inputStream, Uint8List.fromList([13, 10]));
|
||||
|
||||
_subscription = _transaction.stream.listen((String line) {
|
||||
_keyFromDevice = line;
|
||||
textController.text = line;
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void _getPorts() async {
|
||||
List<UsbDevice> devices = await UsbSerial.listDevices();
|
||||
print(devices);
|
||||
bool targetDeviceFound = false;
|
||||
|
||||
devices.forEach((device) {
|
||||
logMessage = "[Log] device pid: ${device.pid}, device vid: ${device.vid}";
|
||||
if (device.pid == 60000 && device.vid == 4292) {
|
||||
targetDeviceFound = true;
|
||||
|
||||
if (!hardwareKeyConnected.value) {
|
||||
_connectTo(device).then((res) {
|
||||
hardwareKeyConnected.value = res;
|
||||
sendString("key?");
|
||||
Get.snackbar("Device Status", "Connected!",
|
||||
duration: Duration(seconds: 5));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!targetDeviceFound) {
|
||||
hardwareKeyConnected.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
void sendString(String text) async {
|
||||
if (_port == null) return;
|
||||
await _port.write(Uint8List.fromList(text.codeUnits));
|
||||
}
|
||||
|
||||
Future<bool> validation() async {
|
||||
if (textController.text == _keyFromDevice) {
|
||||
sendString("done");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
textController = TextEditingController();
|
||||
UsbSerial.usbEventStream.listen((UsbEvent event) {
|
||||
_getPorts();
|
||||
});
|
||||
_getPorts();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
textController?.dispose();
|
||||
_connectTo(null);
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
113
ESP32_TTGO/my_hardware_key_demo_app/lib/main.dart
Normal file
113
ESP32_TTGO/my_hardware_key_demo_app/lib/main.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:my_hardware_key_demo_app/controller/serial_controller.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
runApp(GetMaterialApp(
|
||||
home: First(),
|
||||
theme: ThemeData.light(),
|
||||
darkTheme: ThemeData.dark(),
|
||||
themeMode: ThemeMode.system,
|
||||
debugShowCheckedModeBanner: false,
|
||||
));
|
||||
}
|
||||
|
||||
class First extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("My Hardware Key Demo App"),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
GetX<SerialController>(
|
||||
init: SerialController(),
|
||||
builder: (_) => Column(
|
||||
children: [
|
||||
Text(_.logMessage),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
Text(
|
||||
'Hardware Key Connected: ${_.hardwareKeyConnected.value.toString().toUpperCase()}',
|
||||
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
_.hardwareKeyConnected.value
|
||||
? Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 32,
|
||||
),
|
||||
Padding(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 32),
|
||||
child: TextFormField(
|
||||
controller: _.textController,
|
||||
keyboardType: TextInputType.text,
|
||||
validator: (value) => value.trim().isEmpty
|
||||
? 'Key required'
|
||||
: null,
|
||||
),
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text('Confirm'),
|
||||
onPressed: () async {
|
||||
var result = await _.validation();
|
||||
if (result) {
|
||||
Get.to(Second());
|
||||
}
|
||||
},
|
||||
)
|
||||
],
|
||||
)
|
||||
: Text(
|
||||
'Please insert your key to proceed!',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold, fontSize: 20),
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Second extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Second Page'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
'Second Page',
|
||||
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
|
||||
),
|
||||
Obx(() {
|
||||
final key =
|
||||
Get.find<SerialController>().hardwareKeyConnected.value;
|
||||
if (!key) {
|
||||
Get.back();
|
||||
}
|
||||
return Text('Key : $key');
|
||||
})
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user