|
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'package:logger/Logger.dart'; |
| 3 | +import 'package:usb_serial/usb_serial.dart'; |
| 4 | + |
| 5 | +/// Wrapper around usb_serial for BadgeMagic devices |
| 6 | +class UsbCdc { |
| 7 | + final logger = Logger(); |
| 8 | + UsbPort? _port; |
| 9 | + |
| 10 | + // FOSSASIA BadgeMagic Device IDs - BOTH MODES |
| 11 | + static const int normalVendorId = 4348; // 0x10FC - Normal mode |
| 12 | + static const int normalProductId = 55200; // 0x55E0 - Normal mode |
| 13 | + static const int bootloaderVendorId = 1046; // 0x0416 - Bootloader mode |
| 14 | + static const int bootloaderProductId = 20512; // 0x5020 - Bootloader mode |
| 15 | + |
| 16 | + /// Open the first available FOSSASIA USB device |
| 17 | + Future<bool> openDevice() async { |
| 18 | + final devices = await listDevices(); |
| 19 | + |
| 20 | + // Filter for FOSSASIA badges - BOTH MODES |
| 21 | + final fossasiaDevices = devices |
| 22 | + .where((device) => |
| 23 | + (device.vid == normalVendorId && device.pid == normalProductId) || |
| 24 | + (device.vid == bootloaderVendorId && |
| 25 | + device.pid == bootloaderProductId)) |
| 26 | + .toList(); |
| 27 | + |
| 28 | + if (fossasiaDevices.isEmpty) { |
| 29 | + logger.e("No FOSSASIA badge found. Available devices: $devices"); |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + final device = fossasiaDevices.first; |
| 34 | + logger.d("Found FOSSASIA device: ${device.vid}:${device.pid}"); |
| 35 | + |
| 36 | + // BOOTLOADER DETECTION |
| 37 | + if (device.vid == bootloaderVendorId && device.pid == bootloaderProductId) { |
| 38 | + logger.e("Device is in bootloader mode - cannot transfer data"); |
| 39 | + throw Exception( |
| 40 | + "Device is in bootloader mode. Please disconnect, then connect without holding any buttons."); |
| 41 | + } |
| 42 | + |
| 43 | + _port = await device.create(); |
| 44 | + if (_port == null) { |
| 45 | + logger.e("Failed to create USB port"); |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + final opened = await _port!.open(); |
| 50 | + if (!opened) { |
| 51 | + logger.e("Failed to open USB port"); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + await _port!.setPortParameters( |
| 56 | + 115200, |
| 57 | + UsbPort.DATABITS_8, |
| 58 | + UsbPort.STOPBITS_1, |
| 59 | + UsbPort.PARITY_NONE, |
| 60 | + ); |
| 61 | + |
| 62 | + logger.d("USB device opened successfully"); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /// Write data to the USB port |
| 67 | + Future<void> write(List<int> data) async { |
| 68 | + if (_port == null) throw Exception("USB port not open"); |
| 69 | + |
| 70 | + try { |
| 71 | + await _port!.write(Uint8List.fromList(data)); |
| 72 | + logger.d("USB chunk written: ${data.length} bytes"); |
| 73 | + } catch (e) { |
| 74 | + logger.e("Failed to write USB chunk: $e"); |
| 75 | + rethrow; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Close the USB port |
| 80 | + Future<void> close() async { |
| 81 | + try { |
| 82 | + await _port?.close(); |
| 83 | + logger.d("USB port closed"); |
| 84 | + } catch (e) { |
| 85 | + logger.e("Error closing USB port: $e"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// List connected USB devices with better logging |
| 90 | + Future<List<UsbDevice>> listDevices() async { |
| 91 | + try { |
| 92 | + final devices = await UsbSerial.listDevices(); |
| 93 | + logger.d( |
| 94 | + "USB devices found: ${devices.map((d) => 'VID:${d.vid?.toRadixString(16)} PID:${d.pid?.toRadixString(16)}').toList()}"); |
| 95 | + return devices; |
| 96 | + } catch (e) { |
| 97 | + logger.e("Error listing USB devices: $e"); |
| 98 | + return []; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// Helper to check if any FOSSASIA device is connected |
| 103 | + Future<bool> isFossasiaDeviceConnected() async { |
| 104 | + final devices = await listDevices(); |
| 105 | + return devices.any((device) => |
| 106 | + (device.vid == normalVendorId && device.pid == normalProductId) || |
| 107 | + (device.vid == bootloaderVendorId && |
| 108 | + device.pid == bootloaderProductId)); |
| 109 | + } |
| 110 | +} |
0 commit comments