Skip to content

Commit

Permalink
Refactor component structure (#12)
Browse files Browse the repository at this point in the history
* Refactor matrix switch component into nested component

* Refactor matrix number component into nested component

* Move library loading to display component

* Rename main component to hub75_matrix_display
  • Loading branch information
TillFleisch authored Jan 31, 2024
1 parent b088bbd commit dd1bade
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 41 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ A minimum working example for setting up the display. A more complex configurati
```yaml
display:
- platform: matrix_display
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
Expand Down Expand Up @@ -87,20 +87,21 @@ The additional settings are used to set the configuration variables for the wrap
- **i2sspeed**(**Optional**): I2SSpeed used for configuring the display. Select one of `HZ_8M`, `HZ_10M`, `HZ_15M`, `HZ_20M`.
- **latch_blanking**(**Optional**, int): Latch blanking value used for configuring the display.
- **clock_phase**(**Optional**, boolean): Clock phase value used for configuring the display.
- **use_custom_library**(**Optional**, boolean): If set to `true` a custom library must be defined using `platformio_options:lib_deps`. Defaults to `false`. See [this example](custom_library.yaml) for more details.

- All other options from [Display](https://esphome.io/components/display/index.html)

Note that the default pin configurations are the ones mentioned in the [ESP32-HUB75-MatrixPanel-DMA](https://github.com/mrfaptastic/ESP32-HUB75-MatrixPanel-DMA) library. Some of these pins are used as strapping pins on ESPs. It is recommended to not use these.
My panel and the ESP do not work unless I change the R2, G2 and B2 pins.

## Matrix Display Switch
## Switch

This switch can be used to turn the display on or off. In it's off state the display is showing a blank screen.

- **matrix_id**(**Required**, string): The matrix display entity to which this power switch belongs.
- All other options from [Switch](https://esphome.io/components/switch/index.html#config-switch)

## Matrix Display Brightness
## Brightness

This number entity can be used to set the display brightness. In combination with a brightness sensor this can used to adaptively change matrix displays brightness.

Expand Down
2 changes: 1 addition & 1 deletion complex_matrix_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
display:
- platform: matrix_display
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
CONF_WIDTH,
)

AUTO_LOAD = ["switch", "matrix_display_switch", "number", "matrix_display_brightness"]
MATRIX_ID = "matrix_id"
CHAIN_LENGTH = "chain_length"
BRIGHTNESS = "brightness"
Expand All @@ -37,6 +36,8 @@
LATCH_BLANKING = "latch_blanking"
CLOCK_PHASE = "clock_phase"

USE_CUSTOM_LIBRARY = "use_custom_library"

matrix_display_ns = cg.esphome_ns.namespace("matrix_display")
MatrixDisplay = matrix_display_ns.class_(
"MatrixDisplay", cg.PollingComponent, display.DisplayBuffer
Expand Down Expand Up @@ -65,6 +66,7 @@
cv.GenerateID(): cv.declare_id(MatrixDisplay),
cv.Required(CONF_WIDTH): cv.positive_int,
cv.Required(CONF_HEIGHT): cv.positive_int,
cv.Optional(USE_CUSTOM_LIBRARY, default=False): cv.boolean,
cv.Optional(CHAIN_LENGTH, default=1): cv.positive_int,
cv.Optional(BRIGHTNESS, default=128): cv.int_range(min=0, max=255),
cv.Optional(
Expand Down Expand Up @@ -93,6 +95,16 @@


async def to_code(config):
if not config[USE_CUSTOM_LIBRARY]:
cg.add_library("SPI", None)
cg.add_library("Wire", None)
cg.add_library("Adafruit BusIO", None)
cg.add_library("adafruit/Adafruit GFX Library", None)
cg.add_library(
"https://github.com/TillFleisch/ESP32-HUB75-MatrixPanel-DMA#optional_logging",
None,
)

var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_panel_width(config[CONF_WIDTH]))
cg.add(var.set_panel_height(config[CONF_HEIGHT]))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from esphome.components import number
from esphome.const import CONF_MODE

from ..matrix_display.display import MATRIX_ID, MatrixDisplay

AUTO_LOAD = ["number"]
from ..display import MATRIX_ID, MatrixDisplay

matrix_display_brightness_ns = cg.esphome_ns.namespace(
"matrix_display::matrix_display_brightness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "esphome/core/component.h"
#include "esphome/components/number/number.h"
#include "../matrix_display/matrix_display.h"
#include "../matrix_display.h"

namespace esphome::matrix_display::matrix_display_brightness
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from esphome.components import switch
from esphome.const import CONF_ID

from ..matrix_display.display import MATRIX_ID, MatrixDisplay

AUTO_LOAD = ["switch"]
from ..display import MATRIX_ID, MatrixDisplay

matrix_display_switch_ns = cg.esphome_ns.namespace(
"matrix_display::matrix_display_switch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "esphome/core/component.h"
#include "esphome/components/switch/switch.h"
#include "../matrix_display/matrix_display.h"
#include "../matrix_display.h"

namespace esphome::matrix_display::matrix_display_switch
{
Expand Down
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions custom_library.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
esphome:
name: matrix-display
platformio_options:
lib_deps:
- SPI
- Wire
- Adafruit BusIO
- adafruit/Adafruit GFX Library
- https://github.com/TillFleisch/ESP32-HUB75-MatrixPanel-DMA#optional_logging

display:
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
use_custom_library: true
13 changes: 3 additions & 10 deletions example.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
esphome:
name: matrix-display
friendly_name: Matrix Display
platformio_options:
lib_deps:
- SPI
- Wire
- Adafruit BusIO
- adafruit/Adafruit GFX Library
- https://github.com/TillFleisch/ESP32-HUB75-MatrixPanel-DMA#optional_logging

external_components:
- source: github://TillFleisch/ESPHome-HUB75-MatrixDisplayWrapper@main
Expand All @@ -24,7 +17,7 @@ font:
size: 10

display:
- platform: matrix_display
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
Expand All @@ -33,12 +26,12 @@ display:
it.print(0, 0, id(roboto), "Hello World!");
switch:
- platform: matrix_display_switch
- platform: hub75_matrix_display
matrix_id: matrix
name: "Power"
id: power

number:
- platform: matrix_display_brightness
- platform: hub75_matrix_display
matrix_id: matrix
name: "Brightness"
9 changes: 1 addition & 8 deletions tests/base.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
esphome:
name: matrix-display
platformio_options:
lib_deps:
- SPI
- Wire
- Adafruit BusIO
- adafruit/Adafruit GFX Library
- https://github.com/TillFleisch/ESP32-HUB75-MatrixPanel-DMA#optional_logging

esp32:
board: esp32dev
Expand All @@ -27,7 +20,7 @@ uart:
stop_bits: 2

display:
- platform: matrix_display
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
13 changes: 3 additions & 10 deletions tests/full.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
esphome:
name: matrix-display
platformio_options:
lib_deps:
- SPI
- Wire
- Adafruit BusIO
- adafruit/Adafruit GFX Library
- https://github.com/TillFleisch/ESP32-HUB75-MatrixPanel-DMA#optional_logging

esp32:
board: esp32dev
Expand All @@ -27,7 +20,7 @@ uart:
stop_bits: 2

display:
- platform: matrix_display
- platform: hub75_matrix_display
id: matrix
width: 64
height: 32
Expand All @@ -41,14 +34,14 @@ display:
update_interval: 8 ms

switch:
- platform: matrix_display_switch
- platform: hub75_matrix_display
matrix_id: matrix
name: "Power"
id: power
restore_mode: RESTORE_DEFAULT_OFF

number:
- platform: matrix_display_brightness
- platform: hub75_matrix_display
id: brightness
matrix_id: matrix
name: "Brightness"

0 comments on commit dd1bade

Please sign in to comment.