{"id":13613112,"url":"https://github.com/robert-hh/XPT2046-touch-pad-driver","last_synced_at":"2025-04-13T15:32:30.556Z","repository":{"id":82726508,"uuid":"53210966","full_name":"robert-hh/XPT2046-touch-pad-driver","owner":"robert-hh","description":"A driver for the XPT2046 touch pad controller used in many TFT modules","archived":false,"fork":false,"pushed_at":"2021-02-18T10:58:38.000Z","size":56,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-22T02:14:22.893Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robert-hh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-05T16:28:55.000Z","updated_at":"2024-08-01T20:44:30.304Z","dependencies_parsed_at":null,"dependency_job_id":"f153fbb0-3c03-4bec-abf9-cdecda5d9236","html_url":"https://github.com/robert-hh/XPT2046-touch-pad-driver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FXPT2046-touch-pad-driver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FXPT2046-touch-pad-driver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FXPT2046-touch-pad-driver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FXPT2046-touch-pad-driver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robert-hh","download_url":"https://codeload.github.com/robert-hh/XPT2046-touch-pad-driver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248735918,"owners_count":21153501,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:00:39.866Z","updated_at":"2025-04-13T15:32:30.309Z","avatar_url":"https://github.com/robert-hh.png","language":"Python","funding_links":[],"categories":["精选驱动库","Libraries"],"sub_categories":["传感器","Sensors"],"readme":"#touch pad Class for the XPT2046 controller\n\n**Description**\n\nA Python class for using a resistive touch pad with a XPT2046 controller. This port uses a software SPI for communication to the TFT, whic uses the following GPIO ports:\n\n- X12 for Clock\n- X11 for Data Out (from Pyboard to XPT2046)\n- Y2 for Data In  (from XPT2046 to Pyboard)\n\nCS of the touch pad must be tied to GND. The touch pad will typically uses in combination with a TFT. In my case, it was glued to a 4.3\" TFT with an SSD1963 controller. The class itself does not rely on an TFT, but for calibration a TFT is used.\n\nAt the moment, the code is a basic package. It will deliver touch events. But the mapping to a TFT's coordinates is tested with that single TFT in Landscape mode only.\n\nThe touch pad is used in 12 bit mode, returning values in the range of 0..4095 for the coordinates. A single raw sampling takes about 60µs. The result is somewhat noisy, and the touch pad has the habit of creating false touches in the transition of press and release. The function get_touch caters for that.\n\n\n**Functions**\n```\nCreate instance:\n\nmytouch = TOUCH(controller, asyn=False, *, confidence=5, margin=50,\n          delay=10, calibration=None, spi=None)\n    controller: String with the controller model. At the moment, it is ignored\n    asyn: Set True if asynchronous operation intended. In this instance the\n        uasyncio library must be available.\n    confidence: confidence level - number of consecutive touches with a\n        margin smaller than the given level which the function will sample\n        until it accepts it as a valid touch\n    margin: Difference from mean centre at which touches are considered\n        at the same position\n    delay: Delay between samples in ms. (n/a if asynchronous)\n    calibration: Tuple of 8 numbers, which transpose touch pad coordinates\n        into TFT  coordinates.\n        You can determine these with the tool calibraty.py (see below) of the\n        package. A vector of (0, 1, 0, 1, 0, 1, 0, 1) will deliver the raw\n        values.  The calibration is performed typically once. Once determined,\n        you may also code these values into the sources.\n    spi: A spi object which is used for communiation. If None is supplied, the\n        driver creates this object with the pins X12, X11 and Y2\n\nMethods:\n\ntouch_parameter(confidence=5, margin=10, delay=10, calibration=None)\n    # Set the operational parameters of the touch pad. All parameters are optional\n    confidence: confidence level - number of consecutive touches with a\n        margin smaller than the given level which the function will sample\n        until it accepts it as a valid touch\n    margin: Difference from mean centre at which touches are considered\n        at the same position\n    delay: Delay between samples in ms. (n/a if asynchronous)\n    calibration: Tuple of 8 numbers, which transpose touch pad coordinates\n        into TFT  coordinates.\n\nget_touch(initial=True, wait=True, raw=False, timeout=None)\n    # This is the major data entry function. Parameters:\n    initial: if True, wait for a non-touch state of the touch pad before getting\n        the touch coordinates. This is the natural behavior. If False, get the next touch\n        immediately and do not what for the stylus to be released.\n    wait: If True, wait for a valid touch event. If False, return immediately if no\n        touch is made.\n    raw: Setting whether raw touch coordinates (True) or normalized ones\n        (False) are returned setting the calibration vector to\n        (0, 1, 0, 1, 0, 1, 0, 1) result in a identity mapping\n    timeout: Timeout for the function, unit ms, for all situations where the function is\n        told to wait, e.g. initial = True or wait = True.\n        A value of None is considered as a timeout of an hour.\n\n    The function returns a two value tuple (x,y) of the touch coordinates,\n    or 'None', if either no touch is pressed or the timeout triggers.\n\ndo_normalize(touch)\n    # Transpose touch coordinates into TFT coordinates. The function requires\n      the calibration values to be set to a reasonable value. It is called\n      within get_touch too. Parameter:\n    touch: a touch pad value tuple returned by get_touch() in raw mode\n        or raw_touch()\n----- lower level functions ---\n\nraw_touch()\n    # determine the raw touch value and return immediately. The return value is a pair of\n      touch pad coordinates, is a touch is present, or 'None'\n\ntouch_talk(command, bits)\n    # send commands to the touch pad controller and retrieves 'bits' data from it.\n      It will always perform and return. No checking is done for the command value\n      and the returned information.\n```\n\n**Files:**\n- touch.py: Source file with comments.\n- calibration.py: Code to determine the calibration of the touch pad, which\nallows to map between touch pad and screen coordinates. You will be asked\nto touch four points at the screen indicated by a cross-hair.\nThe confidence level is set high, so keep your hand steady and use a stylus.\nIf it fails at a certain point, release and touch again.\nThe determined values are printed on the screen and at the USB interface.\nSo you can copy them from there. Once the values are know, they are set\ntemporarily, and you may try them. Just touch the screen. At the point of\ntouching, a small green circle should light up. If the match is bad,\nrepeat the calibration.\n- touchtest.py: Another sample test program, which creates a small four button\nkeypad, which is defined by a table.\n- README.md: this one\n- LICENSE: The MIT license file\n\n**To Do**\n- consider ISR mode\n- test portrait mode\n\n**Short Version History**\n\n**0.1**\nInitial release with the basic functions\n\n**1.0**\nAdded an asynchronous mode implemented by Peter Hinch\n\n**1.1**\nAsynchronous mode adapted to use uasyncio\n\n**1.2**\nReplace bit-bang communication by SPI built-in methods, making it less hardware\ndependent.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2FXPT2046-touch-pad-driver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobert-hh%2FXPT2046-touch-pad-driver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2FXPT2046-touch-pad-driver/lists"}