#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
#include "bmp.h"
#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif
#ifndef TFT_SLPIN
#define TFT_SLPIN 0x10
#endif
#define TFT_BL 17
TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
void showImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data);
void setup()
{
Serial.begin(9600);
Serial.println("Start");
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(2);
tft.setTextColor(TFT_MAGENTA);
tft.setCursor(0, 0);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(1);
// if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
// //显示屏背光
// ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
// ledcAttachPin(TFT_BL, 10);
// analogReadResolution(10);
// ledcWrite(10,512);
// }
}
void loop()
{
tft.fillScreen(TFT_WHITE);
tft.setSwapBytes(true);
showImage(50, 50, 32, 32, bmp2);
showImage(90, 90, 32, 32, bmp3);
delay(1000);
}
#define PI_BUF_SIZE 128
void showImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data){
int32_t dx = 0;
int32_t dy = 0;
int32_t dw = w;
int32_t dh = h*2;
if (x < 0) { dw += x; dx = -x; x = 0; }
if (y < 0) { dh += y; dy = -y; y = 0; }
if (dw < 1 || dh < 1) return;
CS_L;
data += dx + dy *w ;
uint16_t buffer[PI_BUF_SIZE];
uint16_t* pix_buffer = buffer;
uint16_t high,low;
tft.setWindow(x, y, x + dw - 1 , y + dh -1);
// Work out the number whole buffers to send
uint16_t nb = (dw * dh) / (2 * PI_BUF_SIZE) ;
// Fill and send "nb" buffers to TFT
// 第一个for是纵向渲染图片
for (int32_t i = 0; i < nb; i++) {
for (int32_t j = 0; j < PI_BUF_SIZE ; j++) {
high = pgm_read_word(&data[(i * 2 * PI_BUF_SIZE) + 2 * j - 1]);
low = pgm_read_word(&data[(i * 2 * PI_BUF_SIZE) + 2 * j ]);
pix_buffer[j] = (high<<8)+low;
}
tft.pushPixels(pix_buffer, PI_BUF_SIZE);
}
// Work out number of pixels not yet sent
uint16_t np = (dw * dh) % (2 * PI_BUF_SIZE);
// Send any partial buffer left over
if (np) {
for (int32_t i = 0; i < np; i++)
{
high = pgm_read_word(&data[(nb * 2 * PI_BUF_SIZE) + 2 * i -1]);
low = pgm_read_word(&data[(nb * 2 * PI_BUF_SIZE) + 2 * i ]);
pix_buffer[i] = (high<<8)+low;
}
tft.pushPixels(pix_buffer, np);
}
CS_H;
}
这么弄在线等急!
我感觉是数组的问题渲染歪了但是不知道改哪里
没有渲染成功,对吗