From c906053c07d27a8facda38d9c369a2f35f7a90f9 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Thu, 21 Mar 2019 15:26:10 -0400 Subject: [PATCH 1/3] Fixed up for release. --- .travis.yml | 4 +- README.md | 2 - README.rst | 6 +- adafruit_button.py | 96 ++++++++++++++++++--------- docs/api.rst | 2 +- docs/conf.py | 2 +- docs/index.rst | 4 -- examples/display_button_simpletest.py | 9 +-- examples/display_button_soundboard.py | 2 - 9 files changed, 73 insertions(+), 54 deletions(-) delete mode 100644 README.md diff --git a/.travis.yml b/.travis.yml index 0e1f837..34d0fef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ cache: # or remove the env block entirely and remove the condition in the # deploy block. env: - - DEPLOY_PYPI="false" + - DEPLOY_PYPI="true" deploy: - provider: releases @@ -31,7 +31,7 @@ deploy: - provider: pypi user: adafruit-travis password: - secure: #-- PASTE ENCRYPTED PASSWORD HERE --# + secure: ZxsMZrdftjJBqxdW4fowdLWFR5ykdCawV6pQt1BwRO8Q+6PGRloGkpIBQ2tj9eX85YY8q5sBIhqNwODJ5od9/B7nbx3xTo8T79O5Nq//tv9zmZaQS/uqZo8fj9TaHpaq+MVH2aCRfepey9/OzVRRsIZW/nEpaHuDPow7UtaoGFij4VsZ66RoVm88J7Zer8/bVlOdYatmMpb8SOSRE8Hj6jx7F4ayVxF2hVdzd8wOxrKObDkEFZ9ym0xYKHMWdPehtbnCOo/rgm0jtutd8plrKnTm//qNFEp6CdRcLbCEL6cQoPOWzk47p7tGe42yHllquB//f2VqmGzep3+YverAAOrPG2XOxp2ypQFc0RL6KY6CpDqWAdfYh+/H5o74oxCvRVWyUyHZ2eTHrd6YKnwlhxDTk0+A7FwforEPODm/YGxoTrRXduiD+LR4xvPFaQISAyFjIOeCYA1Yyfz4ZTQgcpXwqAa4irLlfb3rdjWRZIRnhR6mQsd1nTqDaxXcqlbL/EIH8KKG0ZBIAXL7F73ajRblaVn2iHvYkrTDKQhR8yaIFBUgAoSXdCBY1TIg3/RWU/knRyQItEKiQHXgua+gVO95GT4a2Hf2yV4y3PoaXCSoAVF24hgnHN9WRIEpyVRHgcMJpH2bLnXmDnl8KPgEqZD586tQaCqZdIdKtNJhUbo= on: tags: true condition: $DEPLOY_PYPI = "true" diff --git a/README.md b/README.md deleted file mode 100644 index bdf3b81..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# Adafruit_CircuitPython_Display_Button -Press it! diff --git a/README.rst b/README.rst index 4a499dc..a1c2b73 100644 --- a/README.rst +++ b/README.rst @@ -28,10 +28,6 @@ This is easily achieved by downloading Installing from PyPI -------------------- -.. note:: This library is not available on PyPI yet. Install documentation is included - as a standard element. Stay tuned for PyPI availability! -.. todo:: Remove the above note if PyPI version is/will be available at time of release. - If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section. On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from PyPI `_. To install for current user: @@ -57,7 +53,7 @@ To install in a virtual environment in your current project: Usage Example ============= -.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst. +See examples in examples/ folder. Contributing ============ diff --git a/adafruit_button.py b/adafruit_button.py index be49cb4..e3d602a 100644 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ -`adafruit_display_button` +`adafruit_button` ================================================================================ UI Buttons for displayio @@ -38,9 +38,9 @@ """ +from micropython import const import displayio from adafruit_display_text.text_area import TextArea -from adafruit_bitmap_font import bitmap_font from adafruit_display_shapes.rect import Rect from adafruit_display_shapes.roundrect import RoundRect @@ -48,11 +48,40 @@ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git" +def _check_color(color): + # if a tuple is supplied, convert it to a RGB number + if isinstance(color, tuple): + r, g, b = color + return int((r << 16) + (g << 8) + (b & 0xff)) + return color + + class Button(): + # pylint: disable=too-many-instance-attributes, too-many-locals + """Helper class for creating UI buttons for ``displayio``. + + :param x: The x position of the button. + :param y: The y position of the button. + :param width: The width of the button in pixels. + :param height: The height of the button in pixels. + :param name: The name of the button. + :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT. + Defaults to RECT. + :param fill_color: The color to fill the button. Defaults to 0xFFFFFF. + :param outline_color: The color of the outline of the button. + :param label: The button label. Defaults to 0x0. + :param label_font: The button label font. + :param label_color: The color of the button label. Defaults to 0x0. + :param selected_fill: Inverts the fill color. + :param selected_outline: Inverts the outline color. + :param selected_label: Inverts the label color. + + """ RECT = const(0) ROUNDRECT = const(1) SHADOWRECT = const(2) SHADOWROUNDRECT = const(3) + def __init__(self, *, x, y, width, height, name=None, style=RECT, fill_color=0xFFFFFF, outline_color=0x0, label=None, label_font=None, label_color=0x0, @@ -66,66 +95,66 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, self._selected = False self.group = displayio.Group() self.name = name + self.label = label - self.fill_color = fill_color - self.outline_color = outline_color + self.fill_color = _check_color(fill_color) + self.outline_color = _check_color(outline_color) self.label_color = label_color # Selecting inverts the button colors! - self.selected_fill = selected_fill - self.selected_outline = selected_outline - self.selected_label = selected_label + self.selected_fill = _check_color(selected_fill) + self.selected_outline = _check_color(selected_outline) + self.selected_label = _check_color(selected_label) if self.selected_fill is None and fill_color is not None: - self.selected_fill = (~fill_color) & 0xFFFFFF + self.selected_fill = (~self.fill_color) & 0xFFFFFF if self.selected_outline is None and outline_color is not None: - self.selected_outline = (~outline_color) & 0xFFFFFF + self.selected_outline = (~self.outline_color) & 0xFFFFFF if outline_color or fill_color: self.body = self.shadow = None - if style == RECT: + if style == Button.RECT: self.body = Rect(x, y, width, height, - fill=fill_color, outline=outline_color) - elif style == ROUNDRECT: + fill=self.fill_color, outline=self.outline_color) + elif style == Button.ROUNDRECT: self.body = RoundRect(x, y, width, height, r=10, - fill=fill_color, outline=outline_color) - elif style == SHADOWRECT: - self.shadow = Rect(x+2, y+2, width-2, height-2, - fill=outline_color) - self.body = Rect(x, y, width-2, height-2, - fill=fill_color, outline=outline_color) - elif style == SHADOWROUNDRECT: - self.shadow = RoundRect(x+2, y+2, width-2, height-2, r=10, + fill=self.fill_color, outline=self.outline_color) + elif style == Button.SHADOWRECT: + self.shadow = Rect(x + 2, y + 2, width - 2, height - 2, fill=outline_color) - self.body = RoundRect(x, y, width-2, height-2, r=10, - fill=fill_color, outline=outline_color) + self.body = Rect(x, y, width - 2, height - 2, + fill=self.fill_color, outline=self.outline_color) + elif style == Button.SHADOWROUNDRECT: + self.shadow = RoundRect(x + 2, y + 2, width - 2, height - 2, r=10, + fill=self.outline_color) + self.body = RoundRect(x, y, width - 2, height - 2, r=10, + fill=self.fill_color, outline=self.outline_color) if self.shadow: self.group.append(self.shadow) self.group.append(self.body) - if label and (label_color is not None): # button with text label + if label and (label_color is not None): # button with text label if not label_font: raise RuntimeError("Please provide label font") dims = label_font.text_bounding_box(label) if dims[2] >= width or dims[3] >= height: raise RuntimeError("Button not large enough for label") self.label = TextArea(label_font, text=label) - self.label.x = x + (width - dims[2])//2 + self.label.x = x + (width - dims[2]) // 2 self.label.y = y + (height - dims[3]) self.label.color = label_color self.group.append(self.label) if self.selected_label is None and label_color is not None: self.selected_label = (~label_color) & 0xFFFFFF - #print(dims) + # print(dims) - """ - #else: # ok just a bounding box - #self.bodyshape = displayio.Shape(width, height) - #self.group.append(self.bodyshape) - """ + # else: # ok just a bounding box + # self.bodyshape = displayio.Shape(width, height) + # self.group.append(self.bodyshape) @property def selected(self): + """Selected inverts the colors.""" return self._selected @selected.setter @@ -142,4 +171,9 @@ def selected(self, value): self.label.color = self.label_color def contains(self, point): - return (self.x <= point[0] <= self.x+self.width) and (self.y <= point[1] <= self.y+self.height) + """Used to determine if a point is contained within a button. For example, + ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for + determining that a button has been touched. + """ + return (self.x <= point[0] <= self.x + self.width) and (self.y <= point[1] <= + self.y + self.height) diff --git a/docs/api.rst b/docs/api.rst index a55e0b3..49c1de1 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -4,5 +4,5 @@ .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) .. use this format as the module name: "adafruit_foo.foo" -.. automodule:: adafruit_display_button +.. automodule:: adafruit_button :members: diff --git a/docs/conf.py b/docs/conf.py index d45c2af..276f652 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,7 +20,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -# autodoc_mock_imports = ["digitalio", "busio"] +autodoc_mock_imports = ["displayio", "adafruit_display_text", "adafruit_display_shapes"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} diff --git a/docs/index.rst b/docs/index.rst index 7ea3d96..bf53505 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -23,14 +23,10 @@ Table of Contents .. toctree:: :caption: Tutorials -.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave - the toctree above for use later. .. toctree:: :caption: Related Products -.. todo:: Add any product links here. If there are none, then simply delete this todo and leave - the toctree above for use later. .. toctree:: :caption: Other Links diff --git a/examples/display_button_simpletest.py b/examples/display_button_simpletest.py index 198e75b..04fda4b 100644 --- a/examples/display_button_simpletest.py +++ b/examples/display_button_simpletest.py @@ -1,10 +1,7 @@ -import time +import os import board import displayio -import os -from adafruit_display_text.text_area import TextArea from adafruit_bitmap_font import bitmap_font -from adafruit_display_shapes.rect import Rect from adafruit_button import Button import adafruit_touchscreen @@ -17,8 +14,8 @@ # the current working directory (where this file is) cwd = ("/"+__file__).rsplit('/', 1)[0] -fonts = [file for file in os.listdir(cwd+"/fonts/") - if (file.endswith(".bdf") and not file.startswith("._"))] +fonts = [file for file in os.listdir(cwd+"/fonts/") + if (file.endswith(".bdf") and not file.startswith("._"))] for i, filename in enumerate(fonts): fonts[i] = cwd+"/fonts/"+filename print(fonts) diff --git a/examples/display_button_soundboard.py b/examples/display_button_soundboard.py index e9b1680..340b3ff 100644 --- a/examples/display_button_soundboard.py +++ b/examples/display_button_soundboard.py @@ -1,6 +1,4 @@ import time -import board -import displayio from adafruit_pyportal import PyPortal from adafruit_button import Button From a9ac552fa1929ad640047f3e1407af863d90203f Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Thu, 21 Mar 2019 15:50:56 -0400 Subject: [PATCH 2/3] Fix .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 34d0fef..6a11ef3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ install: - pip install --force-reinstall pylint==1.9.2 script: - - pylint adafruit_display_button.py + - pylint adafruit_button.py - ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py) - circuitpython-build-bundles --filename_prefix adafruit-circuitpython-display_button --library_location . - cd docs && sphinx-build -E -W -b html . _build/html && cd .. From e4a9a6aa06990c877e812800862747cb76691c1b Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 22 Mar 2019 15:00:33 -0400 Subject: [PATCH 3/3] Changes to Label and label. --- adafruit_button.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index e3d602a..b188b8f 100644 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -40,7 +40,7 @@ from micropython import const import displayio -from adafruit_display_text.text_area import TextArea +from adafruit_display_text.label import Label from adafruit_display_shapes.rect import Rect from adafruit_display_shapes.roundrect import RoundRect @@ -69,9 +69,9 @@ class Button(): Defaults to RECT. :param fill_color: The color to fill the button. Defaults to 0xFFFFFF. :param outline_color: The color of the outline of the button. - :param label: The button label. Defaults to 0x0. + :param label: The text that appears inside the button. Defaults to not displaying the label. :param label_font: The button label font. - :param label_color: The color of the button label. Defaults to 0x0. + :param label_color: The color of the button label text. Defaults to 0x0. :param selected_fill: Inverts the fill color. :param selected_outline: Inverts the outline color. :param selected_label: Inverts the label color. @@ -138,7 +138,7 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, dims = label_font.text_bounding_box(label) if dims[2] >= width or dims[3] >= height: raise RuntimeError("Button not large enough for label") - self.label = TextArea(label_font, text=label) + self.label = Label(label_font, text=label) self.label.x = x + (width - dims[2]) // 2 self.label.y = y + (height - dims[3]) self.label.color = label_color