Skip to content

Commit 04f8140

Browse files
authored
Merge pull request #17 from CedarGroveStudios/main
Add brightness getter/setter.
2 parents 12fe157 + a14018f commit 04f8140

File tree

4 files changed

+99
-33
lines changed

4 files changed

+99
-33
lines changed

adafruit_neotrellis/multitrellis.py

100644100755
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44

55
"""
6-
Interface for connecting together multiple NeoTrellis boards.
7-
"""
6+
``adafruit_multitrellis``
7+
====================================================
8+
9+
A CircuitPython driver class for interfacing clusters of 4x4 NeoTrellis with
10+
elastomer buttons and NeoPixel RGB LEDs.
11+
12+
* Author(s): Dean Miller, JG for CedarGroveMakerStudios
13+
14+
Implementation Notes
15+
--------------------
16+
17+
**Hardware:**
18+
19+
* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954
20+
<https://www.adafruit.com/product/3954>'
821
9-
# imports
22+
**Software and Dependencies:**
23+
24+
* Adafruit CircuitPython firmware for the supported boards:
25+
https://github.com/adafruit/circuitpython/releases
26+
27+
* Adafruit Seesaw CircuitPython library
28+
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
29+
"""
1030

1131
__version__ = "0.0.0-auto.0"
1232
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
1333

34+
1435
from time import sleep
1536
from micropython import const
1637
from adafruit_seesaw.keypad import KeyEvent
@@ -78,3 +99,17 @@ def sync(self):
7899
y = int(evt.number / 4) + _n * 4
79100
x = int(evt.number % 4) + _m * 4
80101
_t.callbacks[evt.number](x, y, evt.edge)
102+
103+
@property
104+
def brightness(self):
105+
"""The NeoPixel brightness level of all clustered NeoTrellis boards."""
106+
return self._brightness
107+
108+
@brightness.setter
109+
def brightness(self, new_brightness):
110+
"""Select a NeoPixel brightness level for all all clustered boards. A
111+
valid brightness value is in the range of 0.0 to 1.0."""
112+
self._brightness = new_brightness
113+
for _r in range(self._rows):
114+
for _c in range(self._cols):
115+
self._trelli[_r][_c].brightness = self._brightness

adafruit_neotrellis/neotrellis.py

100644100755
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44

55
"""
66
``adafruit_neotrellis``
77
====================================================
88
9-
4x4 elastomer buttons and RGB LEDs
9+
A CircuitPython driver class for the 4x4 NeoTrellis with elastomer buttons and
10+
NeoPixel RGB LEDs.
1011
11-
* Author(s): Dean Miller
12+
* Author(s): Dean Miller, JG for CedarGroveMakerStudios
1213
1314
Implementation Notes
1415
--------------------
1516
1617
**Hardware:**
1718
19+
* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954
20+
<https://www.adafruit.com/product/3954>'
21+
1822
**Software and Dependencies:**
1923
2024
* Adafruit CircuitPython firmware for the supported boards:
@@ -24,8 +28,6 @@
2428
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
2529
"""
2630

27-
# imports
28-
2931
__version__ = "0.0.0-auto.0"
3032
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
3133

@@ -53,15 +55,28 @@ def _seesaw_key(xval):
5355
return int(int(xval / 8) * 4 + (xval % 8))
5456

5557

58+
# pylint: disable=too-many-arguments
5659
class NeoTrellis(Keypad):
57-
"""Driver for the Adafruit NeoTrellis."""
58-
59-
def __init__(self, i2c_bus, interrupt=False, addr=_NEO_TRELLIS_ADDR, drdy=None):
60+
"""Driver for the Adafruit 4x4 NeoTrellis."""
61+
62+
def __init__(
63+
self,
64+
i2c_bus,
65+
interrupt=False,
66+
addr=_NEO_TRELLIS_ADDR,
67+
drdy=None,
68+
brightness=1.0,
69+
):
6070
super().__init__(i2c_bus, addr, drdy)
6171
self.interrupt_enabled = interrupt
72+
self._brightness = brightness
6273
self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
6374
self.pixels = NeoPixel(
64-
self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS, pixel_order=GRB
75+
self,
76+
_NEO_TRELLIS_NEOPIX_PIN,
77+
_NEO_TRELLIS_NUM_KEYS,
78+
brightness=self._brightness,
79+
pixel_order=GRB,
6580
)
6681

6782
def activate_key(self, key, edge, enable=True):
@@ -87,3 +102,15 @@ def sync(self):
87102
and self.callbacks[evt.number] is not None
88103
):
89104
self.callbacks[evt.number](evt)
105+
106+
@property
107+
def brightness(self):
108+
"""The NeoPixel brightness level of the board."""
109+
return self._brightness
110+
111+
@brightness.setter
112+
def brightness(self, new_brightness):
113+
"""Select a NeoPixel brightness level for the board. A valid brightness
114+
value is in the range of 0.0 to 1.0."""
115+
self._brightness = new_brightness
116+
self.pixels.brightness = self._brightness
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

44
import time
5-
6-
from board import SCL, SDA
7-
import busio
5+
import board
86
from adafruit_neotrellis.neotrellis import NeoTrellis
97
from adafruit_neotrellis.multitrellis import MultiTrellis
108

11-
# create the i2c object for the trellis
12-
i2c_bus = busio.I2C(SCL, SDA)
9+
# Create the I2C object for the NeoTrellis
10+
i2c_bus = board.I2C()
1311

14-
"""create the trellis. This is for a 2x2 array of NeoTrellis boards
15-
for a 2x1 array (2 boards connected left to right) you would use:
12+
# Create the NeoTrellis object
13+
"""
14+
# This is for a 2x1 array (2 boards connected left to right):
1615
1716
trelli = [
18-
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)]
17+
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
1918
]
2019
2120
"""
21+
# This is for a 2x2 array of NeoTrellis boards:
2222
trelli = [
2323
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
2424
[NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31)],
2525
]
2626

