mirror of
https://github.com/adafruit/Adafruit_ILI9341.git
synced 2026-10-26 22:10:38 +00:00
BMP examples moved to Adafruit_ImageReader library
This commit is contained in:
1 parent
90d6708465
commit
4b17be306c
4 files changed
+3
-485
No files matched your search
Whitespace-only changes.
@@ -1,267 +0,0 @@
|
||||
/***************************************************
|
||||
This is our touchscreen painting example for the Adafruit TFT FeatherWing
|
||||
----> http://www.adafruit.com/products/3315
|
||||
|
||||
Check out the links above for our tutorials and wiring diagrams
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Wire.h> // this is needed even tho we aren't using it
|
||||
|
||||
#include <Adafruit_GFX.h> // Core graphics library
|
||||
#include <Adafruit_ILI9341.h> // Hardware-specific library
|
||||
#include <SD.h>
|
||||
#include <Adafruit_STMPE610.h>
|
||||
|
||||
#ifdef ESP8266
|
||||
#define STMPE_CS 16
|
||||
#define TFT_CS 0
|
||||
#define TFT_DC 15
|
||||
#define SD_CS 2
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
#define STMPE_CS 32
|
||||
#define TFT_CS 15
|
||||
#define TFT_DC 33
|
||||
#define SD_CS 14
|
||||
#endif
|
||||
#ifdef TEENSYDUINO
|
||||
#define TFT_DC 10
|
||||
#define TFT_CS 4
|
||||
#define STMPE_CS 3
|
||||
#define SD_CS 8
|
||||
#endif
|
||||
#ifdef ARDUINO_STM32_FEATHER
|
||||
#define TFT_DC PB4
|
||||
#define TFT_CS PA15
|
||||
#define STMPE_CS PC7
|
||||
#define SD_CS PC5
|
||||
#endif
|
||||
#ifdef ARDUINO_NRF52832_FEATHER /* BSP 0.6.5 and higher! */
|
||||
#define TFT_DC 11
|
||||
#define TFT_CS 31
|
||||
#define STMPE_CS 30
|
||||
#define SD_CS 27
|
||||
#endif
|
||||
#if defined(ARDUINO_MAX32620FTHR) || defined(ARDUINO_MAX32630FTHR)
|
||||
#define TFT_DC P5_4
|
||||
#define TFT_CS P5_3
|
||||
#define STMPE_CS P3_3
|
||||
#define SD_CS P3_2
|
||||
#endif
|
||||
|
||||
// Anything else!
|
||||
#if defined (__AVR_ATmega32U4__) || defined(ARDUINO_SAMD_FEATHER_M0) || defined (__AVR_ATmega328P__) || \
|
||||
defined(ARDUINO_SAMD_ZERO) || defined(__SAMD51__) || defined(__SAM3X8E__) || defined(ARDUINO_NRF52840_FEATHER)
|
||||
#define STMPE_CS 6
|
||||
#define TFT_CS 9
|
||||
#define TFT_DC 10
|
||||
#define SD_CS 5
|
||||
#endif
|
||||
|
||||
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
|
||||
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
|
||||
|
||||
|
||||
// This is calibration data for the raw touch data to the screen coordinates
|
||||
#define TS_MINX 3800
|
||||
#define TS_MAXX 100
|
||||
#define TS_MINY 100
|
||||
#define TS_MAXY 3750
|
||||
|
||||
#define PENRADIUS 3
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(10);
|
||||
Serial.println("FeatherWing TFT");
|
||||
if (!ts.begin()) {
|
||||
Serial.println("Couldn't start touchscreen controller");
|
||||
while (1);
|
||||
}
|
||||
Serial.println("Touchscreen started");
|
||||
|
||||
tft.begin();
|
||||
tft.fillScreen(ILI9341_BLUE);
|
||||
|
||||
yield();
|
||||
|
||||
Serial.print("Initializing SD card...");
|
||||
if (!SD.begin(SD_CS)) {
|
||||
Serial.println("failed!");
|
||||
}
|
||||
Serial.println("OK!");
|
||||
|
||||
bmpDraw("/purple.bmp", 0, 0);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Retrieve a point
|
||||
TS_Point p = ts.getPoint();
|
||||
|
||||
Serial.print("X = "); Serial.print(p.x);
|
||||
Serial.print("\tY = "); Serial.print(p.y);
|
||||
Serial.print("\tPressure = "); Serial.println(p.z);
|
||||
|
||||
// Scale from ~0->4000 to tft.width using the calibration #'s
|
||||
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
|
||||
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
|
||||
|
||||
if (((p.y-PENRADIUS) > 0) && ((p.y+PENRADIUS) < tft.height())) {
|
||||
tft.fillCircle(p.x, p.y, PENRADIUS, ILI9341_RED);
|
||||
}
|
||||
}
|
||||
|
||||
// This function opens a Windows Bitmap (BMP) file and
|
||||
// displays it at the given coordinates. It's sped up
|
||||
// by reading many pixels worth of data at a time
|
||||
// (rather than pixel by pixel). Increasing the buffer
|
||||
// size takes more of the Arduino's precious RAM but
|
||||
// makes loading a little faster. 20 pixels seems a
|
||||
// good balance.
|
||||
|
||||
#define BUFFPIXEL 20
|
||||
|
||||
void bmpDraw(char *filename, int16_t x, int16_t y) {
|
||||
|
||||
File bmpFile;
|
||||
int bmpWidth, bmpHeight; // W+H in pixels
|
||||
uint8_t bmpDepth; // Bit depth (currently must be 24)
|
||||
uint32_t bmpImageoffset; // Start of image data in file
|
||||
uint32_t rowSize; // Not always = bmpWidth; may have padding
|
||||
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
|
||||
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
||||
boolean goodBmp = false; // Set to true on valid header parse
|
||||
boolean flip = true; // BMP is stored bottom-to-top
|
||||
int w, h, row, col;
|
||||
uint8_t r, g, b;
|
||||
uint32_t pos = 0, startTime = millis();
|
||||
|
||||
if((x >= tft.width()) || (y >= tft.height())) return;
|
||||
|
||||
Serial.println();
|
||||
Serial.print(F("Loading image '"));
|
||||
Serial.print(filename);
|
||||
Serial.println('\'');
|
||||
|
||||
// Open requested file on SD card
|
||||
if ((bmpFile = SD.open(filename)) == NULL) {
|
||||
Serial.print(F("File not found"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse BMP header
|
||||
if(read16(bmpFile) == 0x4D42) { // BMP signature
|
||||
Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
|
||||
(void)read32(bmpFile); // Read & ignore creator bytes
|
||||
bmpImageoffset = read32(bmpFile); // Start of image data
|
||||
Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
|
||||
// Read DIB header
|
||||
Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
|
||||
bmpWidth = read32(bmpFile);
|
||||
bmpHeight = read32(bmpFile);
|
||||
if(read16(bmpFile) == 1) { // # planes -- must be '1'
|
||||
bmpDepth = read16(bmpFile); // bits per pixel
|
||||
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
|
||||
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
|
||||
|
||||
goodBmp = true; // Supported BMP format -- proceed!
|
||||
Serial.print(F("Image size: "));
|
||||
Serial.print(bmpWidth);
|
||||
Serial.print('x');
|
||||
Serial.println(bmpHeight);
|
||||
|
||||
// BMP rows are padded (if needed) to 4-byte boundary
|
||||
rowSize = (bmpWidth * 3 + 3) & ~3;
|
||||
|
||||
// If bmpHeight is negative, image is in top-down order.
|
||||
// This is not canon but has been observed in the wild.
|
||||
if(bmpHeight < 0) {
|
||||
bmpHeight = -bmpHeight;
|
||||
flip = false;
|
||||
}
|
||||
|
||||
// Crop area to be loaded
|
||||
w = bmpWidth;
|
||||
h = bmpHeight;
|
||||
if((x+w-1) >= tft.width()) w = tft.width() - x;
|
||||
if((y+h-1) >= tft.height()) h = tft.height() - y;
|
||||
|
||||
// Set TFT address window to clipped image bounds
|
||||
tft.startWrite();
|
||||
tft.setAddrWindow(x, y, w, h);
|
||||
|
||||
for (row=0; row<h; row++) { // For each scanline...
|
||||
|
||||
// Seek to start of scan line. It might seem labor-
|
||||
// intensive to be doing this on every line, but this
|
||||
// method covers a lot of gritty details like cropping
|
||||
// and scanline padding. Also, the seek only takes
|
||||
// place if the file position actually needs to change
|
||||
// (avoids a lot of cluster math in SD library).
|
||||
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
||||
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
|
||||
else // Bitmap is stored top-to-bottom
|
||||
pos = bmpImageoffset + row * rowSize;
|
||||
if(bmpFile.position() != pos) { // Need seek?
|
||||
tft.endWrite();
|
||||
bmpFile.seek(pos);
|
||||
buffidx = sizeof(sdbuffer); // Force buffer reload
|
||||
}
|
||||
|
||||
for (col=0; col<w; col++) { // For each pixel...
|
||||
// Time to read more pixel data?
|
||||
if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
||||
tft.endWrite();
|
||||
bmpFile.read(sdbuffer, sizeof(sdbuffer));
|
||||
tft.startWrite();
|
||||
buffidx = 0; // Set index to beginning
|
||||
}
|
||||
|
||||
// Convert pixel from BMP to TFT format, push to display
|
||||
b = sdbuffer[buffidx++];
|
||||
g = sdbuffer[buffidx++];
|
||||
r = sdbuffer[buffidx++];
|
||||
uint16_t c = tft.color565(r,g,b);
|
||||
tft.writePixels(&c, 1);
|
||||
} // end pixel
|
||||
tft.endWrite();
|
||||
} // end scanline
|
||||
Serial.print(F("Loaded in "));
|
||||
Serial.print(millis() - startTime);
|
||||
Serial.println(" ms");
|
||||
} // end goodBmp
|
||||
}
|
||||
}
|
||||
|
||||
bmpFile.close();
|
||||
if(!goodBmp) Serial.println(F("BMP format not recognized."));
|
||||
}
|
||||
|
||||
// These read 16- and 32-bit types from the SD card file.
|
||||
// BMP data is stored little-endian, Arduino is little-endian too.
|
||||
// May need to reverse subscript order if porting elsewhere.
|
||||
|
||||
uint16_t read16(File &f) {
|
||||
uint16_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t read32(File &f) {
|
||||
uint32_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read();
|
||||
((uint8_t *)&result)[2] = f.read();
|
||||
((uint8_t *)&result)[3] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
/***************************************************
|
||||
This is our Bitmap drawing example for the Adafruit ILI9341 Breakout and Shield
|
||||
----> http://www.adafruit.com/products/1651
|
||||
|
||||
Check out the links above for our tutorials and wiring diagrams
|
||||
These displays use SPI to communicate, 4 or 5 pins are required to
|
||||
interface (RST is optional)
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
|
||||
#include <Adafruit_GFX.h> // Core graphics library
|
||||
#include "Adafruit_ILI9341.h" // Hardware-specific library
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
|
||||
// TFT display and SD card will share the hardware SPI interface.
|
||||
// Hardware SPI pins are specific to the Arduino board type and
|
||||
// cannot be remapped to alternate pins. For Arduino Uno,
|
||||
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
|
||||
|
||||
#define TFT_DC 9
|
||||
#define TFT_CS 10
|
||||
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
|
||||
|
||||
#define SD_CS 4
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
|
||||
tft.begin();
|
||||
|
||||
yield();
|
||||
|
||||
Serial.print("Initializing SD card...");
|
||||
if (!SD.begin(SD_CS)) {
|
||||
Serial.println("failed!");
|
||||
}
|
||||
Serial.println("OK!");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for(uint8_t r=0; r<4; r++) {
|
||||
tft.setRotation(r);
|
||||
tft.fillScreen(ILI9341_BLUE);
|
||||
for(int8_t i=-2; i<1; i++) {
|
||||
bmpDraw("/purple.bmp",
|
||||
(tft.width() / 2) + (i * 120),
|
||||
(tft.height() / 2) + (i * 160));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This function opens a Windows Bitmap (BMP) file and
|
||||
// displays it at the given coordinates. It's sped up
|
||||
// by reading many pixels worth of data at a time
|
||||
// (rather than pixel by pixel). Increasing the buffer
|
||||
// size takes more of the Arduino's precious RAM but
|
||||
// makes loading a little faster. 20 pixels seems a
|
||||
// good balance.
|
||||
|
||||
#define BUFFPIXEL 20
|
||||
|
||||
void bmpDraw(char *filename, int16_t x, int16_t y) {
|
||||
|
||||
File bmpFile;
|
||||
int bmpWidth, bmpHeight; // W+H in pixels
|
||||
uint8_t bmpDepth; // Bit depth (currently must be 24)
|
||||
uint32_t bmpImageoffset; // Start of image data in file
|
||||
uint32_t rowSize; // Not always = bmpWidth; may have padding
|
||||
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
|
||||
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
||||
boolean goodBmp = false; // Set to true on valid header parse
|
||||
boolean flip = true; // BMP is stored bottom-to-top
|
||||
int w, h, row, col, x2, y2, bx1, by1;
|
||||
uint8_t r, g, b;
|
||||
uint32_t pos = 0, startTime = millis();
|
||||
|
||||
if((x >= tft.width()) || (y >= tft.height())) return;
|
||||
|
||||
Serial.println();
|
||||
Serial.print(F("Loading image '"));
|
||||
Serial.print(filename);
|
||||
Serial.println('\'');
|
||||
|
||||
// Open requested file on SD card
|
||||
if ((bmpFile = SD.open(filename)) == NULL) {
|
||||
Serial.print(F("File not found"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse BMP header
|
||||
if(read16(bmpFile) == 0x4D42) { // BMP signature
|
||||
Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
|
||||
(void)read32(bmpFile); // Read & ignore creator bytes
|
||||
bmpImageoffset = read32(bmpFile); // Start of image data
|
||||
Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
|
||||
// Read DIB header
|
||||
Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
|
||||
bmpWidth = read32(bmpFile);
|
||||
bmpHeight = read32(bmpFile);
|
||||
if(read16(bmpFile) == 1) { // # planes -- must be '1'
|
||||
bmpDepth = read16(bmpFile); // bits per pixel
|
||||
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
|
||||
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
|
||||
|
||||
goodBmp = true; // Supported BMP format -- proceed!
|
||||
Serial.print(F("Image size: "));
|
||||
Serial.print(bmpWidth);
|
||||
Serial.print('x');
|
||||
Serial.println(bmpHeight);
|
||||
|
||||
// BMP rows are padded (if needed) to 4-byte boundary
|
||||
rowSize = (bmpWidth * 3 + 3) & ~3;
|
||||
|
||||
// If bmpHeight is negative, image is in top-down order.
|
||||
// This is not canon but has been observed in the wild.
|
||||
if(bmpHeight < 0) {
|
||||
bmpHeight = -bmpHeight;
|
||||
flip = false;
|
||||
}
|
||||
|
||||
// Crop area to be loaded
|
||||
x2 = x + bmpWidth - 1; // Lower-right corner
|
||||
y2 = y + bmpHeight - 1;
|
||||
if((x2 >= 0) && (y2 >= 0)) { // On screen?
|
||||
w = bmpWidth; // Width/height of section to load/display
|
||||
h = bmpHeight;
|
||||
bx1 = by1 = 0; // UL coordinate in BMP file
|
||||
if(x < 0) { // Clip left
|
||||
bx1 = -x;
|
||||
x = 0;
|
||||
w = x2 + 1;
|
||||
}
|
||||
if(y < 0) { // Clip top
|
||||
by1 = -y;
|
||||
y = 0;
|
||||
h = y2 + 1;
|
||||
}
|
||||
if(x2 >= tft.width()) w = tft.width() - x; // Clip right
|
||||
if(y2 >= tft.height()) h = tft.height() - y; // Clip bottom
|
||||
|
||||
// Set TFT address window to clipped image bounds
|
||||
tft.startWrite(); // Requires start/end transaction now
|
||||
tft.setAddrWindow(x, y, w, h);
|
||||
|
||||
for (row=0; row<h; row++) { // For each scanline...
|
||||
|
||||
// Seek to start of scan line. It might seem labor-
|
||||
// intensive to be doing this on every line, but this
|
||||
// method covers a lot of gritty details like cropping
|
||||
// and scanline padding. Also, the seek only takes
|
||||
// place if the file position actually needs to change
|
||||
// (avoids a lot of cluster math in SD library).
|
||||
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
||||
pos = bmpImageoffset + (bmpHeight - 1 - (row + by1)) * rowSize;
|
||||
else // Bitmap is stored top-to-bottom
|
||||
pos = bmpImageoffset + (row + by1) * rowSize;
|
||||
pos += bx1 * 3; // Factor in starting column (bx1)
|
||||
if(bmpFile.position() != pos) { // Need seek?
|
||||
tft.endWrite(); // End TFT transaction
|
||||
bmpFile.seek(pos);
|
||||
buffidx = sizeof(sdbuffer); // Force buffer reload
|
||||
tft.startWrite(); // Start new TFT transaction
|
||||
}
|
||||
for (col=0; col<w; col++) { // For each pixel...
|
||||
// Time to read more pixel data?
|
||||
if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
||||
tft.endWrite(); // End TFT transaction
|
||||
bmpFile.read(sdbuffer, sizeof(sdbuffer));
|
||||
buffidx = 0; // Set index to beginning
|
||||
tft.startWrite(); // Start new TFT transaction
|
||||
}
|
||||
// Convert pixel from BMP to TFT format, push to display
|
||||
b = sdbuffer[buffidx++];
|
||||
g = sdbuffer[buffidx++];
|
||||
r = sdbuffer[buffidx++];
|
||||
tft.writePixel(tft.color565(r,g,b));
|
||||
} // end pixel
|
||||
} // end scanline
|
||||
tft.endWrite(); // End last TFT transaction
|
||||
} // end onscreen
|
||||
Serial.print(F("Loaded in "));
|
||||
Serial.print(millis() - startTime);
|
||||
Serial.println(" ms");
|
||||
} // end goodBmp
|
||||
}
|
||||
}
|
||||
|
||||
bmpFile.close();
|
||||
if(!goodBmp) Serial.println(F("BMP format not recognized."));
|
||||
}
|
||||
|
||||
// These read 16- and 32-bit types from the SD card file.
|
||||
// BMP data is stored little-endian, Arduino is little-endian too.
|
||||
// May need to reverse subscript order if porting elsewhere.
|
||||
|
||||
uint16_t read16(File &f) {
|
||||
uint16_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t read32(File &f) {
|
||||
uint32_t result;
|
||||
((uint8_t *)&result)[0] = f.read(); // LSB
|
||||
((uint8_t *)&result)[1] = f.read();
|
||||
((uint8_t *)&result)[2] = f.read();
|
||||
((uint8_t *)&result)[3] = f.read(); // MSB
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user