diff --git a/adafruit_trellism4.py b/adafruit_trellism4.py index 6394e6b..9a65899 100644 --- a/adafruit_trellism4.py +++ b/adafruit_trellism4.py @@ -68,6 +68,8 @@ def __init__(self, pin, *, width, height, rotation=0): def __setitem__(self, index, value): if not isinstance(index, tuple) or len(index) != 2: raise IndexError("Index must be tuple") + if index[0] >= self.width or index[1] >= self.height: + raise IndexError("Pixel assignment outside available coordinates.") if self._rotation == 0 or self._rotation == 180: offset = self.width * index[1] + index[0] @@ -78,6 +80,9 @@ def __setitem__(self, index, value): elif self._rotation == 90: offset = self.height * (self.width - index[0] - 1) + index[1] + if offset < 0: + raise IndexError("Pixel assignment outside available coordinates.") + self._neopixel[offset] = value self._neopixel.show() @@ -267,9 +272,9 @@ def __init__(self, rotation=0): row = [] for x in range(4): if rotation == 0: - coord = (x, y) + coord = (y, x) elif rotation == 180: - coord = (3 - x, 7 - y) + coord = (7 - y, 3 - x) elif rotation == 90: coord = (3 - x, y) elif rotation == 270: diff --git a/examples/neopixel_trellism4.simpletest.py b/examples/neopixel_trellism4.simpletest.py index f9219e8..1381a47 100755 --- a/examples/neopixel_trellism4.simpletest.py +++ b/examples/neopixel_trellism4.simpletest.py @@ -30,7 +30,7 @@ def wheel(pos): for press in pressed - current_press: if press: print("Pressed:", press) - pixel = (press[0] * 8) + press[1] + pixel = (press[1] * 8) + press[0] pixel_index = (pixel * 256 // 32) trellis.pixels.fill(wheel(pixel_index & 255)) for release in current_press - pressed: