Skip to content

Add brightness getter/setter. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions adafruit_neotrellis/multitrellis.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Interface for connecting together multiple NeoTrellis boards.
"""
``adafruit_multitrellis``
====================================================

A CircuitPython driver class for interfacing clusters of 4x4 NeoTrellis with
elastomer buttons and NeoPixel RGB LEDs.

* Author(s): Dean Miller, JG for CedarGroveMakerStudios

Implementation Notes
--------------------

**Hardware:**

* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954
<https://www.adafruit.com/product/3954>'

# imports
**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases

* Adafruit Seesaw CircuitPython library
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
"""

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


from time import sleep
from micropython import const
from adafruit_seesaw.keypad import KeyEvent
Expand Down Expand Up @@ -78,3 +99,17 @@ def sync(self):
y = int(evt.number / 4) + _n * 4
x = int(evt.number % 4) + _m * 4
_t.callbacks[evt.number](x, y, evt.edge)

@property
def brightness(self):
"""The NeoPixel brightness level of all clustered NeoTrellis boards."""
return self._brightness

@brightness.setter
def brightness(self, new_brightness):
"""Select a NeoPixel brightness level for all all clustered boards. A
valid brightness value is in the range of 0.0 to 1.0."""
self._brightness = new_brightness
for _r in range(self._rows):
for _c in range(self._cols):
self._trelli[_r][_c].brightness = self._brightness
46 changes: 37 additions & 9 deletions adafruit_neotrellis/neotrellis.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
# SPDX-FileCopyrightText: 2021 Dean Miller for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
``adafruit_neotrellis``
====================================================

4x4 elastomer buttons and RGB LEDs
A CircuitPython driver class for the 4x4 NeoTrellis with elastomer buttons and
NeoPixel RGB LEDs.

* Author(s): Dean Miller
* Author(s): Dean Miller, JG for CedarGroveMakerStudios

Implementation Notes
--------------------

**Hardware:**

* 'NeoTrellis RGB Driver PCB for 4x4 Keypad, PID: 3954
<https://www.adafruit.com/product/3954>'

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
Expand All @@ -24,8 +28,6 @@
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
"""

# imports

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

Expand Down Expand Up @@ -53,14 +55,28 @@ def _seesaw_key(xval):
return int(int(xval / 8) * 4 + (xval % 8))


# pylint: disable=too-many-arguments
class NeoTrellis(Keypad):
"""Driver for the Adafruit NeoTrellis."""

def __init__(self, i2c_bus, interrupt=False, addr=_NEO_TRELLIS_ADDR, drdy=None):
"""Driver for the Adafruit 4x4 NeoTrellis."""

def __init__(
self,
i2c_bus,
interrupt=False,
addr=_NEO_TRELLIS_ADDR,
drdy=None,
brightness=1.0,
):
super().__init__(i2c_bus, addr, drdy)
self.interrupt_enabled = interrupt
self._brightness = brightness
self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS)
self.pixels = NeoPixel(
self,
_NEO_TRELLIS_NEOPIX_PIN,
_NEO_TRELLIS_NUM_KEYS,
brightness=self._brightness,
)

def activate_key(self, key, edge, enable=True):
"""Activate or deactivate a key on the trellis. Key is the key number from
Expand All @@ -85,3 +101,15 @@ def sync(self):
and self.callbacks[evt.number] is not None
):
self.callbacks[evt.number](evt)

@property
def brightness(self):
"""The NeoPixel brightness level of the board."""
return self._brightness

@brightness.setter
def brightness(self, new_brightness):
"""Select a NeoPixel brightness level for the board. A valid brightness
value is in the range of 0.0 to 1.0."""
self._brightness = new_brightness
self.pixels.brightness = self._brightness
33 changes: 18 additions & 15 deletions examples/neotrellis_multitrellis_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

from board import SCL, SDA
import busio
import board
from adafruit_neotrellis.neotrellis import NeoTrellis
from adafruit_neotrellis.multitrellis import MultiTrellis

# create the i2c object for the trellis
i2c_bus = busio.I2C(SCL, SDA)
# Create the I2C object for the NeoTrellis
i2c_bus = board.I2C()

"""create the trellis. This is for a 2x2 array of NeoTrellis boards
for a 2x1 array (2 boards connected left to right) you would use:
# Create the NeoTrellis object
"""
# This is for a 2x1 array (2 boards connected left to right):

trelli = [
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)]
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
]

"""
# This is for a 2x2 array of NeoTrellis boards:
trelli = [
[NeoTrellis(i2c_bus, False, addr=0x2E), NeoTrellis(i2c_bus, False, addr=0x2F)],
[NeoTrellis(i2c_bus, False, addr=0x30), NeoTrellis(i2c_bus, False, addr=0x31)],
]

trellis = MultiTrellis(trelli)

# Set the brightness value (0 to 1.0)
trellis.brightness = 0.5

# some color definitions
OFF = (0, 0, 0)
RED = (255, 0, 0)
Expand All @@ -35,21 +38,21 @@
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

# this will be called when button events are received
# This will be called when button events are received
def blink(xcoord, ycoord, edge):
# turn the LED on when a rising edge is detected
# Turn the LED on when a rising edge is detected
if edge == NeoTrellis.EDGE_RISING:
trellis.color(xcoord, ycoord, BLUE)
# turn the LED off when a falling edge is detected
# Turn the LED off when a falling edge is detected
elif edge == NeoTrellis.EDGE_FALLING:
trellis.color(xcoord, ycoord, OFF)


for y in range(8):
for x in range(8):
# activate rising edge events on all keys
# Activate rising edge events on all keys
trellis.activate_key(x, y, NeoTrellis.EDGE_RISING)
# activate falling edge events on all keys
# Activate falling edge events on all keys
trellis.activate_key(x, y, NeoTrellis.EDGE_FALLING)
trellis.set_callback(x, y, blink)
trellis.color(x, y, PURPLE)
Expand All @@ -61,6 +64,6 @@ def blink(xcoord, ycoord, edge):
time.sleep(0.05)

while True:
# the trellis can only be read every 17 millisecons or so
# The NeoTrellis can only be read every 17 milliseconds or so
trellis.sync()
time.sleep(0.02)
11 changes: 6 additions & 5 deletions examples/neotrellis_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

from board import SCL, SDA
import busio
import board
from adafruit_neotrellis.neotrellis import NeoTrellis

# create the i2c object for the trellis
i2c_bus = busio.I2C(SCL, SDA)
i2c_bus = board.I2C()

# create the trellis
trellis = NeoTrellis(i2c_bus)

# Set the brightness value (0 to 1.0)
trellis.brightness = 0.5

# some color definitions
OFF = (0, 0, 0)
RED = (255, 0, 0)
Expand Down