Compare commits

...

4 Commits

5 changed files with 97 additions and 52 deletions

View File

@ -45,26 +45,29 @@ All the configuration can be done in the [config.json](config.json) file in JSON
| Attribute name (on top level in config.json) | Type | Description | Default |
| -------------------------------------------- | ---- | ----------- | ------- |
| "LOG\_LEVEL" | int | defines up to which log level to show log messages in the serial console: warn (0), info (1), debug (2) | 2 |
| "STARTUP\_WELCOME\_SHOW" | bool | show the startup screen? | true |
| "STARTUP\_PROJECT\_NAME" | str | the name shown at the welcome/startup screen | " UV-Belichter " |
| "STARTUP\_MESSAGE\_STARTING | str | the message shown at startup when starting | "Starting..." |
| "STARTUP\_PROJECT\_FINISHED" | str | the message shown at startup when finished | " Started! " |
| "STARTUP\_WELCOME\_CYCLES" | int | how often the starting string shall go over the welcome screen | 1 |
| "PIN\_IN\_BTN\_1" | dict | the dict must contain the "pin" and "pull" keys with respective values | {"pin": 15, "pull": "down"} |
| "PIN\_IN\_BTN\_2" | dict | as above | {"pin": 14, "pull": "down"} |
| "PIN\_IN\_SWITCH" | dict | as above | {"pin": 13, "pull": "down"} |
| "PIN\_OUT\_RELAIS" | int | pin number where the relais is connected | 21 |
| "PIN\_SDA" | int | the pin number of the sda wire connected to the lcd | 8 |
| "PIN\_SCL" | int | the pin number of the scl wire connected to the lcd | 9 |
| "LCD\_I2C\_CH" | int | the channel of the i2c bus used | 0 |
| "LCD\_I2C\_ADDR" | int | the i2c address of the lcd; make sure to convert hexadecimal to decimal numbers | 38 |
| "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 |
| `"LOG\_LEVEL"` | int | defines up to which log level to show log messages in the serial console: warn (0), info (1), debug (2) | `2` |
| `"STARTUP\_WELCOME\_SHOW"` | bool | show the startup screen? | `true` |
| `"STARTUP\_PROJECT\_NAME"` | str | the name shown at the welcome/startup screen | `" UV-Belichter "` |
| `"STARTUP\_MESSAGE\_STARTING"` | str | the message shown at startup when starting | `"Starting..."` |
| `"STARTUP\_PROJECT\_FINISHED"` | str | the message shown at startup when finished | `" Started! "` |
| `"STARTUP\_WELCOME\_CYCLES"` | int | how often the starting string shall go over the welcome screen | `1` |
| `"PIN\_IN\_BTN\_1"` | dict | the dict must contain the "pin" and "pull" keys with respective values | `{"pin": 15, "pull": "down"}` |
| `"PIN\_IN\_BTN\_2"` | dict | as above | `{"pin": 14, "pull": "down"}` |
| `"PIN\_IN\_SWITCH"` | dict | as above | `{"pin": 13, "pull": "down"}` |
| `"PIN\_OUT\_RELAIS"` | int | pin number where the relais is connected | `21` |
| `"PIN\_SDA"` | int | the pin number of the sda wire connected to the lcd | `8` |
| `"PIN\_SCL"` | int | the pin number of the scl wire connected to the lcd | `9` |
| `"LCD\_I2C\_CH"` | int | the channel of the i2c bus used | `0` |
| `"LCD\_I2C\_ADDR"` | int | the i2c address of the lcd; make sure to convert hexadecimal to decimal numbers | `38` |
| `"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` |
| `"TIMER\_1\_DURATION"` | int | the timer duration of the first timer; IN SECONDS | `60` (1min) |
| `"TIMER\_2\_DURATION"` | int | as above, but of the seconds timer; IN SECONDS | `2400` (40min) |
| `"TIMER\_3\_DURATION"` | int | as above, but of the third timer; IN SECONDS | `2700` (45min) |
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.
This wont work:
This will NOT work:
```python
from utils import Config

View File

@ -15,4 +15,7 @@
"LCD_I2C_ADDR": 39,
"LCD_I2C_NUM_ROWS": 2,
"LCD_I2C_NUM_COLS": 16,
"TIMER_1_DURATION": 60,
"TIMER_2_DURATION": 2400,
"TIMER_3_DURATION": 2700,
}

View File

@ -14,26 +14,13 @@ You should have received a copy of the GNU General Public License along with thi
@ Feature: Fades "HELLO" over two lines in and out
"""
import machine
import time
import config
lcd = config.LCD
lcd.custom_char(0, bytearray([0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03])) # right
lcd.custom_char(1, bytearray([0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18])) # left
lcd.custom_char(2, bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F])) # 2-down
lcd.custom_char(3, bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00])) # 2-up
lcd.custom_char(4, bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x1F])) # e-up
lcd.custom_char(5, bytearray([0x1F,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F])) # e-down
lcd.custom_char(6, bytearray([0x18,0x18,0x18,0x18,0x18,0x18,0x1F,0x1F])) # l-down
lcd.custom_char(7, bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18])) # l-right
line1 = str(chr(0) + chr(2) + chr(1) + chr(0) + chr(4) + " " + chr(1) + " " + chr(1) + " " + chr(0) + chr(3) + chr(1))
line2 = str(chr(0) + chr(3) + chr(1) + chr(0) + chr(5) + " " + chr(6) + chr(7) + chr(6) + chr(7) + chr(0) + chr(2) + chr(1))
def right2left(line1, line2, speed=0.3):
def right2left(lcd, line1, line2, speed=0.3):
padding = " " # 16 spaces
line2 = padding + line2 + padding
line1 = padding + line1 + padding
@ -44,7 +31,7 @@ def right2left(line1, line2, speed=0.3):
time.sleep(speed)
def top2bottom(line1, line2, speed=0.2):
def top2bottom(lcd, line1, line2, speed=0.2):
lcd.clear()
time.sleep(speed)
lcd.putstr(line2)
@ -61,7 +48,7 @@ def top2bottom(line1, line2, speed=0.2):
lcd.clear()
time.sleep(speed)
def showAll(waitAfter=0.5):
def showAll(lcd, waitAfter=0.5):
lcd.clear()
lcd.putstr(line1)
lcd.move_to(0,1)
@ -69,11 +56,21 @@ def showAll(waitAfter=0.5):
time.sleep(waitAfter)
def run(): # for the ProgramChooser as callback
right2left(line1, line2, 0.2)
top2bottom(line1, line2, 0.2)
showAll()
def run(lcd): # for the ProgramChooser as callback
lcd.custom_char(0, bytearray([0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03])) # right
lcd.custom_char(1, bytearray([0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18])) # left
lcd.custom_char(2, bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F])) # 2-down
lcd.custom_char(3, bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00])) # 2-up
lcd.custom_char(4, bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x1F])) # e-up
lcd.custom_char(5, bytearray([0x1F,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F])) # e-down
lcd.custom_char(6, bytearray([0x18,0x18,0x18,0x18,0x18,0x18,0x1F,0x1F])) # l-down
lcd.custom_char(7, bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18])) # l-right
right2left(lcd, line1, line2, 0.2)
top2bottom(lcd, line1, line2, 0.2)
showAll(lcd)
if __name__ == "__main__":
run() # run once
from utils import Config
cfg = Config()
run(cfg.LCD) # run once

64
main.py
View File

@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License along with thi
import utils
from lcdMenu import lcdMenu
from WelcomeScreen import WelcomeScreen
from time import sleep
from time import sleep, time_ns
from gc import collect # garbage collector for better memory performance
config = utils.Config()
@ -30,7 +30,7 @@ def manual():
set_value = config.PIN_OUT_RELAIS.value()
config.LCD.putstr(f"---- MANUAL ---- State: {set_value} ")
if config.PIN_IN_BTN_1.value() == 1 or config.PIN_IN_BTN_2.value() == 1:
return True # exit on press of these buttons
return True # exit on press of these buttons; True to disable the Quitting message from lcdMenu
def timer():
# display WIP
config.LCD.clear()
@ -38,20 +38,20 @@ def timer():
sleep(3)
return True # disable the "Quitting" message from lcdMenu
def uv_on():
config.RELAIS.value(1)
config.PIN_OUT_RELAIS.value(1)
config.LCD.clear()
config.LCD.putstr("------ UV ------ turned on ")
sleep(1)
return True # disable the "Quitting" message from lcdMenu
def uv_off():
config.RELAIS.value(0)
config.PIN_OUT_RELAIS.value(0)
config.LCD.clear()
config.LCD.putstr("------ UV ------ turned off ")
sleep(1)
return True # disable the "Quitting" message from lcdMenu
def lcd_big_hello():
import lcd_big_hello as lbh
lbh.run()
lbh.run(config.LCD)
del lbh
collect()
return True
@ -61,10 +61,56 @@ def input_tests():
collect()
return True
def settings():
# display WIP
config.LCD.clear()
config.LCD.putstr(" Still work-in-progress")
sleep(3)
settings_menu = lcdMenu(config.LCD, btn_mapping, scroll_direction=True, cycle=True, hide_menu_name=False, name="SETTINGS")
def swap_welcome():
config.STARTUP_WELCOME_SHOW = not config.STARTUP_WELCOME_SHOW
config.LCD.clear()
config.LCD.putstr(f" Welcome Screen ")
if config.STARTUP_WELCOME_SHOW:
config.LCD.putstr("--- Enabled! ---")
else:
config.LCD.putstr("--- Disabled ---")
sleep(1)
def welcome_cycles():
config.LCD.clear()
current_cycles = config.STARTUP_WELCOME_CYCLES
option_down = [" ", "v"][current_cycles>1]
config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^")
while True:
if config.PIN_IN_BTN_1.value() == 1:
time_ns_when_pressed = time_ns()
while config.PIN_IN_BTN_1.value() == 1:
if (time_ns() - time_ns_when_pressed) > 1000000000: # if the time passed is longer than a second
while config.PIN_IN_BTN_1.value() == 1:
pass # wait till release
config.STARTUP_WELCOME_CYCLES = current_cycles
return True
sleep(0.05)
current_cycles -= 1
if current_cycles < 1: current_cycles = 1
option_down = [" ", "v"][current_cycles>1]
config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^")
if config.PIN_IN_BTN_2.value() == 1:
time_ns_when_pressed = time_ns()
while config.PIN_IN_BTN_2.value() == 1:
if (time_ns() - time_ns_when_pressed) > 1000000000: # if the time passed is longer than a second
while config.PIN_IN_BTN_2.value() == 1:
pass # wait till release
config.STARTUP_WELCOME_CYCLES = current_cycles
return True
sleep(0.05)
current_cycles += 1
option_down = [" ", "v"][current_cycles>1]
config.LCD.putstr(f" Cycles \n{option_down} {str(current_cycles).center(12)} ^")
settings_programs = [("Show welcome", swap_welcome),
("Welcome cycles", welcome_cycles),
("Exit", settings_menu.stop)]
settings_menu.setup(settings_programs) # give it the callback list
settings_menu.run() # run the menu until it's closed
del settings_menu, settings_programs
collect()
return True
def run_demo_menu():
demo_menu = lcdMenu(config.LCD, btn_mapping, scroll_direction=True, cycle=True, hide_menu_name=False, name="DEMOS")

View File

@ -117,8 +117,6 @@ class Config:
def __delattr__(self, name):
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
cfg = Config()
"""
Very simple logging function
@ -133,5 +131,3 @@ def log(log_level: int, message: str):
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
print(f"[{log_mapping[log_level]}] {message}")