25
25
26
26
SETTINGSFILE = "settings.json" # The file in which the settings are saved
27
27
MACROFILE = "macros.json" # The file in which the macros are saved
28
- SLEEPTIME = 2 # Time in seconds until the display turns off
29
- KEYBOARDLAYOUT = "us" # Supported keyboard layouts: br, cz, da, de, es, fr, hu, it, po, sw, tr, uk, us
30
- USEUNICODEFONT = False # Use a unicode bitmap font, which will increas the initial load time!
28
+
29
+ SETTINGS = {
30
+ "sleeptime" : 2 , # Time in seconds until the display turns off
31
+ "keyboardlayout" : "us" , # Supported keyboard layouts: br, cz, da, de, es, fr, hu, it, po, sw, tr, uk, us
32
+ "useunicodefont" : False , # Use a unicode bitmap font, which will increas the initial load time!
33
+ "fliprotation" : False , # Flips the rotation of the device by 180 degrees
34
+ "brightness" : 0.1 # Set the LCD and LED Brightness
35
+ }
31
36
32
37
try :
33
38
with open (SETTINGSFILE , "r" ) as f :
34
39
settings = json .load (f )
35
- if "sleeptime" in settings :
36
- SLEEPTIME = settings ["sleeptime" ]
37
- if "keyboardlayout" in settings :
38
- KEYBOARDLAYOUT = settings ["keyboardlayout" ]
39
- if "useunicodefont" in settings :
40
- USEUNICODEFONT = settings ["useunicodefont" ]
40
+ for key in SETTINGS :
41
+ if key in settings :
42
+ SETTINGS [key ] = settings [key ]
41
43
except Exception :
42
44
pass
43
45
44
- if KEYBOARDLAYOUT == "br" :
46
+ if SETTINGS [ "keyboardlayout" ] == "br" :
45
47
from adafruit_hid .keyboard_layout_win_br import KeyboardLayout
46
48
from adafruit_hid .keycode_win_br import Keycode
47
- elif KEYBOARDLAYOUT == "cz" :
49
+ elif SETTINGS [ "keyboardlayout" ] == "cz" :
48
50
from adafruit_hid .keyboard_layout_win_cz import KeyboardLayout
49
51
from adafruit_hid .keycode_win_cz import Keycode
50
- elif KEYBOARDLAYOUT == "da" :
52
+ elif SETTINGS [ "keyboardlayout" ] == "da" :
51
53
from adafruit_hid .keyboard_layout_win_da import KeyboardLayout
52
54
from adafruit_hid .keycode_win_da import Keycode
53
- elif KEYBOARDLAYOUT == "de" :
55
+ elif SETTINGS [ "keyboardlayout" ] == "de" :
54
56
from adafruit_hid .keyboard_layout_win_de import KeyboardLayout
55
57
from adafruit_hid .keycode_win_de import Keycode
56
- elif KEYBOARDLAYOUT == "es" :
58
+ elif SETTINGS [ "keyboardlayout" ] == "es" :
57
59
from adafruit_hid .keyboard_layout_win_es import KeyboardLayout
58
60
from adafruit_hid .keycode_win_es import Keycode
59
- elif KEYBOARDLAYOUT == "fr" :
61
+ elif SETTINGS [ "keyboardlayout" ] == "fr" :
60
62
from adafruit_hid .keyboard_layout_win_fr import KeyboardLayout
61
63
from adafruit_hid .keycode_win_fr import Keycode
62
- elif KEYBOARDLAYOUT == "hu" :
64
+ elif SETTINGS [ "keyboardlayout" ] == "hu" :
63
65
from adafruit_hid .keyboard_layout_win_hu import KeyboardLayout
64
66
from adafruit_hid .keycode_win_hu import Keycode
65
- elif KEYBOARDLAYOUT == "it" :
67
+ elif SETTINGS [ "keyboardlayout" ] == "it" :
66
68
from adafruit_hid .keyboard_layout_win_it import KeyboardLayout
67
69
from adafruit_hid .keycode_win_it import Keycode
68
- elif KEYBOARDLAYOUT == "po" :
70
+ elif SETTINGS [ "keyboardlayout" ] == "po" :
69
71
from adafruit_hid .keyboard_layout_win_po import KeyboardLayout
70
72
from adafruit_hid .keycode_win_po import Keycode
71
- elif KEYBOARDLAYOUT == "sw" :
73
+ elif SETTINGS [ "keyboardlayout" ] == "sw" :
72
74
from adafruit_hid .keyboard_layout_win_sw import KeyboardLayout
73
75
from adafruit_hid .keycode_win_sw import Keycode
74
- elif KEYBOARDLAYOUT == "tr" :
76
+ elif SETTINGS [ "keyboardlayout" ] == "tr" :
75
77
from adafruit_hid .keyboard_layout_win_tr import KeyboardLayout
76
78
from adafruit_hid .keycode_win_tr import Keycode
77
- elif KEYBOARDLAYOUT == "uk" :
79
+ elif SETTINGS [ "keyboardlayout" ] == "uk" :
78
80
from adafruit_hid .keyboard_layout_win_uk import KeyboardLayout
79
81
from adafruit_hid .keycode_win_uk import Keycode
80
82
else :
85
87
class MacroApp ():
86
88
""" Main Class """
87
89
def __init__ (self ) -> None :
88
- self .macropad = MacroPad (layout_class = KeyboardLayout )
90
+ self .macropad = MacroPad (layout_class = KeyboardLayout , rotation = 180 if SETTINGS [ "fliprotation" ] else 0 )
89
91
self .macropad .display .auto_refresh = False
90
- self .macropad .display .brightness = 0.1
92
+ self .macropad .display .brightness = SETTINGS [ "brightness" ]
91
93
self .macropad .pixels .auto_write = False
92
- self .macropad .pixels .brightness = 0.1
94
+ self .macropad .pixels .brightness = SETTINGS [ "brightness" ]
93
95
96
+ self .readonly = storage .getmount ('/' ).readonly
94
97
self .serial_data = usb_cdc .data
95
98
self .serial_last_state = False
96
99
97
- self .settings = self ._init_settings ()
98
100
self .macros = self ._init_macros ()
99
101
self .keys = self ._init_keys ()
100
102
self .toolbar = self ._init_toolbar ()
101
103
self .encoder = Encoder (self .macropad )
102
104
103
105
self .show_homescreen ()
104
106
105
- def _init_settings (self ) -> dict :
106
- """ initiate the settings json file
107
-
108
- Returns:
109
- dict: the json file as a dict
110
- """
111
- try :
112
- with open (SETTINGSFILE , "r" ) as f :
113
- return json .load (f )
114
- except OSError :
115
- return {
116
- "keyboardlayout" : KEYBOARDLAYOUT ,
117
- "sleeptime" : SLEEPTIME ,
118
- "useunicodefont" : USEUNICODEFONT
119
- }
120
-
121
- def _save_settings (self ) -> None :
122
- """ store the settings in the settingsfile
107
+ def _save_settings (self , new_settings ) -> None :
108
+ """ store the new settings in the settingsfile
123
109
"""
110
+ if self .readonly :
111
+ return False
124
112
with open (SETTINGSFILE , "w" ) as f :
125
- f .write (json .dumps (self .settings , separators = ("," , ":" )))
113
+ f .write (json .dumps (new_settings , separators = ("," , ":" )))
114
+ return True
126
115
127
116
def _init_macros (self ) -> list [dict ]:
128
117
""" initiate the macro json file
@@ -148,8 +137,11 @@ def _init_macros(self) -> list[dict]:
148
137
def _save_macros (self ) -> None :
149
138
""" store the macros in the macrofile
150
139
"""
140
+ if self .readonly :
141
+ return False
151
142
with open (MACROFILE , "w" ) as f :
152
143
f .write (json .dumps (self .macros , separators = ("," , ":" )))
144
+ return True
153
145
154
146
def _init_keys (self ) -> list [Key ]:
155
147
""" Initiate the keys and a display group for each key
@@ -162,7 +154,7 @@ def _init_keys(self) -> list[Key]:
162
154
163
155
for i in range (self .macropad .keys .key_count ):
164
156
label = Label (
165
- font = load_font ("/fonts/6x12.pcf" ) if USEUNICODEFONT else terminalio .FONT ,
157
+ font = load_font ("/fonts/6x12.pcf" ) if SETTINGS [ "useunicodefont" ] else terminalio .FONT ,
166
158
text = "" ,
167
159
padding_top = 1 ,
168
160
padding_bottom = 2 ,
@@ -293,7 +285,7 @@ def run_macro(self, item:dict, *args) -> None:
293
285
if 'sys' in key :
294
286
method = getattr (System , key ['sys' ], None )
295
287
if method :
296
- method (self . macropad )
288
+ method (self )
297
289
298
290
self .macropad .keyboard .release_all ()
299
291
self .macropad .mouse .release_all ()
@@ -396,7 +388,7 @@ def _handle_serial_data(self, payload:str) -> dict:
396
388
397
389
if command == 'get_settings' :
398
390
response ['ACK' ] = 'settings'
399
- response ['CONTENT' ] = self . settings
391
+ response ['CONTENT' ] = SETTINGS
400
392
return response
401
393
402
394
elif command == 'set_settings' :
@@ -405,12 +397,10 @@ def _handle_serial_data(self, payload:str) -> dict:
405
397
return response
406
398
407
399
content = payload ['content' ]
408
- self .settings = content
409
400
410
- try :
411
- self ._save_settings ()
401
+ if self ._save_settings (content ):
412
402
response ['ACK' ] = 'Settings are set'
413
- except OSError as e :
403
+ else :
414
404
response ['ERR' ] = 'Cannot set settings because USB storage is enabled'
415
405
416
406
return response
@@ -434,10 +424,9 @@ def _handle_serial_data(self, payload:str) -> dict:
434
424
return response
435
425
436
426
elif command == 'save_macros' :
437
- try :
438
- self ._save_macros ()
427
+ if self ._save_macros ():
439
428
response ['ACK' ] = 'Macros stored'
440
- except OSError as e :
429
+ else :
441
430
response ['ERR' ] = 'Cannot store macros because USB storage is enabled'
442
431
443
432
return response
@@ -486,7 +475,7 @@ def start(self) -> None:
486
475
"""
487
476
self .sleep_timer = time .monotonic ()
488
477
while True :
489
- if not self .macropad .display_sleep and time .monotonic () - self .sleep_timer > SLEEPTIME :
478
+ if not self .macropad .display_sleep and time .monotonic () - self .sleep_timer > SETTINGS [ "sleeptime" ] :
490
479
self .macropad .display_sleep = True
491
480
492
481
self .macropad .display .refresh ()
@@ -495,8 +484,7 @@ def start(self) -> None:
495
484
if self .serial_last_state != self .serial_data .connected :
496
485
self .serial_last_state = self .serial_data .connected
497
486
if self .serial_data .connected :
498
- readonly = storage .getmount ('/' ).readonly
499
- self ._send_serial_data ({'ACK' : 'usbenabled' , 'CONTENT' : readonly })
487
+ self ._send_serial_data ({'ACK' : 'usbenabled' , 'CONTENT' : self .readonly })
500
488
501
489
if self .serial_data .connected :
502
490
if self .serial_data .in_waiting > 0 :
0 commit comments