1602浏览
楼主: 驴友花雕

[项目] 【Arduino】168种传感器模块系列实验(165)---2.4寸TFT液晶触...

[复制链接]

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 10:37:58

实验场景图

48.jpg
回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 10:41:56


修改后的项目18,实验程序

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目十八:测试并使用 TFT 触摸屏驱动程序 0x9341
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <Adafruit_GFX.h>
  12. #include <TouchScreen.h>
  13. #include <Adafruit_TFTLCD.h>
  14. #define YP A2  // must be an analog pin, use "An" notation!
  15. #define XM A1  // must be an analog pin, use "An" notation!
  16. #define YM 6   // can be a digital pin
  17. #define XP 7   // can be a digital pin
  18. #define TS_MINX 150
  19. #define TS_MINY 120
  20. #define TS_MAXX 850
  21. #define TS_MAXY 891
  22. //SPI Communication
  23. #define LCD_CS A3
  24. #define LCD_CD A2
  25. #define LCD_WR A1
  26. #define LCD_RD A0
  27. // optional
  28. #define LCD_RESET A4
  29. //Color Definitons
  30. #define BLACK   0x0000
  31. #define WHITE   0xFFFF
  32. #define RED     0xF800
  33. #define GREEN   0x07E0
  34. #define CYAN    0x07FF
  35. #define MINPRESSURE 1
  36. #define MAXPRESSURE 1000
  37. // For better pressure precision, we need to know the resistance
  38. // between X+ and X- Use any multimeter to read it
  39. // For the one we're using, its 300 ohms across the X plate
  40. // Pins A2-A6
  41. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);
  42. //2.4 = 240 x 320
  43. //Height 319 to fit on screen
  44. //Size of key containers 70px
  45. #define BOXSIZE 70
  46. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  47. //Container variables for touch coordinates
  48. int X, Y, Z;
  49. //Screen height without hidden pixel
  50. double tHeight = tft.height() - 1;
  51. //Centering the mid square
  52. double center = (tft.width() / 2) - (BOXSIZE / 2);
  53. //Space between squares
  54. double padding = 10;
  55. //Position of squares to the left and right of center
  56. double fromCenter = BOXSIZE + padding;
  57. //Second row Y-Axis position
  58. double secondRow = BOXSIZE + padding;
  59. //Third row Y-Axis position
  60. double thirdRow = secondRow + BOXSIZE + padding;
  61. //Fourth row Y-Axis position
  62. double fourthRow = thirdRow + BOXSIZE + padding;
  63. //Y-Axis align for all squares
  64. double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;
  65. //Left column starting x posision
  66. double leftColPositionX = center - fromCenter;
  67. //Mid column starting x posision
  68. double midColPositionX = center;
  69. //Right column starting x posision
  70. double rightColPositionX = center + fromCenter;
  71. void setup() {
  72.   Serial.begin(9600);
  73.   tft.reset();
  74.   tft.begin(0x9341);
  75.   //Background color
  76.   tft.fillScreen(BLACK);
  77.   Serial.println(F("准备就绪OK!"));
  78.   createButtons();
  79.   Serial.println(F("请按 TFT 屏幕上的任意按钮:"));
  80. }
  81. void loop() {
  82.   retrieveTouch();
  83.   int boxHeightRow1 = verticalAlign + BOXSIZE;
  84.   int boxHeightRow2 = secondRow + BOXSIZE;
  85.   int boxHeightRow3 = thirdRow + BOXSIZE;
  86.   int boxHeightRow4 = fourthRow + BOXSIZE;
  87.   if (Z > MINPRESSURE && Z < MAXPRESSURE) {
  88.     //Check if element clicked is in left column
  89.     if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
  90.       //Check if element clicked is in row 1
  91.       if (Y > verticalAlign) {
  92.         if (Y < boxHeightRow1) {
  93.           Serial.println("1");
  94.           delay(150);
  95.         }
  96.         //Check if element clicked is in row 2
  97.         else if (Y < boxHeightRow2) {
  98.           Serial.println("4");
  99.           delay(150);
  100.         }
  101.         //Check if element clicked is in row 3
  102.         else if (Y < boxHeightRow3) {
  103.           Serial.println("7");
  104.           delay(150);
  105.         }
  106.         //Check if element clicked is in row 4
  107.         else if (Y < boxHeightRow4) {
  108.           Serial.println("0");
  109.           delay(150);
  110.         }
  111.       }
  112.       //Check if element clicked is in mid column
  113.     } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
  114.       //Check if element clicked is in row 1
  115.       if (Y > verticalAlign) {
  116.         if (Y < boxHeightRow1) {
  117.           Serial.println("2");
  118.           delay(150);
  119.         }
  120.         //Check if element clicked is in row 2
  121.         else if (Y < boxHeightRow2) {
  122.           Serial.println("5");
  123.           delay(150);
  124.         }
  125.         //Check if element clicked is in row 3
  126.         else if (Y < boxHeightRow3) {
  127.           Serial.println("8");
  128.           delay(150);
  129.         }
  130.         //Check if element clicked is in row 4
  131.         else if (Y < boxHeightRow4) {
  132.           Serial.println("0");
  133.           delay(150);
  134.         }
  135.       }
  136.       //Check if element clicked is in third column
  137.     } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
  138.       if (Y > verticalAlign) {
  139.         //Check if element clicked is in row 1
  140.         if (Y < boxHeightRow1) {
  141.           Serial.println("3");
  142.           delay(150);
  143.         }
  144.         //Check if element clicked is in row 2
  145.         else if (Y < boxHeightRow2) {
  146.           Serial.println("6");
  147.           delay(150);
  148.         }
  149.         //Check if element clicked is in row 3
  150.         else if (Y < boxHeightRow3) {
  151.           Serial.println("9");
  152.           delay(150);
  153.         }
  154.         //Check if element clicked is in row 3
  155.         else if (Y < boxHeightRow4) {
  156.           Serial.println(".");
  157.           delay(150);
  158.         }
  159.       }
  160.     }
  161.   }
  162. }
  163. void retrieveTouch()
  164. {
  165.   digitalWrite(13, HIGH);
  166.   TSPoint p = ts.getPoint();
  167.   digitalWrite(13, LOW);
  168.   //If sharing pins, you'll need to fix the directions of the touchscreen pins
  169.   pinMode(XM, OUTPUT);
  170.   pinMode(YP, OUTPUT);
  171.   //Scale from 0->1023 to tft.width
  172.   X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  173.   Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  174.   Z = p.z;
  175. }
  176. void createButtons() {
  177.   //(initial x,initial y,width,height,color)
  178.   double secondRowVertialAlign = secondRow + verticalAlign;
  179.   double thirdRowVertialAlign = thirdRow + verticalAlign;
  180.   double fourthRowVertialAlign = fourthRow + verticalAlign;
  181.   /***Draw squares with specified dimensions and position (No Fill)***/
  182.   //(initial x,initial y,width,height,color)
  183.   //First Row
  184.   tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);
  185.   tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);
  186.   tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, WHITE);
  187.   //Second Row
  188.   tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);
  189.   tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);
  190.   tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, CYAN);
  191.   //Third Row
  192.   tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);
  193.   tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);
  194.   tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREEN);
  195.   //Fourth Row
  196.   tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, RED);
  197.   tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, RED);
  198. }
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 10:45:49

