Repository navigation
Expand file tree
/
Copy pathtflipper.py
More file actions
executable file
·1065 lines (832 loc) · 38.4 KB
/
Copy pathtflipper.py
File metadata and controls
executable file
·1065 lines (832 loc) · 38.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Flipper Zero remote control for the terminal
Version: 1.7.1
"""
## Modules
#
import re
import sys
import argparse
from time import time
from copy import copy
import queue
import threading
import multiprocessing
from readchar import key, readkey
from flipperzero_protobuf.flipper_proto import FlipperProto
try:
import colorama
except:
pass
try:
from PIL import Image
except:
pass
## Keyboard-to-Flipper input mapping
#
keyboard_to_flipper_input = {
"a": "SHORT LEFT",
"h": "SHORT LEFT",
key.LEFT: "SHORT LEFT",
"s": "SHORT DOWN",
"j": "SHORT DOWN",
key.DOWN: "SHORT DOWN",
"w": "SHORT UP",
"k": "SHORT UP",
key.UP: "SHORT UP",
"d": "SHORT RIGHT",
"l": "SHORT RIGHT",
key.RIGHT: "SHORT RIGHT",
"o": "SHORT OK",
key.SPACE: "SHORT OK",
key.ENTER: "SHORT OK",
"b": "SHORT BACK",
key.BACKSPACE: "SHORT BACK",
key.DELETE: "SHORT BACK",
"A": "LONG LEFT",
"H": "LONG LEFT",
"S": "LONG DOWN",
"J": "LONG DOWN",
"W": "LONG UP",
"K": "LONG UP",
"D": "LONG RIGHT",
"L": "LONG RIGHT",
"O": "LONG OK",
"B": "LONG BACK"
}
keyboard_to_flipper_help = [
" _________________ ",
" | UP: | ",
" Long > | [W] [K] | ",
" Short > | w ^ k | ",
" +---------------+ ",
" ___________ _________________ ___________ ",
" | LEFT: | | OK: | | RIGHT: | ",
" Long > | [A] [H] | | [O] | | [D] [L] | ",
"Short > | < a h | | SPACE ENTER o | | d l > | ",
" +---------+ +---------------+ +---------+ ",
" _________________ ______________",
" | DOWN: | | BACK: |",
" Long > | [S] [V] | Long > | [B] |",
" Short > | s v j | Short > | BACK DEL b |",
" +---------------+ +------------+"]
## defines
#
unicode_blocks_1x2 = [
# # . # .
# # # . .
"\u2588","\u2584","\u2580", " ",
]
unicode_blocks_2x3 = [
# ## .# ## .#
# ## ## .# .#
# ## ## ## ##
"\u2588", "\U0001fb3b","\U0001fb38","\U0001fb37",
# ## .# ## .#
# ## ## .# .#
# .# .# .# .#
"\U0001fb2c","\U0001fb2b","\U0001fb28", "\u2590",
# #. .. #. ..
# ## ## .# .#
# ## ## ## ##
"\U0001fb3a","\U0001fb39","\U0001fb36","\U0001fb35",
# #. .. #. ..
# ## ## .# .#
# .# .# .# .#
"\U0001fb2a","\U0001fb29","\U0001fb27","\U0001fb26",
# ## .# ## .#
# #. #. .. ..
# ## ## ## ##
"\U0001fb34","\U0001fb33","\U0001fb30","\U0001fb2f",
# ## .# ## .#
# #. #. .. ..
# .# .# .# .#
"\U0001fb25","\U0001fb24","\U0001fb21","\U0001fb20",
# #. .. #. ..
# #. #. .. ..
# ## ## ## ##
"\U0001fb32","\U0001fb31","\U0001fb2e","\U0001fb2d",
# #. .. #. ..
# #. #. .. ..
# .# .# .# .#
"\U0001fb23","\U0001fb22","\U0001fb1f","\U0001fb1e",
# ## .# ## .#
# ## ## .# .#
# #. #. #. #.
"\U0001fb1d","\U0001fb1c","\U0001fb19","\U0001fb18",
# ## .# ## .#
# ## ## .# .#
# .. .. .. ..
"\U0001fb0e","\U0001fb0d","\U0001fb0a","\U0001fb09",
# #. .. #. ..
# ## ## .# .#
# #. #. #. #.
"\U0001fb1b","\U0001fb1a","\U0001fb17","\U0001fb16",
# #. .. #. ..
# ## ## .# .#
# .. .. .. ..
"\U0001fb0c","\U0001fb0b","\U0001fb08","\U0001fb07",
# ## .# ## .#
# #. #. .. ..
# #. #. #. #.
"\U0001fb15","\U0001fb14","\U0001fb12","\U0001fb11",
# ## .# ## .#
# #. #. .. ..
# .. .. .. ..
"\U0001fb06","\U0001fb05","\U0001fb02","\U0001fb01",
# #. .. #. ..
# #. #. .. ..
# #. #. #. #.
"\u258c", "\U0001fb13","\U0001fb10","\U0001fb0f",
# #. .. #. ..
# #. #. .. ..
# .. .. .. ..
"\U0001fb04","\U0001fb03","\U0001fb00", " ",
]
unicode_braille_2x4 = [
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# ## ## ## ## ## ## ## ##
"\u28ff","\u28fe","\u28fd","\u28fc","\u28fb","\u28fa","\u28f9","\u28f8",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# .# .# .# .# .# .# .# .#
"\u28bf","\u28be","\u28bd","\u28bc","\u28bb","\u28ba","\u28b9","\u28b8",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# ## ## ## ## ## ## ## ##
"\u28f7","\u28f6","\u28f5","\u28f4","\u28f3","\u28f2","\u28f1","\u28f0",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# .# .# .# .# .# .# .# .#
"\u28b7","\u28b6","\u28b5","\u28b4","\u28b3","\u28b2","\u28b1","\u28b0",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# ## ## ## ## ## ## ## ##
"\u28ef","\u28ee","\u28ed","\u28ec","\u28eb","\u28ea","\u28e9","\u28e8",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# .# .# .# .# .# .# .# .#
"\u28af","\u28ae","\u28ad","\u28ac","\u28ab","\u28aa","\u28a9","\u28a8",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# ## ## ## ## ## ## ## ##
"\u28e7","\u28e6","\u28e5","\u28e4","\u28e3","\u28e2","\u28e1","\u28e0",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# .# .# .# .# .# .# .# .#
"\u28a7","\u28a6","\u28a5","\u28a4","\u28a3","\u28a2","\u28a1","\u28a0",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# ## ## ## ## ## ## ## ##
"\u28df","\u28de","\u28dd","\u28dc","\u28db","\u28da","\u28d9","\u28d8",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# .# .# .# .# .# .# .# .#
"\u289f","\u289e","\u289d","\u289c","\u289b","\u289a","\u2899","\u2898",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# ## ## ## ## ## ## ## ##
"\u28d7","\u28d6","\u28d5","\u28d4","\u28d3","\u28d2","\u28d1","\u28d0",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# .# .# .# .# .# .# .# .#
"\u2897","\u2896","\u2895","\u2894","\u2893","\u2892","\u2891","\u2890",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# ## ## ## ## ## ## ## ##
"\u28cf","\u28ce","\u28cd","\u28cc","\u28cb","\u28ca","\u28c9","\u28c8",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# .# .# .# .# .# .# .# .#
"\u288f","\u288e","\u288d","\u288c","\u288b","\u288a","\u2889","\u2888",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# ## ## ## ## ## ## ## ##
"\u28c7","\u28c6","\u28c5","\u28c4","\u28c3","\u28c2","\u28c1","\u28c0",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# .# .# .# .# .# .# .# .#
"\u2887","\u2886","\u2885","\u2884","\u2883","\u2882","\u2881","\u2880",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# #. #. #. #. #. #. #. #.
"\u287f","\u287e","\u287d","\u287c","\u287b","\u287a","\u2879","\u2878",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# .. .. .. .. .. .. .. ..
"\u283f","\u283e","\u283d","\u283c","\u283b","\u283a","\u2839","\u2838",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# #. #. #. #. #. #. #. #.
"\u2877","\u2876","\u2875","\u2874","\u2873","\u2872","\u2871","\u2870",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# ## ## ## ## .# .# .# .#
# .. .. .. .. .. .. .. ..
"\u2837","\u2836","\u2835","\u2834","\u2833","\u2832","\u2831","\u2830",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# #. #. #. #. #. #. #. #.
"\u286f","\u286e","\u286d","\u286c","\u286b","\u286a","\u2869","\u2868",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# .. .. .. .. .. .. .. ..
"\u282f","\u282e","\u282d","\u282c","\u282b","\u282a","\u2829","\u2828",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# #. #. #. #. #. #. #. #.
"\u2867","\u2866","\u2865","\u2864","\u2863","\u2862","\u2861","\u2860",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# ## ## ## ## .# .# .# .#
# .. .. .. .. .. .. .. ..
"\u2827","\u2826","\u2825","\u2824","\u2823","\u2822","\u2821","\u2820",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# #. #. #. #. #. #. #. #.
"\u285f","\u285e","\u285d","\u285c","\u285b","\u285a","\u2859","\u2858",
# ## .# ## .# ## .# ## .#
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# .. .. .. .. .. .. .. ..
"\u281f","\u281e","\u281d","\u281c","\u281b","\u281a","\u2819","\u2818",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# #. #. #. #. #. #. #. #.
"\u2857","\u2856","\u2855","\u2854","\u2853","\u2852","\u2851","\u2850",
# #. .. #. .. #. .. #. ..
# ## ## .# .# ## ## .# .#
# #. #. #. #. .. .. .. ..
# .. .. .. .. .. .. .. ..
"\u2817","\u2816","\u2815","\u2814","\u2813","\u2812","\u2811","\u2810",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# #. #. #. #. #. #. #. #.
"\u284f","\u284e","\u284d","\u284c","\u284b","\u284a","\u2849","\u2848",
# ## .# ## .# ## .# ## .#
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# .. .. .. .. .. .. .. ..
"\u280f","\u280e","\u280d","\u280c","\u280b","\u280a","\u2809","\u2808",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# #. #. #. #. #. #. #. #.
"\u2847","\u2846","\u2845","\u2844","\u2843","\u2842","\u2841","\u2840",
# #. .. #. .. #. .. #. ..
# #. #. .. .. #. #. .. ..
# #. #. #. #. .. .. .. ..
# .. .. .. .. .. .. .. ..
"\u2807","\u2806","\u2805","\u2804","\u2803","\u2802","\u2801","\u2800",
]
# Special characters
ESC = "\x1b"
CR = "\r"
LF = "\n"
# VT100 cursor invisible
set_cursor_invisible = ESC + "[?25l"
# VT100 cursor visible
set_cursor_visible = ESC + "[?25h"
# VT100 invisible text
set_text_invisible = ESC + "[8m"
# ANSI 8-bit color selection
set_bg_color = ESC + "[48;5;{}m"
set_fg_color = ESC + "[38;5;{}m"
# VT100 bold attribute
attribute_bold = ESC + "[1m"
# VT100 attributes reset
attributes_reset = ESC + "[0m"
# VT100 x lines up
x_lines_up = ESC + "[{}A"
# Predefined colors
ansi_8bit_black = 0
rgb_black1 = [0, 0, 0]
rgb_black2 = [0, 0, 1]
ansi_8bit_orange = 208
rgb_orange1 = [0xfe, 0x8a, 0x2c]
rgb_orange2 = [0xfe, 0x8a, 0x2d]
# 4-color palette for the GIF file: color 0 & 2 = orange, color 1 & 3 = black
gif_palette = rgb_orange1 + rgb_black1 + rgb_orange2 + rgb_black2
# Minimum and maximum duration of one GIF frame
min_gif_frame_duration_ms = 10 #ms # because the GIF format encodes the
max_gif_frame_duration_ms = 655350 #ms # frame duration in 1/100th of a second
# in an unsigned short
# Format of the invisible timecode and button presses marker
invisible_tc_btn_marker_fmt = "[{:0.3f}s] [{}]"
# Precompiled regex for an invisible timecode and button presses marker with
# a non-empty list of button presses
re_tc_btn_marker = re.compile((set_text_invisible.replace("[", "\\[") + \
r"\[([0-9]+\.[0-9]{3})s\] " \
r"\[([lLdDuUrRoObB]*)\]" + \
attributes_reset.replace("[", "\\[")))
## Routines
#
class argparse_gif_filename_parser(argparse.Action):
"""Argparse parser for a valid GIF filename
"""
def __call__(self , parser, namespace, value, option_string = None):
"""__call__ method
"""
if not value.upper().endswith(".GIF"):
parser.error("{}: invalid GIF filename".format(value))
setattr(namespace, self.dest, value)
def input_thread(msg_queue):
""" Get keyboard keypresses, turn them into Flipper Zero input events and send
them to the main thread
"""
t = threading.current_thread()
try:
# Run until we're told to stop
while getattr(t, "do_run", True):
# Get one key
k = readkey()
# It the user wants to show / hide the keymap, send "k" to the main thread
if k == key.CTRL_K:
msg_queue.put(("k", None))
# Send the main thread the input event to send the Flipper if there is one
else:
msg_queue.put((keyboard_to_flipper_input.get(k, ""), None))
except KeyboardInterrupt:
msg_queue.put((None, None))
except Exception as e:
msg_queue.put((None, e))
return
def steg_encode(image, s):
""" Encode a hidden string in the first line of an image that is 512 pixels
wide or wider, with a 4-color palette such that:
- Colors 0 & 2 are very close: color 0 encodes bit 0, color 2 encodes bit 1
- Colors 1 & 3 are very close: color 1 encodes bit 0, color 3 encodes bit 1
The string's characters' bits are encoded on the first line from left to
right, big-endian (512 / 8 = 64 characters in total).
If the string is shorter than 64 characters, the remaining bits are 0
If the string is longer than 64 characters, only the first 64 characters
are encoded into the image.
"""
color_transforms = map(lambda c: ((0, 1, 0, 1), (2, 3, 2, 3))[int(c)],
("".join(["{:08b}".format(ord(c)) \
for c in s]) \
+ "0" * 512)[:512])
for x in range(512):
image.putpixel((x, 0), next(color_transforms)[image.getpixel((x, 0))])
def steg_decode(image, palette):
""" Decode a hidden string encoded by the steg_encode() function in the first
line of an image that is 512 wide or wider, with a 4-color palette
"""
s = ""
try:
for x in range(0, 512, 8):
c = chr(sum([(0 if palette[image.getpixel((x + o, 0))] < 2 else 1) << \
(7 - o) for o in range(8)]))
if not c.isprintable() and c != ESC:
break
s += c
except:
s = ""
return s
## Main routine
#
def main():
# If we run on Windows, initialize colorama, so the Windows console
# understands ANSI escape codes
if sys.platform[0:3] == "win":
colorama.init()
# Parse the command line arguments
argparser = argparse.ArgumentParser()
argparser.add_argument(
"-d", "--device",
help = "Flipper Zero serial device to use. Default: autodetect",
type = str
)
argparser.add_argument(
"-M", "--mid-density-semigraphics",
help = "Use 2x3 unicode block characters for rendering (smaller "
"console needed but incorrect aspect ratio). "
"Default: 1x2 unicode block characters",
action = "store_true"
)
argparser.add_argument(
"-H", "--high-density-semigraphics",
help = "Use 2x4 unicode Braille characters for rendering (smaller "
"console needed but uses dots instead of full pixels). "
"Default: 1x2 unicode block characters",
action = "store_true"
)
argparser.add_argument(
"-B", "--bold",
help = "Display semigraphic characters in bold",
action = "store_true"
)
argparser.add_argument(
"-t", "--txt",
help = "Text file to record the session into (play it back with "
"tfreplay or replay button presses encoded into it "
"with -r)",
type = str
)
argparser.add_argument(
"-g", "--gif",
help = "Animated GIF to record the session into",
action = argparse_gif_filename_parser
)
mutexargs = argparser.add_mutually_exclusive_group(required = False)
mutexargs.add_argument(
"-rt", "--replay-buttons-from-txt",
help = "Text file session recording to replay button presses from "
"(generated by -t)",
type = str
)
mutexargs.add_argument(
"-rg", "--replay-buttons-from-gif",
help = "GIF file session recording to replay button presses from "
"(generated by -g)",
type = str
)
argparser.add_argument(
"-n", "--no-display",
help = "Suppress the display output, only print button presses",
action = "store_true"
)
args = argparser.parse_args()
# Semigraphic-ize the keymap help strings
keymap_help = [(" " + l + " ").\
replace(" _", " \u250c").replace("_ ", "\u2510 ").\
replace("_", "\u2500").replace("|", "\u2502").\
replace(" +", " \u2514").replace("+ ", "\u2518 ").\
replace("-", "\u2500").replace("<", "\u2190").\
replace("^", "\u2191").replace(">", "\u2192").\
replace("v", "\u2193")[1:-1]
for l in keyboard_to_flipper_help]
width = 64 if args.high_density_semigraphics or \
args.mid_density_semigraphics else 128
height = 16 if args.high_density_semigraphics else \
22 if args.mid_density_semigraphics else 32
extra_attributes = attribute_bold if args.bold else ""
keymap_help_line_len = len(keymap_help[0])
keymap_help_overlay_at_col = int((width - keymap_help_line_len) / 2)
keymap_help_overlay_at_line = int((height - len(keymap_help)) / 2)
# If a text file to replay button presses from was specified, load it and
# extract the button press events from it
if args.replay_buttons_from_txt:
with open(args.replay_buttons_from_txt, "r") as f:
replay_buttons_at = [(float(t), b) \
for t, b in re_tc_btn_marker.findall(f.read())]
# If a GIF file to replay button presses from was specified, load it and
# extract the button press events from it
elif args.replay_buttons_from_gif:
with Image.open(args.replay_buttons_from_gif) as gif:
assert gif.is_animated
assert gif.size == (512, 256)
assert len(gif.palette.colors) == 4
palette = gif.palette.colors
replay_buttons_at = []
for frame in range(gif.n_frames):
gif.seek(frame)
s = steg_decode(gif if gif.mode == "RGB" else gif.convert("RGB"),
palette)
replay_buttons_at.extend([(float(t), b) \
for t, b in re_tc_btn_marker.findall(s)])
# No text file or GIF file to replay button presses from
else:
replay_buttons_at = None
# If we replay button presses, prune empty button presses apart from the last
# one that marks the end of the replay
if replay_buttons_at is not None:
replay_buttons_at = [e for e in replay_buttons_at[:-1] if e[1]] + \
replay_buttons_at[-1:]
# Connect to the Flipper Zero
p = FlipperProto(serial_port = args.device)
# Get the Flipper Zero's name
flipper_name = "[ " + p.device_info["hardware_name"] + " ]"
# Left and right spacers for the Flipper Zero's name
len_flipper_name_spc1 = int((width - len(flipper_name)) / 2)
flipper_name_spc2 = " " * (width - len_flipper_name_spc1 - len(flipper_name))
# Bottom help line
bottom_line = ("[ Ctrl-K to show/hide keymap ] " \
if replay_buttons_at is None else "") + \
"[ Ctrl-C to stop ]"
# Left and right spacers for the bottom help line
bottom_line_spc1 = " " * int((width - len(bottom_line)) / 2)
bottom_line_spc2 = " " * (width - len(bottom_line_spc1) - len(bottom_line))
# Create a queue for the input thread to send messages to the main thread
q = multiprocessing.Queue()
# Spawn the input thread to get keypresses if we don't replay button presses
if replay_buttons_at is None:
t = threading.Thread(target = input_thread, args = (q,))
t.start()
# Open the text file if the session is recorded in a text file
rt = open(args.txt, "w", encoding = "utf-8") if args.txt else None
# Create a list of frames and durations if the session is recorded in a GIF
gif_frames = [] if args.gif else None
gif_frame_durations_ms = [] if args.gif else None
gif_frame_no = 0
nb_lines_back_up = 0
nb_lines_back_up_text_record = 0
cursor_visible = True
show_keymap = False
start_time = None
screen_data = b""
update_display = True
try:
# Run until stopped by Ctrl-C
while True:
flipper_inputs = ""
# Process messages from the input thread
while True:
# Try to get one message out of the queue
try:
flipper_input, e = q.get_nowait()
except queue.Empty:
break
# If we got an exception from the input thread, re-raise it
if e is not None:
raise e
# Do we have a Flipper Zero input event string?
elif flipper_input is not None:
# Should we show or hide the keymap?
if flipper_input == "k":
show_keymap = not show_keymap
update_display = True
# If the string isn't empty, send the event to the Flipper Zero and
# store them for recording in the session text file in short form
elif flipper_input:
p.rpc_gui_send_input(flipper_input)
i1, i2 = flipper_input.split()
flipper_inputs += i2[0].lower() if i1 == "SHORT" else i2[0].upper()
# The input thread exited normally so do the same thing
else:
raise KeyboardInterrupt
# Get the current time and calculate the current timecode
now = time()
if start_time is None:
start_time = now
timecode = now - start_time
if timecode < 0:
timecode = 0
# Do we replay button presses?
if replay_buttons_at is not None:
# If there are no buttons left to replay, stop as if the user hit Ctrl-C
if not replay_buttons_at:
raise KeyboardInterrupt
# If we're due to replay Flipper Zero button presses, add them to the
# inputs and replay them
while replay_buttons_at and timecode >= replay_buttons_at[0][0]:
_, btns = replay_buttons_at.pop(0)
flipper_inputs += btns
# Send the button press events to the Flipper Zero
for b in btns:
p.rpc_gui_send_input(("SHORT " if b.islower() else "LONG ") + \
{"L": "LEFT", "D": "DOWN", "U": "UP",
"R": "RIGHT", "O": "OK",
"B": "BACK"}[b.upper()])
# Get a screenshot from the Flipper Zero
prev_screen_data = screen_data
screen_data = p.rpc_gui_snapshot_screen()
assert len(screen_data) == 1024
screen_data_changed = screen_data != prev_screen_data
# If the Flipper's display hasn't changed and no input was sent to the
# Flipper, there is nothing more to do with this frame
if not screen_data_changed and not update_display and not flipper_inputs:
continue
# Do we record GIF frames and do we have a reason to record this frame?
if gif_frames is not None and (screen_data_changed or flipper_inputs):
# Is there at least one stored GIF frame?
if gif_frames:
# The difference between this timecode and the previous GIF frame's
# timecode is the previous GIF frame's duration
prev_frame_duration_ms = (timecode - last_gif_frame_timecode) * 1000
# Repeat the previous GIF frame as many times as needed, encode only
# the frame number invisibly into the image then increment the
# frame number
while prev_frame_duration_ms > max_gif_frame_duration_ms + \
min_gif_frame_duration_ms:
image = copy(gif_frames[-1])
steg_encode(image, "{}".format(gif_frame_no))
gif_frame_no += 1
gif_frame_durations_ms.append(max_gif_frame_duration_ms)
gif_frames.append(image)
prev_frame_duration_ms -= max_gif_frame_duration_ms
# Add the duration of the last GIF frame
gif_frame_durations_ms.append(max(prev_frame_duration_ms,
min_gif_frame_duration_ms))
# Convert the Flipper's screen data into an image and scale it up x4
image_data = bytes([(screen_data[i] >> j) & 1 \
for k in range(0, 1024, 128) \
for j in range(8) \
for i in range(k, k + 128)])
image = Image.frombytes(mode = "P", size = (128, 64),
data = image_data).\
resize((512, 256), resample = Image.BOX)
image.putpalette(gif_palette)
# Encode the frame number, timecode and flipper inputs invisibly into
# the image then increment the frame number
steg_encode(image, "[{}] ".format(gif_frame_no) + \
set_text_invisible + \
invisible_tc_btn_marker_fmt.
format(timecode, flipper_inputs) + \
attributes_reset)
gif_frame_no += 1
# Add the image to the GIF frames
gif_frames.append(image)
last_gif_frame_timecode = timecode
# Hide the cursor if needed
if not args.no_display and cursor_visible:
sys.stdout.write(set_cursor_invisible)
cursor_visible = False
# Turn the screen data into lines of unicode elements
if args.high_density_semigraphics:
imglines = ["".join([unicode_braille_2x4[((screen_data[i] >> j) & \
0b1111) | \
(((screen_data[i + 1] >> j) & \
0b1111) << 4)]
for i in range(k, k + 128, 2)])
for k in range(0, 1024, 128) for j in (0, 4)]
elif args.mid_density_semigraphics:
imglines = []
for k in range(0, 1024, 384):
c = [screen_data[i] + (screen_data[i + 128] << 8) + \
((screen_data[i + 256] << 16) if i < 768 else 0xff0000) \
for i in range(k, k + 128)]
for j in (0, 3, 6, 9, 12, 15) + ((18, 21) if k < 768 else ()):
imglines.append("".join([unicode_blocks_2x3[
((c[i] >> j) & 7) | \
(((c[i + 1] >> j) & 7) << 3)] \
for i in range(0, 128, 2)]))
else:
imglines = ["".join([unicode_blocks_1x2[(screen_data[i] >> j) & 0b11]
for i in range(k, k + 128)])
for k in range(0, 1024, 128) for j in (0, 2, 4, 6)]
# Encode the invisible timecode and button presses marker into the left
# spacer for the Flipper Zero's name
tcbtns = invisible_tc_btn_marker_fmt.format(timecode, flipper_inputs)
flipper_name_spc1 = set_text_invisible + tcbtns + attributes_reset + \
" " * (len_flipper_name_spc1 - len(tcbtns))
# Do we record the session in a text file and do we have a reason to
# record this frame?
if rt is not None and (screen_data_changed or flipper_inputs):
# Generate the ANSI text for the record without help overlay or bottom
# help line
at = flipper_name_spc1 + flipper_name + flipper_name_spc2 + CR + LF + \
"".join([set_bg_color.format(ansi_8bit_black) + \
set_fg_color.format(ansi_8bit_orange) + \
extra_attributes + l + attributes_reset + \
CR + LF \
for l in imglines]) + \
x_lines_up.format(height + 1)
# Save the ANSI text into the file
rt.write(at)
nb_lines_back_up_text_record = height + 1
# Should we update the display?
if screen_data_changed or update_display or flipper_inputs:
# Is the normal display output suppressed?
if args.no_display:
# Print the timecode and keypresses
for b in flipper_inputs:
print("[{:0.3f}s] {}{}".
format(timecode,
{"L": "\u2190", "D": "\u2193",
"U": "\u2191", "R": "\u2192",
"O": "o", "B": "\u21B0"}[b.upper()],
"" if b.islower() else " (long press)"))
# Output the normal display
else:
# If the keymap help should be displayed, overlay it over the lines
if show_keymap:
for i, l in enumerate(keymap_help):
imglines[keymap_help_overlay_at_line + i] = \
imglines[keymap_help_overlay_at_line + i]\
[:keymap_help_overlay_at_col] + \
attributes_reset + l + \
set_bg_color.format(ansi_8bit_black) + \
set_fg_color.format(ansi_8bit_orange) + \
extra_attributes + \
imglines[keymap_help_overlay_at_line + i]\
[keymap_help_overlay_at_col + \
keymap_help_line_len:]
# Generate the display ANSI text with help overlay & bottom help line
at = flipper_name_spc1 + flipper_name + flipper_name_spc2 + \
CR + LF + \
"".join([set_bg_color.format(ansi_8bit_black) + \
set_fg_color.format(ansi_8bit_orange) + \
extra_attributes + l + attributes_reset + \
CR + LF \
for l in imglines]) + \
bottom_line_spc1 + bottom_line + bottom_line_spc2 + \
x_lines_up.format(1) + CR + LF + x_lines_up.format(height + 1)
# Print the ANSI text & flush the console so it's updated immediately
sys.stdout.write(at)
sys.stdout.flush()
nb_lines_back_up = height + 1
update_display = False
except KeyboardInterrupt:
pass
except:
raise
finally:
# Get the current time and calculate this last output's timecode
now = time()
if start_time is None:
start_time = now
timecode = now - start_time
# If GIF frames are recorded and we have at least one frame, set this last
# timecode as the final GIF frame's duration
if gif_frames:
# The difference between this timecode and the previous GIF frame's
# timecode is the previous GIF frame's duration
prev_frame_duration_ms = (timecode - last_gif_frame_timecode) * 1000
# Repeat the previous GIF frame as many times as needed, encode only the
# frame number invisibly into the image then increment the frame number
while prev_frame_duration_ms > max_gif_frame_duration_ms + \
min_gif_frame_duration_ms:
image = copy(gif_frames[-1])
steg_encode(image, "[{}]".format(gif_frame_no))
gif_frame_no += 1
gif_frame_durations_ms.append(max_gif_frame_duration_ms)
gif_frames.append(image)
prev_frame_duration_ms -= max_gif_frame_duration_ms
# Add the duration of the final GIF frame
gif_frame_durations_ms.append(max(prev_frame_duration_ms,
min_gif_frame_duration_ms))
# If we output the normal display, output the last invisible timecode and
# button presses marker then skip past the rendering
if not args.no_display:
sys.stdout.write(CR + set_text_invisible + \
invisible_tc_btn_marker_fmt.
format(timecode, flipper_inputs) + \