diff --git a/data/config/color.py b/data/config/color.py index 115167f5..b3a3dad1 100644 --- a/data/config/color.py +++ b/data/config/color.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics class Color: diff --git a/data/config/layout.py b/data/config/layout.py index 88fdef50..f7d1c69b 100644 --- a/data/config/layout.py +++ b/data/config/layout.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics import os.path diff --git a/driver/__init__.py b/driver/__init__.py deleted file mode 100644 index 5685e40d..00000000 --- a/driver/__init__.py +++ /dev/null @@ -1,50 +0,0 @@ -import sys - -from utils import args -from driver.mode import DriverMode - -class DriverWrapper: - def __init__(self): - self.hardware_load_failed = False - self.mode = None - - if args().emulated: - self.set_mode(DriverMode.SOFTWARE_EMULATION) - else: - self.set_mode(DriverMode.HARDWARE) - - - @property - def __name__(self): - return 'driver' - - def is_hardware(self): - return self.mode == DriverMode.HARDWARE - - def is_emulated(self): - return self.mode == DriverMode.SOFTWARE_EMULATION - - def set_mode(self, mode): - self.mode = mode - - if self.is_hardware(): - try: - import rgbmatrix - - self.driver = rgbmatrix - except ImportError: - import RGBMatrixEmulator - - self.mode = DriverMode.SOFTWARE_EMULATION - self.driver = RGBMatrixEmulator - self.hardware_load_failed = True - else: - import RGBMatrixEmulator - - self.driver = RGBMatrixEmulator - - def __getattr__(self, name): - return getattr(self.driver, name) - - -sys.modules['driver'] = DriverWrapper() diff --git a/driver/mode.py b/driver/mode.py deleted file mode 100644 index 5cfe61fc..00000000 --- a/driver/mode.py +++ /dev/null @@ -1,6 +0,0 @@ -from enum import Enum - - -class DriverMode(Enum): - HARDWARE = 0 - SOFTWARE_EMULATION = 1 diff --git a/main.py b/main.py index 563c3d8e..7d5285c2 100755 --- a/main.py +++ b/main.py @@ -23,10 +23,9 @@ from PIL import Image -# Important! Import the driver first to initialize it, then import submodules as needed. -import driver -from driver import RGBMatrix, __version__ -from utils import args, led_matrix_options +import RGBMatrixDriver +from RGBMatrixDriver import RGBMatrix, prefilled_matrix_options, __version__ +from utils import scoreboard_args from data import Data from data.config import Config @@ -47,8 +46,8 @@ def main(matrix, config_base): # Print some basic info on startup debug.info("%s - v%s (%sx%s)", SCRIPT_NAME, SCRIPT_VERSION, matrix.width, matrix.height) - if driver.is_emulated(): - if driver.hardware_load_failed: + if RGBMatrixDriver.is_emulated(): + if RGBMatrixDriver.hardware_load_failed: debug.log("rgbmatrix not installed, falling back to emulator!") debug.log("Using RGBMatrixEmulator version %s", __version__) @@ -60,7 +59,7 @@ def main(matrix, config_base): # MLB image disabled when using renderer, for now. # see: https://github.com/ty-porter/RGBMatrixEmulator/issues/9#issuecomment-922869679 - if os.path.exists(logo_path) and driver.is_hardware(): + if os.path.exists(logo_path) and RGBMatrixDriver.is_hardware(): logo = Image.open(logo_path) matrix.SetImage(logo.convert("RGB")) logo.close() @@ -158,11 +157,10 @@ def __render_main(matrix, data): if __name__ == "__main__": # Check for led configuration arguments - command_line_args = args() - matrixOptions = led_matrix_options(command_line_args) + command_line_args = scoreboard_args() # Initialize the matrix - matrix = RGBMatrix(options=matrixOptions) + matrix = RGBMatrix(options=prefilled_matrix_options(command_line_args)) try: config, _ = os.path.splitext(command_line_args.config) main(matrix, config) diff --git a/renderers/games/game.py b/renderers/games/game.py index 5c2e1960..183cecc3 100644 --- a/renderers/games/game.py +++ b/renderers/games/game.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from data.config.color import Color from data.config.layout import Layout from data.scoreboard import Scoreboard diff --git a/renderers/games/irregular.py b/renderers/games/irregular.py index a3543a2e..22d3df72 100644 --- a/renderers/games/irregular.py +++ b/renderers/games/irregular.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from data import status from data.config.color import Color from data.config.layout import Layout diff --git a/renderers/games/nohitter.py b/renderers/games/nohitter.py index f7e88387..acd99ede 100644 --- a/renderers/games/nohitter.py +++ b/renderers/games/nohitter.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics import data.config.layout as cfglayout import debug diff --git a/renderers/games/postgame.py b/renderers/games/postgame.py index cf11fc0d..0c66e086 100644 --- a/renderers/games/postgame.py +++ b/renderers/games/postgame.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from data.config.color import Color from data.config.layout import Layout from data.scoreboard import Scoreboard diff --git a/renderers/games/pregame.py b/renderers/games/pregame.py index b7e3abab..70248f45 100644 --- a/renderers/games/pregame.py +++ b/renderers/games/pregame.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from data.config.color import Color from data.config.layout import Layout from data.scoreboard.pregame import Pregame diff --git a/renderers/games/teams.py b/renderers/games/teams.py index 572e306d..1f3390b9 100644 --- a/renderers/games/teams.py +++ b/renderers/games/teams.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics ABSOLUTE = "absolute" RELATIVE = "relative" diff --git a/renderers/network.py b/renderers/network.py index cfea419e..bd7ec1e8 100755 --- a/renderers/network.py +++ b/renderers/network.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from utils import center_text_position NETWORK_ERROR_TEXT = "!" diff --git a/renderers/offday.py b/renderers/offday.py index 338b70c3..690e1916 100644 --- a/renderers/offday.py +++ b/renderers/offday.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics import time diff --git a/renderers/scrollingtext.py b/renderers/scrollingtext.py index 7b74e0fb..429d67d5 100644 --- a/renderers/scrollingtext.py +++ b/renderers/scrollingtext.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from utils import center_text_position diff --git a/renderers/standings.py b/renderers/standings.py index c440c88a..7a337843 100644 --- a/renderers/standings.py +++ b/renderers/standings.py @@ -1,4 +1,4 @@ -from driver import graphics +from RGBMatrixDriver import graphics from data.config.color import Color from data.config.layout import Layout diff --git a/requirements.txt b/requirements.txt index 3f95f664..43270cc5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ feedparser==6.0.10 MLB_StatsAPI>=1.6.1 Pillow>=10.0.1 pyowm==3.3.0 +RGBMatrixDriver RGBMatrixEmulator>=0.8.4 tzlocal==4.2 diff --git a/utils.py b/utils.py index f4624f3a..8f75af2e 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,8 @@ import argparse from collections.abc import Mapping +from RGBMatrixDriver import RGBMatrixArguments + import debug @@ -11,177 +13,17 @@ def center_text_position(text, center_pos, font_width): def split_string(string, num_chars): return [(string[i : i + num_chars]).strip() for i in range(0, len(string), num_chars)] # noqa: E203 - -def args(): - parser = argparse.ArgumentParser() - - # Options for the rpi-rgb-led-matrix library - parser.add_argument( - "--led-rows", - action="store", - help="Display rows. 16 for 16x32, 32 for 32x32. (Default: 32)", - default=32, - type=int, - ) - parser.add_argument( - "--led-cols", action="store", help="Panel columns. Typically 32 or 64. (Default: 32)", default=32, type=int - ) - parser.add_argument("--led-chain", action="store", help="Daisy-chained boards. (Default: 1)", default=1, type=int) - parser.add_argument( - "--led-parallel", - action="store", - help="For Plus-models or RPi2: parallel chains. 1..3. (Default: 1)", - default=1, - type=int, - ) - parser.add_argument( - "--led-pwm-bits", action="store", help="Bits used for PWM. Range 1..11. (Default: 11)", default=11, type=int - ) - parser.add_argument( - "--led-brightness", - action="store", - help="Sets brightness level. Range: 1..100. (Default: 100)", - default=100, - type=int, - ) - parser.add_argument( - "--led-gpio-mapping", - help="Hardware Mapping: regular, adafruit-hat, adafruit-hat-pwm", - choices=["regular", "adafruit-hat", "adafruit-hat-pwm"], - type=str, - ) - parser.add_argument( - "--led-scan-mode", - action="store", - help="Progressive or interlaced scan. 0 = Progressive, 1 = Interlaced. (Default: 1)", - default=1, - choices=range(2), - type=int, - ) - parser.add_argument( - "--led-pwm-lsb-nanoseconds", - action="store", - help="Base time-unit for the on-time in the lowest significant bit in nanoseconds. (Default: 130)", - default=130, - type=int, - ) - parser.add_argument( - "--led-show-refresh", action="store_true", help="Shows the current refresh rate of the LED panel." - ) - parser.add_argument( - "--led-slowdown-gpio", - action="store", - help="Slow down writing to GPIO. Range: 0..4. (Default: 1)", - choices=range(5), - type=int, - ) - parser.add_argument("--led-no-hardware-pulse", action="store", help="Don't use hardware pin-pulse generation.") - parser.add_argument( - "--led-rgb-sequence", - action="store", - help="Switch if your matrix has led colors swapped. (Default: RGB)", - default="RGB", - type=str, - ) - parser.add_argument( - "--led-pixel-mapper", action="store", help='Apply pixel mappers. e.g "Rotate:90"', default="", type=str - ) - parser.add_argument( - "--led-row-addr-type", - action="store", - help="0 = default; 1 = AB-addressed panels; 2 = direct row select; 3 = ABC-addressed panels. (Default: 0)", - default=0, - type=int, - choices=[0, 1, 2, 3], - ) - parser.add_argument( - "--led-multiplexing", - action="store", - help="Multiplexing type: 0 = direct; 1 = strip; 2 = checker; 3 = spiral; 4 = Z-strip; 5 = ZnMirrorZStripe;" - "6 = coreman; 7 = Kaler2Scan; 8 = ZStripeUneven. (Default: 0)", - default=0, - type=int, - ) - parser.add_argument( - "--led-limit-refresh", - action="store", - help="Limit refresh rate to this frequency in Hz. Useful to keep a constant refresh rate on loaded system. " - "0=no limit. Default: 0", - default=0, - type=int, - ) - parser.add_argument( - "--led-pwm-dither-bits", action="store", help="Time dithering of lower bits (Default: 0)", default=0, type=int, - ) - parser.add_argument( +def scoreboard_args(): + sb_parser = argparse.ArgumentParser(add_help=False) + sb_parser.add_argument( "--config", action="store", help="Base file name for config file. Can use relative path, e.g. config/rockies.config", default="config", type=str, ) - parser.add_argument( - "--emulated", - action="store_const", - help="Force using emulator mode over default matrix display.", - const=True - ) - parser.add_argument( - "--drop-privileges", action="store_true", help="Force the matrix driver to drop root privileges after setup." - ) - return parser.parse_args() - - -def led_matrix_options(args): - from driver import RGBMatrixOptions - - options = RGBMatrixOptions() - - if args.led_gpio_mapping is not None: - options.hardware_mapping = args.led_gpio_mapping - - options.rows = args.led_rows - options.cols = args.led_cols - options.chain_length = args.led_chain - options.parallel = args.led_parallel - options.row_address_type = args.led_row_addr_type - options.multiplexing = args.led_multiplexing - options.pwm_bits = args.led_pwm_bits - options.brightness = args.led_brightness - options.scan_mode = args.led_scan_mode - options.pwm_lsb_nanoseconds = args.led_pwm_lsb_nanoseconds - options.led_rgb_sequence = args.led_rgb_sequence - options.drop_privileges = args.drop_privileges - - try: - options.pixel_mapper_config = args.led_pixel_mapper - except AttributeError: - debug.warning("Your compiled RGB Matrix Library is out of date.") - debug.warning("The --led-pixel-mapper argument will not work until it is updated.") - - try: - options.pwm_dither_bits = args.led_pwm_dither_bits - except AttributeError: - debug.warning("Your compiled RGB Matrix Library is out of date.") - debug.warning("The --led-pwm-dither-bits argument will not work until it is updated.") - - try: - options.limit_refresh_rate_hz = args.led_limit_refresh - except AttributeError: - debug.warning("Your compiled RGB Matrix Library is out of date.") - debug.warning("The --led-limit-refresh argument will not work until it is updated.") - - if args.led_show_refresh: - options.show_refresh_rate = 1 - - if args.led_slowdown_gpio is not None: - options.gpio_slowdown = args.led_slowdown_gpio - - if args.led_no_hardware_pulse: - options.disable_hardware_pulsing = True - - return options + return RGBMatrixArguments(parents=[sb_parser]).parse_args() def deep_update(source, overrides): """Update a nested dictionary or similar mapping.