触摸屏幕方格后,实验串口返回情况



47.jpg





回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 11:24:54

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目十九:Arduino Uno 数字键盘(浅灰色)

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目十九:Arduino Uno 数字键盘(浅灰色)
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <Adafruit_GFX.h>
  12. #include <TouchScreen.h>
  13. #include <Adafruit_TFTLCD.h>
  14. #define YP A2  // must be an analog pin, use "An" notation!
  15. #define XM A1  // must be an analog pin, use "An" notation!
  16. #define YM 6   // can be a digital pin
  17. #define XP 7  // can be a digital pin
  18. #define TS_MINX 150
  19. #define TS_MINY 120
  20. #define TS_MAXX 850
  21. #define TS_MAXY 891
  22. //SPI Communication
  23. #define LCD_CS A3
  24. #define LCD_CD A2
  25. #define LCD_WR A1
  26. #define LCD_RD A0
  27. // optional
  28. #define LCD_RESET A4
  29. //Color Definitons
  30. #define BLACK     0x0000
  31. #define BLUE      0x001F
  32. #define GREY      0xCE79
  33. #define LIGHTGREY 0xDEDB
  34. #define MINPRESSURE 1
  35. #define MAXPRESSURE 1000
  36. // For better pressure precision, we need to know the resistance
  37. // between X+ and X- Use any multimeter to read it
  38. // For the one we're using, its 300 ohms across the X plate
  39. // Pins A2-A6
  40. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);
  41. //Size of key containers 70px
  42. #define BOXSIZE 70
  43. //2.4 = 240 x 320
  44. //Height 319 to fit on screen
  45. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  46. //Container variables for touch coordinates
  47. int X, Y, Z;
  48. //Screen height without hidden pixel
  49. double tHeight = tft.height() - 1;
  50. //Centering the mid square
  51. double center = (tft.width() / 2) - (BOXSIZE / 2);
  52. //Space between squares
  53. double padding = 10;
  54. //Position of squares to the left and right of center
  55. double fromCenter = BOXSIZE + padding;
  56. //Second row Y-Axis position
  57. double secondRow = BOXSIZE + padding;
  58. //Third row Y-Axis position
  59. double thirdRow = secondRow + BOXSIZE + padding;
  60. //Fourth row Y-Axis position
  61. double fourthRow = thirdRow + BOXSIZE + padding;
  62. //Y-Axis align for all squares
  63. double verticalAlign = (tHeight - ((BOXSIZE * 4) + (padding * 3))) / 2;
  64. //Left column starting x posision
  65. double leftColPositionX = center - fromCenter;
  66. //Mid column starting x posision
  67. double midColPositionX = center;
  68. //Right column starting x posision
  69. double rightColPositionX = center + fromCenter;
  70. void setup() {
  71.   Serial.begin(9600);
  72.   tft.reset();
  73.   tft.begin(0x9341);
  74.   //Background color
  75.   tft.fillScreen(LIGHTGREY);
  76.   createButtons();
  77.   insertNumbers();
  78.   Serial.println(F("Press any button on the TFT screen: "));
  79. }
  80. void loop() {
  81.   retrieveTouch();
  82.   int boxHeightRow1 = verticalAlign + BOXSIZE;
  83.   int boxHeightRow2 = secondRow + BOXSIZE;
  84.   int boxHeightRow3 = thirdRow + BOXSIZE;
  85.   int boxHeightRow4 = fourthRow + BOXSIZE;
  86.   if (Z > MINPRESSURE && Z < MAXPRESSURE) {
  87.     //Check if element clicked is in left column
  88.     if (X > leftColPositionX && X < (leftColPositionX + BOXSIZE)) {
  89.       //Check if element clicked is in row 1
  90.       if (Y > verticalAlign) {
  91.         if (Y < boxHeightRow1) {
  92.           Serial.println("1");
  93.           delay(150);
  94.         }
  95.         //Check if element clicked is in row 2
  96.         else if (Y < boxHeightRow2) {
  97.           Serial.println("4");
  98.           delay(150);
  99.         }
  100.         //Check if element clicked is in row 3
  101.         else if (Y < boxHeightRow3) {
  102.           Serial.println("7");
  103.           delay(150);
  104.         }
  105.         //Check if element clicked is in row 4
  106.         else if (Y < boxHeightRow4) {
  107.           Serial.println("0");
  108.           delay(150);
  109.         }
  110.       }
  111.       //Check if element clicked is in mid column
  112.     } else if (X > midColPositionX && X < (midColPositionX + BOXSIZE)) {
  113.       //Check if element clicked is in row 1
  114.       if (Y > verticalAlign) {
  115.         if (Y < boxHeightRow1) {
  116.           Serial.println("2");
  117.           delay(150);
  118.         }
  119.         //Check if element clicked is in row 2
  120.         else if (Y < boxHeightRow2) {
  121.           Serial.println("5");
  122.           delay(150);
  123.         }
  124.         //Check if element clicked is in row 3
  125.         else if (Y < boxHeightRow3) {
  126.           Serial.println("8");
  127.           delay(150);
  128.         }
  129.         //Check if element clicked is in row 4
  130.         else if (Y < boxHeightRow4) {
  131.           Serial.println("0");
  132.           delay(150);
  133.         }
  134.       }
  135.       //Check if element clicked is in third column
  136.     } else if (X > rightColPositionX && X < (rightColPositionX + BOXSIZE)) {
  137.       if (Y > verticalAlign) {
  138.         //Check if element clicked is in row 1
  139.         if (Y < boxHeightRow1) {
  140.           Serial.println("3");
  141.           delay(150);
  142.         }
  143.         //Check if element clicked is in row 2
  144.         else if (Y < boxHeightRow2) {
  145.           Serial.println("6");
  146.           delay(150);
  147.         }
  148.         //Check if element clicked is in row 3
  149.         else if (Y < boxHeightRow3) {
  150.           Serial.println("9");
  151.           delay(150);
  152.         }
  153.         //Check if element clicked is in row 3
  154.         else if (Y < boxHeightRow4) {
  155.           Serial.println(".");
  156.           delay(150);
  157.         }
  158.       }
  159.     }
  160.   }
  161. }
  162. void retrieveTouch()
  163. {
  164.   digitalWrite(13, HIGH);
  165.   TSPoint p = ts.getPoint();
  166.   digitalWrite(13, LOW);
  167.   //If sharing pins, you'll need to fix the directions of the touchscreen pins
  168.   pinMode(XM, OUTPUT);
  169.   pinMode(YP, OUTPUT);
  170.   //Scale from 0->1023 to tft.width
  171.   X = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  172.   Y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
  173.   Z = p.z;
  174. }
  175. void createButtons() {
  176.   //(initial x,initial y,width,height,color)
  177.   double secondRowVertialAlign = secondRow + verticalAlign;
  178.   double thirdRowVertialAlign = thirdRow + verticalAlign;
  179.   double fourthRowVertialAlign = fourthRow + verticalAlign;
  180.   /***Draw filled squares with specified dimensions and position***/
  181.   //First Row
  182.   tft.fillRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);
  183.   tft.fillRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);
  184.   tft.fillRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, GREY);
  185.   //Second Row
  186.   tft.fillRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  187.   tft.fillRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  188.   tft.fillRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  189.   //Third Row
  190.   tft.fillRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  191.   tft.fillRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  192.   tft.fillRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  193.   //Fourth Row
  194.   tft.fillRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, GREY);
  195.   tft.fillRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, GREY);
  196.   /***Draw Borders around squares***/
  197.   //First Row
  198.   tft.drawRect(leftColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);
  199.   tft.drawRect(midColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);
  200.   tft.drawRect(rightColPositionX, verticalAlign, BOXSIZE, BOXSIZE, BLACK);
  201.   //Second Row
  202.   tft.drawRect(leftColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  203.   tft.drawRect(midColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  204.   tft.drawRect(rightColPositionX, secondRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  205.   //Third Row
  206.   tft.drawRect(leftColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  207.   tft.drawRect(midColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  208.   tft.drawRect(rightColPositionX, thirdRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  209.   //Fourth Row
  210.   tft.drawRect(leftColPositionX, fourthRowVertialAlign, (BOXSIZE * 2) + padding, BOXSIZE, BLACK);
  211.   tft.drawRect(rightColPositionX, fourthRowVertialAlign, BOXSIZE, BOXSIZE, BLACK);
  212. }
  213. void insertNumbers() {
  214.   //Centers text horizontally on all three columns
  215.   double leftColCursorX   = leftColPositionX + (BOXSIZE / 3);
  216.   double midColCursorX    = midColPositionX  + (BOXSIZE / 3);
  217.   double rightColCursorX  = rightColPositionX + (BOXSIZE / 3);
  218.   //Centers text horizontally on all four rows
  219.   double firstRowCursorY  = verticalAlign + (BOXSIZE / 3);
  220.   double secondRowCursorY = secondRow + firstRowCursorY;
  221.   double thirdRowCursorY  = thirdRow  + firstRowCursorY;
  222.   double fourthRowCursorY = fourthRow + firstRowCursorY;
  223.   tft.setTextSize(4);
  224.   tft.setTextColor(BLACK);
  225.   //Insert Number 1
  226.   tft.setCursor(leftColCursorX, firstRowCursorY);
  227.   tft.println("1");
  228.   //Insert Number 2
  229.   tft.setCursor(midColCursorX, firstRowCursorY);
  230.   tft.println("2");
  231.   //Insert Number 3
  232.   tft.setCursor(rightColCursorX, firstRowCursorY);
  233.   tft.println("3");
  234.   //Insert Number 4
  235.   tft.setCursor(leftColCursorX, secondRowCursorY);
  236.   tft.println("4");
  237.   //Insert Number 5
  238.   tft.setCursor(midColCursorX, secondRowCursorY);
  239.   tft.println("5");
  240.   //Insert Number 6
  241.   tft.setCursor(rightColCursorX, secondRowCursorY);
  242.   tft.println("6");
  243.   //Insert Number 7
  244.   tft.setCursor(leftColCursorX, thirdRowCursorY);
  245.   tft.println("7");
  246.   //Insert Number 8
  247.   tft.setCursor(midColCursorX, thirdRowCursorY);
  248.   tft.println("8");
  249.   //Insert Number 9
  250.   tft.setCursor(rightColCursorX, thirdRowCursorY);
  251.   tft.println("9");
  252.   //Insert Number 0
  253.   tft.setCursor(leftColPositionX + BOXSIZE, fourthRowCursorY);
  254.   tft.println("0");
  255.   //Insert Period Character
  256.   tft.setCursor(rightColCursorX, fourthRowCursorY);
  257.   tft.println(".");
  258. }
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 11:28:17

实验场景图

49.jpg
回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 18:05:46

本帖最后由 驴友花雕 于 2021-7-7 18:28 编辑

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十:串口打印触摸屏检测的数据

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十:串口打印触摸屏检测的数据
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <stdint.h>
  12. #include "TouchScreen.h" //导入触摸驱动库
  13. #define YP A2
  14. #define XM A1
  15. #define YM 6
  16. #define XP 7
  17. // 为了更好的压力精度,我们需要知道阻力
  18. // 在 X+ 和 X- 之间使用任何万用表读取它
  19. // 对于我们使用的那个,它在 X 板上的 300 欧姆
  20. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  21. void setup(void) {
  22.   Serial.begin(9600);
  23. }
  24. void loop(void) {
  25.   TSPoint p = ts.getPoint();
  26.   if (p.z > ts.pressureThreshhold) {
  27.     Serial.print("X = "); Serial.print(p.x);
  28.     Serial.print("\tY = "); Serial.print(p.y);
  29.     Serial.print("\tPressure = "); Serial.println(p.z);
  30.   }
  31.   delay(100);
  32. }
复制代码

TSPoint p = ts.getPoint(); 将长度(x)、宽度(y)和压力(z)存储到p对象。
回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 18:07:10

50.jpg

回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 19:17:43

Adafruit_TFTLCD库相关函数

  1. class Adafruit_TFTLCD : public Adafruit_GFX {
  2. public:
  3.   Adafruit_TFTLCD(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t rst);
  4.   Adafruit_TFTLCD(void);
  5.   void begin(uint16_t id = 0x9325);
  6.   void drawPixel(int16_t x, int16_t y, uint16_t color);
  7.   void drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color);
  8.   void drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color);
  9.   void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
  10.   void fillScreen(uint16_t color);
  11.   void reset(void);
  12.   void setRegisters8(uint8_t *ptr, uint8_t n);
  13.   void setRegisters16(uint16_t *ptr, uint8_t n);
  14.   void setRotation(uint8_t x);
  15.   void setAddrWindow(int x1, int y1, int x2, int y2);
  16.   void pushColors(uint16_t *data, uint8_t len, boolean first);
  17.   uint16_t color565(uint8_t r, uint8_t g, uint8_t b),
  18.       readPixel(int16_t x, int16_t y), readID(void);
  19.   uint32_t readReg(uint8_t r);
  20. private:
  21.   void init(),
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 19:23:55

1、创建对象

  1. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  2. void setup(void) {
  3.   tft.reset();
  4.   tft.begin(0x9341);
  5. }
复制代码

首先创建了一个Adafruit_TFTLCD对象,名为tft,管脚定义这里省去了。
begin方法中的0x9341表示改TFT LCD的驱动为ILI9341,其它的这里不做介绍

2、屏幕

  1. void fillScreen(uint16_t color);
  2. uint16_t width();  //屏幕的宽度
  3. uint16_t height();  //屏幕的高度
复制代码

全屏填充颜色color,再次之前显示的内容会被挡住

案例

  1. tft.fillScreen(BLACK);
  2.   delay(1000);
  3.   tft.fillScreen(RED);
  4.   delay(1000);
  5.   tft.fillScreen(BLUE);
  6.    delay(1000);
复制代码

3、点

  1. void drawPixel(int16_t x, int16_t y, uint16_t color);
复制代码

在点(x,y)上画一个颜色为color的像素点。

案例

  1. tft.drawPixel(1,1,RED);
  2.   tft.drawPixel(10,10,RED);
  3.   tft.drawPixel(20,20,RED);
  4.   tft.drawPixel(40,40,RED);
  5.   tft.drawPixel(60,60,RED);
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 19:35:16

4、线

  1. void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);  
  2.   void drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color);
  3.   void drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color);
复制代码


最简单的是两点确定一条直线,当然也可以确定一个点、方向、长度,后两个是画水平线或者铅锤线。

案例

  1. tft.drawFastHLine(10,10,170,RED);
  2. tft.drawFastVLine(10,10,170,RED);
  3. tft.drawLine(10,10,100,180,RED);
复制代码

5、矩形&&圆角矩形

  1. void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
  2. void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
  3. void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t radius, uint16_t color);
  4. void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t radius, uint16_t color);
