esp32-按键中断点亮lcd屏幕(二)

目录

准备工作

代码


准备工作

1.54寸ips屏幕如下,esp32 devkit-c开发板一个,其中lcd的gnd,vcc接开发板的gnd,vcc,SCL接D18,SDA接D23,RES接D26,DC接D25,CS接D27,BLK接D22,TFT_eSPI库配置文件User_Setup.h如下,实验使用Platformio。按钮接D15然后接地。

#define ST7789_DRIVER // Full configuration option, define 
#define TFT_RGB_ORDER TFT_BGR // Colour order Blue-Green-Red

// For M5Stack ESP32 module with integrated ILI9341 display ONLY, remove // in line below

// #define M5STACK

// For ST7789, ST7735 and ILI9163 ONLY, define the pixel width and height in portrait orientation
// #define TFT_WIDTH  80
// #define TFT_WIDTH  128
#define TFT_WIDTH 240  // ST7789 240 x 240 and 240 x 320
                       // #define TFT_HEIGHT 160
                       // #define TFT_HEIGHT 128
#define TFT_HEIGHT 240 // ST7789 240 x 240
#define TFT_MISO 19
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_CS 27  // Chip select control pin
#define TFT_DC 25  // Data Command control pin
#define TFT_RST 26 // Reset pin (could connect to RST pin)
#define LOAD_GLCD  // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH
#define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters
#define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters
#define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm
#define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:-.
#define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-.
// #define LOAD_FONT8N // Font 8. Alternative to Font 8 above, slightly narrower, so 3 digits fit a 160 pixel TFT
#define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts

// Comment out the #define below to stop the SPIFFS filing system and smooth font code being loaded
// this will save ~20kbytes of FLASH
#define SMOOTH_FONT
#define SPI_FREQUENCY 27000000
// #define SPI_FREQUENCY  40000000
//  #define SPI_FREQUENCY  55000000 // STM32 SPI1 only (SPI2 maximum is 27MHz)
//  #define SPI_FREQUENCY  80000000

// Optional reduced SPI frequency for reading TFT
#define SPI_READ_FREQUENCY 20000000

// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
#define SPI_TOUCH_FREQUENCY 2500000

代码


#include <Arduino.h>
// Set delay after plotting the sprite
#define DELAY 1000

// Width and height of sprite
#define WIDTH 128
#define HEIGHT 128
#define PIN_INPUT 15  //按钮口
// Attach a LED using GPIO 25 and VCC. The LED is on when output level is LOW.
#define PIN_BLK 22  //led屏幕gpio口

#include <TFT_eSPI.h> // Include the graphics library (this includes the sprite functions)

TFT_eSPI tft = TFT_eSPI(); // Declare object "tft"
TFT_eSprite spr = TFT_eSprite(&tft); // Declare Sprite object "spr" with pointer to "tft" object

int ledState = LOW;

unsigned long button_time = 0;
unsigned long last_button_time = 0;

void IRAM_ATTR ISR()
{
    //中断处理程序
    button_time = millis();
    if (button_time - last_button_time > 500)
    {
        Serial.println("x1");
        ledState = !ledState; // reverse the BLK
        digitalWrite(PIN_BLK, ledState);
        last_button_time = button_time;
    }
}
void setup()
{

    Serial.begin(115200);
    pinMode(PIN_BLK, OUTPUT); // sets the digital pin as output
    pinMode(PIN_INPUT, INPUT_PULLUP);//设置初始模式 输入 ,3.3v
    // Initialise the TFT registers
    digitalWrite(PIN_BLK, ledState);
    Serial.println("button initial");
    tft.init();
    attachInterrupt(PIN_INPUT, ISR, FALLING);
    // Optionally set colour depth to 8 or 16 bits, default is 16 if not spedified
    // spr.setColorDepth(8);

    // Create a sprite of defined size
    spr.createSprite(WIDTH, HEIGHT);

    // Clear the TFT screen to blue
    tft.fillScreen(TFT_BLUE);
}

void loop(void)
{
    // Fill the whole sprite with black (Sprite is in memory so not visible yet)

    spr.fillSprite(TFT_BLACK);
    Serial.println("fillSrpite");
    // Number of pixels to draw
    uint16_t n = 100;

    // Draw 100 random colour pixels at random positions in sprite
    while (n--)
    {
        uint16_t colour = random(0x10000); // Returns colour 0 - 0xFFFF
        int16_t x = random(WIDTH);         // Random x coordinate
        int16_t y = random(HEIGHT);        // Random y coordinate
        spr.drawPixel(x, y, colour);       // Draw pixel in sprite
        Serial.println("drawPixel");
    }

    // Draw some lines
    spr.drawLine(1, 0, WIDTH, HEIGHT - 1, TFT_GREEN);
    spr.drawLine(0, 0, WIDTH, HEIGHT, TFT_GREEN);
    spr.drawLine(0, 1, WIDTH - 1, HEIGHT, TFT_GREEN);
    spr.drawLine(0, HEIGHT - 1, WIDTH - 1, 0, TFT_RED);
    spr.drawLine(0, HEIGHT, WIDTH, 0, TFT_RED);
    spr.drawLine(1, HEIGHT, WIDTH, 1, TFT_RED);

    // Draw some text with Middle Centre datum
    spr.setTextDatum(MC_DATUM);
    spr.drawString("Sprite", WIDTH / 2, HEIGHT / 2, 4);

    // Now push the sprite to the TFT at position 0,0 on screen
    spr.pushSprite(-40, -40);
    spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2);
    spr.pushSprite(tft.width() - WIDTH + 40, tft.height() - HEIGHT + 40);

    delay(DELAY);

    // Fill TFT screen with blue
    tft.fillScreen(TFT_BLUE);
    Serial.println("Draw a blue rectangle");
    // Draw a blue rectangle in sprite so when we move it 1 pixel it does not leave a trail
    // on the blue screen background
    spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLUE);

    int x = tft.width() / 2 - WIDTH / 2;
    int y = tft.height() / 2 - HEIGHT / 2;

    uint32_t updateTime = 0; // time for next update

    while (true)
    {
      Serial.println("Random movement direction");
      //  Random movement direction
      int dx = 1;
      if (random(2))
        dx = -1;
      int dy = 1;
      if (random(2))
        dy = -1;

      // Pull it back onto screen if it wanders off
      if (x < -WIDTH / 2)
        dx = 1;
      if (x >= tft.width() - WIDTH / 2)
        dx = -1;
      if (y < -HEIGHT / 2)
        dy = 1;
      if (y >= tft.height() - HEIGHT / 2)
        dy = -1;

      // Draw it 50 time, moving in random direct or staying still
      n = 50;
      int wait = random(50);
      while (n)
      {
        if (updateTime <= millis())
        {
          // Use time delay so sprtie does not move fast when not all on screen
          updateTime = millis() + wait;

          // Push the sprite to the TFT screen
          spr.pushSprite(x, y);
          // Serial.println("pushSprite");
          //  Change coord for next loop
          x += dx;
          y += dy;
          n--;
          yield(); // Stop watchdog reset
        }
        }
        Serial.println("pushSprite down");

    } // Infinite while, will not exit!
}

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值