Skip to content

Mini Color TFT with Joystick FeatherWing #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 12, 2019
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ These drivers depends on:
* `DotStar <https://github.com/adafruit/Adafruit_CircuitPython_DotStar>`_
* `NeoPixel <https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel>`_
* `DS3231 <https://github.com/adafruit/Adafruit_CircuitPython_DS3231>`_
* `ST7735R <https://github.com/adafruit/Adafruit_CircuitPython_ST7735R>`_

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
Expand Down
109 changes: 109 additions & 0 deletions adafruit_featherwing/minitft_featherwing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 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.minitft_featherwing`
====================================================

Helper for using the `Mini Color TFT with Joystick FeatherWing
<https://www.adafruit.com/product/3321>`_.

* Author(s): Melissa LeBlanc-Williams
"""

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"

from collections import namedtuple
import board
from micropython import const
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
import displayio
from adafruit_st7735r import ST7735R

BUTTON_RIGHT = const(7)
BUTTON_DOWN = const(4)
BUTTON_LEFT = const(3)
BUTTON_UP = const(2)
BUTTON_SEL = const(11)
BUTTON_A = const(10)
BUTTON_B = const(9)

class MiniTFTFeatherWing:
"""Class representing an `Mini Color TFT with Joystick FeatherWing
<https://www.adafruit.com/product/3321>`_.

Automatically uses the feather's I2C bus."""
def __init__(self, address=0x5E, i2c=None, spi=None):
if i2c is None:
i2c = board.I2C()
if spi is None:
spi = board.SPI()
self._ss = Seesaw(i2c, address)
self._backlight = PWMOut(self._ss, 5)
self._backlight.duty_cycle = 0
self._ss.pin_mode_bulk(self._button_mask, self._ss.INPUT_PULLUP)
displayio.release_displays()
display_bus = displayio.FourWire(spi, command=board.D6, chip_select=board.D5)
self._ss.pin_mode(8, self._ss.OUTPUT)
self._ss.digital_write(8, True) # Reset the Display via Seesaw
self.display = ST7735R(display_bus, width=160, height=80, colstart=24,
rotation=270, bgr=True)

@property
def _button_mask(self):
return ((1 << BUTTON_RIGHT) |
(1 << BUTTON_DOWN) |
(1 << BUTTON_LEFT) |
(1 << BUTTON_UP) |
(1 << BUTTON_SEL) |
(1 << BUTTON_A) |
(1 << BUTTON_B))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can you put this on the class, like this:

class MiniTFTFeatherWing:
    _button_mask = ((1 << BUTTON_RIGHT) |
                 (1 << BUTTON_DOWN) |
                 (1 << BUTTON_LEFT) |
                 (1 << BUTTON_UP) |
                 (1 << BUTTON_SEL) |
                 (1 << BUTTON_A) |
                 (1 << BUTTON_B))

    def __init_(self, ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do this as well? I think making it a property is wasteful.


@property
def backlight(self):
"""
Return the current backlight duty cycle value
"""
return self._backlight.duty_cycle / 255

@backlight.setter
def backlight(self, brightness):
"""
Set the backlight duty cycle
"""
self._backlight.duty_cycle = int(255 * min(max(1 - brightness, 0.0), 1.0))

@property
def buttons(self):
"""
Return a set of buttons with current push values
"""
buttons = namedtuple("Buttons", "up down left right a b select")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this outside the class, where the constants are, so that users have access to it? Also, since it's a type, it should start with upper case letter.

button_values = self._ss.digital_read_bulk(self._button_mask)
return buttons(up=(not button_values & (1 << BUTTON_UP)),
down=(not button_values & (1 << BUTTON_DOWN)),
left=(not button_values & (1 << BUTTON_LEFT)),
right=(not button_values & (1 << BUTTON_RIGHT)),
a=(not button_values & (1 << BUTTON_A)),
b=(not button_values & (1 << BUTTON_B)),
select=(not button_values & (1 << BUTTON_SEL)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write this as:

return Buttons(*[not button_values & (1 << button) for button in
                 (BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_A, BUTTON_B,BUTTON_SEL)])

3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@

.. automodule:: adafruit_featherwing.matrix_featherwing
:members:

.. automodule:: adafruit_featherwing.minitft_featherwing
:members:
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 = ["board", "busio"]
autodoc_mock_imports = ["board", "busio", "displayio", "adafruit_st7735r"]

intersphinx_mapping = {
'python': ('https://docs.python.org/3.4', None),
Expand Down
4 changes: 4 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Ensure your device works with this simple test.
:caption: examples/featherwing_matrix_simpletest.py
:linenos:

.. literalinclude:: ../examples/featherwing_minitft_simpletest.py
:caption: examples/featherwing_minitft_simpletest.py
:linenos:

Other Examples
---------------

Expand Down
34 changes: 34 additions & 0 deletions examples/featherwing_minitft_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
This example display a CircuitPython console and
print which button that is being pressed if any
"""
import time
from adafruit_featherwing import minitft_featherwing

minitft = minitft_featherwing.MiniTFTFeatherWing()

while True:
buttons = minitft.buttons

if buttons.right:
print("Button RIGHT!")

if buttons.down:
print("Button DOWN!")

if buttons.left:
print("Button LEFT!")

if buttons.up:
print("Button UP!")

if buttons.select:
print("Button SELECT!")

if buttons.a:
print("Button A!")

if buttons.b:
print("Button B!")

time.sleep(.001)