复制代码

drawRect的矩形直画出边框,内部不填充,如果需要内部填充颜色则需要使用fillRect

圆角矩形则是多了一个设置圆角半径的参数raduis,是否填充与上面一样。

案例

  1.    tft.drawRect(10,10,150,100,RED);
  2.    tft.fillRect(10,120,150,100,RED);
复制代码
  1. tft.drawRoundRect(10,10,150,100,10,RED);
  2. tft.fillRoundRect(10,120,150,100,10,RED);
复制代码

6、圆形

  1. void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
  2. void fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
复制代码

这个就比较简单,圆心坐标(x0,y0),半径r,颜色color

案例

  1. tft.drawCircle(100,100,50,WHITE);
  2. tft.fillCircle(100,260,50,BLUE);
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 19:45:30

7、三角形

  1. void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
  2. void fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
复制代码

三角形需要确定三个顶点以及颜色

案例

  1. tft.drawTriangle(10,10,100,15,180,100,GREEN);
  2. tft.fillTriangle(10,110,100,115,180,200,GREEN);
复制代码

8、字符&&英文文本

  1. void drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
  2. void setCursor(uint16_t x0, uint16_t y0);  //字体左上角顶点坐标
  3. void setTextColor(uint16_t color); //字体前景色
  4. void setTextColor(uint16_t color, uint16_t backgroundcolor);//字体前景色与背景色
  5. void setTextSize(uint8_t size);  //字体大小放法因子
  6. void setTextWrap(boolean w);        //是否自动换行,默认为true,滚动显示设置为false
