|
| 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.led_segments` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Base Class for the AlphaNumeric FeatherWing and 7-Segment FeatherWing helpers_. |
| 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 | +from time import sleep |
| 35 | + |
| 36 | +#pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation |
| 37 | + |
| 38 | +class Segments: |
| 39 | + """Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing |
| 40 | + <https://www.adafruit.com/product/3139>`_. |
| 41 | +
|
| 42 | + Automatically uses the feather's I2C bus.""" |
| 43 | + def __init__(self): |
| 44 | + self._segments = None |
| 45 | + |
| 46 | + def print(self, value): |
| 47 | + """ |
| 48 | + Print a number or text to the display |
| 49 | +
|
| 50 | + :param value: The text or number to display |
| 51 | + :type value: str or int or float |
| 52 | +
|
| 53 | + """ |
| 54 | + self._segments.print(value) |
| 55 | + self._segments.show() |
| 56 | + |
| 57 | + def marquee(self, text, delay=0.25, loop=True): |
| 58 | + """ |
| 59 | + Automatically scroll the text at the specified delay between characters |
| 60 | +
|
| 61 | + :param str text: The text to display |
| 62 | + :param float delay: (optional) The delay in seconds to pause before scrolling |
| 63 | + to the next character (default=0.25) |
| 64 | + :param bool loop: (optional) Whether to endlessly loop the text (default=True) |
| 65 | +
|
| 66 | + """ |
| 67 | + if isinstance(text, str): |
| 68 | + self.fill(False) |
| 69 | + if loop: |
| 70 | + while True: |
| 71 | + self._scroll_marquee(text, delay) |
| 72 | + else: |
| 73 | + self._scroll_marquee(text, delay) |
| 74 | + |
| 75 | + def _scroll_marquee(self, text, delay): |
| 76 | + """ |
| 77 | + Scroll through the text string once using the delay |
| 78 | + """ |
| 79 | + char_is_dot = False |
| 80 | + for character in text: |
| 81 | + self._segments.print(character) |
| 82 | + # Add delay if character is not a dot or more than 2 in a row |
| 83 | + if character != '.' or char_is_dot: |
| 84 | + sleep(delay) |
| 85 | + char_is_dot = (character == '.') |
| 86 | + self._segments.show() |
| 87 | + |
| 88 | + def fill(self, fill): |
| 89 | + """Change all Segments on or off |
| 90 | +
|
| 91 | + :param bool fill: True turns all segments on, False turns all segments off |
| 92 | +
|
| 93 | + """ |
| 94 | + if isinstance(fill, bool): |
| 95 | + self._segments.fill(1 if fill else 0) |
| 96 | + self._segments.show() |
| 97 | + else: |
| 98 | + raise ValueError('Must set to either True or False.') |
| 99 | + |
| 100 | + @property |
| 101 | + def blink_rate(self): |
| 102 | + """ |
| 103 | + Blink Rate returns the current rate that the text blinks. |
| 104 | + 0 = Off |
| 105 | + 1-3 = Successively slower blink rates |
| 106 | + """ |
| 107 | + return self._segments.blink_rate |
| 108 | + |
| 109 | + @blink_rate.setter |
| 110 | + def blink_rate(self, rate): |
| 111 | + self._segments.blink_rate = rate |
| 112 | + |
| 113 | + @property |
| 114 | + def brightness(self): |
| 115 | + """ |
| 116 | + Brightness returns the current display brightness. |
| 117 | + 0-15 = Dimmest to Brightest Setting |
| 118 | + """ |
| 119 | + return self._segments.brightness |
| 120 | + |
| 121 | + @brightness.setter |
| 122 | + def brightness(self, brightness): |
| 123 | + self._segments.brightness = brightness |
0 commit comments