{"id":85,"date":"2024-11-16T22:57:11","date_gmt":"2024-11-16T22:57:11","guid":{"rendered":"https:\/\/praxismechatronics.com\/?p=85"},"modified":"2024-11-24T03:01:15","modified_gmt":"2024-11-24T03:01:15","slug":"tft_espi-cheat-sheet","status":"publish","type":"post","link":"https:\/\/praxismechatronics.com\/index.php\/2024\/11\/16\/tft_espi-cheat-sheet\/","title":{"rendered":"TFT_eSPI Cheat Sheet"},"content":{"rendered":"\n<p>Quick post here to add a link to the little cheat sheet I put together for TFT_eSPI by BODMER. While there is plenty of documentation and some tutorials available, when working with an unfamiliar library, I typically look for a quick syntax\/keyword\/usage overview and I was unable to find one. <br>Most of the trouble people run into with this library is the setup, which I will not go into here as there is plenty of information available elsewhere. <\/p>\n\n\n\n<p>Below the document embed\/download link are brief proof-of-concept programs showing the creation of a clickable button using the standard TFT_eSPI library by drawing discreet shapes, as well as the TFT_eWidget approach to accomplishing the same task. <\/p>\n\n\n\n<p>Good luck!<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mike at <strong>Praxis Mechatronics<\/strong><\/li>\n<\/ul>\n\n\n\n<div data-wp-interactive=\"core\/file\" class=\"wp-block-file\"><object data-wp-bind--hidden=\"!state.hasPdfPreview\" hidden class=\"wp-block-file__embed\" data=\"https:\/\/praxismechatronics.com\/wp-content\/uploads\/2024\/11\/tft_eSPI-cheat-sheet-PM.pdf\" type=\"application\/pdf\" style=\"width:100%;height:600px\" aria-label=\"Embed of tft_eSPI-cheat-sheet-PM.\"><\/object><a id=\"wp-block-file--media-d84047c5-09d3-4403-9422-d1b50e1290df\" href=\"https:\/\/praxismechatronics.com\/wp-content\/uploads\/2024\/11\/tft_eSPI-cheat-sheet-PM.pdf\">tft_eSPI-cheat-sheet-PM<\/a><a href=\"https:\/\/praxismechatronics.com\/wp-content\/uploads\/2024\/11\/tft_eSPI-cheat-sheet-PM.pdf\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-d84047c5-09d3-4403-9422-d1b50e1290df\">Download<\/a><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ TFT_eSPI library to create a single clickable rectangle (button)\n#include &lt;TFT_eSPI.h&gt; \/\/ Include the TFT_eSPI library\n\nTFT_eSPI tft = TFT_eSPI(); \/\/ Create TFT object\n\n\/\/ Define button properties\n#define BUTTON_X 100\n#define BUTTON_Y 100\n#define BUTTON_W 80\n#define BUTTON_H 40\n#define BUTTON_COLOR TFT_BLUE\n#define BUTTON_TEXT_COLOR TFT_WHITE\n\nvoid setup() {\n  tft.begin();               \/\/ Initialize the display\n  tft.setRotation(1);        \/\/ Set display rotation\n  tft.fillScreen(TFT_BLACK); \/\/ Clear screen\n\n  \/\/ Draw a simple button\n  drawButton(false);\n}\n\nvoid loop() {\n  uint16_t x, y;\n\n  \/\/ Check for touch input\n  if (tft.getTouch(&amp;x, &amp;y)) {\n    \/\/ Check if the touch is within the button bounds\n    if (x &gt; BUTTON_X - BUTTON_W \/ 2 &amp;&amp; x &lt; BUTTON_X + BUTTON_W \/ 2 &amp;&amp;\n        y &gt; BUTTON_Y - BUTTON_H \/ 2 &amp;&amp; y &lt; BUTTON_Y + BUTTON_H \/ 2) {\n      \/\/ If the button is pressed, redraw it as \"pressed\"\n      drawButton(true);\n      delay(200); \/\/ Debounce delay\n      drawButton(false); \/\/ Restore the button\n    }\n  }\n}\n\n\/\/ Function to draw the button\nvoid drawButton(bool pressed) {\n  uint16_t color = pressed ? TFT_RED : BUTTON_COLOR;\n  tft.fillRoundRect(BUTTON_X - BUTTON_W \/ 2, BUTTON_Y - BUTTON_H \/ 2, BUTTON_W, BUTTON_H, 5, color);\n  tft.drawRoundRect(BUTTON_X - BUTTON_W \/ 2, BUTTON_Y - BUTTON_H \/ 2, BUTTON_W, BUTTON_H, 5, TFT_WHITE);\n  tft.setTextColor(BUTTON_TEXT_COLOR, color);\n  tft.setTextDatum(MC_DATUM);\n  tft.drawString(\"Click\", BUTTON_X, BUTTON_Y);\n}\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><br>\/\/ TFT_eWidget approach to the same task<br>#include &lt;TFT_eSPI.h><br>#include &lt;TFT_eWidget.h><br><br>\/\/ Create TFT and Button objects<br>TFT_eSPI tft = TFT_eSPI();<br>ButtonWidget *btn = new ButtonWidget(&amp;tft); \/\/ ButtonWidget requires TFT pointer in constructor<br><br>\/\/ Define button properties<br>#define BUTTON_X 100<br>#define BUTTON_Y 100<br>#define BUTTON_W 80<br>#define BUTTON_H 40<br>#define BUTTON_COLOR TFT_BLUE<br>#define BUTTON_TEXT_COLOR TFT_WHITE<br><br>\/\/ Button label buffer<br>char buttonLabel[] = \"Click\";<br><br>void setup() {<br>    tft.begin();<br>    tft.setRotation(1);<br>    tft.fillScreen(TFT_BLACK);<br>    <br>    \/\/ Initialize touch calibration data<br>    \/\/uint16_t calData[5] = { 195, 3456, 301, 3456, 7 }; \/\/ Example values - use your actual calibration data<br>    \/\/tft.setTouch(calData);<br>    <br>    \/\/ Configure the button<br>    btn->initButtonUL(<br>        BUTTON_X - BUTTON_W\/2,  \/\/ x coord of upper left<br>        BUTTON_Y - BUTTON_H\/2,  \/\/ y coord of upper left<br>        BUTTON_W,   \/\/ width<br>        BUTTON_H,   \/\/ height<br>        TFT_WHITE,  \/\/ outline color<br>        BUTTON_COLOR, \/\/ fill color when not pressed<br>        TFT_RED,    \/\/ fill color when pressed<br>        buttonLabel,    \/\/ button text<br>        1           \/\/ text size<br>    );<br>    <br>    \/\/ Set text color<br>    btn->setTextColor(BUTTON_TEXT_COLOR);<br>    <br>    \/\/ Draw the button for the first time<br>    btn->drawButton(false);<br>}<br><br>void loop() {<br>    static uint16_t x, y;<br>    bool pressed = false;<br>    <br>    \/\/ Check for touch input<br>    if (tft.getTouch(&amp;x, &amp;y)) {<br>        \/\/ Check if touch is within button bounds<br>        if (btn->contains(x, y)) {<br>            pressed = true;<br>            btn->drawButton(true); \/\/ Draw pressed state<br>            delay(200);           \/\/ Debounce delay<br>        }<br>    }<br>    <br>    \/\/ If button was pressed, restore unpressed state<br>    if (pressed) {<br>        btn->drawButton(false);<br>        pressed = false;<br>    }<br>}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick post here to add a link to the little cheat sheet I put together for TFT_eSPI by BODMER. While there is plenty of documentation and some tutorials available, when working with an unfamiliar library, I typically look for a quick syntax\/keyword\/usage overview and I was unable to find one. Most of the trouble people [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":89,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,1,13,15,11,16,17],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-blog","category-c","category-esp32","category-python","category-tft","category-tft_espi"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":5,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/media\/89"}],"wp:attachment":[{"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/praxismechatronics.com\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}