复制代码

drawChar只能显示单个字符,需要确定左上角顶点坐标(x,y),字符c,颜色color,前景色color,背景色bg,大小size,大小为1时表示5 * 8像素,为2就表示10 * 16

自定义字体不支持背景色,可在此之前绘制填充颜色的形状,如圆角矩形

案例

  1. tft.fillScreen(GREEN);
  2. tft.drawChar(150,10,'A',RED,WHITE,5);
  3. tft.setCursor(10,50);
  4. tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1
  5. tft.setCursor(10,80);
  6. tft.setTextSize( 4);
  7. tft.print("AB 3.14");
  8. tft.setCursor(10,115);
  9. tft.setTextColor(RED); //背景色不做设置
  10. tft.setTextSize( 4);
  11. tft.print("AB你好3.141516");
  12.   tft.setCursor(10,180);
  13.   tft.setTextColor(RED, WHITE);
  14. tft.setTextSize( 4);
  15. tft.setTextWrap(false);
  16. tft.print("AB你好3.141516");
复制代码

可以看到默认的字体不支持中文,需要使用中文的改日再补上

9、旋转

  1. void setRotation(uint8_t rotation);
复制代码

旋转参数可以是0、1、2或3,分别对应0,90,180或270度。

对于属于Arduino屏蔽的显示,旋转值0将显示设置为竖屏(高)模式,旋转值2也是纵向模式,。旋转1是横屏模式,,而旋转3也是横屏模式。

