{"id":2302,"date":"2020-01-16T22:59:48","date_gmt":"2020-01-16T22:59:48","guid":{"rendered":"http:\/\/www.richa1.com\/RichardAlbritton\/?p=2302"},"modified":"2020-02-12T00:56:54","modified_gmt":"2020-02-12T00:56:54","slug":"function-for-rotating-the-touchscreen-matrix-to-match-display-orientation","status":"publish","type":"post","link":"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/","title":{"rendered":"Function for rotating the TouchScreen matrix to match Display orientation."},"content":{"rendered":"\n<p> Ok, so I ran into an issue when I rotated Display that repositioned the Button images but not the touchscreen coordinates. So the button hotspots did not remap. Thankfully I fingered&nbsp;out a way to recalculate the screen touch coordinates so that they line up with the Display orientation.<\/p>\n\n\n\n<!--more-->\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"660\" height=\"372\" src=\"https:\/\/www.youtube.com\/embed\/rIVqSVtrogM?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<p>I have this setup as a function&nbsp;that can be used to replace&nbsp;<strong>.touch_point<\/strong> when figuring out what part of the screen was touched. This function will look at the <strong>size<\/strong> that we set for the Touchscreen as well as the <strong>rotation<\/strong> that we set for display. Based on that information it will recalculate the same relative position based on where the screen was touched.<\/p>\n\n\n\n<p>This function assumes that you have set up the display and touchscreen like the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>display = board.DISPLAY\ndisplay.rotation=90\n\n# Touchscreen setup\nts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,\n                                      board.TOUCH_YD, board.TOUCH_YU,\n                                      calibration=((5200, 59000), (5800, 57000)),\n                                      size=(320, 240))<\/code><\/pre>\n\n\n\n<p>Next you will want to add the following function to your code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### Function for recalculating .touch_point based on Display rotation\ndef touch_rotate():\n    touchData = ts.touch_point\n    if touchData:\n        if display.rotation == 0:\n            return touchData\n        elif display.rotation == 90:\n            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)\n            return (touchData[1],int(touchX) )\n        elif display.rotation == 180:\n            touchX = adafruit_touchscreen.map_range(touchData[0], 0, ts._size[0], ts._size[0], 0)\n            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)\n            return (int(touchX), int(touchY))\n        elif display.rotation == 270:\n            touchY = adafruit_touchscreen.map_range(touchData[1], 0, ts._size[1], ts._size[1], 0)\n            return (int(touchY),touchData[0] )<\/code><\/pre>\n\n\n\n<p>To use this you will call&nbsp;<code>touch_rotate()<\/code>&nbsp;when you need to know where the screen was touched rather than using&nbsp;<code>ts.touch_point<\/code><\/p>\n\n\n\n<p>This is most useful for the onscreen buttons.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2302\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2302\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Ok, so I ran into an issue when I rotated Display that repositioned the Button images but not the touchscreen coordinates. So the button hotspots did not remap. Thankfully I fingered&nbsp;out a way to recalculate the screen touch coordinates so that they line up with the Display orientation.<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2302\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2302\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/function-for-rotating-the-touchscreen-matrix-to-match-display-orientation\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":2325,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[191,57,79],"tags":[23,190,189],"jetpack_featured_media_url":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-content\/uploads\/2020\/01\/CircuitPython.png","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5AhH6-B8","_links":{"self":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2302"}],"collection":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/comments?post=2302"}],"version-history":[{"count":4,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2302\/revisions"}],"predecessor-version":[{"id":2308,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2302\/revisions\/2308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/media\/2325"}],"wp:attachment":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/media?parent=2302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/categories?post=2302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/tags?post=2302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}