diff --git a/adafruit_featherwing/alphanum_featherwing.py b/adafruit_featherwing/alphanum_featherwing.py index da29f46..06b0409 100755 --- a/adafruit_featherwing/alphanum_featherwing.py +++ b/adafruit_featherwing/alphanum_featherwing.py @@ -31,150 +31,16 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" -from time import sleep import adafruit_ht16k33.segments as segments from adafruit_featherwing import shared +from adafruit_featherwing.led_segments import Segments -class AlphaNumFeatherWing: +class AlphaNumFeatherWing(Segments): """Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing `_. Automatically uses the feather's I2C bus.""" def __init__(self, address=0x70): - self._seg14x4 = segments.Seg14x4(shared.I2C_BUS, address) - self._seg14x4.auto_write = False - - def print(self, value): - """ - Print a number or text to the display - - :param value: The text or number to display - :type value: str or int or float - - .. code-block:: python - - from adafruit_featherwing import alphanum_featherwing - - display = alphanum_featherwing.AlphaNumFeatherWing() - display.print(1234) - - """ - self._seg14x4.print(value) - self._seg14x4.show() - - def marquee(self, text, delay=0.25, loop=True): - """ - Automatically scroll the text at the specified delay between characters - - :param str text: The text to display - :param float delay: (optional) The delay in seconds to pause before scrolling - to the next character (default=0.25) - :param bool loop: (optional) Whether to endlessly loop the text (default=True) - - .. code-block:: python - - from adafruit_featherwing import alphanum_featherwing - - display = alphanum_featherwing.AlphaNumFeatherWing() - display.marquee('This is some really long text ') - - """ - if isinstance(text, str): - self.fill(False) - if loop: - while True: - self._scroll_marquee(text, delay) - else: - self._scroll_marquee(text, delay) - - def _scroll_marquee(self, text, delay): - for character in text: - self._seg14x4.scroll() - if character == '.': - self._seg14x4[3] = ' ' - self._seg14x4[3] = character - sleep(delay) - self._seg14x4.show() - - @property - def blink_rate(self): - """ - Blink Rate returns the current rate that the text blinks. - 0 = Off - 1-3 = Successively slower blink rates - - This example changes the blink rate and prints out the current setting - - .. code-block:: python - - from time import sleep - from adafruit_featherwing import alphanum_featherwing - - display = alphanum_featherwing.AlphaNumFeatherWing() - display.print('Text') - - for blink_rate in range(3, -1, -1): - display.blink_rate = blink_rate - print("Current Blink Rate is {}".format(display.blink_rate)) - sleep(4) - - """ - return self._seg14x4.blink_rate - - @blink_rate.setter - def blink_rate(self, rate): - self._seg14x4.blink_rate = rate - - @property - def brightness(self): - """ - Brightness returns the current display brightness. - 0-15 = Dimmest to Brightest Setting - - This example changes the brightness and prints out the current setting - - .. code-block:: python - - from time import sleep - from adafruit_featherwing import alphanum_featherwing - - display = alphanum_featherwing.AlphaNumFeatherWing() - display.print('Text') - - for brightness in range(0, 16): - display.brightness = brightness - print("Current Brightness is {}".format(display.brightness)) - sleep(0.2) - - """ - return self._seg14x4.brightness - - @brightness.setter - def brightness(self, brightness): - self._seg14x4.brightness = brightness - - def fill(self, fill): - """Change all Segments on or off - :param bool fill: True turns all segments on, False turns all segments off - - This example alternates between all filled and all empty segments. - - .. code-block:: python - - from time import sleep - from adafruit_featherwing import alphanum_featherwing - - display = alphanum_featherwing.AlphaNumFeatherWing() - - while True: - display.fill(True) - sleep(0.5) - display.fill(False) - sleep(0.5) - - """ - if isinstance(fill, bool): - self._seg14x4.fill(1 if fill else 0) - self._seg14x4.show() - else: - raise ValueError('Must set to either True or False.') + super().__init__() + self._segments = segments.Seg14x4(shared.I2C_BUS, address) + self._segments.auto_write = False diff --git a/adafruit_featherwing/led_segments.py b/adafruit_featherwing/led_segments.py new file mode 100755 index 0000000..89005a1 --- /dev/null +++ b/adafruit_featherwing/led_segments.py @@ -0,0 +1,123 @@ +# The MIT License (MIT) +# +# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_featherwing.led_segments` +==================================================== + +Base Class for the AlphaNumeric FeatherWing and 7-Segment FeatherWing helpers_. + +* Author(s): Melissa LeBlanc-Williams +""" + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" + +from time import sleep + +#pylint: disable-msg=unsubscriptable-object, unsupported-assignment-operation + +class Segments: + """Class representing an `Adafruit 14-segment AlphaNumeric FeatherWing + `_. + + Automatically uses the feather's I2C bus.""" + def __init__(self): + self._segments = None + + def print(self, value): + """ + Print a number or text to the display + + :param value: The text or number to display + :type value: str or int or float + + """ + self._segments.print(value) + self._segments.show() + + def marquee(self, text, delay=0.25, loop=True): + """ + Automatically scroll the text at the specified delay between characters + + :param str text: The text to display + :param float delay: (optional) The delay in seconds to pause before scrolling + to the next character (default=0.25) + :param bool loop: (optional) Whether to endlessly loop the text (default=True) + + """ + if isinstance(text, str): + self.fill(False) + if loop: + while True: + self._scroll_marquee(text, delay) + else: + self._scroll_marquee(text, delay) + + def _scroll_marquee(self, text, delay): + """ + Scroll through the text string once using the delay + """ + char_is_dot = False + for character in text: + self._segments.print(character) + # Add delay if character is not a dot or more than 2 in a row + if character != '.' or char_is_dot: + sleep(delay) + char_is_dot = (character == '.') + self._segments.show() + + def fill(self, fill): + """Change all Segments on or off + + :param bool fill: True turns all segments on, False turns all segments off + + """ + if isinstance(fill, bool): + self._segments.fill(1 if fill else 0) + self._segments.show() + else: + raise ValueError('Must set to either True or False.') + + @property + def blink_rate(self): + """ + Blink Rate returns the current rate that the text blinks. + 0 = Off + 1-3 = Successively slower blink rates + """ + return self._segments.blink_rate + + @blink_rate.setter + def blink_rate(self, rate): + self._segments.blink_rate = rate + + @property + def brightness(self): + """ + Brightness returns the current display brightness. + 0-15 = Dimmest to Brightest Setting + """ + return self._segments.brightness + + @brightness.setter + def brightness(self, brightness): + self._segments.brightness = brightness diff --git a/adafruit_featherwing/sevensegment_featherwing.py b/adafruit_featherwing/sevensegment_featherwing.py new file mode 100755 index 0000000..00d0e74 --- /dev/null +++ b/adafruit_featherwing/sevensegment_featherwing.py @@ -0,0 +1,46 @@ +# The MIT License (MIT) +# +# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries LLC +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +""" +`adafruit_featherwing.sevensegment_featherwing` +==================================================== + +Helper for using the `7-Segment LED HT16K33 FeatherWing `_. + +* Author(s): Melissa LeBlanc-Williams +""" + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" + +import adafruit_ht16k33.segments as segments +from adafruit_featherwing import shared +from adafruit_featherwing.led_segments import Segments + +class SevenSegmentFeatherWing(Segments): + """Class representing an `Adafruit 7-Segment LED HT16K33 FeatherWing + `_. + + Automatically uses the feather's I2C bus.""" + def __init__(self, address=0x70): + super().__init__() + self._segments = segments.Seg7x4(shared.I2C_BUS, address) + self._segments.auto_write = False diff --git a/docs/examples.rst b/docs/examples.rst index 757d8e4..65a2bc9 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -23,6 +23,10 @@ Ensure your device works with this simple test. :caption: examples/featherwing_neopixel_simpletest.py :linenos: +.. literalinclude:: ../examples/featherwing_sevensegment_simpletest.py + :caption: examples/featherwing_sevensegment_simpletest.py + :linenos: + Other tests ------------ diff --git a/examples/featherwing_sevensegment_simpletest.py b/examples/featherwing_sevensegment_simpletest.py new file mode 100644 index 0000000..860105a --- /dev/null +++ b/examples/featherwing_sevensegment_simpletest.py @@ -0,0 +1,47 @@ +"""This example changes the fill, brightness, blink rates, +shows number and text printing, displays a counter +and then shows off the new marquee features.""" + +from time import sleep +from adafruit_featherwing import sevensegment_featherwing + +display = sevensegment_featherwing.SevenSegmentFeatherWing() + +#Fill and empty all segments +for count in range(0, 3): + display.fill(True) + sleep(0.5) + display.fill(False) + sleep(0.5) + +#Display a number and text +display.print(1234) +sleep(1) +display.print('FEED') + +#Change brightness +for brightness in range(0, 16): + display.brightness = brightness + sleep(0.1) + +#Change blink rate +for blink_rate in range(3, 0, -1): + display.blink_rate = blink_rate + sleep(4) +display.blink_rate = 0 + +#Show a counter using decimals +count = 975.0 +while count < 1025: + count += 1 + display.print(count) + sleep(0.1) + +#Display a Time +hour = 12 +for minute in range(15, 26): + display.print("{}:{}".format(hour, minute)) + sleep(1) + +#Show the Marquee +display.marquee('Deadbeef 192.168.100.102... ', 0.2)