Skip to content

Commit 03f2f15

Browse files
authored
Merge pull request #8 from mchilli/flip-rotation
flip rotation, brightness setting
2 parents 969e7fb + 992f4df commit 03f2f15

File tree

4 files changed

+116
-76
lines changed

4 files changed

+116
-76
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ This is an [Adafruit MacroPad](https://www.adafruit.com/product/5128) script tha
1111
- Make groups to organize your macros
1212
- Groups can store more macros or groups
1313
- Define encoder macros for different groups
14-
- Set a display timeout to prevent burn-in
15-
- Select a keyboard layout suitable for your language
16-
- You can use a Unicode Font (but this increases the font loading time)
1714
- Choose colors for every single macro or group
1815
- You can have an almost infinite number of pages
1916
- Save your configurations locally by downloading it as a JSON file
2017

18+
- Device settings:
19+
- Choose a keyboard layout suitable for your language
20+
- Set a display timeout to prevent burn-in
21+
- Use a Unicode Font **(increases the font loading time)**
22+
- Flip the rotation of the device by 180 degrees
23+
- Adjust the LCD and LED brightness
24+
2125
#### Installation:
2226

2327
Flash circuitpython on to your macropad, following this [guide](https://learn.adafruit.com/adafruit-macropad-rp2040/circuitpython).

circuitpython/code.py

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,58 @@
2525

2626
SETTINGSFILE = "settings.json" # The file in which the settings are saved
2727
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+
}
3136

3237
try:
3338
with open(SETTINGSFILE, "r") as f:
3439
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]
4143
except Exception:
4244
pass
4345

44-
if KEYBOARDLAYOUT == "br":
46+
if SETTINGS["keyboardlayout"] == "br":
4547
from adafruit_hid.keyboard_layout_win_br import KeyboardLayout
4648
from adafruit_hid.keycode_win_br import Keycode
47-
elif KEYBOARDLAYOUT == "cz":
49+
elif SETTINGS["keyboardlayout"] == "cz":
4850
from adafruit_hid.keyboard_layout_win_cz import KeyboardLayout
4951
from adafruit_hid.keycode_win_cz import Keycode
50-
elif KEYBOARDLAYOUT == "da":
52+
elif SETTINGS["keyboardlayout"] == "da":
5153
from adafruit_hid.keyboard_layout_win_da import KeyboardLayout
5254
from adafruit_hid.keycode_win_da import Keycode
53-
elif KEYBOARDLAYOUT == "de":
55+
elif SETTINGS["keyboardlayout"] == "de":
5456
from adafruit_hid.keyboard_layout_win_de import KeyboardLayout
5557
from adafruit_hid.keycode_win_de import Keycode
56-
elif KEYBOARDLAYOUT == "es":
58+
elif SETTINGS["keyboardlayout"] == "es":
5759
from adafruit_hid.keyboard_layout_win_es import KeyboardLayout
5860
from adafruit_hid.keycode_win_es import Keycode
59-
elif KEYBOARDLAYOUT == "fr":
61+
elif SETTINGS["keyboardlayout"] == "fr":
6062
from adafruit_hid.keyboard_layout_win_fr import KeyboardLayout
6163
from adafruit_hid.keycode_win_fr import Keycode
62-
elif KEYBOARDLAYOUT == "hu":
64+
elif SETTINGS["keyboardlayout"] == "hu":
6365
from adafruit_hid.keyboard_layout_win_hu import KeyboardLayout
6466
from adafruit_hid.keycode_win_hu import Keycode
65-
elif KEYBOARDLAYOUT == "it":
67+
elif SETTINGS["keyboardlayout"] == "it":
6668
from adafruit_hid.keyboard_layout_win_it import KeyboardLayout
6769
from adafruit_hid.keycode_win_it import Keycode
68-
elif KEYBOARDLAYOUT == "po":
70+
elif SETTINGS["keyboardlayout"] == "po":
6971
from adafruit_hid.keyboard_layout_win_po import KeyboardLayout
7072
from adafruit_hid.keycode_win_po import Keycode
71-
elif KEYBOARDLAYOUT == "sw":
73+
elif SETTINGS["keyboardlayout"] == "sw":
7274
from adafruit_hid.keyboard_layout_win_sw import KeyboardLayout
7375
from adafruit_hid.keycode_win_sw import Keycode
74-
elif KEYBOARDLAYOUT == "tr":
76+
elif SETTINGS["keyboardlayout"] == "tr":
7577
from adafruit_hid.keyboard_layout_win_tr import KeyboardLayout
7678
from adafruit_hid.keycode_win_tr import Keycode
77-
elif KEYBOARDLAYOUT == "uk":
79+
elif SETTINGS["keyboardlayout"] == "uk":
7880
from adafruit_hid.keyboard_layout_win_uk import KeyboardLayout
7981
from adafruit_hid.keycode_win_uk import Keycode
8082
else:
@@ -85,44 +87,31 @@
8587
class MacroApp():
8688
""" Main Class """
8789
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)
8991
self.macropad.display.auto_refresh = False
90-
self.macropad.display.brightness = 0.1
92+
self.macropad.display.brightness = SETTINGS["brightness"]
9193
self.macropad.pixels.auto_write = False
92-
self.macropad.pixels.brightness = 0.1
94+
self.macropad.pixels.brightness = SETTINGS["brightness"]
9395

