Added the currently running program

This commit is contained in:
BlueFox 2024-10-26 09:44:14 +02:00
parent 113febc851
commit 7f7f5a042b
7 changed files with 677 additions and 0 deletions

65
config.py Normal file
View File

@ -0,0 +1,65 @@
"""
uv-belichter-software - Configuration file
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
---------------------------
----- LOGGING SECTION -----
---------------------------
"""
LOG_LEVEL = 2 # 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 SECTION -----
---------------------------
"""
STARTUP_PROJECT_NAME = " UV-Belichter " # the name to show at startup
STARTUP_MESSAGE_STARTING = "Starting..." # the message to show at startup
STARTUP_MESSAGE_FINISHED = " Started! " # the message to show at startup
STARTUP_WELCOME_SHOW = True # show the name and a startup message
STARTUP_WELCOME_CYCLES = 1 # how often shall "Starting..." run over the screen
"""
--------------------------
----- PINOUT SECTION -----
--------------------------
"""
from machine import Pin
BTN_1 = Pin(15, Pin.IN, Pin.PULL_DOWN) # input of the first btn
BTN_2 = Pin(14, Pin.IN, Pin.PULL_DOWN) # input of the second btn
SWITCH = Pin(13, Pin.IN, Pin.PULL_DOWN) # input of switch
LCD_SDA = Pin(8) # just some standard I2C serial data (SDA) outputs (on I2C channel 0 on Pi Pico)
LCD_SCL = Pin(9) # just some standard I2C serial clock (SCL) outputs (on I2C channel 0 on Pi Pico)
#LCD_SDA = Pin(16) # another pinout (soldered on the original project's circuit board)
#LCD_SCL = Pin(17) # another pinout (soldered on the original project's circuit board)
RELAIS = Pin(21, Pin.OUT) # where the relais is connected (for the UV lights)
"""
-----------------------
----- LCD SECTION -----
-----------------------
"""
from machine import I2C, Pin
from lib.PCF8574T import I2C_LCD
LCD_I2C_ADDR = 0x27 # the i2c adress of the display (yours might be different to this one)
LCD_I2C_NUM_ROWS = 2 # how many rows for character display has the display?
LCD_I2C_NUM_COLS = 16 # and how many characters can it display per row?
LCD = I2C_LCD(I2C(0, sda=LCD_SDA, scl=LCD_SCL, freq=400000),
LCD_I2C_ADDR,
LCD_I2C_NUM_ROWS,
LCD_I2C_NUM_COLS)

19
input-tests.py Normal file
View File

@ -0,0 +1,19 @@
"""
uv-belichter-software - A simple program that prints out the current sate of the three input devices present on my "UV-Belichter"
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import config as cfg
from time import sleep
if __name__ == "__main__":
while True:
print(f"Y_BTN: {cfg.BTN_1.value()}; G_BTN: {cfg.BTN_2.value()}; Lever: {cfg.SWITCH.value()}")
sleep(0.1)

79
lcd_big_hello.py Normal file
View File

@ -0,0 +1,79 @@
"""
An example "program" which can be used with the ProgramChooser library, see also main.py
Copyright (C) 2024 Benjamin Burkhardt <bluefox@privacynerd.de>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
"""
@ 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):
padding = " " # 16 spaces
line2 = padding + line2 + padding
line1 = padding + line1 + padding
for i in range(32):
line2 = line2[1:]
line1 = line1[1:]
lcd.putstr(line1[0:16] + line2[0:16])
time.sleep(speed)
def top2bottom(line1, line2, speed=0.2):
lcd.clear()
time.sleep(speed)
lcd.putstr(line2)
time.sleep(speed)
lcd.clear()
lcd.putstr(line1)
lcd.move_to(0,1)
lcd.putstr(line2)
time.sleep(speed)
lcd.clear()
lcd.move_to(0,1)
lcd.putstr(line1)
time.sleep(speed)
lcd.clear()
time.sleep(speed)
def showAll(waitAfter=0.5):
lcd.clear()
lcd.putstr(line1)
lcd.move_to(0,1)
lcd.putstr(line2)
time.sleep(waitAfter)
def run(): # for the ProgramChooser as callback
right2left(line1, line2, 0.2)
top2bottom(line1, line2, 0.2)
showAll()
if __name__ == "__main__":
run() # run once

