73
73
0x13 , 0x12 , 0x11 , 0x31 )
74
74
# pylint: enable=bad-whitespace, invalid-name
75
75
76
+ class TrellisLEDs ():
77
+ def __init__ (self , trellis_obj ):
78
+ self ._parent = trellis_obj
79
+
80
+ def __getitem__ (self , x ):
81
+ if 0 < x >= self ._parent ._num_leds :
82
+ raise ValueError (('LED number must be between 0 -' , self ._parent ._num_leds - 1 ))
83
+ led = ledLUT [x % 16 ] >> 4
84
+ mask = 1 << (ledLUT [x % 16 ] & 0x0f )
85
+ return bool (((self ._parent ._led_buffer [x // 16 ][led * 2 ] | \
86
+ self ._parent ._led_buffer [x // 16 ][(led * 2 ) + 1 ] << 8 ) & mask ) > 0 )
87
+
88
+ def __setitem__ (self , x , value ):
89
+ if 0 < x >= self ._parent ._num_leds :
90
+ raise ValueError (('LED number must be between 0 -' , self ._parent ._num_leds - 1 ))
91
+ led = ledLUT [x % 16 ] >> 4
92
+ mask = 1 << (ledLUT [x % 16 ] & 0x0f )
93
+ if value == True :
94
+ self ._parent ._led_buffer [x // 16 ][led * 2 ] |= mask
95
+ self ._parent ._led_buffer [x // 16 ][(led * 2 ) + 1 ] |= mask >> 8
96
+ elif value == False :
97
+ self ._parent ._led_buffer [x // 16 ][led * 2 ] &= ~ mask
98
+ self ._parent ._led_buffer [x // 16 ][(led * 2 ) + 1 ] &= ~ mask >> 8
99
+ else :
100
+ raise ValueError ("LED value must be True or False" )
101
+
102
+ if self ._parent ._auto_show :
103
+ self ._parent .show ()
104
+
105
+ def fill (self , on ):
106
+ fill = 0xff if on else 0x00
107
+ for d in range (len (self ._parent ._i2c_devices )):
108
+ for i in range (16 ):
109
+ self ._parent ._led_buffer [d ][i ] = fill
110
+ if self ._parent ._auto_show :
111
+ self ._parent .show ()
112
+
76
113
class Trellis ():
77
114
"""
78
115
Driver base for a single Trellis Board
@@ -84,36 +121,11 @@ class Trellis():
84
121
Trellis product guide for using different/multiple I2C addresses.
85
122
https://learn.adafruit.com/adafruit-trellis-diy-open-source-led-keypad
86
123
87
- Example:
88
-
89
- .. code-block:: python
90
-
91
- import time
92
- import busio
93
- from board import SCL, SDA
94
- from adafruit_trellis import Trellis
95
-
96
- i2c = busio.I2C(SCL, SDA)
97
- trellis = Trellis(i2c) # single Trellis (0x70 default address)
98
- #trellis = Trellis(i2c, [0x70, 0x71]) # multiple Trellis w/address list
99
- print('Starting button sensory loop...')
100
- while True:
101
- try:
102
- just_pressed, released = trellis.read_buttons()
103
- for b in just_pressed:
104
- print('pressed:', b)
105
- trellis.led[b] = True
106
- pressed_buttons.update(just_pressed)
107
- for b in released:
108
- print('released:', b)
109
- trellis.led[b] = False
110
- pressed_buttons.difference_update(released)
111
- for b in pressed_buttons:
112
- print('still pressed:', b)
113
- trellis.led[b] = True
114
- except KeyboardInterrupt:
115
- break
116
- time.sleep(.1)
124
+
125
+ .. literalinclude:: ../examples/trellis_simpletest.py
126
+ :caption: Usage Example
127
+ :linenos:
128
+
117
129
"""
118
130
def __init__ (self , i2c , addresses = None ):
119
131
if addresses is None :
@@ -130,15 +142,16 @@ def __init__(self, i2c, addresses=None):
130
142
self ._blink_rate = None
131
143
self ._brightness = None
132
144
self ._auto_show = True
133
- self .led = self . _led_obj (self . _num_leds , self . _led_buffer , self . show , self . _auto_show )
145
+ self .led = TrellisLEDs (self )
134
146
"""
135
147
The LED object used to interact with Trellis LEDs.
136
148
137
149
- ``trellis.led[x]`` returns the current LED status of LED ``x`` (True/False)
138
150
- ``trellis.led[x] = True`` turns the LED at ``x`` on
139
151
- ``trellis.led[x] = False`` turns the LED at ``x`` off
152
+ - ``trellis.led.fill(bool)`` turns every LED on (True) or off (False)
140
153
"""
141
- self .fill (0 )
154
+ self .led . fill (False )
142
155
self ._write_cmd (_HT16K33_OSCILATOR_ON )
143
156
self .blink_rate = 0
144
157
self .brightness = 15
@@ -204,20 +217,6 @@ def auto_show(self, value):
204
217
raise ValueError ("Auto show value must be True or False" )
205
218
self ._auto_show = value
206
219
207
- def fill (self , color ):
208
- """
209
- Fill the whole board with the given color.
210
-
211
- :param int color: 0 == OFF, > 0 == ON
212
-
213
- """
214
- fill = 0xff if color else 0x00
215
- for buff in range (len (self ._i2c_devices )):
216
- for i in range (16 ):
217
- self ._led_buffer [buff ][i ] = fill
218
- if self ._auto_show :
219
- self .show ()
220
-
221
220
def read_buttons (self ):
222
221
"""
223
222
Read the button matrix register on the Trellis board(s). Returns two
@@ -242,60 +241,17 @@ def read_buttons(self):
242
241
return pressed , released
243
242
244
243
def _is_pressed (self , button ):
245
- if button > self ._num_leds :
246
- return None
247
244
mask = 1 << (buttonLUT [button % 16 ] & 0x0f )
248
245
return self ._buttons [button // 16 ][1 ][(buttonLUT [button % 16 ] >> 4 )] & mask
249
246
250
247
def _was_pressed (self , button ):
251
- if button > self ._num_leds :
252
- return None
253
248
mask = 1 << (buttonLUT [button % 16 ] & 0x0f )
254
249
return self ._buttons [button // 16 ][0 ][(buttonLUT [button % 16 ] >> 4 )] & mask
255
250
256
251
def _just_pressed (self , button ):
257
- if button > self ._num_leds :
258
- raise ValueError ('Button must be between 0 &' , self ._num_leds )
259
252
# pylint: disable=invalid-unary-operand-type
260
253
return self ._is_pressed (button ) & ~ self ._was_pressed (button )
261
- # pylint: enable=invalid-unary-operand-type
262
254
263
255
def _just_released (self , button ):
264
- if button > self ._num_leds :
265
- raise ValueError ('Button must be between 0 &' , self ._num_leds )
266
256
# pylint: disable=invalid-unary-operand-type
267
257
return ~ self ._is_pressed (button ) & self ._was_pressed (button )
268
- # pylint: enable=invalid-unary-operand-type
269
-
270
- # pylint: disable=protected-access, too-few-public-methods
271
- class _led_obj ():
272
- def __init__ (self , num_leds , led_buffer , show , auto_show ):
273
- self ._parent_num_leds = num_leds
274
- self ._parent_led_buffer = led_buffer
275
- self ._parent_auto_show = auto_show
276
- self ._parent_show = show
277
-
278
- def __getitem__ (self , x ):
279
- if 0 < x > self ._parent_num_leds :
280
- raise ValueError ("LED number must be between 0 -" , self ._parent_num_leds )
281
- led = ledLUT [x % 16 ] >> 4
282
- mask = 1 << (ledLUT [x % 16 ] & 0x0f )
283
- return bool (((self ._parent_led_buffer [x // 16 ][led * 2 ] | \
284
- self ._parent_led_buffer [x // 16 ][(led * 2 ) + 1 ] << 8 ) & mask ) > 0 )
285
-
286
- def __setitem__ (self , x , value ):
287
- if 0 < x > self ._parent_num_leds :
288
- raise ValueError ("LED number must be between 0 -" , self ._parent_num_leds )
289
- led = ledLUT [x % 16 ] >> 4
290
- mask = 1 << (ledLUT [x % 16 ] & 0x0f )
291
- if value :
292
- self ._parent_led_buffer [x // 16 ][led * 2 ] |= mask
293
- self ._parent_led_buffer [x // 16 ][(led * 2 ) + 1 ] |= mask >> 8
294
- elif not value :
295
- self ._parent_led_buffer [x // 16 ][led * 2 ] &= ~ mask
296
- self ._parent_led_buffer [x // 16 ][(led * 2 ) + 1 ] &= ~ mask >> 8
297
- else :
298
- raise ValueError ("LED value must be True or False" )
299
-
300
- if self ._parent_auto_show :
301
- self ._parent_show ()
0 commit comments