Skip to content

Commit

Permalink
Replace custom driver with RGBMatrixDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
ty-porter committed May 20, 2024
1 parent e8f2829 commit 289ed72
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 242 deletions.
2 changes: 1 addition & 1 deletion data/config/color.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics


class Color:
Expand Down
2 changes: 1 addition & 1 deletion data/config/layout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics

import os.path

Expand Down
50 changes: 0 additions & 50 deletions driver/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions driver/mode.py

This file was deleted.

18 changes: 8 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion renderers/games/game.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion renderers/games/irregular.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion renderers/games/nohitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics
import data.config.layout as cfglayout
import debug

Expand Down
2 changes: 1 addition & 1 deletion renderers/games/postgame.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion renderers/games/pregame.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion renderers/games/teams.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics

ABSOLUTE = "absolute"
RELATIVE = "relative"
Expand Down
2 changes: 1 addition & 1 deletion renderers/network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics
from utils import center_text_position

NETWORK_ERROR_TEXT = "!"
Expand Down
2 changes: 1 addition & 1 deletion renderers/offday.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics

import time

Expand Down
2 changes: 1 addition & 1 deletion renderers/scrollingtext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics

from utils import center_text_position

Expand Down
2 changes: 1 addition & 1 deletion renderers/standings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from driver import graphics
from RGBMatrixDriver import graphics

from data.config.color import Color
from data.config.layout import Layout
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
170 changes: 6 additions & 164 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
from collections.abc import Mapping

from RGBMatrixDriver import RGBMatrixArguments

import debug


Expand All @@ -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.
Expand Down

0 comments on commit 289ed72

Please sign in to comment.