-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflipper.py
More file actions
142 lines (116 loc) · 4.57 KB
/
Copy pathflipper.py
File metadata and controls
142 lines (116 loc) · 4.57 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import serial.tools.list_ports
import serial
import time
import sys
import shutil
port = None
def divider(char="-"):
terminal_width = shutil.get_terminal_size(fallback=(80, 24)).columns
print(char * terminal_width)
def find_flipper():
ports = serial.tools.list_ports.comports()
for port in ports:
# debug info
print(f"Checking: {port.device} | Description: {port.description} | VID: {port.vid} | PID: {port.pid}")
# hardwere id
if port.vid == 0x0483 and port.pid == 0x5740:
return port.device
# Fallback
if port.description and "Flipper" in port.description:
return port.device
return None
def start():
port = find_flipper()
if port:
print(f"\n- success - target Flipper found on: {port}")
else:
print("\n- Flipper not detected\nTry plugging it in.")
def landingPage():
try:
with serial.Serial(port, baudrate=115200, timeout=1) as flipper:
#clears cmd's
flipper.write(b'\r\n\r\n')
time.sleep(0.2)
flipper.read_all()
print("Loading... (1/2)")
flipper.write(b'info\r\n')
time.sleep(0.5)
raw_info = flipper.read_all().decode('utf-8', errors='ignore')
print("Loading... (2/2)")
flipper.write(b'storage info /ext\r\n')
time.sleep(0.5)
raw_storage = flipper.read_all().decode('utf-8', errors='ignore')
return raw_info, raw_storage
except serial.SerialException as e:
print(f"connection failed: {e}")
return None, None
def cli(port):
try:
with serial.Serial(port, baudrate=115200, timeout=0.1) as flipper:
flipper.write(b'\r\n\r\n')
time.sleep(0.2)
flipper.read_all()
print(f"--- connected to Flipper Zero on {port} ---\n")
flipper.write(f"{'neofetch'}\r\n".encode('utf-8'))
response = ""
while True:
chunk = flipper.read(256).decode('utf-8', errors='ignore')
response += chunk
if ">:" in response:
break
if not chunk:
time.sleep(0.01)
print('\n'.join(response.replace(">:", "").strip().split('\n')[1:]) + '\n')
divider()
while True:
user_cmd = input("Flipper> ")
if user_cmd.lower() in ['exit', 'quit']:
print("Exiting...")
break
#if user_cmd.lower() in ["w","a","s","d","q","e"]:
# exec(["p.up()","p.left()","p.down()","p.right()","p.back()","p.ok()",][["w","a","s","d","q","e"].index(user_cmd.lower())])
if not user_cmd.strip():
continue
# send to flipper
flipper.write(f"{user_cmd}\r\n".encode('utf-8'))
# get reponce
response = ""
while True:
chunk = flipper.read(256).decode('utf-8', errors='ignore')
response += chunk
if ">:" in response:
break
if not chunk:
time.sleep(0.01)
print('\n'.join(response.replace(">:", "").strip().split('\n')[1:]) + '\n')
except serial.SerialException as e:
print(f"\nConnection failed: {e}")
except KeyboardInterrupt:
# Handles the user hitting Ctrl+C
print("\n\nquiting CLI...")
sys.exit(0)
def cmd_btn(flipper, button_name):
flipper.write(f"input send {button_name} short\r\n".encode('utf-8'))
time.sleep(0.05)
flipper.read_all()
class press:
def up(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "up")
def right(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "right")
def down(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "down")
def left(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "left")
def ok(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "ok")
def back(self):
cmd_btn(serial.Serial(port, baudrate=115200, timeout=0.1), "back")
p = press()
def seeRaw():
system_data, storage_data = landingPage()
if system_data:
print("\n--- raw (1/2) ---")
print(system_data)
print("\n--- raw (2/2) ---")
print(storage_data)