案例

  1. //tft.setRotation(1); //注释和未注释的情况下做对比
  2. tft.fillScreen(GREEN);
  3. tft.drawChar(150,10,'A',RED,WHITE,5);
  4. tft.setCursor(10,50);
  5. tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1
  6. tft.setCursor(10,80);
  7. tft.setTextSize( 4);
  8. tft.print("AB 3.14");
  9. tft.setCursor(10,115);
  10. tft.setTextColor(RED); //背景色不做设置
  11. tft.setTextSize( 4);
  12. tft.print("AB你好3.141516");
  13.   tft.setCursor(10,180);
  14.   tft.setTextColor(RED, WHITE);
  15. tft.setTextSize( 4);
  16. tft.setTextWrap(false);
  17. tft.print("AB你好3.141516");
复制代码



回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-7 20:30:44

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十一:几何图形的点线面循环

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十一:几何图形的点线面循环
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <Adafruit_GFX.h>    // Core graphics library
  12. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  13. #include <TouchScreen.h>
  14. #define LCD_CS A3
  15. #define LCD_CD A2
  16. #define LCD_WR A1
  17. #define LCD_RD A0
  18. #define LCD_RESET A4
  19. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  20. #define BLACK   0x0000
  21. #define BLUE    0x001F
  22. #define RED     0xF800
  23. #define GREEN   0x07E0
  24. #define WHITE   0xFFFF
  25. void setup(void) {
  26.   tft.reset();
  27.   tft.begin(0x9341);
  28.   tft.fillScreen(BLACK);
  29. }
  30. void loop() {
  31.   // 点
  32.   tft.drawPixel(1, 1, RED);
  33.   tft.drawPixel(10, 10, RED);
  34.   tft.drawPixel(20, 20, RED);
  35.   tft.drawPixel(40, 40, RED);
  36.   tft.drawPixel(60, 60, RED);
  37.   delay(500);
  38.   // 线
  39.   tft.drawFastHLine(10, 10, 170, RED);
  40.   tft.drawFastVLine(10, 10, 170, RED);
  41.   tft.drawLine(10, 10, 100, 180, RED);
  42.   delay(500);
  43.   // 矩形
  44.   tft.drawRect(10, 10, 150, 100, RED);
  45.   tft.fillRect(10, 120, 150, 100, RED);
  46.   delay(500);
  47.   // 圆角矩形
  48.   tft.drawRoundRect(10, 10, 150, 100, 10, RED);
  49.   tft.fillRoundRect(10, 120, 150, 100, 10, RED);
  50.   delay(500);
  51.   //  圆形
  52.   tft.drawCircle(100, 100, 50, WHITE);
  53.   tft.fillCircle(100, 260, 50, BLUE);
  54.   delay(500);
  55.   //  三角形
  56.   tft.drawTriangle(10, 10, 100, 15, 180, 100, GREEN);
  57.   tft.fillTriangle(10, 110, 100, 115, 180, 200, GREEN);
  58.   delay(500);
  59.   //旋转
  60.   tft.setRotation(0);
  61.   delay(500);
  62.   //字符与文本
  63.   tft.fillScreen(GREEN);
  64.   tft.drawChar(150, 10, 'A', RED, WHITE, 5);
  65.   tft.setCursor(10, 50);
  66.   tft.print("AB 3.14");    //默认前景色white、无背景色、大小为1
  67.   tft.setCursor(10, 80);
  68.   tft.setTextSize( 4);
  69.   tft.print("AB 3.14");
  70.   tft.setCursor(10, 115);
  71.   tft.setTextColor(RED); //背景色不做设置
  72.   tft.setTextSize( 4);
  73.   tft.print("AB 3.141516");
  74.   tft.setCursor(10, 180);
  75.   tft.setTextColor(RED, WHITE);
  76.   tft.setTextSize( 4);
  77.   tft.setTextWrap(false);
  78.   tft.print("AB 3.141516");
  79.   delay(500);
  80.   //屏幕
  81.   tft.fillScreen(BLACK);
  82.   delay(1000);
  83.   tft.fillScreen(GREEN);
  84.   delay(1000);
  85.   //旋转
  86.   tft.setRotation(1);
  87.   delay(500);
  88.   tft.fillScreen(RED);
  89.   delay(1000);
  90.   tft.fillScreen(BLUE);
  91.   delay(1000);
  92.   //旋转
  93.   tft.setRotation(2);
  94.   delay(500);
  95.   tft.fillScreen(BLACK);
  96.   delay(1000);
  97.   //旋转
  98.   tft.setRotation(0);
  99.   delay(500);
  100. }
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 06:55:42

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十一:几何图形的点线面循环

实验动态图 WeChat_20210708064908.gif

回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 07:02:57

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十一:几何图形的点线面循环

实验动态图 WeChat_20210708064908.gif

