Giter VIP home page Giter VIP logo

adafruit_circuitpython_touchscreen's Introduction

Introduction

Documentation Status Discord Build Status Code Style: Black

CircuitPython library for 4-wire resistive touchscreens

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-touchscreen

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-touchscreen

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-touchscreen

Usage Example

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)

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

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

adafruit_circuitpython_touchscreen's People

Contributors

cedargrovestudios avatar dhalbert avatar evaherrada avatar foamyguy avatar jposada202020 avatar kattni avatar kmatch98 avatar ladyada avatar prcutler avatar richarda1 avatar rtwfroody avatar siddacious avatar sommersoft avatar tcfranks avatar tekktrik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_circuitpython_touchscreen's Issues

Missing Type Annotations

There are missing type annotations for some functions in this library.

The typing module does not exist on CircuitPython devices so the import needs to be wrapped in try/except to catch the error for missing import. There is an example of how that is done here:

try:
    from typing import List, Tuple
except ImportError:
    pass

Once imported the typing annotations for the argument type(s), and return type(s) can be added to the function signature. Here is an example of a function that has had this done already:

def wrap_text_to_pixels(
    string: str, max_width: int, font=None, indent0: str = "", indent1: str = ""
) -> List[str]:

If you are new to Git or Github we have a guide about contributing to our projects here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

There is also a guide that covers our CI utilities and how to run them locally to ensure they will pass in Github Actions here: https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code In particular the pages: Sharing docs on ReadTheDocs and Check your code with pre-commit contain the tools to install and commands to run locally to run the checks.

If you are attempting to resolve this issue and need help, you can post a comment on this issue and tag both @FoamyGuy and @kattni or reach out to us on Discord: https://adafru.it/discord in the #circuitpython-dev channel.

The following locations are reported by mypy to be missing type annotations:

  • adafruit_touchscreen.py:31
  • adafruit_touchscreen.py:90

Please add rotation to Touchscreen

I am not sure how it could be added to this library, but I created the following code to recalculate the touch coordinates based on the orientation of the screen set by board.DISPLAY.rotation. This fixes an issue with how the hotspots are setup from the Button() function used in Displayio.

### Function for recalculating .touch_point based on Display rotation
def touch_rotate():
    touchData = ts.touch_point
    if touchData:
        if display.rotation == 0:
            return touchData
        elif display.rotation == 90:
            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)
            return (touchData[1],int(touchX) )
        elif display.rotation == 180:
            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)
            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)
            return (int(touchX), int(touchY))
        elif display.rotation == 270:
            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)
            return (int(touchY),touchData[0] )

Will not import in python script on raspberry pi

I installed this library on my raspberry pi zero w by following the instructions on the README which dictate I use pip3.

I imported the library in my script in the same way shown in the example but I receive the following error...

Traceback (most recent call last):
  File "touch_import.py", line 2, in <module>
    import adafruit_touchscreen
  File "/usr/local/lib/python3.7/dist-packages/adafruit_touchscreen.py", line 47, in <module>
    from analogio import AnalogIn
  File "/usr/local/lib/python3.7/dist-packages/analogio.py", line 21, in <module>
    raise NotImplementedError("analogio not supported for this board.")
NotImplementedError: analogio not supported for this board.

on ILI9341 more pressure results in higher z values

I have https://www.adafruit.com/product/2478, and the only way I can get it to work is with the following diff:

diff --git a/adafruit_touchscreen.py b/adafruit_touchscreen.py
index 5bf0fe0..4f28761 100644
--- a/adafruit_touchscreen.py
+++ b/adafruit_touchscreen.py
@@ -136,7 +136,7 @@ class Touchscreen:
                 with AnalogIn(self._yp_pin) as y_p:
                     z_2 = y_p.value
         # print(z_1, z_2)
-        z = 65535 - (z_2 - z_1)
+        z = z_2 + z_1
         if z > self._zthresh:
             with DigitalInOut(self._yp_pin) as y_p:

and a threshold value of around 40000.
Without this diff, the bottom right of the screen never registers a press.

I don't know much about these kinds of screens. Presumably this code works for other screens. Do some screens reverse this? I triple checked my wires and I don't think I wired anything backwards.

I also had to reverse the X calibration to make the coordinates correspond to the ones used when drawing on the screen:

                calibration=([59889, 12545], [16373, 61473]),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.