|
| 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.pixelmatrix_featherwing` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Base Class for the `NeoPixel FeatherWing <https://www.adafruit.com/product/2945>` and |
| 27 | +`DotStar FeatherWing <https://www.adafruit.com/product/3449>`_. |
| 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 | +#pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation |
| 36 | + |
| 37 | +class PixelMatrixFeatherWing: |
| 38 | + """Base Class for DotStar and NeoPixel FeatherWings |
| 39 | +
|
| 40 | + The feather uses pins D13 and D11""" |
| 41 | + def __init__(self): |
| 42 | + self.rows = 0 |
| 43 | + self.columns = 0 |
| 44 | + self._matrix = None |
| 45 | + self._auto_write = True |
| 46 | + |
| 47 | + def __setitem__(self, indices, value): |
| 48 | + """ |
| 49 | + indices can be one of three things: |
| 50 | + x and y ints that are calculated to the DotStar index |
| 51 | + a slice of DotStar indexes with a set of values that match the slice |
| 52 | + a single int that specifies the DotStar index |
| 53 | + value can be one of three things: |
| 54 | + a (r,g,b) list/tuple |
| 55 | + a (r,g,b, brightness) list/tuple |
| 56 | + a single, longer int that contains RGB values, like 0xFFFFFF |
| 57 | + brightness, if specified should be a float 0-1 |
| 58 | + """ |
| 59 | + self._matrix[self._get_index(indices)] = value |
| 60 | + self._update() |
| 61 | + |
| 62 | + def __getitem__(self, indices): |
| 63 | + """ |
| 64 | + indices can be one of three things: |
| 65 | + x and y ints that are calculated to the DotStar index |
| 66 | + a slice of DotStar indexes to retrieve |
| 67 | + a single int that specifies the DotStar index |
| 68 | + """ |
| 69 | + return self._matrix[self._get_index(indices)] |
| 70 | + |
| 71 | + def _get_index(self, indices): |
| 72 | + """ |
| 73 | + Figure out which DotStar to address based on what was passed in |
| 74 | + """ |
| 75 | + if isinstance(indices, int): |
| 76 | + if not 0 <= indices < self.rows * self.columns: |
| 77 | + raise ValueError('The index of {} is out of range'.format(indices)) |
| 78 | + return indices |
| 79 | + elif isinstance(indices, slice): |
| 80 | + return indices |
| 81 | + elif len(indices) == 2: |
| 82 | + x, y = indices |
| 83 | + if not 0 <= x < self.columns: |
| 84 | + raise ValueError('The X value of {} is out of range'.format(x)) |
| 85 | + if not 0 <= y < self.rows: |
| 86 | + raise ValueError('The Y value of {} is out of range'.format(y)) |
| 87 | + return y * self.columns + x |
| 88 | + else: |
| 89 | + raise ValueError('Index must be 1 or 2 number') |
| 90 | + |
| 91 | + def _update(self): |
| 92 | + """ |
| 93 | + Update the Display automatically if auto_write is set to True |
| 94 | + """ |
| 95 | + if self._auto_write: |
| 96 | + self._matrix.show() |
| 97 | + |
| 98 | + def _fill(self, color=0): |
| 99 | + """ |
| 100 | + Fills all of the Pixels with a color or unlit if empty. |
| 101 | + """ |
| 102 | + self._matrix.fill(color) |
| 103 | + self._update() |
| 104 | + |
| 105 | + def _show(self): |
| 106 | + """ |
| 107 | + Update the Pixels. This is only needed if auto_write is set to False |
| 108 | + This can be very useful for more advanced graphics effects. |
| 109 | + """ |
| 110 | + self._matrix.show() |
| 111 | + |
| 112 | + def _shift_right(self, rotate=False): |
| 113 | + """ |
| 114 | + Shift all pixels right |
| 115 | + """ |
| 116 | + for y in range(0, self.rows): |
| 117 | + last_pixel = self._matrix[(y + 1) * self.columns - 1] if rotate else 0 |
| 118 | + for x in range(self.columns - 1, 0, -1): |
| 119 | + self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x - 1] |
| 120 | + self._matrix[y * self.columns] = last_pixel |
| 121 | + self._update() |
| 122 | + |
| 123 | + def _shift_left(self, rotate=False): |
| 124 | + """ |
| 125 | + Shift all pixels left |
| 126 | + """ |
| 127 | + for y in range(0, self.rows): |
| 128 | + last_pixel = self._matrix[y * self.columns] if rotate else 0 |
| 129 | + for x in range(0, self.columns - 1): |
| 130 | + self._matrix[y * self.columns + x] = self._matrix[y * self.columns + x + 1] |
| 131 | + self._matrix[(y + 1) * self.columns - 1] = last_pixel |
| 132 | + self._update() |
| 133 | + |
| 134 | + def _shift_up(self, rotate=False): |
| 135 | + """ |
| 136 | + Shift all pixels up |
| 137 | + """ |
| 138 | + for x in range(0, self.columns): |
| 139 | + last_pixel = self._matrix[(self.rows - 1) * self.columns + x] if rotate else 0 |
| 140 | + for y in range(self.rows - 1, 0, -1): |
| 141 | + self._matrix[y * self.columns + x] = self._matrix[(y - 1) * self.columns + x] |
| 142 | + self._matrix[x] = last_pixel |
| 143 | + self._update() |
| 144 | + |
| 145 | + def _shift_down(self, rotate=False): |
| 146 | + """ |
| 147 | + Shift all pixels down |
| 148 | + """ |
| 149 | + for x in range(0, self.columns): |
| 150 | + last_pixel = self._matrix[x] if rotate else 0 |
| 151 | + for y in range(0, self.rows - 1): |
| 152 | + self._matrix[y * self.columns + x] = self._matrix[(y + 1) * self.columns + x] |
| 153 | + self._matrix[(self.rows - 1) * self.columns + x] = last_pixel |
| 154 | + self._update() |
0 commit comments