288
lib/PCF8574T.py Normal file
View File

@ -0,0 +1,288 @@
# https://github.com/T-622/RPI-PICO-I2C-LCD
# Original driver by Tyler Peppy, modified by Benjamin Burkhardt (2024)
"""
MIT License
Copyright (c) 2023 Tyler Peppy
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import time
from machine import I2C
import utime
import gc
# PCF8574 pin definitions
MASK_RS = 0x01 # P0
MASK_RW = 0x02 # P1
MASK_E = 0x04 # P2
SHIFT_BACKLIGHT = 3 # P3
SHIFT_DATA = 4 # P4-P7
class LCD_API:
# Implements the API for talking with HD44780 compatible character LCDs.
# This class only knows what commands to send to the LCD, and not how to get
# them to the LCD.
#
# It is expected that a derived class will implement the hal_xxx functions.
#
# The following constant names were lifted from the avrlib lcd.h header file,
# with bit numbers changed to bit masks.
# HD44780 LCD controller command set
LCD_CLR = 0x01 # DB0: clear display
LCD_HOME = 0x02 # DB1: return to home position
LCD_ENTRY_MODE = 0x04 # DB2: set entry mode
LCD_ENTRY_INC = 0x02 # DB1: increment
LCD_ENTRY_SHIFT = 0x01 # DB0: shift
LCD_ON_CTRL = 0x08 # DB3: turn lcd/cursor on
LCD_ON_DISPLAY = 0x04 # DB2: turn display on
LCD_ON_CURSOR = 0x02 # DB1: turn cursor on
LCD_ON_BLINK = 0x01 # DB0: blinking cursor
LCD_MOVE = 0x10 # DB4: move cursor/display
LCD_MOVE_DISP = 0x08 # DB3: move display (0-> move cursor)
LCD_MOVE_RIGHT = 0x04 # DB2: move right (0-> left)
LCD_FUNCTION = 0x20 # DB5: function set
LCD_FUNCTION_8BIT = 0x10 # DB4: set 8BIT mode (0->4BIT mode)
LCD_FUNCTION_2LINES = 0x08 # DB3: two lines (0->one line)
LCD_FUNCTION_10DOTS = 0x04 # DB2: 5x10 font (0->5x7 font)
LCD_FUNCTION_RESET = 0x30 # See "Initializing by Instruction" section
LCD_CGRAM = 0x40 # DB6: set CG RAM address
LCD_DDRAM = 0x80 # DB7: set DD RAM address
LCD_RS_CMD = 0
LCD_RS_DATA = 1
LCD_RW_WRITE = 0
LCD_RW_READ = 1
def __init__(self, num_lines, num_columns):
self.num_lines = num_lines
if self.num_lines > 4:
self.num_lines = 4
self.num_columns = num_columns
if self.num_columns > 40:
self.num_columns = 40
self.cursor_x = 0
self.cursor_y = 0
self.implied_newline = False
self.backlight = True
self.display_off()
self.backlight_on()
self.clear()
self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC)
self.hide_cursor()
self.display_on()
def clear(self):
# Clears the LCD display and moves the cursor to the top left corner
self.hal_write_command(self.LCD_CLR)
self.hal_write_command(self.LCD_HOME)
self.cursor_x = 0
self.cursor_y = 0
def show_cursor(self):
# Causes the cursor to be made visible
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
self.LCD_ON_CURSOR)
def hide_cursor(self):
# Causes the cursor to be hidden
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
def blink_cursor_on(self):
# Turns on the cursor, and makes it blink
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
self.LCD_ON_CURSOR | self.LCD_ON_BLINK)
def blink_cursor_off(self):
# Turns on the cursor, and makes it no blink (i.e. be solid)
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
self.LCD_ON_CURSOR)
def display_on(self):
# Turns on (i.e. unblanks) the LCD
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)
def display_off(self):
# Turns off (i.e. blanks) the LCD
self.hal_write_command(self.LCD_ON_CTRL)
def backlight_on(self):
# Turns the backlight on.
# This isn't really an LCD command, but some modules have backlight
# controls, so this allows the hal to pass through the command.
self.backlight = True
self.hal_backlight_on()
def backlight_off(self):
# Turns the backlight off.
# This isn't really an LCD command, but some modules have backlight
# controls, so this allows the hal to pass through the command.
self.backlight = False
self.hal_backlight_off()
def move_to(self, cursor_x, cursor_y):
# Moves the cursor position to the indicated position. The cursor
# position is zero based (i.e. cursor_x == 0 indicates first column).
self.cursor_x = cursor_x
self.cursor_y = cursor_y
addr = cursor_x & 0x3f
if cursor_y & 1:
addr += 0x40 # Lines 1 & 3 add 0x40
if cursor_y & 2: # Lines 2 & 3 add number of columns
addr += self.num_columns
self.hal_write_command(self.LCD_DDRAM | addr)
def putchar(self, char):
# Writes the indicated character to the LCD at the current cursor
# position, and advances the cursor by one position.
if char == '\n':
if self.implied_newline:
# self.implied_newline means we advanced due to a wraparound,
# so if we get a newline right after that we ignore it.
pass
else:
self.cursor_x = self.num_columns
else:
self.hal_write_data(ord(char))
self.cursor_x += 1
if self.cursor_x >= self.num_columns:
self.cursor_x = 0
self.cursor_y += 1
self.implied_newline = (char != '\n')
if self.cursor_y >= self.num_lines:
self.cursor_y = 0
self.move_to(self.cursor_x, self.cursor_y)
def putstr(self, string):
# Write the indicated string to the LCD at the current cursor
# position and advances the cursor position appropriately.
for char in string:
self.putchar(char)
def custom_char(self, location, charmap):
# Write a character to one of the 8 CGRAM locations, available
# as chr(0) through chr(7).
location &= 0x7
self.hal_write_command(self.LCD_CGRAM | (location << 3))
self.hal_sleep_us(40)
for i in range(8):
self.hal_write_data(charmap[i])
self.hal_sleep_us(40)
self.move_to(self.cursor_x, self.cursor_y)
def hal_backlight_on(self):
# Allows the hal layer to turn the backlight on.
# If desired, a derived HAL class will implement this function.
pass
def hal_backlight_off(self):
# Allows the hal layer to turn the backlight off.
# If desired, a derived HAL class will implement this function.
pass
def hal_write_command(self, cmd):
# Write a command to the LCD.
# It is expected that a derived HAL class will implement this function.
raise NotImplementedError
def hal_write_data(self, data):
# Write data to the LCD.
# It is expected that a derived HAL class will implement this function.
raise NotImplementedError
def hal_sleep_us(self, usecs):
# Sleep for some time (given in microseconds)
time.sleep_us(usecs)
class I2C_LCD(LCD_API):
#Implements a HD44780 character LCD connected via PCF8574 on I2C
def __init__(self, i2c, i2c_addr, num_lines, num_columns):
self.i2c = i2c
self.i2c_addr = i2c_addr
self.i2c.writeto(self.i2c_addr, bytes([0]))
utime.sleep_ms(20) # Allow LCD time to powerup
# Send reset 3 times
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(5) # Need to delay at least 4.1 msec
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(1)
self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
utime.sleep_ms(1)
# Put LCD into 4-bit mode
self.hal_write_init_nibble(self.LCD_FUNCTION)
utime.sleep_ms(1)
LCD_API.__init__(self, num_lines, num_columns)
cmd = self.LCD_FUNCTION
if num_lines > 1:
cmd |= self.LCD_FUNCTION_2LINES
self.hal_write_command(cmd)
gc.collect()
def hal_write_init_nibble(self, nibble):
# Writes an initialization nibble to the LCD.
# This particular function is only used during initialization.
byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
gc.collect()
def hal_backlight_on(self):
# Allows the hal layer to turn the backlight on
self.i2c.writeto(self.i2c_addr, bytes([1 << SHIFT_BACKLIGHT]))
gc.collect()
def hal_backlight_off(self):
#Allows the hal layer to turn the backlight off
self.i2c.writeto(self.i2c_addr, bytes([0]))
gc.collect()
def hal_write_command(self, cmd):
# Write a command to the LCD. Data is latched on the falling edge of E.
byte = ((self.backlight << SHIFT_BACKLIGHT) |
(((cmd >> 4) & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
byte = ((self.backlight << SHIFT_BACKLIGHT) |
((cmd & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
if cmd <= 3:
# The home and clear commands require a worst case delay of 4.1 msec
utime.sleep_ms(5)
gc.collect()
def hal_write_data(self, data):
# Write data to the LCD. Data is latched on the falling edge of E.
byte = (MASK_RS |
(self.backlight << SHIFT_BACKLIGHT) |
(((data >> 4) & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
byte = (MASK_RS |
(self.backlight << SHIFT_BACKLIGHT) |
((data & 0x0f) << SHIFT_DATA))
self.i2c.writeto(self.i2c_addr, bytes([byte | MASK_E]))
self.i2c.writeto(self.i2c_addr, bytes([byte]))
gc.collect()

View File

@ -0,0 +1,93 @@
"""
ProgramChooser - a program launcher for microPython (adapted to the UV Belichter needs)
Copyright (C) 2024 Benjamin Burkhardt <bluefox@privacynerd.de>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import utils, config
from PCF8574T import I2C_LCD
import time
class ProgramChooser:
def __init__(self, programs, debug=False, run_directly=False):
self.lcd = config.LCD
self.ok_btn = config.BTN_1
self.next_btn = config.BTN_2
self.lcd.move_to(0,0)
self.lcd.putstr("[ PROGRAMS ]< >")
self.current_selection = None # no selection
self.programs = programs # a dictionary of programs and it's callbacks e.g. {"lora_test": some_callback}
self.show_selection()
if run_directly: self.run()
def log(self, msg, is_debug=False):
if is_debug:
utils.log(2, f"[ProgramChooser] {msg}")
else:
utils.log(1, f"[ProgramChooser] {msg}")
def show_selection(self):
self.lcd.move_to(1,1)
if len(self.programs) == 0:
self.lcd.putstr(" No programs!")
return True
if self.current_selection == None: # set it initially
self.current_selection = 0
# the actual displaying process
to_show = list(self.programs.keys())[self.current_selection]
if len(to_show) > 14:
to_show = to_show[:13] + chr(0)
else:
to_show = to_show[:14]
to_show = to_show.center(14)
self.lcd.putstr(to_show)
return True
def run(self):
while True:
if self.next_btn.value() == 1:
former_program_name = list(self.programs.keys())[self.current_selection]
self.current_selection = (self.current_selection+1)%len(list(self.programs.keys()))
self.show_selection()
now_program_name = list(self.programs.keys())[self.current_selection]
self.log(f"Selected next program (\"{former_program_name}\" -> \"{now_program_name}\")")
while self.next_btn.value() == 1: time.sleep(0.01) # wait till release
if self.ok_btn.value() == 1:
program_name = list(self.programs.keys())[self.current_selection]
self.log(f"Running selected program! (\"{program_name}\")")
# shorten the name for displaying (if too long)
if len(program_name) > 14:
program_name = program_name[:13] + chr(0)
else:
program_name = program_name[:14]
program_name = program_name.center(14)
self.lcd.move_to(0,0)
self.lcd.putstr(f" {program_name} Executing... ")
self.execute_selection()
while self.ok_btn.value() == 1: time.sleep(0.01) # wait till release (e.g. if the "program" is a simple send action)
self.lcd.putstr(f" {program_name} Closing... ")
time.sleep(1)
self.lcd.move_to(0,0)
self.lcd.putstr("[ PROGRAMS ]< >")
self.show_selection()
time.sleep(0.01)
def execute_selection(self): # execute the current selected program's callback
self.programs[list(self.programs.keys())[self.current_selection]]()