96+
self.readonly = storage.getmount('/').readonly
9497
self.serial_data = usb_cdc.data
9598
self.serial_last_state = False
9699

97-
self.settings = self._init_settings()
98100
self.macros = self._init_macros()
99101
self.keys = self._init_keys()
100102
self.toolbar = self._init_toolbar()
101103
self.encoder = Encoder(self.macropad)
102104

103105
self.show_homescreen()
104106

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
123109
"""
110+
if self.readonly:
111+
return False
124112
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
126115

127116
def _init_macros(self) -> list[dict]:
128117
""" initiate the macro json file
@@ -148,8 +137,11 @@ def _init_macros(self) -> list[dict]:
148137
def _save_macros(self) -> None:
149138
""" store the macros in the macrofile
150139
"""
140+
if self.readonly:
141+
return False
151142
with open(MACROFILE, "w") as f:
152143
f.write(json.dumps(self.macros, separators=(",", ":")))
144+
return True
153145

154146
def _init_keys(self) -> list[Key]:
155147
""" Initiate the keys and a display group for each key
@@ -162,7 +154,7 @@ def _init_keys(self) -> list[Key]:
162154

163155
for i in range(self.macropad.keys.key_count):
164156
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,
166158
text="",
167159
padding_top=1,
168160
padding_bottom=2,
@@ -293,7 +285,7 @@ def run_macro(self, item:dict, *args) -> None:
293285
if 'sys' in key:
294286
method = getattr(System, key['sys'], None)
295287
if method:
296-
method(self.macropad)
288+
method(self)
297289

298290
self.macropad.keyboard.release_all()
299291
self.macropad.mouse.release_all()
@@ -396,7 +388,7 @@ def _handle_serial_data(self, payload:str) -> dict:
396388

397389
if command == 'get_settings':
398390
response['ACK'] = 'settings'
399-
response['CONTENT'] = self.settings
391+
response['CONTENT'] = SETTINGS
400392
return response
401393

402394
elif command == 'set_settings':
@@ -405,12 +397,10 @@ def _handle_serial_data(self, payload:str) -> dict:
405397
return response
406398

407399
content = payload['content']
408-
self.settings = content
409400

410-
try:
411-
self._save_settings()
401+
if self._save_settings(content):
412402
response['ACK'] = 'Settings are set'
413-
except OSError as e:
403+
else:
414404
response['ERR'] = 'Cannot set settings because USB storage is enabled'
415405

416406
return response
@@ -434,10 +424,9 @@ def _handle_serial_data(self, payload:str) -> dict:
434424
return response
435425

436426
elif command == 'save_macros':
437-
try:
438-
self._save_macros()
427+
if self._save_macros():
439428
response['ACK'] = 'Macros stored'
440-
except OSError as e:
429+
else:
441430
response['ERR'] = 'Cannot store macros because USB storage is enabled'
442431

443432
return response
@@ -486,7 +475,7 @@ def start(self) -> None:
486475
"""
487476
self.sleep_timer = time.monotonic()
488477
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"]:
490479
self.macropad.display_sleep = True
491480

492481
self.macropad.display.refresh()
@@ -495,8 +484,7 @@ def start(self) -> None:
495484
if self.serial_last_state != self.serial_data.connected:
496485
self.serial_last_state = self.serial_data.connected
497486
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 })
500488

501489
if self.serial_data.connected:
502490
if self.serial_data.in_waiting > 0:

circuitpython/utils/system.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import microcontroller
22
import supervisor
3-
from adafruit_macropad import MacroPad
43

54
USBENABLEDFILE = "usbenabled"
65

76
class System():
8-
def enable_usb(macropad:MacroPad=None) -> None:
7+
def enable_usb(app=None) -> None:
98
try:
109
with open(USBENABLEDFILE, "a") as f: pass
1110
System.hard_reset()
1211
except Exception:
1312
pass
1413

