Compare commits
No commits in common. "2aac36e15cd00e64c25399fa9c5dbb25113285e8" and "f03cc4a44cd0a47737ec18d29edd49fa2f5ea763" have entirely different histories.
2aac36e15c
...
f03cc4a44c
32
README.md
32
README.md
@ -62,37 +62,7 @@ All the configuration can be done in the [config.json](config.json) file in JSON
|
|||||||
| "LCD\_I2C\_NUM\_ROWS" | int | how many rows for character display has the display? | 2 |
|
| "LCD\_I2C\_NUM\_ROWS" | int | how many rows for character display has the display? | 2 |
|
||||||
| "LCD\_I2C\_NUM\_COLS" | int | and how many characters can it display per row? | 16 |
|
| "LCD\_I2C\_NUM\_COLS" | int | and how many characters can it display per row? | 16 |
|
||||||
|
|
||||||
Note that this software has it's own small wrapper for the config file, e.g. to have instant access to an LCD object generated on the fly. These are all documented in the [utils.py](utils.py) file! When setting configuration options from your custom code, keep in mind that doing this via the `Config().<ATTR_NAME>` way just means writing the value directly to the file, while getting it goes through the wrapper to make e.g. the pin a machine.Pin object. But you just can't write a pin back into an attribute.
|
Note that this software has it's own small wrapper for the config file, e.g. to have instant access to an LCD object generated on the fly. These are all documented in the [utils.py](utils.py) file!
|
||||||
|
|
||||||
This wont work:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from utils import Config
|
|
||||||
from machine import Pin
|
|
||||||
cfg = Config()
|
|
||||||
|
|
||||||
btn1 = cfg.PIN_IN_BTN_1
|
|
||||||
|
|
||||||
# Now we don't like this setting anymore
|
|
||||||
new_btn1 = Pin(10, Pin.IN, Pin.PULL_DOWN)
|
|
||||||
cfg.PIN_IN_BTN_1 = new_btn1
|
|
||||||
```
|
|
||||||
|
|
||||||
Instead, you have to do it that way:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from utils import Config
|
|
||||||
from machine import Pin
|
|
||||||
cfg = Config
|
|
||||||
|
|
||||||
btn1 = cfg.PIN_IN_BTN_1
|
|
||||||
|
|
||||||
# Now we don't like it so we write the new pin in our json format
|
|
||||||
cfg.PIN_IN_BTN_1 = {"pin": 10, "pull": "down"}
|
|
||||||
```
|
|
||||||
|
|
||||||
So, as the `Config` class uses python's magic function for getting and setting attributes, the process of changing some config is not completely intuitive, but when keeping that in mind, it's very handy!
|
|
||||||
|
|
||||||
|
|
||||||
## Used libraries
|
## Used libraries
|
||||||
|
|
||||||
|
17
utils.py
17
utils.py
@ -54,7 +54,7 @@ class Config:
|
|||||||
def save_config(self):
|
def save_config(self):
|
||||||
with open(self._config_file, "w") as f:
|
with open(self._config_file, "w") as f:
|
||||||
from json import dump
|
from json import dump
|
||||||
dump(self._config, f, separators=(',\n', ': '))
|
dump(self._config, f)
|
||||||
del dump
|
del dump
|
||||||
collect()
|
collect()
|
||||||
|
|
||||||
@ -102,17 +102,8 @@ class Config:
|
|||||||
|
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
if name.startswith("_"): # make private attributes settable as normal
|
#print(f"Someone tried to edit my poor attributes! Affected: '{name}' should be set to '{value}'")
|
||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
elif name in self._attr_list: # valid attributes (only capital letters and -_ etc. are allowed)
|
|
||||||
try:
|
|
||||||
self._config[name] = value
|
|
||||||
self.save_config()
|
|
||||||
except KeyError:
|
|
||||||
raise AttributeError(f"Attribute '{name}' does not exist in the config file '{self._config_file}'")
|
|
||||||
else:
|
|
||||||
raise AttributeError(f"Can't set attribute '{name}' for a '{self.__class__.__name__}' object: forbidden")
|
|
||||||
|
|
||||||
|
|
||||||
def __delattr__(self, name):
|
def __delattr__(self, name):
|
||||||
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
|
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
|
||||||
@ -133,5 +124,3 @@ def log(log_level: int, message: str):
|
|||||||
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
|
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
|
||||||
print(f"[{log_mapping[log_level]}] {message}")
|
print(f"[{log_mapping[log_level]}] {message}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user