回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 07:55:10

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序
  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <Adafruit_GFX.h>
  12. #include <Adafruit_TFTLCD.h>
  13. #include <TouchScreen.h>
  14. #if defined(__SAM3X8E__)
  15. #undef __FlashStringHelper::F(string_literal)
  16. #define F(string_literal) string_literal
  17. #endif
  18. #define YP A2
  19. #define XM A1
  20. #define YM 6
  21. #define XP 7
  22. #define TS_MINX 150
  23. #define TS_MINY 120
  24. #define TS_MAXX 920
  25. #define TS_MAXY 940
  26. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  27. #define LCD_CS A3
  28. #define LCD_CD A2
  29. #define LCD_WR A1
  30. #define LCD_RD A0
  31. #define LCD_RESET A4
  32. #define BLACK   0x0000
  33. #define BLUE    0x001F
  34. #define RED     0xF800
  35. #define GREEN   0x07E0
  36. #define CYAN    0x07FF
  37. #define MAGENTA 0xF81F
  38. #define YELLOW  0xFFE0
  39. #define WHITE   0xFFFF
  40. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  41. #define BOXSIZE 40
  42. #define PENRADIUS 3
  43. int oldcolor, currentcolor;
  44. void setup(void) {
  45.   Serial.begin(9600);
  46.   tft.reset();
  47.   tft.begin(0x9341);
  48.   tft.fillScreen(BLACK);
  49.   tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  50.   tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  51.   tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  52.   tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  53.   tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  54.   tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  55.   tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  56.   currentcolor = RED;
  57.   pinMode(13, OUTPUT);
  58. }
  59. #define MINPRESSURE 10
  60. #define MAXPRESSURE 1000
  61. void loop() {
  62.   digitalWrite(13, HIGH);
  63.   TSPoint p = ts.getPoint();
  64.   digitalWrite(13, LOW);
  65.   pinMode(XM, OUTPUT);
  66.   pinMode(YP, OUTPUT);
  67.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
  68.     if (p.y < (TS_MINY - 5)) {
  69.       tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
  70.     }
  71.     p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
  72.     p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
  73.     if (p.y < BOXSIZE) {
  74.       oldcolor = currentcolor;
  75.       if (p.x < BOXSIZE) {
  76.         currentcolor = RED;
  77.         tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  78.       } else if (p.x < BOXSIZE * 2) {
  79.         currentcolor = YELLOW;
  80.         tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
  81.       } else if (p.x < BOXSIZE * 3) {
  82.         currentcolor = GREEN;
  83.         tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
  84.       } else if (p.x < BOXSIZE * 4) {
  85.         currentcolor = CYAN;
  86.         tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
  87.       } else if (p.x < BOXSIZE * 5) {
  88.         currentcolor = BLUE;
  89.         tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
  90.       } else if (p.x < BOXSIZE * 6) {
  91.         currentcolor = MAGENTA;
  92.         tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
  93.       } if (oldcolor != currentcolor) {
  94.         if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  95.         if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  96.         if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  97.         if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  98.         if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  99.         if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  100.       }
  101.     } if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < tft.height())) {
  102.       tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  103.     }
  104.   }
  105.   tft.setRotation(3);
  106. }
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 07:59:32

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十二:使用 Arduino 2.4 触摸屏创建简易绘画应用程序

实验场景图