2727
trellis = MultiTrellis(trelli)
2828

29+
# Set the brightness value (0 to 1.0)
30+
trellis.brightness = 0.5
31+
2932
# some color definitions
3033
OFF = (0, 0, 0)
3134
RED = (255, 0, 0)
@@ -35,21 +38,21 @@
3538
BLUE = (0, 0, 255)
3639
PURPLE = (180, 0, 255)
3740

38-
# this will be called when button events are received
41+
# This will be called when button events are received
3942
def blink(xcoord, ycoord, edge):
40-
# turn the LED on when a rising edge is detected
43+
# Turn the LED on when a rising edge is detected
4144
if edge == NeoTrellis.EDGE_RISING:
4245
trellis.color(xcoord, ycoord, BLUE)
43-
# turn the LED off when a falling edge is detected
46+
# Turn the LED off when a falling edge is detected
4447
elif edge == NeoTrellis.EDGE_FALLING:
4548
trellis.color(xcoord, ycoord, OFF)
4649

4750

4851
for y in range(8):
4952
for x in range(8):
50-
# activate rising edge events on all keys
53+
# Activate rising edge events on all keys
5154
trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
52-
# activate falling edge events on all keys
55+
# Activate falling edge events on all keys
5356
trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING)
5457
trellis.set_callback(x, y, blink)
5558
trellis.color(x, y, PURPLE)
@@ -61,6 +64,6 @@ def blink(xcoord, ycoord, edge):
6164
time.sleep(0.05)
6265

6366
while True:
64-
# the trellis can only be read every 17 millisecons or so
67+
# The NeoTrellis can only be read every 17 milliseconds or so
6568
trellis.sync()
6669
time.sleep(0.02)

examples/neotrellis_simpletest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

44
import time
5-
6-
from board import SCL, SDA
7-
import busio
5+
import board
86
from adafruit_neotrellis.neotrellis import NeoTrellis
97

108
# create the i2c object for the trellis
11-
i2c_bus = busio.I2C(SCL, SDA)
9+
i2c_bus = board.I2C()
1210

1311
# create the trellis
1412
trellis = NeoTrellis(i2c_bus)
1513

14+
# Set the brightness value (0 to 1.0)
15+
trellis.brightness = 0.5
16+
1617
# some color definitions
1718
OFF = (0, 0, 0)
1819
RED = (255, 0, 0)

0 commit comments

Comments
 (0)