Seeeduino + TFT(带触摸)

从箱子里翻出几年前买的seeeduino和一块2.4寸的TFT,全新的!当初买来是准备在大学里做比赛用的,但是后来学了单片机,觉得这东西鸡肋,就放在角落里落灰了。
上图:
这里写图片描述
讲真,用arduino比我学过的任何一款单片机都简单,不但半个小时就把软件、驱动装好了并且走了一遍开发流程。但是arduino IDE的功能实在是太鸡肋了,除了自带了一个串口助手,其他功能还真是少的可怜。接下来就是找一个合适的库,找到后,直接调用现成的函数,搞了将近一个小时的成果:
这里写图片描述
再把触摸屏加上去,参考网上的例子做个画板
这里写图片描述
再加个开机界面:
这里写图片描述
最后把刷屏的程序统统放在一起,定时1分钟不用画板就开始刷屏!
C如下:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>


#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

#define TS_MINX 150//触摸屏的参数
#define TS_MINY 180
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

// Assign human-readable names to some common 16-bit color values:
#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_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BOXSIZE 40
#define PENRADIUS 2
int oldcolor, currentcolor;

void setup(void) 
{
  Serial.begin(115200);
  Serial.println(F("Paint!"));
  
  tft.reset();
  
  uint16_t identifier = tft.readID();
  if(identifier == 0x9328)
  {
    Serial.println(F("Found ILI9328 LCD driver"));
  } 
  else
  {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    return;
  }
  tft.begin(identifier);

  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.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);
  tft.fillRect(0, tft.height()-BOXSIZE, BOXSIZE, tft.height(),WHITE);
  tft.fillRect(tft.width()-20, tft.height()-20,tft.width(), tft.height(),CYAN);
  tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);*/
  currentcolor = RED;
 
  pinMode(8, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000
int showFlag(2);
unsigned long int showTime(0);
void loop()
{
  
  if(showFlag==0)
  {
   // Serial.println(showTime);
    showTime++;
    if(showTime>60000)
    {
      showFlag=1;
     }
      digitalWrite(8, HIGH);
      TSPoint p = ts.getPoint();
      digitalWrite(8, LOW);
    
    
      //tft.fillRect(tes, test, tes+BOXSIZE, test+BOXSIZE, BLUE); 
      //Serial.print(tes);
      //Serial.print("    ");
      //Serial.println(test);
      // if sharing pins, you'll need to fix the directions of the touchscreen pins
     // pinMode(XP, OUTPUT);
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
    //  pinMode(YM, OUTPUT);
    
      // we have some minimum pressure we consider 'valid'
      // pressure of 0 means no pressing!
    
      if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
      {
        /*
        Serial.print("X = "); Serial.print(p.x);
        Serial.print("\tY = "); Serial.print(p.y);
        Serial.print("\tPressure = "); Serial.println(p.z);
        */
      
        // scale from 0->1023 to tft.width
        p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
        p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
        
       // Serial.print("("); Serial.print(p.x);
       // Serial.print(", "); Serial.print(p.y);
    
    
        if (p.y < BOXSIZE) 
        {
           showTime=0;
           oldcolor = currentcolor;
    
           if (p.x < BOXSIZE) 
           { 
             currentcolor = RED; 
             tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
           } 
           else if (p.x < BOXSIZE*2)
           {
             currentcolor = YELLOW;
             tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
           }
           else if (p.x < BOXSIZE*3) 
           {
             currentcolor = GREEN;
             tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
           }
           else if (p.x < BOXSIZE*4) 
           {
             currentcolor = CYAN;
             tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
           } 
           else if (p.x < BOXSIZE*5)
           {
             currentcolor = BLUE;
             tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
           }
           else if (p.x < 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 (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) //画点
        {
          showTime=0;
          tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
        } if (p.y >tft.height()-BOXSIZE && p.x < BOXSIZE)
        {
          showTime=0;
     //     Serial.println("erase");
          // press the bottom of the screen to erase 
          tft.fillRect(0, BOXSIZE, tft.width(), tft.height(), BLACK);
          tft.fillRect(0, tft.height()-BOXSIZE,BOXSIZE, tft.height(),WHITE);
          tft.fillRect(tft.width()-20, tft.height()-20,tft.width(), tft.height(),currentcolor);
        }
        if(p.y >tft.height()-20 && p.x > tft.width()-20)
        {
            showFlag=1;
         }
      }
     
  }
  if(showFlag==1)
  {
     showTime=0;
    for (uint8_t rotation = 0; rotation <= 4; rotation++) 
    {
      tft.setRotation(rotation);
      testText();
      delay(5000);
     }
    testFillScreen();
    delay(1000);
    
    testText();
    delay(3000);
    testLines(CYAN);
    delay(1000);
    testFastLines(RED, BLUE);
    delay(1000);
    
    testRects(GREEN);
    delay(1000);
    testFilledRects(YELLOW, MAGENTA);
    delay(1000);
    testFilledCircles(10, MAGENTA);
    testCircles(10, WHITE);
    delay(1000);
    testTriangles();
    delay(1000);
    testFilledTriangles();
    delay(1000);
    
    testRoundRects();
    delay(1000);
    testFilledRoundRects();
    delay(1000);
    showFlag=2;
  }
  if(showFlag==2)
  {
    tft.reset();
    delay(500);
    tft.begin(0x9328);
    tft.setCursor(0, 0);//光标放在原点,以免下次回来时串行
    tft.fillScreen(BLACK);
    tft.setTextColor(BLUE);
    tft.setTextSize(6);
    tft.println(" ");
    tft.println(" ");
    tft.println(" PRINT");
    tft.println(" ");
    tft.setTextColor(MAGENTA);
    tft.setTextSize(2);
    tft.println("    It is easy !!");
    tft.println(" ");
    tft.setTextSize(1);
    tft.setTextColor(GREEN);
    tft.println("          Touch me like you do!");
    delay(5000);
    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.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);
    tft.fillRect(0, tft.height()-BOXSIZE, BOXSIZE, tft.height(),WHITE);
    tft.fillRect(tft.width()-20, tft.height()-20,tft.width(), tft.height(),CYAN);
    tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
    currentcolor = RED;
   
    pinMode(13, OUTPUT);
    showFlag=0;
  }
  
}
unsigned long testFillScreen() 
{
  unsigned long start = micros();
  tft.fillScreen(BLACK);
  tft.fillScreen(RED);
  tft.fillScreen(GREEN);
  tft.fillScreen(BLUE);
  tft.fillScreen(BLACK);
  return micros() - start;
}

unsigned long testText() 
{
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.setTextColor(WHITE);  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(YELLOW); tft.setTextSize(2);
  tft.println(20170806);
  tft.setTextColor(RED);    tft.setTextSize(3);
  //tft.println(0xABCDEF, HEX);
  tft.print(15776);
  tft.print(696109);
  tft.println();
  tft.setTextColor(GREEN);
  tft.setTextSize(4);
  tft.println("Hou Lidong");
  tft.setTextSize(2);
  tft.println("I like play it!");
  tft.setTextSize(1);
  tft.println("I try to using the TFT");
  tft.println("boring will become a habit");
  tft.println("I am so lonely and hungry");
  tft.println("I just feel not good");
  tft.println("and the arduino ide is too simple");
  tft.println("I think IAR or keil will be more useful");
  tft.println("just write something here");
  //delay(999999);
  return micros() - start;
  
}

unsigned long testLines(uint16_t color)
{
  unsigned long start, t;
  int           x1, y1, x2, y2,
                w = tft.width(),
                h = tft.height();

  tft.fillScreen(BLACK);

  x1 = y1 = 0;
  y2    = h - 1;
  start = micros();
  for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  x2    = w - 1;
  for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  t     = micros() - start; // fillScreen doesn't count against timing

  tft.fillScreen(BLACK);

  x1    = w - 1;
  y1    = 0;
  y2    = h - 1;
  start = micros();
  for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  x2    = 0;
  for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  t    += micros() - start;

  tft.fillScreen(BLACK);

  x1    = 0;
  y1    = h - 1;
  y2    = 0;
  start = micros();
  for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  x2    = w - 1;
  for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  t    += micros() - start;

  tft.fillScreen(BLACK);

  x1    = w - 1;
  y1    = h - 1;
  y2    = 0;
  start = micros();
  for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
  x2    = 0;
  for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);

  return micros() - start;
}

unsigned long testFastLines(uint16_t color1, uint16_t color2) 
{
  unsigned long start;
  int           x, y, w = tft.width(), h = tft.height();

  tft.fillScreen(BLACK);
  start = micros();
  for (y = 0; y < h; y += 5) tft.drawFastHLine(0, y, w, color1);
  for (x = 0; x < w; x += 5) tft.drawFastVLine(x, 0, h, color2);

  return micros() - start;
}

unsigned long testRects(uint16_t color) 
{
  unsigned long start;
  int           n, i, i2,
                cx = tft.width()  / 2,
                cy = tft.height() / 2;

  tft.fillScreen(BLACK);
  n     = min(tft.width(), tft.height());
  start = micros();
  for (i = 2; i < n; i += 6) 
  {
    i2 = i / 2;
    tft.drawRect(cx - i2, cy - i2, i, i, color);
  }

  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;
}

unsigned long testFilledCircles(uint8_t radius, uint16_t color) 
{
  unsigned long start;
  int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;

  tft.fillScreen(BLACK);
  start = micros();
  for (x = radius; x < w; x += r2) 
  {
    for (y = radius; y < h; y += r2)
    {
      tft.fillCircle(x, y, radius, color);
    }
  }

  return micros() - start;
}

unsigned long testCircles(uint8_t radius, uint16_t color)
{
  unsigned long start;
  int    x, y, r2 = radius * 2,
                      w = tft.width()  + radius,
                      h = tft.height() + radius;

  // Screen is not cleared for this one -- this is
  // intentional and does not affect the reported time.
  start = micros();
  for (x = 0; x < w; x += r2)
  {
    for (y = 0; y < h; y += r2)
    {
      tft.drawCircle(x, y, radius, color);
    }
  }

  return micros() - start;
}

unsigned long testTriangles() 
{
  unsigned long start;
  int           n, i, cx = tft.width()  / 2 - 1,
                      cy = tft.height() / 2 - 1;

  tft.fillScreen(BLACK);
  n     = min(cx, cy);
  start = micros();
  for (i = 0; i < n; i += 5)
  {
    tft.drawTriangle(
      cx    , cy - i, // peak
      cx - i, cy + i, // bottom left
      cx + i, cy + i, // bottom right
      tft.color565(0, 0, i));
  }

  return micros() - start;
}

unsigned long testFilledTriangles()
{
  unsigned long start, t = 0;
  int           i, cx = tft.width()  / 2 - 1,
                   cy = tft.height() / 2 - 1;

  tft.fillScreen(BLACK);
  start = micros();
  for (i = min(cx, cy); i > 10; i -= 5) 
  {
    start = micros();
    tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
                     tft.color565(0, i, i));
    t += micros() - start;
    tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
                     tft.color565(i, i, 0));
  }

  return t;
}

unsigned long testRoundRects() 
{
  unsigned long start;
  int           w, i, i2,
                cx = tft.width()  / 2 - 1,
                cy = tft.height() / 2 - 1;

  tft.fillScreen(BLACK);
  w     = min(tft.width(), tft.height());
  start = micros();
  for (i = 0; i < w; i += 6)
  {
    i2 = i / 2;
    tft.drawRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(i, 0, 0));
  }

  return micros() - start;
}

unsigned long testFilledRoundRects() 
{
  unsigned long start;
  int           i, i2,
                cx = tft.width()  / 2 - 1,
                cy = tft.height() / 2 - 1;

  tft.fillScreen(BLACK);
  start = micros();
  for (i = min(tft.width(), tft.height()); i > 20; i -= 6) 
  {
    i2 = i / 2;
    tft.fillRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(0, i, 0));
  }

  return micros() - start;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值