adafruit_support_rick wrote:You'd have to re-write the bmpDraw routine to multiplex the data from the four different files. Essentially draw a buffer-load on the first display, then draw a buffer-load on the second display, etc.
OK, so I had a bit of time this week and tried to give this a go. I've re-written bmpDraw in an attempt to draw both images simultaneously, but I've gotten a bit stuck.
In the code I'm writing two different images to two TFT screens, in an attempt to cut the time it takes to draw them one at a time. The result of the code below is that both screens are populated with a rather fuzzy version of bmpFile1. They are drawn simultaneously but it's the same image. I've had to guess values for row and column. Perhaps someone with a better grasp of coding who can see where I may have gone wrong? Also, given the time it takes to draw a file is this method actually possible to save overall time in the long run? i.e with a whole load of screens. Much appreciated.
Code: Select all
/***************************************************
This is an example sketch for the Adafruit 2.2" SPI display.
This library works with the Adafruit 2.2" TFT Breakout w/SD card
----> http://www.adafruit.com/products/1480
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_ILI9340.h" // Hardware-specific library
#include <SPI.h>
#include <SD.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
// 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_RST 8
//#define TFT_DC 9
//#define TFT_CS 10
//#define SD_CS 4
//
//Adafruit_ILI9340 tft1 = Adafruit_ILI9340(TFT_CS, TFT_dDC, TFT_RST);
// These are the pins used for the UNO
// for Due/Mega/Leonardo use the hardware SPI pins (which are different)
//#define _sclk 13//13
//#define _miso 12//12
//#define _mosi 11//11
#define _cs 10 //10
#define _dc 9
#define _rst 8
#define SD_CS1 4
#define SD_CS2 6
#define _cs1 2
#define _dc1 7
#define _rst1 5
// Using software SPI is really not suggested, its incredibly slow
//Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _mosi, _sclk, _rst, _miso);
// Use hardware SPI
Adafruit_ILI9340 tft1 = Adafruit_ILI9340(_cs, _dc, _rst);
Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs1, _dc1, _rst1);
void setup(void) {
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS2)) {
Serial.println("failed!");
return;
}
Serial.println("OK!");
tft.begin();
tft1.begin();
// tft1.fillScreen(ILI9340_BLUE);
bmpDraw("rose.bmp", "woof1.bmp", 0, 0, 0, 0);
}
void loop() {
}
// 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 80
void bmpDraw(char *filename, char *filename2, uint8_t x, uint8_t y, uint8_t x2, uint8_t y2)
{
File bmpfile1;
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;
uint8_t r1, g1, b1;
uint32_t pos = 0, startTime = millis();
File bmpfile2;
int bmpWidth2, bmpHeight2; // W+H in pixels
uint8_t bmpDepth2; // Bit depth (currently must be 24)
uint32_t bmpImageoffset2; // Start of image data in file
uint32_t rowSize2; // Not always = bmpWidth; may have padding
uint8_t sdbuffer2[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx2 = sizeof(sdbuffer2); // Current position in sdbuffer
boolean goodBmp2 = false; // Set to true on valid header parse
boolean flip2 = true; // BMP is stored bottom-to-top
int w2, h2, row2, col2;
uint8_t r2, g2, b2;
uint32_t pos2 = 0, startTime2 = millis();
if((x >= tft1.width()) || (y >= tft1.height())) return;
if((x2 >= tft.width()) || (y2 >= tft.height())) return;
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
Serial.println("made it this far");
// // Open requested file on SD card
if ((bmpfile1 = SD.open(filename)) == NULL) {
Serial.print("File not found");
return;
}
//// // Open requested file on SD card
if ((bmpfile2 = SD.open(filename2)) == NULL) {
Serial.print("File not found");
return;
}
Serial.println("made it this far");
// Parse BMP header
if((read16(bmpfile1) == 0x4D42) && (read16(bmpfile2) == 0x4D42)) { // BMP signature
Serial.print("File size2: "); Serial.println(read32(bmpfile2));
(void)read32(bmpfile2); // Read & ignore creator bytes
bmpImageoffset2 = read32(bmpfile2); // Start of image data
Serial.print("Image Offset2: "); Serial.println(bmpImageoffset2, DEC);
// Read DIB header
Serial.print("Header size2: "); Serial.println(read32(bmpfile2));
bmpWidth2 = read32(bmpfile2);
bmpHeight2 = read32(bmpfile2);
Serial.print("File size: "); Serial.println(read32(bmpfile1));
(void)read32(bmpfile1); // Read & ignore creator bytes
bmpImageoffset = read32(bmpfile1); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: "); Serial.println(read32(bmpfile1));
bmpWidth = read32(bmpfile1);
bmpHeight = read32(bmpfile1);
if((read16(bmpfile1) == 1) && (read16(bmpfile2) == 1)) { // # planes -- must be '1'. Need condition here for more images
bmpDepth = read16(bmpfile1); // bits per pixel
bmpDepth2 = read16(bmpfile2); // bits per pixel
Serial.print("Bit Depth2: "); Serial.println(bmpDepth2);
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpfile1) == 0) && (bmpDepth2 == 24) && (read32(bmpfile2) == 0)) { // 0 = uncompressed. Need conditions here for more images
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
goodBmp2 = true; // Supported BMP format -- proceed!
Serial.print("Image size: ");
Serial.print(bmpWidth2);
Serial.print('x');
Serial.println(bmpHeight2);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
rowSize2 = (bmpWidth2 * 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;
}
if(bmpHeight2 < 0) {
bmpHeight2 = -bmpHeight2;
flip2 = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft1.width()) w = tft1.width() - x;
if((y+h-1) >= tft1.height()) h = tft1.height() - y;
w2 = bmpWidth2;
h2 = bmpHeight2;
if((x2+w2-1) >= tft.width()) w2 = tft.width() - x2;
// Set TFT address window to clipped image bounds
tft1.setAddrWindow(x, y, x+w-1, y+h-1);
tft.setAddrWindow(x2, y2, x2+w2-1, y2+h2-1);
int h1;
int h2;
int w1;
int w2;
int row;
int col;
int idx;
int minRows;
int minCols;
int maxRows;
int maxCols;
// -----------------------------------
// These varaibles are kind of unknown
h1 = 300; // image rows 1
h2 = 300; // image rows 2
w1 = 300; // image cols 1
w2 = 300; // image cols 2
// -----------------------------------
minRows = min(h1,h2); //
maxRows = max(h1,h2); //
minCols = min(w1,w2); //
maxCols = max(w1,w2); //
bmpImageoffset=54; // this is the value reported in serial monitor
for (row=0; row<maxRows; 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(row<w1 && bmpfile1.position() != pos) { // Need seek?
bmpfile1.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
if(row<w2 && bmpfile2.position() != pos) { // Need seek?
bmpfile2.seek(pos);
buffidx2 = sizeof(sdbuffer2); // Force buffer reload
}
for (col=0; col<maxCols; col++)
{ // For each pixel...
if (col<w1 && row<w1)
{
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpfile1.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// IMAGE 1
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft1.pushColor(tft.Color565(r,g,b));
// Time to read more pixel data?
}
if (col<w2 && row2<w2)
{
if (buffidx2 >= sizeof(sdbuffer2)) { // Indeed
bmpfile2.read(sdbuffer2, sizeof(sdbuffer2));
buffidx2 = 0; // Set index to beginning
}
// IMAGE 2
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx2++];
g = sdbuffer[buffidx2++];
r = sdbuffer[buffidx2++];
tft.pushColor(tft.Color565(r,g,b));
// Time to read more pixel data?
}
}
}
}
}
Serial.print("Loaded in ");
Serial.print(millis() - startTime2);
Serial.println(" ms");
}
bmpfile1.close();
if(!goodBmp) Serial.println("BMP1 format not recognized.");
bmpfile2.close();
if(!goodBmp2) Serial.println("BMP2 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;
}