티스토리 뷰

728x90

출처

Arduino : 2.4인치 터치 스크린 제어 ( ILI9341, UTouch )

스크린 핀연결

  • RESET 7
  • DC    9
  • CS   10
  • MOSI 11
  • CLK  13

터치 핀연결

  • CLK 6
  • CS 5
  • DIN 4
  • DO 3
  • IRQ 2

터치 테스트

#include <UTouch.h>

// Initialize touchscreen
#define PIN_CLK 6
#define PIN_CS 5
#define PIN_DIN 4
#define PIN_DO 3
#define PIN_IRQ 2

UTouch  myTouch(PIN_CLK, PIN_CS, PIN_DIN, PIN_DO, PIN_IRQ);

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
}

void loop() {
  // put your main code here, to run repeatedly:
  long x, y;
  
  while (myTouch.dataAvailable() == true) {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((x!=-1) and (y!=-1)) {
      Serial.print("X = "); Serial.print(x);
      Serial.print("\tY = "); Serial.println(y);
    }
  }
}

텍스트 및 그래픽 함수 테스트

/*
 TFT LCD - TFT Simple driving
 modified on 21 Feb 2019
 by Saeed Hosseini
 https://electropeak.com/learn/
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define TFT_RESET 7
#define TFT_DC    9
#define TFT_CS   10
#define TFT_MOSI 11
#define TFT_CLK  13

#define BLACK       0x0000
#define BLUE        0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN        0x07FF
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFD20
#define GREENYELLOW 0xAFE5
#define NAVY        0x000F
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define MAROON      0x7800
#define PURPLE      0x780F
#define OLIVE       0x7BE0
#define LIGHTGREY   0xC618
#define DARKGREY    0x7BEF

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RESET);

void setup() {
  Serial.begin(9600);
  Serial.println(F("TFT LCD test"));
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
  Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
#else
  Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
#endif
  Serial.print("TFT size is ");
  Serial.print(tft.width());
  Serial.print("x");
  Serial.println(tft.height());
  
  tft.begin();
  Serial.println(F("Benchmark                Time (microseconds)"));
  Serial.print(F("Screen fill              "));
  Serial.println(FillScreen());
  delay(500);
  
  tft.setTextColor(YELLOW);
  tft.setCursor(70, 180);
  tft.setTextSize(1);
  tft.println("Electropeak");
  delay(200);
  
  tft.fillScreen(PURPLE);
  tft.setCursor(50, 170);
  tft.setTextSize(2);
  tft.println("Electropeak");
  delay(200);
  
  tft.fillScreen(PURPLE);
  tft.setCursor(20, 160);
  tft.setTextSize(3);
  tft.println("Electropeak");
  delay(500);
  
  tft.fillScreen(PURPLE);
  for (int rotation = 0; rotation < 4; rotation++) { 
    tft.setRotation(rotation); 
    tft.setCursor(0, 0); 
    tft.setTextSize(3); 
    tft.println("Electropeak"); 
    delay(700); 
  }
  delay(500); 
  
  Serial.print(F("Rectangles (filled) ")); 
  Serial.println(testFilledRects(YELLOW, MAGENTA)); delay(500); 
} 
 
void loop() { 
} 

unsigned long FillScreen() { 
  unsigned long start = micros(); 
  tft.fillScreen(RED); 
  delay(500); 
  tft.fillScreen(GREEN); 
  delay(500); 
  tft.fillScreen(BLUE); 
  delay(500); 
  tft.fillScreen(WHITE); 
  delay(500); 
  tft.fillScreen(MAGENTA); 
  delay(500); 
  tft.fillScreen(PURPLE); 
  delay(500); 
  return micros() - start; 
} 

unsigned long testFilledRects(uint16_t color1, uint16_t color2) { 
  unsigned long start, t = 0; 
  int n, i, i2, cx = tft.width() / 2 - 1, cy = tft.height() / 2 - 1; 
  tft.fillScreen(BLACK); 
  n = min(tft.width(), tft.height()); 
  for (i = n; i > 0; i -= 6) {
   i2    = i / 2;
   start = micros();
   tft.fillRect(cx - i2, cy - i2, i, i, color1);
   t += micros() - start;
   // Outlines are not included in timing results
   tft.drawRect(cx - i2, cy - i2, i, i, color2);
 }
 return t;
}

터치스크린을 이용한 그림판

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <UTouch.h>

#define PIN_CLK 6
#define PIN_CS 5
#define PIN_DIN 4
#define PIN_DO 3
#define PIN_IRQ 2
#define TS_MINX 10
#define TS_MINY 0
#define TS_MAXX 380
#define TS_MAXY 239
UTouch  ts(PIN_CLK, PIN_CS, PIN_DIN, PIN_DO, PIN_IRQ);

#define TFT_RESET 7
#define TFT_DC    9
#define TFT_CS   10
#define TFT_MOSI 11
#define TFT_CLK  13
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RESET);

#define BOXSIZE 40
#define PENRADIUS 3

int oldcolor, currentcolor;

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("Paint!"));

  ts.InitTouch(LANDSCAPE); // PORTRAIT, LANDSCAPE
  ts.setPrecision(PREC_HI); // PREC_LOW, PREC_MEDIUM, PREC_HI, PREC_EXTREME
 
  tft.begin();
  tft.fillScreen(BLACK);
  
  tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
  tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
  tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
  tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  
  currentcolor = RED;
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop() {
  long px, py;
  while (ts.dataAvailable() == true) {
    ts.read();
    px = ts.getX();
    py = TS_MAXY - 10 - ts.getY();
    if ((px!=-1) and (py!=-1)) {
      Serial.print("X = "); Serial.print(px);
      Serial.print("\tY = "); Serial.println(py);
    }
    
    if (py < (TS_MINY-5)) {
      Serial.println("erase");
      tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
    }
    px = map(px, TS_MINX, TS_MAXX, tft.width(), 0);
    py = map(py, TS_MINY, TS_MAXY, tft.height(), 0);
    if (py < BOXSIZE) {
      oldcolor = currentcolor;
      if (px < BOXSIZE) { 
        currentcolor = RED; 
        tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (px < BOXSIZE*2) {
        currentcolor = YELLOW;
        tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (px < BOXSIZE*3) {
        currentcolor = GREEN;
        tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (px < BOXSIZE*4) {
        currentcolor = CYAN;
        tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (px < BOXSIZE*5) {
        currentcolor = BLUE;
        tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
      } else if (px < BOXSIZE*6) { 
        currentcolor = MAGENTA; 
        tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE); 
      } 
      if (oldcolor != currentcolor) {
        if (oldcolor == RED) 
          tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED); 
        if (oldcolor == YELLOW) 
          tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW); 
        if (oldcolor == GREEN) 
          tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN); 
        if (oldcolor == CYAN) 
          tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN); 
        if (oldcolor == BLUE) 
          tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE); 
        if (oldcolor == MAGENTA) 
          tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA); 
      }
    }
    
    if (((py-PENRADIUS) > BOXSIZE) && ((py+PENRADIUS) < tft.height())) {
      tft.fillCircle(px, py, PENRADIUS, currentcolor);
    }
  }
}

실행 영상

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함