51.jpg
回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 08:37:47

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十三:两个按钮的极简开关板
  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十三:两个按钮的极简开关板
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #if 1
  12. #include <Adafruit_GFX.h>
  13. #include <MCUFRIEND_kbv.h>
  14. MCUFRIEND_kbv tft;
  15. #include <TouchScreen.h>
  16. #define MINPRESSURE 200
  17. #define MAXPRESSURE 1000
  18. // 所有触摸屏和接线都是不同的
  19. const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
  20. const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
  21. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  22. Adafruit_GFX_Button on_btn, off_btn;
  23. int pixel_x, pixel_y;     //更新全局变量
  24. bool Touch_getXY(void) {
  25.   TSPoint p = ts.getPoint();
  26.   pinMode(YP, OUTPUT);      //恢复共享引脚
  27.   pinMode(XM, OUTPUT);
  28.   digitalWrite(YP, HIGH);   //因为 TFT 控制引脚
  29.   digitalWrite(XM, HIGH);
  30.   bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  31.   if (pressed) {
  32.     pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //kbv 对我有意义
  33.     pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  34.   }
  35.   return pressed;
  36. }
  37. #define BLACK   0x0000
  38. #define BLUE    0x001F
  39. #define RED     0xF800
  40. #define GREEN   0x07E0
  41. #define CYAN    0x07FF
  42. #define MAGENTA 0xF81F
  43. #define YELLOW  0xFFE0
  44. #define WHITE   0xFFFF
  45. void setup(void) {
  46.   Serial.begin(9600);
  47.   tft.reset();
  48.   tft.begin(0x9341);
  49.   tft.fillScreen(BLACK);
  50.   tft.setRotation(2); // 翻转180度,对应硬件
  51.   delay(500);
  52.   on_btn.initButton(&tft,  60, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
  53.   off_btn.initButton(&tft, 180, 200, 100, 40, WHITE, CYAN, BLACK, "OFF", 2);
  54.   on_btn.drawButton(false);
  55.   off_btn.drawButton(false);
  56.   tft.fillRect(40, 80, 160, 80, RED);
  57. }
  58. // 两个按钮很简单
  59. void loop(void) {
  60.   bool down = Touch_getXY();
  61.   on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  62.   off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
  63.   if (on_btn.justReleased())
  64.     on_btn.drawButton();
  65.   if (off_btn.justReleased())
  66.     off_btn.drawButton();
  67.   if (on_btn.justPressed()) {
  68.     on_btn.drawButton(true);
  69.     tft.fillRect(40, 80, 160, 80, GREEN);
  70.   }
  71.   if (off_btn.justPressed()) {
  72.     off_btn.drawButton(true);
  73.     tft.fillRect(40, 80, 160, 80, RED);
  74.   }
  75. }
  76. #endif
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 08:48:02

本帖最后由 驴友花雕 于 2021-7-8 08:52 编辑

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十三:两个按钮的极简开关板

  实验场景图 WeChat_20210708084507.gif

回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 09:30:02

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十四:动态环形百分比图表
  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十四:动态环形百分比图表
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include "Adafruit_GFX.h"
  12. #include "MCUFRIEND_kbv.h"
  13. #include "Temperature.h"
  14. MCUFRIEND_kbv tft;
  15. #define PI 3.1415926535897932384626433832795
  16. int n, f;
  17. int j, j2;
  18. int i, i2;
  19. int pct = 0;
  20. int d[5] = {10, 60, 16, 9, 10};
  21. uint16_t col[5] = {0x7006, 0xF986, 0x6905, 0x7FF7, 0x024D};
  22. void setup() {
  23.   tft.reset();
  24.   Serial.begin(9600);
  25.   tft.begin(0x9341);
  26.   tft.invertDisplay(true);
  27.   tft.setTextSize(2);
  28. }
  29. void loop() {
  30.   tft.fillScreen(0x0042);
  31.   tft.setRotation(1);
  32.   for (int p = 0; p < 4000; p++) {
  33.     j = 120 * (sin(PI * p / 2000));
  34.     i = 120 * (cos(PI * p / 2000));
  35.     j2 = 60 * (sin(PI * p / 2000));
  36.     i2 = 60 * (cos(PI * p / 2000));
  37.     tft.drawLine(i2 + 160, j2 + 160, i + 160, j + 160, col[n]);
  38.   }
  39.   n = 0;
  40.   for (int a = 0; a < 5; a++) {
  41.     pct += d[n] * 40;
  42.     f = 4000 - pct;
  43.     for (int b = 0; b < f; b++) {
  44.       j = 120 * (sin(PI * b / 2000));
  45.       i = 120 * (cos(PI * b / 2000));
  46.       j2 = 60 * (sin(PI * b / 2000));
  47.       i2 = 60 * (cos(PI * b / 2000));
  48.       tft.drawLine(i2 + 160, j2 + 160, i + 160, j + 160, col[n + 1]);
  49.     }
  50.     tft.fillCircle(380, 100 + (30 * n), 10,  col[n]);
  51.     tft.setTextColor(0xffff);
  52.     tft.setCursor(400, 94 + (30 * n));
  53.     tft.print(d[n]); tft.print("%");
  54.     n++;
  55.   }
  56.   while (1);
  57. }
复制代码


回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 09:32:08

本帖最后由 驴友花雕 于 2021-7-8 10:13 编辑

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十四:动态环形百分比图表

  实验场景图 ezgif.com-video-to-gif-9.gif

回复

使用道具 举报

驴友花雕  高级技匠
 楼主|

发表于 2021-7-8 10:00:47

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  项目二十五:显示触摸屏电话面板
  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百六十五:2.4寸TFT液晶触摸屏 彩屏模块 TFT-LCD 高清真彩显示屏
  4.   项目二十五:显示触摸屏电话面板
  5.   模块直插,引脚用法如下:
  6.   LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
  7.   Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
  8.   LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
  9.   Arduino Uno 8 9 2 3 4 5 6 7
  10. */
  11. #include <TouchScreen.h> //导入触摸库
  12. #include <LCDWIKI_GUI.h> //Core graphics library
  13. #include <LCDWIKI_KBV.h> //Hardware-specific library
  14. //if the IC model is known or the modules is unreadable,you can use this constructed function
  15. LCDWIKI_KBV my_lcd(ILI9341, A3, A2, A1, A0, A4); //model,cs,cd,wr,rd,reset
  16. //if the IC model is not known and the modules is readable,you can use this constructed function
  17. //LCDWIKI_KBV my_lcd(240,320,A3,A2,A1,A0,A4);//width,height,cs,cd,wr,rd,reset
  18. /*  r     g    b */
  19. #define BLACK        0x0000  /*   0,   0,   0 */
  20. #define BLUE         0x001F  /*   0,   0, 255 */
  21. #define RED          0xF800  /* 255,   0,   0 */
  22. #define GREEN        0x07E0  /*   0, 255,   0 */
  23. #define CYAN         0x07FF  /*   0, 255, 255 */
  24. #define MAGENTA      0xF81F  /* 255,   0, 255 */
  25. #define YELLOW       0xFFE0  /* 255, 255,   0 */
  26. #define WHITE        0xFFFF  /* 255, 255, 255 */
  27. #define NAVY         0x000F  /*   0,   0, 128 */
  28. #define DARKGREEN    0x03E0  /*   0, 128,   0 */
  29. #define DARKCYAN     0x03EF  /*   0, 128, 128 */
  30. #define MAROON       0x7800  /* 128,   0,   0 */
  31. #define PURPLE       0x780F  /* 128,   0, 128 */
  32. #define OLIVE        0x7BE0  /* 128, 128,   0 */
  33. #define LIGHTGREY    0xC618  /* 192, 192, 192 */
  34. #define DARKGREY     0x7BEF  /* 128, 128, 128 */
  35. #define ORANGE       0xFD20  /* 255, 165,   0 */
  36. #define GREENYELLOW  0xAFE5  /* 173, 255,  47 */
  37. #define PINK         0xF81F  /* 255,   0, 255 */
  38. /******************* UI details */
  39. #define BUTTON_R 25 //the radius of button
  40. #define BUTTON_SPACING_X 25 //the horizontal distance between button
  41. #define BUTTON_SPACING_Y 5  //the vertical distance between button
  42. #define EDG_Y 5 //lower edge distance
  43. #define EDG_X 20 //left and right distance
  44. #define YP A2  // must be an analog pin, use "An" notation!
  45. #define XM A1  // must be an analog pin, use "An" notation!
  46. #define YM 6   // can be a digital pin
  47. #define XP 7   // can be a digital pin
  48. //X 的触摸灵敏度
  49. #define TS_MINX 124
  50. #define TS_MAXX 906
  51. //Y 的触摸灵敏度
  52. #define TS_MINY 83
  53. #define TS_MAXY 893
  54. // 有一个状态行,例如 FONA 是否在工作
  55. #define STATUS_X 10
  56. #define STATUS_Y 65
  57. //按下时的触摸灵敏度
  58. #define MINPRESSURE 10
  59. #define MAXPRESSURE 1000
  60. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  61. typedef struct _button_info {
  62.   uint8_t button_name[10];
  63.   uint8_t button_name_size;
  64.   uint16_t button_name_colour;
  65.   uint16_t button_colour;
  66.   uint16_t button_x;
  67.   uint16_t button_y;
  68. } button_info;
  69. //按钮的定义
  70. button_info phone_button[15] = {
  71.   "1", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
  72.   "2", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
  73.   "3", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 4 * BUTTON_SPACING_Y - 9 * BUTTON_R - 1,
  74.   "4", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
  75.   "5", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
  76.   "6", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 3 * BUTTON_SPACING_Y - 7 * BUTTON_R - 1,
  77.   "7", 3, BLACK, CYAN, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
  78.   "8", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
  79.   "9", 3, BLACK, CYAN, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - 2 * BUTTON_SPACING_Y - 5 * BUTTON_R - 1,
  80.   "*", 3, BLACK, PINK, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
  81.   "0", 3, BLACK, CYAN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
  82.   "#", 3, BLACK, PINK, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_SPACING_Y - 3 * BUTTON_R - 1,
  83.   "end", 2, BLACK, RED, EDG_X + BUTTON_R - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
  84.   "call", 2, BLACK, GREEN, EDG_X + 3 * BUTTON_R + BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
  85.   "dele", 2, BLACK, LIGHTGREY, EDG_X + 5 * BUTTON_R + 2 * BUTTON_SPACING_X - 1, my_lcd.Get_Display_Height() - EDG_Y - BUTTON_R - 1,
  86. };
  87. //显示字符串
  88. void show_string(uint8_t *str, int16_t x, int16_t y, uint8_t csize, uint16_t fc, uint16_t bc, boolean mode) {
  89.   my_lcd.Set_Text_Mode(mode);
  90.   my_lcd.Set_Text_Size(csize);
  91.   my_lcd.Set_Text_colour(fc);
  92.   my_lcd.Set_Text_Back_colour(bc);
  93.   my_lcd.Print_String(str, x, y);
  94. }
  95. //检查是否按下
  96. boolean is_pressed(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t px, int16_t py) {
  97.   if ((px > x1 && px < x2) && (py > y1 && py < y2))
  98.   {
  99.     return true;
  100.   }
  101.   else
  102.   {
  103.     return false;
  104.   }
  105. }
  106. //显示主菜单
  107. void show_menu(void)
  108. {
  109.   uint16_t i;
  110.   for (i = 0; i < sizeof(phone_button) / sizeof(button_info); i++)
  111.   {
  112.     my_lcd.Set_Draw_color(phone_button[i].button_colour);
  113.     my_lcd.Fill_Circle(phone_button[i].button_x, phone_button[i].button_y, BUTTON_R);
  114.     show_string(phone_button[i].button_name, phone_button[i].button_x - strlen(phone_button[i].button_name)*phone_button[i].button_name_size * 6 / 2 + 1, phone_button[i].button_y - phone_button[i].button_name_size * 8 / 2 + 1, phone_button[i].button_name_size, phone_button[i].button_name_colour, BLACK, 1);
  115.   }
  116.   my_lcd.Set_Draw_color(BLACK);
  117.   my_lcd.Fill_Rectangle(1, 1, my_lcd.Get_Display_Width() - 2, 3);
  118.   my_lcd.Fill_Rectangle(1, 29, my_lcd.Get_Display_Width() - 2, 31);
  119.   my_lcd.Fill_Rectangle(1, 1, 3, 31);
  120.   my_lcd.Fill_Rectangle(my_lcd.Get_Display_Width() - 4, 1, my_lcd.Get_Display_Width() - 2, 31);
  121. }
  122. void setup(void) {
  123.   Serial.begin(9600);
  124.   my_lcd.Init_LCD();
  125.   Serial.println(my_lcd.Read_ID(), HEX);
  126.   my_lcd.Fill_Screen(BLUE);
  127.   show_menu();
  128. }
  129. uint16_t text_x = 10, text_y = 6, text_x_add = 6 * phone_button[0].button_name_size, text_y_add = 8 * phone_button[0].button_name_size;
  130. uint16_t n = 0;
  131. void loop(void) {
  132.   uint16_t i;
  133.   digitalWrite(13, HIGH);
  134.   TSPoint p = ts.getPoint();
  135.   digitalWrite(13, LOW);
  136.   pinMode(XM, OUTPUT);
  137.   pinMode(YP, OUTPUT);
  138.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  139.   {
  140.     //p.x = my_lcd.Get_Display_Width()-map(p.x, TS_MINX, TS_MAXX, my_lcd.Get_Display_Width(), 0);
  141.     //p.y = my_lcd.Get_Display_Height()-map(p.y, TS_MINY, TS_MAXY, my_lcd.Get_Display_Height(), 0);
  142.     p.x = map(p.x, TS_MINX, TS_MAXX, 0, my_lcd.Get_Display_Width());
  143.     p.y = map(p.y, TS_MINY, TS_MAXY, 0, my_lcd.Get_Display_Height());
  144.     // }
  145.     for (i = 0; i < sizeof(phone_button) / sizeof(button_info); i++) {
  146.       //按下按钮
  147.       if (is_pressed(phone_button[i].button_x - BUTTON_R, phone_button[i].button_y - BUTTON_R, phone_button[i].button_x + BUTTON_R, phone_button[i].button_y + BUTTON_R, p.x, p.y))
  148.       {
  149.         my_lcd.Set_Draw_color(DARKGREY);
  150.         my_lcd.Fill_Circle(phone_button[i].button_x, phone_button[i].button_y, BUTTON_R);
  151.         show_string(phone_button[i].button_name, phone_button[i].button_x - strlen(phone_button[i].button_name)*phone_button[i].button_name_size * 6 / 2 + 1, phone_button[i].button_y - phone_button[i].button_name_size * 8 / 2 + 1, phone_button[i].button_name_size, WHITE, BLACK, 1);
  152.         delay(100);
  153.         my_lcd.Set_Draw_color(phone_button[i].button_colour);
  154.         my_lcd.Fill_Circle(phone_button[i].button_x, phone_button[i].button_y, BUTTON_R);
  155.         show_string(phone_button[i].button_name, phone_button[i].button_x - strlen(phone_button[i].button_name)*phone_button[i].button_name_size * 6 / 2 + 1, phone_button[i].button_y - phone_button[i].button_name_size * 8 / 2 + 1, phone_button[i].button_name_size, phone_button[i].button_name_colour, BLACK, 1);
  156.         if (i < 12)
  157.         {
  158.           if (n < 13)
  159.           {
  160.             show_string(phone_button[i].button_name, text_x, text_y, phone_button[i].button_name_size, GREENYELLOW, BLACK, 1);
  161.             text_x += text_x_add - 1;
  162.             n++;
  163.           }
  164.         }
  165.         else if (12 == i) //节目通话结束
  166.         {
  167.           my_lcd.Set_Draw_color(BLUE);
  168.           my_lcd.Fill_Rectangle(0, 33, my_lcd.Get_Display_Width() - 1, 42);
  169.           show_string("Calling ended", CENTER, 33, 1, OLIVE, BLACK, 1);
  170.         }
  171.         else if (13 == i) //显示呼叫
  172.         {
  173.           my_lcd.Set_Draw_color(BLUE);
  174.           my_lcd.Fill_Rectangle(0, 33, my_lcd.Get_Display_Width() - 1, 42);
  175.           show_string("Calling...", CENTER, 33, 1, OLIVE, BLACK, 1);
  176.         }
  177.         else if (14 == i) //删除按钮
  178.         {
  179.           if (n > 0)
  180.           {
  181.             my_lcd.Set_Draw_color(BLUE);
  182.             text_x -= (text_x_add - 1);
  183.             my_lcd.Fill_Rectangle(text_x, text_y, text_x + text_x_add - 1, text_y + text_y_add - 2);
  184.             n--;
  185.           }
  186.         }
  187.       }
  188.     }
  189.   }
  190. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2021 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail