Skip to content

Commit b7822a7

Browse files
add brightness property; update examples
1 parent 9d4862e commit b7822a7

File tree

4 files changed

+213
-188
lines changed

4 files changed

+213
-188
lines changed

adafruit_neotrellis/multitrellis.py

100644100755
Lines changed: 91 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,91 @@
1-
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
2-
#
3-
# SPDX-License-Identifier: MIT
4-
5-
"""
6-
Interface for connecting together multiple NeoTrellis boards.
7-
"""
8-
9-
# imports
10-
11-
__version__ = "0.0.0-auto.0"
12-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
13-
14-
from time import sleep
15-
from micropython import const
16-
from adafruit_seesaw.keypad import KeyEvent
17-
18-
_NEO_TRELLIS_NUM_KEYS = const(16)
19-
20-
21-
def _key(xval):
22-
return int(int(xval / 4) * 8 + (xval % 4))
23-
24-
25-
def _seesaw_key(xval):
26-
return int(int(xval / 8) * 4 + (xval % 8))
27-
28-
29-
class MultiTrellis:
30-
"""Driver for multiple connected Adafruit NeoTrellis boards."""
31-
32-
def __init__(self, neotrellis_array):
33-
self._trelli = neotrellis_array
34-
self._rows = len(neotrellis_array)
35-
self._cols = len(neotrellis_array[0])
36-
37-
def activate_key(self, x, y, edge, enable=True):
38-
"""Activate or deactivate a key on the trellis. x and y are the index
39-
of the key measured from the top lefthand corner. Edge specifies what
40-
edge to register an event on and can be NeoTrellis.EDGE_FALLING or
41-
NeoTrellis.EDGE_RISING. enable should be set to True if the event is
42-
to be enabled, or False if the event is to be disabled."""
43-
xkey = x % 4
44-
ykey = int(int(y % 4) * 4 / 4)
45-
self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable)
46-
47-
def set_callback(self, x, y, function):
48-
"""Set a callback function for when an event for the key at index x, y
49-
(measured from the top lefthand corner) is detected."""
50-
xkey = x % 4
51-
ykey = int(int(y % 4) * 4 / 4)
52-
self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function
53-
54-
def color(self, x, y, color):
55-
"""Set the color of the pixel at index x, y measured from the top
56-
lefthand corner of the matrix"""
57-
xkey = x % 4
58-
ykey = int(int(y % 4) * 4 / 4)
59-
self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color
60-
61-
def sync(self):
62-
"""Read all trellis boards in the matrix and call any callbacks"""
63-
for _n in range(self._rows):
64-
for _m in range(self._cols):
65-
66-
_t = self._trelli[_n][_m]
67-
available = _t.count
68-
sleep(0.0005)
69-
if available > 0:
70-
available = available + 2
71-
buf = _t.read_keypad(available)
72-
for raw in buf:
73-
evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3)
74-
if (
75-
evt.number < _NEO_TRELLIS_NUM_KEYS
76-
and _t.callbacks[evt.number] is not None
77-
):
78-
y = int(evt.number / 4) + _n * 4
79-
x = int(evt.number % 4) + _m * 4
80-
_t.callbacks[evt.number](x, y, evt.edge)
1+
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Interface for connecting together multiple NeoTrellis boards.
7+
"""
8+
9+
# imports
10+
11+
__version__ = "1.1.8"
12+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
13+
14+
from time import sleep
15+
from micropython import const
16+
from adafruit_seesaw.keypad import KeyEvent
17+
18+
_NEO_TRELLIS_NUM_KEYS = const(16)
19+
20+
21+
def _key(xval):
22+
return int(int(xval / 4) * 8 + (xval % 4))
23+
24+
25+
def _seesaw_key(xval):
26+
return int(int(xval / 8) * 4 + (xval % 8))
27+
28+
29+
class MultiTrellis:
30+
"""Driver for multiple connected Adafruit NeoTrellis boards."""
31+
32+
def __init__(self, neotrellis_array):
33+
self._trelli = neotrellis_array
34+
self._rows = len(neotrellis_array)
35+
self._cols = len(neotrellis_array[0])
36+
37+
def activate_key(self, x, y, edge, enable=True):
38+
"""Activate or deactivate a key on the trellis. x and y are the index
39+
of the key measured from the top lefthand corner. Edge specifies what
40+
edge to register an event on and can be NeoTrellis.EDGE_FALLING or
41+
NeoTrellis.EDGE_RISING. enable should be set to True if the event is
42+
to be enabled, or False if the event is to be disabled."""
43+
xkey = x % 4
44+
ykey = int(int(y % 4) * 4 / 4)
45+
self._trelli[int(y / 4)][int(x / 4)].activate_key(ykey * 4 + xkey, edge, enable)
46+
47+
def set_callback(self, x, y, function):
48+
"""Set a callback function for when an event for the key at index x, y
49+
(measured from the top lefthand corner) is detected."""
50+
xkey = x % 4
51+
ykey = int(int(y % 4) * 4 / 4)
52+
self._trelli[int(y / 4)][int(x / 4)].callbacks[ykey * 4 + xkey] = function
53+
54+
def color(self, x, y, color):
55+
"""Set the color of the pixel at index x, y measured from the top
56+
lefthand corner of the matrix"""
57+
xkey = x % 4
58+
ykey = int(int(y % 4) * 4 / 4)
59+
self._trelli[int(y / 4)][int(x / 4)].pixels[ykey * 4 + xkey] = color
60+
61+
def sync(self):
62+
"""Read all trellis boards in the matrix and call any callbacks"""
63+
for _n in range(self._rows):
64+
for _m in range(self._cols):
65+
66+
_t = self._trelli[_n][_m]
67+
available = _t.count
68+
sleep(0.0005)
69+
if available > 0:
70+
available = available + 2
71+
buf = _t.read_keypad(available)
72+
for raw in buf:
73+
evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3)
74+
if (
75+
evt.number < _NEO_TRELLIS_NUM_KEYS
76+
and _t.callbacks[evt.number] is not None
77+
):
78+
y = int(evt.number / 4) + _n * 4
79+
x = int(evt.number % 4) + _m * 4
80+
_t.callbacks[evt.number](x, y, evt.edge)
81+
82+
@property
83+
def brightness(self):
84+
return self.brightness
85+
86+
@brightness.setter
87+
def brightness(self, new_brightness):
88+
self._brightness = new_brightness
89+
for _r in range(self._rows):
90+
for _c in range (self._cols):
91+
self._trelli[_r][_c].brightness = self._brightness

adafruit_neotrellis/neotrellis.py

100644100755
Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,97 @@
1-
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
2-
#
3-
# SPDX-License-Identifier: MIT
4-
5-
"""
6-
``adafruit_neotrellis``
7-
====================================================
8-
9-
4x4 elastomer buttons and RGB LEDs
10-
11-
* Author(s): Dean Miller
12-
13-
Implementation Notes
14-
--------------------
15-
16-
**Hardware:**
17-
18-
**Software and Dependencies:**
19-
20-
* Adafruit CircuitPython firmware for the supported boards:
21-
https://github.com/adafruit/circuitpython/releases
22-
23-
* Adafruit Seesaw CircuitPython library
24-
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
25-
"""
26-
27-
# imports
28-
29-
__version__ = "0.0.0-auto.0"
30-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
31-
32-
from time import sleep
33-
from micropython import const
34-
from adafruit_seesaw.keypad import Keypad, KeyEvent
35-
from adafruit_seesaw.neopixel import NeoPixel
36-
37-
_NEO_TRELLIS_ADDR = const(0x2E)
38-
39-
_NEO_TRELLIS_NEOPIX_PIN = const(3)
40-
41-
_NEO_TRELLIS_NUM_ROWS = const(4)
42-
_NEO_TRELLIS_NUM_COLS = const(4)
43-
_NEO_TRELLIS_NUM_KEYS = const(16)
44-
45-
_NEO_TRELLIS_MAX_CALLBACKS = const(32)
46-
47-
48-
def _key(xval):
49-
return int(int(xval / 4) * 8 + (xval % 4))
50-
51-
52-
def _seesaw_key(xval):
53-
return int(int(xval / 8) * 4 + (xval % 8))
54-
55-
56-
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-
super().__init__(i2c_bus, addr, drdy)
61-
self.interrupt_enabled = interrupt
62-
self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
63-
self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS)
64-
65-
def activate_key(self, key, edge, enable=True):
66-
"""Activate or deactivate a key on the trellis. Key is the key number from
67-
0 to 16. Edge specifies what edge to register an event on and can be
68-
NeoTrellis.EDGE_FALLING or NeoTrellis.EDGE_RISING. enable should be set
69-
to True if the event is to be enabled, or False if the event is to be
70-
disabled."""
71-
self.set_event(_key(key), edge, enable)
72-
73-
def sync(self):
74-
"""read any events from the Trellis hardware and call associated
75-
callbacks"""
76-
available = self.count
77-
sleep(0.0005)
78-
if available > 0:
79-
available = available + 2
80-
buf = self.read_keypad(available)
81-
for raw in buf:
82-
evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3)
83-
if (
84-
evt.number < _NEO_TRELLIS_NUM_KEYS
85-
and self.callbacks[evt.number] is not None
86-
):
87-
self.callbacks[evt.number](evt)
1+
# SPDX-FileCopyrightText: 2018 Dean Miller for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
``adafruit_neotrellis``
7+
====================================================
8+
9+
4x4 elastomer buttons and RGB LEDs
10+
11+
* Author(s): Dean Miller, JG for CedarGroveMakerStudios
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Hardware:**
17+
18+
**Software and Dependencies:**
19+
20+
* Adafruit CircuitPython firmware for the supported boards:
21+
https://github.com/adafruit/circuitpython/releases
22+
23+
* Adafruit Seesaw CircuitPython library
24+
https://github.com/adafruit/Adafruit_CircuitPython_seesaw/releases
25+
"""
26+
27+
# imports
28+
29+
__version__ = "1.1.8"
30+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_neotrellis.git"
31+
32+
from time import sleep
33+
from micropython import const
34+
from adafruit_seesaw.keypad import Keypad, KeyEvent
35+
from adafruit_seesaw.neopixel import NeoPixel
36+
37+
_NEO_TRELLIS_ADDR = const(0x2E)
38+
39+
_NEO_TRELLIS_NEOPIX_PIN = const(3)
40+
41+
_NEO_TRELLIS_NUM_ROWS = const(4)
42+
_NEO_TRELLIS_NUM_COLS = const(4)
43+
_NEO_TRELLIS_NUM_KEYS = const(16)
44+
45+
_NEO_TRELLIS_MAX_CALLBACKS = const(32)
46+
47+
48+
def _key(xval):
49+
return int(int(xval / 4) * 8 + (xval % 4))
50+
51+
52+
def _seesaw_key(xval):
53+
return int(int(xval / 8) * 4 + (xval % 8))
54+
55+
56+
class NeoTrellis(Keypad):
57+
"""Driver for the Adafruit NeoTrellis."""
58+
59+
def __init__(self, i2c_bus, interrupt=False, addr=_NEO_TRELLIS_ADDR, drdy=None, brightness=1.0):
60+
super().__init__(i2c_bus, addr, drdy)
61+
self.interrupt_enabled = interrupt
62+
self._brightness = brightness
63+
self.callbacks = [None] * _NEO_TRELLIS_NUM_KEYS
64+
self.pixels = NeoPixel(self, _NEO_TRELLIS_NEOPIX_PIN, _NEO_TRELLIS_NUM_KEYS, brightness=self._brightness)
65+
66+
def activate_key(self, key, edge, enable=True):
67+
"""Activate or deactivate a key on the trellis. Key is the key number from
68+
0 to 16. Edge specifies what edge to register an event on and can be
69+
NeoTrellis.EDGE_FALLING or NeoTrellis.EDGE_RISING. enable should be set
70+
to True if the event is to be enabled, or False if the event is to be
71+
disabled."""
72+
self.set_event(_key(key), edge, enable)
73+
74+
def sync(self):
75+
"""read any events from the Trellis hardware and call associated
76+
callbacks"""
77+
available = self.count
78+
sleep(0.0005)
79+
if available > 0:
80+
available = available + 2
81+
buf = self.read_keypad(available)
82+
for raw in buf:
83+
evt = KeyEvent(_seesaw_key((raw >> 2) & 0x3F), raw & 0x3)
84+
if (
85+
evt.number < _NEO_TRELLIS_NUM_KEYS
86+
and self.callbacks[evt.number] is not None
87+
):
88+
self.callbacks[evt.number](evt)
89+
90+
@property
91+
def brightness(self):
92+
return self._brightness
93+
94+
@brightness.setter
95+
def brightness(self, new_brightness):
96+
self._brightness = new_brightness
97+
self.pixels.brightness = self._brightness

0 commit comments

Comments
 (0)