|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries LLC |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`adafruit_featherwing.matrix_featherwing` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Helper for using the `Adafruit 8x16 LED Matrix FeatherWing |
| 27 | +<https://www.adafruit.com/product/3155>`_. |
| 28 | +
|
| 29 | +* Author(s): Melissa LeBlanc-Williams |
| 30 | +""" |
| 31 | + |
| 32 | +__version__ = "0.0.0-auto.0" |
| 33 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" |
| 34 | + |
| 35 | +import adafruit_ht16k33.matrix as matrix |
| 36 | +from adafruit_featherwing import shared |
| 37 | + |
| 38 | +class MatrixFeatherWing: |
| 39 | + """Class representing an `Adafruit 8x16 LED Matrix FeatherWing |
| 40 | + <https://www.adafruit.com/product/3155>`_. |
| 41 | +
|
| 42 | + Automatically uses the feather's I2C bus.""" |
| 43 | + def __init__(self, address=0x70): |
| 44 | + self._matrix = matrix.Matrix16x8(shared.I2C_BUS, address) |
| 45 | + self._matrix.auto_write = False |
| 46 | + self.columns = 16 |
| 47 | + self.rows = 8 |
| 48 | + self._auto_write = True |
| 49 | + |
| 50 | + def __getitem__(self, key): |
| 51 | + """ |
| 52 | + Get the current value of a pixel |
| 53 | + """ |
| 54 | + x, y = key |
| 55 | + return self.pixel(x, y) |
| 56 | + |
| 57 | + def __setitem__(self, key, value): |
| 58 | + """ |
| 59 | + Turn a pixel off or on |
| 60 | + """ |
| 61 | + x, y = key |
| 62 | + self.pixel(x, y, value) |
| 63 | + self._update() |
| 64 | + |
| 65 | + def _update(self): |
| 66 | + """ |
| 67 | + Update the Display automatically if auto_write is set to True |
| 68 | + """ |
| 69 | + if self._auto_write: |
| 70 | + self._matrix.show() |
| 71 | + |
| 72 | + def pixel(self, x, y, color=None): |
| 73 | + """ |
| 74 | + Turn a pixel on or off or retrieve a pixel value |
| 75 | +
|
| 76 | + :param int x: The pixel row |
| 77 | + :param int y: The pixel column |
| 78 | + :param color: Whether to turn the pixel on or off |
| 79 | + :type color: int or bool |
| 80 | + """ |
| 81 | + value = self._matrix.pixel(x, y, color) |
| 82 | + self._update() |
| 83 | + return value |
| 84 | + |
| 85 | + def show(self): |
| 86 | + """ |
| 87 | + Update the Pixels. This is only needed if auto_write is set to False |
| 88 | + This can be very useful for more advanced graphics effects. |
| 89 | + """ |
| 90 | + self._matrix.show() |
| 91 | + |
| 92 | + def fill(self, fill): |
| 93 | + """ |
| 94 | + Turn all pixels on or off |
| 95 | +
|
| 96 | + :param bool fill: True turns all pixels on, False turns all pixels off |
| 97 | +
|
| 98 | + """ |
| 99 | + if isinstance(fill, bool): |
| 100 | + self._matrix.fill(1 if fill else 0) |
| 101 | + self._update() |
| 102 | + else: |
| 103 | + raise ValueError('Must set to either True or False.') |
| 104 | + |
| 105 | + def shift_right(self, rotate=False): |
| 106 | + """ |
| 107 | + Shift all pixels right |
| 108 | +
|
| 109 | + :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) |
| 110 | + """ |
| 111 | + for y in range(0, self.rows): |
| 112 | + last_pixel = self._matrix[self.columns - 1, y] if rotate else 0 |
| 113 | + for x in range(self.columns - 1, 0, -1): |
| 114 | + self._matrix[x, y] = self._matrix[x - 1, y] |
| 115 | + self._matrix[0, y] = last_pixel |
| 116 | + self._update() |
| 117 | + |
| 118 | + def shift_left(self, rotate=False): |
| 119 | + """ |
| 120 | + Shift all pixels left |
| 121 | +
|
| 122 | + :param rotate: (Optional) Rotate the shifted pixels to the right side (default=False) |
| 123 | + """ |
| 124 | + for y in range(0, self.rows): |
| 125 | + last_pixel = self._matrix[0, y] if rotate else 0 |
| 126 | + for x in range(0, self.columns - 1): |
| 127 | + self._matrix[x, y] = self._matrix[x + 1, y] |
| 128 | + self._matrix[self.columns - 1, y] = last_pixel |
| 129 | + self._update() |
| 130 | + |
| 131 | + def shift_up(self, rotate=False): |
| 132 | + """ |
| 133 | + Shift all pixels up |
| 134 | +
|
| 135 | + :param rotate: (Optional) Rotate the shifted pixels to bottom (default=False) |
| 136 | + """ |
| 137 | + for x in range(0, self.columns): |
| 138 | + last_pixel = self._matrix[x, self.rows - 1] if rotate else 0 |
| 139 | + for y in range(self.rows - 1, 0, -1): |
| 140 | + self._matrix[x, y] = self._matrix[x, y - 1] |
| 141 | + self._matrix[x, 0] = last_pixel |
| 142 | + self._update() |
| 143 | + |
| 144 | + def shift_down(self, rotate=False): |
| 145 | + """ |
| 146 | + Shift all pixels down |
| 147 | +
|
| 148 | + :param rotate: (Optional) Rotate the shifted pixels to top (default=False) |
| 149 | + """ |
| 150 | + for x in range(0, self.columns): |
| 151 | + last_pixel = self._matrix[x, 0] if rotate else 0 |
| 152 | + for y in range(0, self.rows - 1): |
| 153 | + self._matrix[x, y] = self._matrix[x, y + 1] |
| 154 | + self._matrix[x, self.rows - 1] = last_pixel |
| 155 | + self._update() |
| 156 | + |
| 157 | + @property |
| 158 | + def auto_write(self): |
| 159 | + """ |
| 160 | + Whether or not we are automatically updating |
| 161 | + If set to false, be sure to call show() to update |
| 162 | + """ |
| 163 | + return self._auto_write |
| 164 | + |
| 165 | + @auto_write.setter |
| 166 | + def auto_write(self, write): |
| 167 | + if isinstance(write, bool): |
| 168 | + self._auto_write = write |
| 169 | + |
| 170 | + @property |
| 171 | + def blink_rate(self): |
| 172 | + """ |
| 173 | + Blink Rate returns the current rate that the pixels blink. |
| 174 | + 0 = Not Blinking |
| 175 | + 1-3 = Successively slower blink rates |
| 176 | + """ |
| 177 | + return self._matrix.blink_rate |
| 178 | + |
| 179 | + @blink_rate.setter |
| 180 | + def blink_rate(self, rate): |
| 181 | + self._matrix.blink_rate = rate |
| 182 | + |
| 183 | + @property |
| 184 | + def brightness(self): |
| 185 | + """ |
| 186 | + Brightness returns the current display brightness. |
| 187 | + 0-15 = Dimmest to Brightest Setting |
| 188 | + """ |
| 189 | + return self._matrix.brightness |
| 190 | + |
| 191 | + @brightness.setter |
| 192 | + def brightness(self, brightness): |
| 193 | + self._matrix.brightness = brightness |
0 commit comments