Skip to content

Adding class parameters documentation #14

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 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ To install in a virtual environment in your current project:
Usage Example
=============

.. code-block:: python
.. code-block:: python3

import board
import adafruit_touchscreen
import board
import adafruit_touchscreen

# These pins are used as both analog and digital!
# XR, XL and YU must be analog and digital capable.
# YD just needs to be digital.
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
board.TOUCH_YD, board.TOUCH_YU)
# These pins are used as both analog and digital!
# XR, XL and YU must be analog and digital capable.
# YD just needs to be digital.
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
board.TOUCH_YD, board.TOUCH_YU)

while True:
p = ts.touch_point
if p:
print(p)
while True:
p = ts.touch_point
if p:
print(p)


Contributing
Expand Down
76 changes: 49 additions & 27 deletions adafruit_touchscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
Implementation Notes
--------------------

**Hardware:**


**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
https://circuitpython.org/downloads

"""

__version__ = "0.0.0-auto.0"
Expand All @@ -31,11 +29,18 @@


def map_range(x, in_min, in_max, out_min, out_max):

"""
Maps a number from one range to another.
Note: This implementation handles values < in_min differently than arduino's map function does.

.. note:: This implementation handles values < in_min differently
than arduino's map function does.


:return: Returns value mapped to new range
:rtype: float


"""
mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
if out_min <= out_max:
Expand All @@ -45,7 +50,42 @@ def map_range(x, in_min, in_max, out_min, out_max):

class Touchscreen:
"""A driver for common and inexpensive resistive touchscreens. Analog input
capable pins are required to read the intrinsic potentiometers"""
capable pins are required to read the intrinsic potentiometers

Create the Touchscreen object. At a minimum you need the 4 pins
that will connect to the 4 contacts on a screen. X and Y are just our
names, you can rotate and flip the data if you like. All pins must be
capable of becoming DigitalInOut pins. :attr:`y2_pin`, :attr:`x1_pin`
and :attr:`x2_pin` must also be capable of becoming AnalogIn pins.
If you know the resistance across the x1 and x2 pins when not touched,
pass that in as 'x_resistance'.
:attr:`calibration` is a tuple of two tuples, the default is
((0, 65535), (0, 65535)). The numbers are the min/max readings for the
X and Y coordinate planes, respectively. To figure these out, pass in
no calibration value and read the raw values out while touching the
panel.
:attr:`size` is a tuple that gives the X and Y pixel size of the underlying
screen. If passed in, we will automatically scale/rotate so touches
correspond to the graphical coordinate system.

:param ~microcontroller.Pin x1_pin: Data pin for Left side of the screen.
Must also be capable of becoming AnalogIn pins.
:param ~microcontroller.Pin x2_pin: Data pin for Right side of the screen.
Must also be capable of becoming AnalogIn pins.
:param ~microcontroller.Pin y1_pin: Data pin for Bottom side of the screen.
:param ~microcontroller.Pin y2_pin: Data pin for Top side of the screen.
Must also be capable of becoming AnalogIn pins.
:param int x_resistance: If you know the resistance across the x1 and x2
pins when not touched, pass that in as :attr:`x_resistance`
:param int samples: change by adjusting :attr:`samples` arg. Defaults to :const:`4`
:param int z_threshold: We can also detect the 'z' threshold, how much
its pressed. We don't register a touch unless its higher than :attr:`z_threshold`
:param (int,int),(int,int) calibration: A tuple of two tuples The numbers are the min/max
readings for the X and Y coordinate planes, respectively.
Defaults to :const:`((0, 65535), (0, 65535))`
:param int,int size: The dimensions of the screen as (x, y).

"""

def __init__(
self,
Expand All @@ -56,29 +96,11 @@ def __init__(
*,
x_resistance=None,
samples=4,
z_threshhold=10000,
z_threshold=10000,
calibration=None,
size=None
):
"""Create the Touchscreen object. At a minimum you need the 4 pins
that will connect to the 4 contacts on a screen. X and Y are just our
names, you can rotate and flip the data if you like. All pins must be
capable of becoming DigitalInOut pins. 'y2_pin', 'x1_pin' and 'x2_pin'
must also be capable of becoming AnalogIn pins.
If you know the resistance across the x1 and x2 pins when not touched,
pass that in as 'x_resistance'.
By default we oversample 4 times, change by adjusting 'samples' arg.
We can also detect the 'z' threshold, how much its prssed. We don't
register a touch unless its higher than 'z_threshold'
'calibration' is a tuple of two tuples, the default is
((0, 65535), (0, 65535)). The numbers are the min/max readings for the
X and Y coordinate planes, respectively. To figure these out, pass in
no calibration value and read the raw values out while touching the
panel.
'size' is a tuple that gives the X and Y pixel size of the underlying
screen. If passed in, we will automatically scale/rotate so touches
correspond to the graphical coordinate system.
"""

self._xm_pin = x1_pin
self._xp_pin = x2_pin
self._ym_pin = y1_pin
Expand All @@ -90,7 +112,7 @@ def __init__(
calibration = ((0, 65535), (0, 65535))
self._calib = calibration
self._size = size
self._zthresh = z_threshhold
self._zthresh = z_threshold

@property
def touch_point(self): # pylint: disable=too-many-locals
Expand Down
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/touchscreen_simpletest.py
:caption: examples/touchscreen_simpletest.py
:linenos:


Orientation Example
--------------------

Example showing how to setup the different display orientations

.. literalinclude:: ../examples/touchscreen_orientation.py
:caption: examples/touchscreen_orientation.py
:linenos:
6 changes: 6 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Table of Contents
.. toctree::
:caption: Related Products

Adafruit PyPortal - CircuitPython Powered Internet Display <https://www.adafruit.com/product/4116>

Adafruit PyPortal Titano <https://www.adafruit.com/product/4444>

Adafruit PyPortal Pynt - CircuitPython Powered Internet Display - 2.4" TFT <https://www.adafruit.com/product/4465>


.. toctree::
:caption: Other Links
Expand Down