15-
def soft_reset(macropad:MacroPad=None) -> None:
14+
def soft_reset(app=None) -> None:
1615
supervisor.reload()
1716

18-
def hard_reset(macropad:MacroPad=None) -> None:
17+
def hard_reset(app=None) -> None:
1918
microcontroller.reset()
2019

21-
def decrease_brightness(macropad:MacroPad=None) -> None:
22-
if macropad.display.brightness > 0:
23-
brightness = round(macropad.display.brightness * 10)
24-
macropad.display.brightness = (brightness - 1) / 10
25-
macropad.pixels.brightness = (brightness - 1) / 10
26-
macropad.pixels.show()
20+
def decrease_brightness(app=None) -> None:
21+
if app.macropad.display.brightness > 0:
22+
brightness = (round(app.macropad.display.brightness * 10) - 1) / 10
23+
app.macropad.display.brightness = brightness
24+
app.macropad.pixels.brightness = brightness
25+
app.macropad.pixels.show()
26+
app.settings["brightness"] = brightness
2727

28-
def increase_brightness(macropad:MacroPad=None) -> None:
29-
if macropad.display.brightness < 1:
30-
brightness = round(macropad.display.brightness * 10)
31-
macropad.display.brightness = (brightness + 1) / 10
32-
macropad.pixels.brightness = (brightness + 1) / 10
33-
macropad.pixels.show()
28+
def increase_brightness(app=None) -> None:
29+
if app.macropad.display.brightness < 1:
30+
brightness = (round(app.macropad.display.brightness * 10) + 1) / 10
31+
app.macropad.display.brightness = brightness
32+
app.macropad.pixels.brightness = brightness
33+
app.macropad.pixels.show()
34+
app.settings["brightness"] = brightness

webui/js/modules/classes/Dialogs.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,46 @@ export class SettingsDialog extends BaseDialog {
15361536
})),
15371537
],
15381538
}),
1539+
utils.create({
1540+
attributes: {
1541+
class: 'dialog-input-shorten',
1542+
},
1543+
children: [
1544+
utils.create({
1545+
text: 'Flip Rotation: ',
1546+
}),
1547+
(DOM.fliprotation = utils.create({
1548+
type: 'input',
1549+
attributes: {
1550+
type: 'checkbox',
1551+
},
1552+
})),
1553+
],
1554+
}),
1555+
utils.create({
1556+
attributes: {
1557+
class: 'dialog-input-shorten',
1558+
},
1559+
children: [
1560+
utils.create({
1561+
text: 'LCD/LED Brightness: ',
1562+
}),
1563+
(DOM.brightness = utils.create({
1564+
type: 'input',
1565+
attributes: {
1566+
type: 'range',
1567+
min: 0,
1568+
max: 1,
1569+
step: 0.1,
1570+
},
1571+
events: {
1572+
input: (event) => {
1573+
event.target.title = event.target.value;
1574+
},
1575+
},
1576+
})),
1577+
],
1578+
}),
15391579
],
15401580
}),
15411581
utils.create({
@@ -1578,6 +1618,8 @@ export class SettingsDialog extends BaseDialog {
15781618
DOM.keyboardlayout.disabled = this.readonly;
15791619
DOM.sleeptime.disabled = this.readonly;
15801620
DOM.useunicodefont.disabled = this.readonly;
1621+
DOM.fliprotation.disabled = this.readonly;
1622+
DOM.brightness.disabled = this.readonly;
15811623

15821624
return DOM;
15831625
}
@@ -1597,6 +1639,8 @@ export class SettingsDialog extends BaseDialog {
15971639
this.settings.keyboardlayout = this.DOM.keyboardlayout.value;
15981640
this.settings.sleeptime = parseInt(this.DOM.sleeptime.value);
15991641
this.settings.useunicodefont = this.DOM.useunicodefont.checked;
1642+
this.settings.fliprotation = this.DOM.fliprotation.checked;
1643+
this.settings.brightness = parseFloat(this.DOM.brightness.value);
16001644

16011645
this.resolve({ dialogInstance: this, settings: this.settings });
16021646
this._removeFromParent(this.DOM.container);
@@ -1615,6 +1659,9 @@ export class SettingsDialog extends BaseDialog {
16151659

16161660
this.DOM.sleeptime.value = this.settings.sleeptime;
16171661
this.DOM.useunicodefont.checked = this.settings.useunicodefont;
1662+
this.DOM.fliprotation.checked = this.settings.fliprotation;
1663+
this.DOM.brightness.value = this.settings.brightness;
1664+
this.DOM.brightness.title = this.settings.brightness;
16181665
}
16191666
}
16201667

0 commit comments

Comments
 (0)