70
main.py Normal file
View File

@ -0,0 +1,70 @@
"""
uv-belichter-software - The main program run at the Pi Picos startup
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import config, utils
from lib.ProgramChooserAdapted import ProgramChooser
from time import sleep
import gc # garbage collector for better memory performance
# extra functions to access the garbage collector
def manual():
config.LCD.clear()
set_value = config.RELAIS.value()
config.LCD.putstr(f"---- MANUAL ---- State: {set_value} ")
while True:
if set_value != config.SWITCH.value():
config.RELAIS.value(config.SWITCH.value())
set_value = config.RELAIS.value()
config.LCD.putstr(f"---- MANUAL ---- State: {set_value} ")
if config.BTN_1.value() == 1 or config.BTN_2.value() == 1:
return # exit on press of these buttons
def timer():
# display WIP
config.LCD.clear()
config.LCD.putstr(" Still work-in-progress")
sleep(3)
def uv_on():
config.RELAIS.value(1)
config.LCD.clear()
config.LCD.putstr(" UV turned on ")
sleep(1)
def uv_off():
config.RELAIS.value(0)
config.LCD.clear()
config.LCD.putstr("------ UV ------ turned off ")
sleep(1)
def lcd_big_hello():
import lcd_big_hello
lcd_big_hello.run()
gc.collect()
def settings():
# display WIP
config.LCD.clear()
config.LCD.putstr(" Still work-in-progress")
sleep(3)
# create a programs dict, where the items are callables (functions)
programs = {
"Settings": settings,
"LCD Demo": lcd_big_hello,
"UV off": uv_off,
"UV on": uv_on,
"Timer": timer,
"Manual": manual,
}
if config.STARTUP_WELCOME_SHOW: utils.show_welcome()
pc = ProgramChooser(programs) # initialize the ProgramChooser
pc.run() # and run it (will be an endless loop)

63
utils.py Normal file
View File

@ -0,0 +1,63 @@
"""
uv-belichter-software - Some utilities for better customization and modularization
Copyright (C) 2024 Benjamin Burkhardt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import config as cfg
from time import sleep
"""
Very simple logging function
Overall log level can be specified in config.py
"""
def log(log_level: int, message: str):
log_mapping = {0: "WARN", 1: "INFO", 2: "DEBUG"}
log_level = int(log_level) # make sure log_level is an integer
if log_level not in [0, 1, 2]:
print(f"[LOGGER] Got a message of unknown log level ({log_level}). Original message is printed below.")
print(f"{message}")
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
print(f"[{log_mapping[log_level]}] {message}")
"""
Simple function that displays a startup "welcome" screen
Configurable in config.py
"""
def show_welcome(): # cycles says how often the startup text goes through
cycles = cfg.STARTUP_WELCOME_CYCLES
if cycles < 1:
cycles = 1
padding = " "*cfg.LCD_I2C_NUM_COLS
started_str = cfg.STARTUP_MESSAGE_FINISHED
starting_str = cfg.STARTUP_MESSAGE_STARTING
# slide the first line over the display (animated from right to left)
for i in range(cycles):
line1 = padding + starting_str + padding
line2 = cfg.STARTUP_PROJECT_NAME
for i in range(cfg.LCD_I2C_NUM_COLS + len(starting_str)):
cfg.LCD.putstr(line1[0:16])
cfg.LCD.move_to(0,1)
cfg.LCD.putstr(line2[0:16])
line1 = line1[1:]
cfg.LCD.move_to(0,0)
cfg.LCD.putstr(started_str)
# now fade down
sleep(2)
cfg.LCD.move_to(0,0)
cfg.LCD.putstr(padding + started_str)
sleep(0.1)
cfg.LCD.clear()