-
Notifications
You must be signed in to change notification settings - Fork 11
/
flipper_serial.py
65 lines (53 loc) · 1.97 KB
/
flipper_serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=line-too-long
import serial
import serial.tools.list_ports
import serial_ble
class FlipperSerial():
_flipperusb = "USB VID:PID=0483:5740"
_read_characteristic = '19ed82ae-ed21-4c9d-4145-228e61fe0000'
_write_characteristic = '19ed82ae-ed21-4c9d-4145-228e62fe0000'
_is_cli = True
def discover(self):
ports = serial.tools.list_ports.comports()
for check_port in ports:
if self._flipperusb in check_port.hwid:
print("Found: ", check_port.description, "(",check_port.device,")")
return check_port.device
return None
def open(self, **resource):
for key, value in resource.items():
if key == "serial_device" and value is not None:
rsc = self._create_physical_serial(value)
if key == "ble_address" and value is not None:
rsc = self._create_ble_serial(value)
if rsc is None:
raise FlipperSerialException
return rsc
def close(self):
try:
self._serial_device.stop()
print('stopped bluetooth')
except AttributeError:
pass
def _create_physical_serial(self, file):
resource = serial.Serial(file, timeout=1)
resource.baudrate = 230400
resource.flushOutput()
resource.flushInput()
if self._is_cli:
resource.read_until(b'>: ')
resource.write(b"start_rpc_session\r")
resource.read_until(b'\n')
return resource
def _create_ble_serial(self, address):
bluetooth = serial_ble.BLESerial(address, self._read_characteristic, self._write_characteristic)
print('connecting...')
bluetooth.start(None)
print('connected')
return bluetooth
class FlipperSerialException(Exception):
pass