Moved to json as the configuration format
This commit is contained in:
101
utils.py
101
utils.py
@@ -9,9 +9,106 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
from gc import collect
|
||||
|
||||
import config as cfg
|
||||
from time import sleep
|
||||
"""
|
||||
A small wrapper class as storing machine.Pin, LCD and machine.I2C objects in a file is not that cool :)
|
||||
Now only pin numbers and strings etc. are stored on the uC, and the complex objects are generated on the fly
|
||||
"""
|
||||
class Config:
|
||||
"""
|
||||
The initializer method
|
||||
- config_file: the path to the config file laying on the uC
|
||||
"""
|
||||
def __init__(self, config_file: str = "config.json"):
|
||||
self._attr_list = ["LOG_LEVEL", # there are three log levels: warn (0), info (1), debug (2)
|
||||
# this value defines which log messages to show
|
||||
# e.g. 2 means show [debug], [warn] and [info] messages
|
||||
"STARTUP_WELCOME_SHOW", # show the name and a startup message
|
||||
"STARTUP_PROJECT_NAME", # the name to show at startup
|
||||
"STARTUP_MESSAGE_STARTING", # the message to show at startup
|
||||
"STARTUP_MESSAGE_FINISHED", # the message to show at startup
|
||||
"STARTUP_WELCOME_CYCLES", # how often shall "Starting..." run over the screen
|
||||
"PIN_IN_BTN_1", # input of the first btn
|
||||
"PIN_IN_BTN_2", # input of the second btn
|
||||
"PIN_IN_SWITCH", # input of the switch
|
||||
"PIN_OUT_RELAIS", # where the relais is connected (for the UV lights)
|
||||
"PIN_SDA", # just some standard I2C serial data (SDA) output
|
||||
"PIN_SCL", # just some standard I2C serial clock (SCL) output
|
||||
"LCD_I2C_CH", # where the relais is connected (for the UV lights)
|
||||
"LCD_I2C_ADDR", # the i2c adress of the display
|
||||
"LCD_I2C_NUM_ROWS", # how many rows for character display has the display?
|
||||
"LCD_I2C_NUM_COLS", # and how many characters can it display per row?
|
||||
"LCD"] # the actual lcd object (of the PCF8574T I2C_LCD class, see libraries)
|
||||
self._config_file = config_file
|
||||
self.load_config()
|
||||
|
||||
def load_config(self):
|
||||
# prepare the class
|
||||
with open(self._config_file, "r") as f:
|
||||
from json import load
|
||||
self._config = load(f)
|
||||
del load
|
||||
collect()
|
||||
|
||||
def save_config(self):
|
||||
with open(self._config_file, "w") as f:
|
||||
from json import dump
|
||||
dump(self._config, f)
|
||||
del dump
|
||||
collect()
|
||||
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name.startswith("_"): # make private attributes unaccessible
|
||||
raise AttributeError(f"'Access to the private attribute '{name}' of the object '{self.__class__.__name__}' is forbidden")
|
||||
elif name in self._attr_list: # valid attributes (only capital letters and -_ etc. are allowed)
|
||||
try:
|
||||
# now some if statements to check if the lcd or some pin object is asked
|
||||
if name.startswith("PIN_"):
|
||||
from machine import Pin
|
||||
if name.startswith("PIN_IN"):
|
||||
if self._config[name]["pull"].lower() == "down":
|
||||
p = Pin(self._config[name]["pin"], Pin.IN, Pin.PULL_DOWN)
|
||||
elif self._config[name]["pull"].lower() == "up":
|
||||
p = Pin(self._config[name]["pin"], Pin.IN, Pin.PULL_UP)
|
||||
else:
|
||||
p = Pin(self._config[name]["pin"], Pin.IN)
|
||||
elif name.startswith("PIN_OUT"):
|
||||
p = Pin(self._config[name], Pin.OUT)
|
||||
else:
|
||||
p = Pin(self._config[name])
|
||||
del Pin
|
||||
collect()
|
||||
return p
|
||||
elif name == "LCD":
|
||||
try:
|
||||
return self._lcd
|
||||
except:
|
||||
from machine import I2C, Pin
|
||||
from PCF8574T import I2C_LCD
|
||||
self._lcd = I2C_LCD(I2C(self.LCD_I2C_CH, sda=self.PIN_SDA, scl=self.PIN_SCL, freq=400000),
|
||||
self.LCD_I2C_ADDR,
|
||||
self.LCD_I2C_NUM_ROWS,
|
||||
self.LCD_I2C_NUM_COLS)
|
||||
del I2C, Pin, I2C_LCD
|
||||
collect()
|
||||
return self._lcd
|
||||
return self._config[name]
|
||||
except KeyError:
|
||||
raise AttributeError(f"Attribute '{name}' does not exist in the config file '{self._config_file}'")
|
||||
else:
|
||||
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
|
||||
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
#print(f"Someone tried to edit my poor attributes! Affected: '{name}' should be set to '{value}'")
|
||||
object.__setattr__(self, name, value)
|
||||
|
||||
def __delattr__(self, name):
|
||||
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
|
||||
|
||||
cfg = Config()
|
||||
|
||||
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user