Skip to content

Commit e0996c4

Browse files
author
Melissa LeBlanc-Williams
authored
Merge pull request #18 from bbaraban/pixel_getter
Add pixel getter for TrellisM4Express
2 parents 08c9703 + 3e8ada1 commit e0996c4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

adafruit_trellism4.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ def __setitem__(self, index, value):
6969
if index[0] >= self.width or index[1] >= self.height:
7070
raise IndexError("Pixel assignment outside available coordinates.")
7171

72+
offset = self._calculate_pixel_offset(index)
73+
74+
self._neopixel[offset] = value
75+
76+
def __getitem__(self, index):
77+
if not isinstance(index, tuple) or len(index) != 2:
78+
raise IndexError("Index must be tuple")
79+
if index[0] >= self.width or index[1] >= self.height:
80+
raise IndexError("Pixel outside available coordinates.")
81+
82+
offset = self._calculate_pixel_offset(index)
83+
84+
return self._neopixel[offset]
85+
86+
def _calculate_pixel_offset(self, index):
7287
if self._rotation == 0 or self._rotation == 180:
7388
offset = self.width * index[1] + index[0]
7489
if self._rotation == 180:
@@ -79,9 +94,9 @@ def __setitem__(self, index, value):
7994
offset = self.height * (self.width - index[0] - 1) + index[1]
8095

8196
if offset < 0:
82-
raise IndexError("Pixel assignment outside available coordinates.")
97+
raise IndexError("Pixel outside available coordinates.")
8398

84-
self._neopixel[offset] = value
99+
return offset
85100

86101
def show(self):
87102
"""

0 commit comments

Comments
 (0)