|
| 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.dotstar_featherwing` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Helper for using the `DotStar FeatherWing <https://www.adafruit.com/product/3449>`_. |
| 27 | +
|
| 28 | +* Author(s): Melissa LeBlanc-Williams |
| 29 | +""" |
| 30 | + |
| 31 | +__version__ = "0.0.0-auto.0" |
| 32 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" |
| 33 | + |
| 34 | +import board |
| 35 | +import adafruit_dotstar as dotstar |
| 36 | + |
| 37 | +class DotStarFeatherWing: |
| 38 | + """Class representing a `DotStar FeatherWing |
| 39 | + <https://www.adafruit.com/product/3449>`_. |
| 40 | +
|
| 41 | + The feather uses pins D13 and D11""" |
| 42 | + def __init__(self, clock=board.D13, data=board.D11, brightness=0.2): |
| 43 | + """ |
| 44 | + :param pin clock: The clock pin for the featherwing |
| 45 | + :param pin data: The data pin for the featherwing |
| 46 | + :param float brightness: Optional brightness (0.0-1.0) that defaults to 1.0 |
| 47 | + """ |
| 48 | + self.rows = 6 |
| 49 | + self.columns = 12 |
| 50 | + self._auto_write = True |
| 51 | + self._dotstar = dotstar.DotStar(clock, data, self.rows * self.columns, |
| 52 | + brightness=brightness, auto_write=False) |
| 53 | + |
| 54 | + def __setitem__(self, indices, value): |
| 55 | + """ |
| 56 | + indices can be one of three things: |
| 57 | + x and y ints that are calculated to the DotStar index |
| 58 | + a slice of DotStar indexes with a set of values that match the slice |
| 59 | + a single int that specifies the DotStar index |
| 60 | + value can be one of three things: |
| 61 | + a (r,g,b) list/tuple |
| 62 | + a (r,g,b, brightness) list/tuple |
| 63 | + a single, longer int that contains RGB values, like 0xFFFFFF |
| 64 | + brightness, if specified should be a float 0-1 |
| 65 | + """ |
| 66 | + self._dotstar[self._get_index(indices)] = value |
| 67 | + self._update() |
| 68 | + |
| 69 | + def __getitem__(self, indices): |
| 70 | + """ |
| 71 | + indices can be one of three things: |
| 72 | + x and y ints that are calculated to the DotStar index |
| 73 | + a slice of DotStar indexes to retrieve |
| 74 | + a single int that specifies the DotStar index |
| 75 | + """ |
| 76 | + return self._dotstar[self._get_index(indices)] |
| 77 | + |
| 78 | + def _get_index(self, indices): |
| 79 | + """ |
| 80 | + Figure out which DotStar to address based on what was passed in |
| 81 | + """ |
| 82 | + if isinstance(indices, int): |
| 83 | + if not 0 <= indices < self.rows * self.columns: |
| 84 | + raise ValueError('The index of {} is out of range'.format(indices)) |
| 85 | + return indices |
| 86 | + elif isinstance(indices, slice): |
| 87 | + return indices |
| 88 | + elif len(indices) == 2: |
| 89 | + x, y = indices |
| 90 | + if not 0 <= x < self.columns: |
| 91 | + raise ValueError('The X value of {} is out of range'.format(x)) |
| 92 | + if not 0 <= y < self.rows: |
| 93 | + raise ValueError('The Y value of {} is out of range'.format(y)) |
| 94 | + return y * self.columns + x |
| 95 | + else: |
| 96 | + raise ValueError('Index must be 1 or 2 number') |
| 97 | + |
| 98 | + def fill(self, color=0): |
| 99 | + """ |
| 100 | + Fills all of the DotStars with a color or unlit if empty. |
| 101 | +
|
| 102 | + :param color: (Optional) The text or number to display (default=0) |
| 103 | + :type color: list/tuple or int |
| 104 | +
|
| 105 | + This example shows various ways of using the fill() function |
| 106 | +
|
| 107 | + .. code-block:: python |
| 108 | +
|
| 109 | + import time |
| 110 | + from adafruit_featherwing import dotstar_featherwing |
| 111 | +
|
| 112 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 113 | + dotstar.fill((255, 255, 255)) # Fill White |
| 114 | + time.sleep(1) |
| 115 | + dotstar.fill((255, 255, 255, 0.5)) # Fill White Half Brightness |
| 116 | + time.sleep(1) |
| 117 | + dotstar.fill(0xFF0000) # Fill Red |
| 118 | + time.sleep(1) |
| 119 | + dotstar.fill() # Clear all lit DotStars |
| 120 | +
|
| 121 | + """ |
| 122 | + self._dotstar.fill(color) |
| 123 | + self._update() |
| 124 | + |
| 125 | + def show(self): |
| 126 | + """ |
| 127 | + Update the DotStars. This is only needed if auto_write is set to False |
| 128 | + This can be very useful for more advanced graphics effects. |
| 129 | +
|
| 130 | + This example changes the blink rate and prints out the current setting |
| 131 | +
|
| 132 | + .. code-block:: python |
| 133 | +
|
| 134 | + import time |
| 135 | + from adafruit_featherwing import dotstar_featherwing |
| 136 | +
|
| 137 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 138 | + dotstar.fill() # Clear any lit Dotstars |
| 139 | + dotstar.auto_write = False |
| 140 | + dotstar[0, 0] = (255, 255, 255) # Set White |
| 141 | + time.sleep(1) |
| 142 | + dotstar.show() # Update the DotStars |
| 143 | +
|
| 144 | + """ |
| 145 | + self._dotstar.show() |
| 146 | + |
| 147 | + def shift_right(self, rotate=False): |
| 148 | + """ |
| 149 | + Shift all pixels right |
| 150 | +
|
| 151 | + :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) |
| 152 | +
|
| 153 | + This example shifts 2 pixels to the right |
| 154 | +
|
| 155 | + .. code-block:: python |
| 156 | +
|
| 157 | + import time |
| 158 | + from adafruit_featherwing import dotstar_featherwing |
| 159 | +
|
| 160 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 161 | +
|
| 162 | + # Draw Red and Green Pixels |
| 163 | + dotstar[5, 3] = (255, 0, 0) |
| 164 | + dotstar[6, 3] = (0, 255, 0) |
| 165 | +
|
| 166 | + # Rotate it off the screen |
| 167 | + for i in range(0, 11): |
| 168 | + dotstar.shift_right(True) |
| 169 | + time.sleep(.1) |
| 170 | +
|
| 171 | + time.sleep(1) |
| 172 | + # Shift it off the screen |
| 173 | + for i in range(0, 11): |
| 174 | + dotstar.shift_right() |
| 175 | + time.sleep(.1) |
| 176 | +
|
| 177 | + """ |
| 178 | + for y in range(0, self.rows): |
| 179 | + last_pixel = self._dotstar[(y + 1) * self.columns - 1] if rotate else 0 |
| 180 | + for x in range(self.columns - 1, 0, -1): |
| 181 | + self._dotstar[y * self.columns + x] = self._dotstar[y * self.columns + x - 1] |
| 182 | + self._dotstar[y * self.columns] = last_pixel |
| 183 | + self._update() |
| 184 | + |
| 185 | + def shift_left(self, rotate=False): |
| 186 | + """ |
| 187 | + Shift all pixels left |
| 188 | +
|
| 189 | + :param rotate: (Optional) Rotate the shifted pixels to the right side (default=False) |
| 190 | +
|
| 191 | + This example shifts 2 pixels to the left |
| 192 | +
|
| 193 | + .. code-block:: python |
| 194 | +
|
| 195 | + import time |
| 196 | + from adafruit_featherwing import dotstar_featherwing |
| 197 | +
|
| 198 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 199 | +
|
| 200 | + # Draw Red and Green Pixels |
| 201 | + dotstar[5, 3] = (255, 0, 0) |
| 202 | + dotstar[6, 3] = (0, 255, 0) |
| 203 | +
|
| 204 | + # Rotate it off the screen |
| 205 | + for i in range(0, 11): |
| 206 | + dotstar.shift_left(True) |
| 207 | + time.sleep(.1) |
| 208 | +
|
| 209 | + time.sleep(1) |
| 210 | + # Shift it off the screen |
| 211 | + for i in range(0, 11): |
| 212 | + dotstar.shift_left() |
| 213 | + time.sleep(.1) |
| 214 | +
|
| 215 | + """ |
| 216 | + for y in range(0, self.rows): |
| 217 | + last_pixel = self._dotstar[y * self.columns] if rotate else 0 |
| 218 | + for x in range(0, self.columns - 1): |
| 219 | + self._dotstar[y * self.columns + x] = self._dotstar[y * self.columns + x + 1] |
| 220 | + self._dotstar[(y + 1) * self.columns - 1] = last_pixel |
| 221 | + self._update() |
| 222 | + |
| 223 | + def shift_up(self, rotate=False): |
| 224 | + """ |
| 225 | + Shift all pixels up |
| 226 | +
|
| 227 | + :param rotate: (Optional) Rotate the shifted pixels to bottom (default=False) |
| 228 | +
|
| 229 | + This example shifts 2 pixels up |
| 230 | +
|
| 231 | + .. code-block:: python |
| 232 | +
|
| 233 | + import time |
| 234 | + from adafruit_featherwing import dotstar_featherwing |
| 235 | +
|
| 236 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 237 | +
|
| 238 | + # Draw Red and Green Pixels |
| 239 | + dotstar[5, 3] = (255, 0, 0) |
| 240 | + dotstar[6, 3] = (0, 255, 0) |
| 241 | +
|
| 242 | + # Rotate it off the screen |
| 243 | + for i in range(0, 5): |
| 244 | + dotstar.shift_up(True) |
| 245 | + time.sleep(.1) |
| 246 | +
|
| 247 | + time.sleep(1) |
| 248 | + # Shift it off the screen |
| 249 | + for i in range(0, 5): |
| 250 | + dotstar.shift_up() |
| 251 | + time.sleep(.1) |
| 252 | +
|
| 253 | + """ |
| 254 | + for x in range(0, self.columns): |
| 255 | + last_pixel = self._dotstar[(self.rows - 1) * self.columns + x] if rotate else 0 |
| 256 | + for y in range(self.rows - 1, 0, -1): |
| 257 | + self._dotstar[y * self.columns + x] = self._dotstar[(y - 1) * self.columns + x] |
| 258 | + self._dotstar[x] = last_pixel |
| 259 | + self._update() |
| 260 | + |
| 261 | + def shift_down(self, rotate=False): |
| 262 | + """ |
| 263 | + Shift all pixels down |
| 264 | +
|
| 265 | + :param rotate: (Optional) Rotate the shifted pixels to top (default=False) |
| 266 | +
|
| 267 | + This example shifts 2 pixels down |
| 268 | +
|
| 269 | + .. code-block:: python |
| 270 | +
|
| 271 | + import time |
| 272 | + from adafruit_featherwing import dotstar_featherwing |
| 273 | +
|
| 274 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 275 | +
|
| 276 | + # Draw Red and Green Pixels |
| 277 | + dotstar[5, 3] = (255, 0, 0) |
| 278 | + dotstar[6, 3] = (0, 255, 0) |
| 279 | +
|
| 280 | + # Rotate it off the screen |
| 281 | + for i in range(0, 5): |
| 282 | + dotstar.shift_down(True) |
| 283 | + time.sleep(.1) |
| 284 | +
|
| 285 | + time.sleep(1) |
| 286 | + # Shift it off the screen |
| 287 | + for i in range(0, 5): |
| 288 | + dotstar.shift_down() |
| 289 | + time.sleep(.1) |
| 290 | +
|
| 291 | + """ |
| 292 | + for x in range(0, self.columns): |
| 293 | + last_pixel = self._dotstar[x] if rotate else 0 |
| 294 | + for y in range(0, self.rows - 1): |
| 295 | + self._dotstar[y * self.columns + x] = self._dotstar[(y + 1) * self.columns + x] |
| 296 | + self._dotstar[(self.rows - 1) * self.columns + x] = last_pixel |
| 297 | + self._update() |
| 298 | + |
| 299 | + def _update(self): |
| 300 | + """ |
| 301 | + Update the Display automatically if auto_write is set to True |
| 302 | + """ |
| 303 | + if self._auto_write: |
| 304 | + self._dotstar.show() |
| 305 | + |
| 306 | + @property |
| 307 | + def auto_write(self): |
| 308 | + """ |
| 309 | + Whether or not we are automatically updating |
| 310 | + If set to false, be sure to call show() to update |
| 311 | +
|
| 312 | + This lights DotStars with and without auto_write |
| 313 | +
|
| 314 | + .. code-block:: python |
| 315 | +
|
| 316 | + import time |
| 317 | + from adafruit_featherwing import dotstar_featherwing |
| 318 | +
|
| 319 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 320 | + dotstar.fill() # Clear any lit Dotstars |
| 321 | + dotstar[0, 0] = (255, 255, 255) # Set White |
| 322 | + time.sleep(1) |
| 323 | +
|
| 324 | + dotstar.auto_write = False |
| 325 | + dotstar[1, 0] = (255, 255, 255) # Set White |
| 326 | + time.sleep(1) |
| 327 | + dotstar.show() # Update the DotStars |
| 328 | +
|
| 329 | + """ |
| 330 | + return self._auto_write |
| 331 | + |
| 332 | + @auto_write.setter |
| 333 | + def auto_write(self, write): |
| 334 | + if isinstance(write, bool): |
| 335 | + self._auto_write = write |
| 336 | + |
| 337 | + @property |
| 338 | + def brightness(self): |
| 339 | + """ |
| 340 | + Overall brightness of the display |
| 341 | +
|
| 342 | + This example changes the brightness |
| 343 | +
|
| 344 | + .. code-block:: python |
| 345 | +
|
| 346 | + import time |
| 347 | + from adafruit_featherwing import dotstar_featherwing |
| 348 | +
|
| 349 | + dotstar = dotstar_featherwing.DotStarFeatherWing() |
| 350 | + dotstar.brightness = 0 |
| 351 | + dotstar.fill(0xFFFFFF) |
| 352 | + for i in range(0, 6): |
| 353 | + dotstar.brightness = (i / 10) |
| 354 | + time.sleep(.2) |
| 355 | +
|
| 356 | + dotstar.brightness = 0.3 |
| 357 | +
|
| 358 | + """ |
| 359 | + return self._dotstar.brightness |
| 360 | + |
| 361 | + @brightness.setter |
| 362 | + def brightness(self, brightness): |
| 363 | + self._dotstar.brightness = min(max(brightness, 0.0), 1.0) |
| 364 | + self._update() |
0 commit comments