Partial Leonardo support, BMP examples working on Mega

This commit is contained in:
Phillip Burgess
2012-09-17 16:58:20 -07:00
parent 4e398c8eef
commit f74dd70f2c
13 changed files with 1935 additions and 1535 deletions
+706 -796
View File
File diff suppressed because it is too large Load Diff
+62 -114
View File
@@ -1,4 +1,6 @@
// Graphics library by ladyada/adafruit with init code from Rossum
// IMPORTANT: SEE COMMENTS @ LINE 15 REGARDING SHIELD VS BREAKOUT BOARD USAGE.
// Graphics library by ladyada/adafruit with init code from Rossum
// MIT license
#ifndef _ADAFRUIT_TFTLCD_H_
@@ -11,130 +13,76 @@
#endif
#include <Adafruit_GFX.h>
// comment or uncomment the next line for special pinout!
// **** IF USING THE LCD BREAKOUT BOARD, COMMENT OUT THIS NEXT LINE. ****
// **** IF USING THE LCD SHIELD, LEAVE THE LINE ENABLED: ****
#define USE_ADAFRUIT_SHIELD_PINOUT
// register names from Peter Barrett's Microtouch code
#define ILI932X_START_OSC 0x00
#define ILI932X_DRIV_OUT_CTRL 0x01
#define ILI932X_DRIV_WAV_CTRL 0x02
#define ILI932X_ENTRY_MOD 0x03
#define ILI932X_RESIZE_CTRL 0x04
#define ILI932X_DISP_CTRL1 0x07
#define ILI932X_DISP_CTRL2 0x08
#define ILI932X_DISP_CTRL3 0x09
#define ILI932X_DISP_CTRL4 0x0A
#define ILI932X_RGB_DISP_IF_CTRL1 0x0C
#define ILI932X_FRM_MARKER_POS 0x0D
#define ILI932X_RGB_DISP_IF_CTRL2 0x0F
#define ILI932X_POW_CTRL1 0x10
#define ILI932X_POW_CTRL2 0x11
#define ILI932X_POW_CTRL3 0x12
#define ILI932X_POW_CTRL4 0x13
#define ILI932X_GRAM_HOR_AD 0x20
#define ILI932X_GRAM_VER_AD 0x21
#define ILI932X_RW_GRAM 0x22
#define ILI932X_POW_CTRL7 0x29
#define ILI932X_FRM_RATE_COL_CTRL 0x2B
#define ILI932X_GAMMA_CTRL1 0x30
#define ILI932X_GAMMA_CTRL2 0x31
#define ILI932X_GAMMA_CTRL3 0x32
#define ILI932X_GAMMA_CTRL4 0x35
#define ILI932X_GAMMA_CTRL5 0x36
#define ILI932X_GAMMA_CTRL6 0x37
#define ILI932X_GAMMA_CTRL7 0x38
#define ILI932X_GAMMA_CTRL8 0x39
#define ILI932X_GAMMA_CTRL9 0x3C
#define ILI932X_GAMMA_CTRL10 0x3D
#define ILI932X_HOR_START_AD 0x50
#define ILI932X_HOR_END_AD 0x51
#define ILI932X_VER_START_AD 0x52
#define ILI932X_VER_END_AD 0x53
#define ILI932X_GATE_SCAN_CTRL1 0x60
#define ILI932X_GATE_SCAN_CTRL2 0x61
#define ILI932X_GATE_SCAN_CTRL3 0x6A
#define ILI932X_PART_IMG1_DISP_POS 0x80
#define ILI932X_PART_IMG1_START_AD 0x81
#define ILI932X_PART_IMG1_END_AD 0x82
#define ILI932X_PART_IMG2_DISP_POS 0x83
#define ILI932X_PART_IMG2_START_AD 0x84
#define ILI932X_PART_IMG2_END_AD 0x85
#define ILI932X_PANEL_IF_CTRL1 0x90
#define ILI932X_PANEL_IF_CTRL2 0x92
#define ILI932X_PANEL_IF_CTRL3 0x93
#define ILI932X_PANEL_IF_CTRL4 0x95
#define ILI932X_PANEL_IF_CTRL5 0x97
#define ILI932X_PANEL_IF_CTRL6 0x98
#define HX8347G_COLADDRSTART2 0x02
#define HX8347G_COLADDRSTART1 0x03
#define HX8347G_COLADDREND2 0x04
#define HX8347G_COLADDREND1 0x05
#define HX8347G_ROWADDRSTART2 0x06
#define HX8347G_ROWADDRSTART1 0x07
#define HX8347G_ROWADDREND2 0x08
#define HX8347G_ROWADDREND1 0x09
#define HX8347G_MEMACCESS 0x16
#define TFTLCD_DELAYCMD 0xFF
#define swap(a, b) { int16_t t = a; a = b; b = t; }
class Adafruit_TFTLCD : public Adafruit_GFX {
public:
Adafruit_TFTLCD(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t reset);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
Adafruit_TFTLCD(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t rst);
Adafruit_TFTLCD(void);
// drawing primitives!
void drawPixel(int16_t x, int16_t y, uint16_t color);
uint16_t readPixel(int16_t x, int16_t y);
void fillScreen(uint16_t color);
void drawFastVLine(int16_t x0, int16_t y0, int16_t length, uint16_t color);
void drawFastHLine(int16_t x0, int16_t y0, int16_t length, uint16_t color);
void begin(uint16_t id = 0x9325),
drawPixel(int16_t x, int16_t y, uint16_t color),
drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color),
drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color),
fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c),
fillScreen(uint16_t color),
reset(void),
setRegisters8(uint8_t *ptr, uint8_t n),
setRegisters16(uint16_t *ptr, uint8_t n),
setRotation(uint8_t x),
// These methods are public in order for BMP examples to work:
setAddrWindow(int x1, int y1, int x2, int y2),
pushColors(uint16_t *data, uint8_t len, boolean first);
void fillRect(int16_t x0, int16_t y0, int16_t w, int16_t h, uint16_t color);
#ifndef read8
uint8_t read8(void); // See notes below re: macros
#endif
// commands
void begin(uint16_t id = 0x9325);
void setAddrWindow(int x1, int y1, int x2, int y2);
void goTo(int x, int y); // deprecated
void reset(void);
/* low level */
void writeData(uint16_t d);
void writeCommand(uint16_t c);
void writeCommand8(uint8_t c);
uint16_t readData(void);
uint16_t readRegister(uint16_t addr);
void writeRegister8(uint8_t a, uint8_t d);
void writeRegister16(uint16_t addr, uint16_t data);
static const uint16_t TFTWIDTH = 240;
static const uint16_t TFTHEIGHT = 320;
void writeData_unsafe(uint16_t d);
void setWriteDir(void);
void setReadDir(void);
void write8(uint8_t d);
uint16_t color565(uint8_t r, uint8_t g, uint8_t b),
readPixel(int16_t x, int16_t y),
readID(void);
private:
uint16_t driver;
void drawFastLine(int16_t x0, int16_t y0, int16_t l, uint16_t color, uint8_t flag);
uint8_t read8(void);
uint8_t _cs, _cd, _reset, _wr, _rd;
uint8_t csport, cdport, wrport, rdport;
uint8_t cspin, cdpin, wrpin, rdpin;
void init(),
// These items may have previously been defined as macros
// in pin_magic.h. If not, function versions are declared:
#ifndef write8
write8(uint8_t value),
#endif
#ifndef setWriteDir
setWriteDir(void),
#endif
#ifndef setReadDir
setReadDir(void),
#endif
#ifndef writeRegister8
writeRegister8(uint8_t a, uint8_t d),
#endif
#ifndef writeRegister16
writeRegister16(uint16_t a, uint16_t d),
#endif
#ifndef writeRegisterPair
writeRegisterPair(uint8_t aH, uint8_t aL, uint16_t d),
#endif
setLR(void),
flood(uint16_t color, uint32_t len);
uint8_t driver;
#ifndef USE_ADAFRUIT_SHIELD_PINOUT
volatile uint8_t *csPort , *cdPort , *wrPort , *rdPort;
uint8_t csPinSet , cdPinSet , wrPinSet , rdPinSet ,
csPinUnset, cdPinUnset, wrPinUnset, rdPinUnset,
_reset;
#endif
};
// For compatibility with sketches written for older versions of library.
// Color function name was changed to 'color565' for parity with 2.2" LCD
// library.
#define Color565 color565
#endif
+332 -207
View File
@@ -1,254 +1,379 @@
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins can connect to any pins but we'll use the
// analog lines since that means we can double up the pins
// with the touch screen (see the TFT paint example)
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// you can also just connect RESET to the arduino RESET pin
#define LCD_RESET A4
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// 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);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
void setup(void) {
Serial.begin(9600);
Serial.println("8 Bit LCD test!");
Serial.println("TFT LCD test");
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
Serial.println("Using Adafruit 2.8 inch TFT Arduino Shield Pinout");
Serial.println("Using Adafruit 2.8\" TFT Arduino Shield Pinout");
#else
Serial.println("Using Adafruit 2.8 inch TFT Breakout Pinout");
Serial.println("Using Adafruit 2.8\" TFT Breakout Board Pinout");
#endif
tft.reset();
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else if (identifier == 0x7575) {
Serial.println("Found HX8347G");
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(PSTR("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(PSTR("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(PSTR("Found HX8347G LCD driver"));
} else {
Serial.print("Unknown driver chip ");
Serial.print(PSTR("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
while (1);
}
Serial.println(PSTR("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(PSTR(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(PSTR("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(PSTR("If using the breakout board, it should NOT be #defined!"));
Serial.println(PSTR("Also if using the breakout, double-check that all wiring"));
Serial.println(PSTR("matches the tutorial."));
return;
}
tft.begin(identifier);
uint32_t time = millis();
testtext(RED);
Serial.println(millis() - time);
delay(2000);
time = millis();
testlines(CYAN);
Serial.println(millis() - time);
Serial.println("Benchmark Time (microseconds)");
Serial.print("Screen fill ");
Serial.println(testFillScreen());
delay(500);
time = millis();
testfastlines(RED, BLUE);
Serial.println(millis() - time);
Serial.print("Text ");
Serial.println(testText());
delay(3000);
Serial.print("Lines ");
Serial.println(testLines(CYAN));
delay(500);
testdrawrects(GREEN);
Serial.print("Horiz/Vert Lines ");
Serial.println(testFastLines(RED, BLUE));
delay(500);
testfillrects(YELLOW, MAGENTA);
Serial.print("Rectangles (outline) ");
Serial.println(testRects(GREEN));
delay(500);
tft.fillScreen(BLACK);
testfillcircles(10, MAGENTA);
testdrawcircles(10, WHITE);
delay(500);
testtriangles();
delay(500);
testfilltriangles();
delay(500);
testRoundRect();
delay(500);
testFillRoundRect();
Serial.print("Rectangles (filled) ");
Serial.println(testFilledRects(YELLOW, MAGENTA));
delay(500);
Serial.print("Circles (filled) ");
Serial.println(testFilledCircles(10, MAGENTA));
Serial.print("Circles (outline) ");
Serial.println(testCircles(10, WHITE));
delay(500);
Serial.print("Triangles (outline) ");
Serial.println(testTriangles());
delay(500);
Serial.print("Triangles (filled) ");
Serial.println(testFilledTriangles());
delay(500);
Serial.print("Rounded rects (outline) ");
Serial.println(testRoundRects());
delay(500);
Serial.print("Rounded rects (filled) ");
Serial.println(testFilledRoundRects());
delay(500);
Serial.println("Done!");
}
void loop(void) {
for (uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testtext(RED);
delay(1000);
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testText();
delay(2000);
}
}
void testFillRoundRect() {
unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(BLACK);
for (int16_t x=tft.width(); x > 20 ; x-=6) {
tft.fillRoundRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, x/8, tft.Color565(0, x, 0));
}
tft.fillScreen(RED);
tft.fillScreen(GREEN);
tft.fillScreen(BLUE);
tft.fillScreen(BLACK);
return micros() - start;
}
void testRoundRect() {
unsigned long testText() {
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawRoundRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, x/8, tft.Color565(x, 0, 0));
}
}
void testtriangles() {
tft.fillScreen(BLACK);
for (int16_t i=0; i<tft.width()/2; i+=5) {
tft.drawTriangle(tft.width()/2, tft.height()/2-i,
tft.width()/2-i, tft.height()/2+i,
tft.width()/2+i, tft.height()/2+i, tft.Color565(0, 0, i));
}
}
void testfilltriangles() {
tft.fillScreen(BLACK);
for (int16_t i=tft.width()/2; i>10; i-=5) {
tft.fillTriangle(tft.width()/2, tft.height()/2-i,
tft.width()/2-i, tft.height()/2+i,
tft.width()/2+i, tft.height()/2+i,
tft.Color565(0, i, i));
tft.drawTriangle(tft.width()/2, tft.height()/2-i,
tft.width()/2-i, tft.height()/2+i,
tft.width()/2+i, tft.height()/2+i, tft.Color565(i, i, 0));
}
}
void testtext(uint16_t color) {
tft.fillScreen(BLACK);
tft.setCursor(0, 20);
tft.setTextColor(color);
tft.setTextSize(1);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextSize(2);
tft.setTextColor(YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextSize(3);
tft.setTextColor(RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");
return micros() - start;
}
void testfillcircles(uint8_t radius, uint16_t color) {
for (int16_t x=radius; x < tft.width(); x+=radius*2) {
for (int16_t y=radius; y < tft.height(); y+=radius*2) {
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;
}
void testdrawcircles(uint8_t radius, uint16_t color) {
for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
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);
}
}
}
void testfillrects(uint16_t color1, uint16_t color2) {
tft.fillScreen(BLACK);
for (int16_t x=tft.width()-1; x > 6; x-=6) {
//Serial.println(x, DEC);
tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
}
}
void testdrawrects(uint16_t color) {
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
}
}
void testfastlines(uint16_t color1, uint16_t color2) {
tft.fillScreen(BLACK);
for (int16_t y=0; y < tft.height(); y+=5) {
tft.drawFastHLine(0, y, tft.width(), color1);
}
for (int16_t x=0; x < tft.width(); x+=5) {
tft.drawFastVLine(x, 0, tft.height(), color2);
}
}
void testlines(uint16_t color) {
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawLine(0, 0, x, tft.height()-1, color);
}
for (int16_t y=0; y < tft.height(); y+=6) {
tft.drawLine(0, 0, tft.width()-1, y, color);
}
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
}
for (int16_t y=0; y < tft.height(); y+=6) {
tft.drawLine(tft.width()-1, 0, 0, y, color);
}
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawLine(0, tft.height()-1, x, 0, color);
}
for (int16_t y=0; y < tft.height(); y+=6) {
tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
}
tft.fillScreen(BLACK);
for (int16_t x=0; x < tft.width(); x+=6) {
tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
}
for (int16_t y=0; y < tft.height(); y+=6) {
tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
}
}
void testBars() {
int16_t i,j;
for(i=0; i < tft.height(); i++)
{
for(j=0; j < tft.width(); j++)
{
if(i>279) tft.writeData(WHITE);
else if(i>239) tft.writeData(BLUE);
else if(i>199) tft.writeData(GREEN);
else if(i>159) tft.writeData(CYAN);
else if(i>119) tft.writeData(RED);
else if(i>79) tft.writeData(MAGENTA);
else if(i>39) tft.writeData(YELLOW);
else tft.writeData(BLACK);
}
}
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;
}
+81 -67
View File
@@ -1,64 +1,82 @@
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins can connect to any pins but we'll use the
// analog lines since that means we can double up the pins
// with the touch screen (see the TFT paint example)
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// you can also just connect RESET to the arduino RESET pin
#define LCD_RESET A4
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// 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);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
void setup(void) {
Serial.begin(9600);
Serial.println("8 Bit LCD test!");
Serial.println("TFT LCD test");
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
Serial.println("Using Adafruit 2.8\" TFT Arduino Shield Pinout");
#else
Serial.println("Using Adafruit 2.8\" TFT Breakout Board Pinout");
#endif
tft.reset();
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else if (identifier == 0x7575) {
Serial.println("Found HX8347G");
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println("Found ILI9325 LCD driver");
} else if(identifier == 0x9328) {
Serial.println("Found ILI9328 LCD driver");
} else if(identifier == 0x7575) {
Serial.println("Found HX8347G LCD driver");
} else {
Serial.print("Unknown driver chip ");
Serial.print("Unknown LCD driver chip: ");
Serial.println(identifier, HEX);
while (1);
}
Serial.println("If using the Adafruit 2.8\" TFT Arduino shield, the line:");
Serial.println(" #define USE_ADAFRUIT_SHIELD_PINOUT");
Serial.println("should appear in the library header (Adafruit_TFT.h).");
Serial.println("If using the breakout board, it should NOT be #defined!");
Serial.println("Also if using the breakout, double-check that all wiring");
Serial.println("matches the tutorial.");
return;
}
tft.begin(identifier);
tft.fillScreen(BLACK);
Serial.println("This is a test of the rotation capabilities of the TFT library!");
@@ -82,7 +100,7 @@ void rotateText() {
Serial.println(tft.getRotation(), DEC);
tft.setCursor(0, 30);
tft.setTextColor(RED);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(YELLOW);
@@ -97,7 +115,7 @@ void rotateText() {
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
@@ -106,12 +124,12 @@ void rotateFillcircle(void) {
for (uint8_t i=0; i<4; i++) {
tft.fillScreen(BLACK);
Serial.println(tft.getRotation(), DEC);
tft.fillCircle(10, 30, 10, YELLOW);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
@@ -122,10 +140,10 @@ void rotateDrawcircle(void) {
Serial.println(tft.getRotation(), DEC);
tft.drawCircle(10, 30, 10, YELLOW);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
@@ -134,49 +152,45 @@ void rotateFillrect(void) {
for (uint8_t i=0; i<4; i++) {
tft.fillScreen(BLACK);
Serial.println(tft.getRotation(), DEC);
tft.fillRect(10, 20, 10, 20, GREEN);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
void rotateDrawrect(void) {
for (uint8_t i=0; i<4; i++) {
tft.fillScreen(BLACK);
Serial.println(tft.getRotation(), DEC);
tft.drawRect(10, 20, 10, 20, GREEN);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
void rotateFastline(void) {
for (uint8_t i=0; i<4; i++) {
tft.fillScreen(BLACK);
Serial.println(tft.getRotation(), DEC);
tft.drawFastHLine(0, 20, tft.width(), RED);
tft.drawFastVLine(20, 0, tft.height(), BLUE);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
void rotateLine(void) {
for (uint8_t i=0; i<4; i++) {
tft.fillScreen(BLACK);
@@ -185,7 +199,7 @@ void rotateLine(void) {
tft.drawLine(tft.width()/2, tft.height()/2, 0, 0, RED);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
@@ -198,7 +212,7 @@ void rotatePixel(void) {
tft.drawPixel(10,20, RED);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
tft.setRotation(tft.getRotation()+1);
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

+71 -61
View File
@@ -1,27 +1,32 @@
// BMP-loading example specifically for the TFTLCD breakout board.
// If using the Arduino shield, use the tftbmp_shield.pde sketch instead!
// If using an Arduino Mega, make sure the SD library is configured for
// 'soft' SPI in the file Sd2Card.h.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>
// The control pins can connect to any pins but we'll use the
// analog lines since that means we can double up the pins
// with the touch screen (see the TFT paint example)
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
// For Arduino Uno/Duemilanove, etc
// connect the SD card with DI going to pin 11, DO going to pin 12 and SCK going to pin 13 (standard)
@@ -32,30 +37,36 @@ For Mega's use pins 22 thru 29 (on the double header at the end)
// There are examples in the sketch folder
// our TFT wiring
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, 0);
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);
void setup()
{
Serial.begin(9600);
tft.reset();
// find the TFT display
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else if (identifier == 0x7575) {
Serial.println("Found HX8347G");
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(PSTR("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(PSTR("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(PSTR("Found HX8347G LCD driver"));
} else {
Serial.print("Unknown driver chip ");
Serial.print(PSTR("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
while (1);
}
Serial.println(PSTR("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(PSTR(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(PSTR("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(PSTR("If using the breakout board, it should NOT be #defined!"));
Serial.println(PSTR("Also if using the breakout, double-check that all wiring"));
Serial.println(PSTR("matches the tutorial."));
return;
}
tft.begin(identifier);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
@@ -69,28 +80,16 @@ void setup()
void loop()
{
tft.setRotation(0);
tft.fillScreen(0);
bmpDraw("miniwoof.bmp", 50, 50);
delay(1000);
tft.setRotation(1);
tft.fillScreen(0);
bmpDraw("miniwoof.bmp", 50, 50);
delay(1000);
tft.setRotation(2);
tft.fillScreen(0);
bmpDraw("miniwoof.bmp", 50, 50);
delay(1000);
tft.setRotation(3);
tft.fillScreen(0);
bmpDraw("miniwoof.bmp", 50, 50);
delay(1000);
for(int i = 0; i<4; i++) {
tft.setRotation(i);
tft.fillScreen(0);
for(int j=0; j <= 200; j += 50) {
bmpDraw("miniwoof.bmp", j, j);
}
delay(1000);
}
}
// 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
@@ -108,13 +107,16 @@ void bmpDraw(char *filename, int x, int y) {
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 sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit 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();
uint8_t lcdidx = 0;
boolean first = true;
if((x >= tft.width()) || (y >= tft.height())) return;
@@ -122,7 +124,6 @@ void bmpDraw(char *filename, int x, int y) {
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print("File not found");
@@ -170,7 +171,6 @@ void bmpDraw(char *filename, int x, int y) {
tft.setAddrWindow(x, y, x+w-1, y+h-1);
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
@@ -186,20 +186,30 @@ void bmpDraw(char *filename, int x, int y) {
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
for (col=0; col<w; col++) { // For each column...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
// Push LCD buffer to the display first
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
lcdidx = 0;
first = false;
}
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
// Convert pixel from BMP to TFT format
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.writeData(tft.Color565(r,g,b));
lcdbuffer[lcdidx++] = tft.color565(r,g,b);
} // end pixel
} // end scanline
// Write any remaining data to LCD
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
}
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
Binary file not shown.

Before

Width:  |  Height:  |  Size: 150 KiB

+180 -226
View File
@@ -1,271 +1,225 @@
// BMP-loading example specifically for the TFTLCD Arduino shield.
// If using the breakout board, use the tftbmp.pde sketch instead!
// If using an Arduino Mega, make sure the SD library is configured for
// 'soft' SPI in the file Sd2Card.h.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>
#include <SPI.h>
#if not defined USE_ADAFRUIT_SHIELD_PINOUT
#error "For use with the shield, make sure to #define USE_ADAFRUIT_SHIELD_PINOUT in the TFTLCD.h library file"
#endif
// These are the pins as connected in the shield
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// The chip select pin for the SD card on the shield
#define SD_CS 5
// In the SD card, place 24 bit color BMP files (be sure they are 24-bit!)
// There are examples in the sketch folder
// our TFT wiring
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, 0);
#define SD_CS 5 // Card select for shield use
// the file itself
File bmpFile;
Adafruit_TFTLCD tft;
uint8_t spi_save;
// information we extract about the bitmap file
int bmpWidth, bmpHeight;
uint8_t bmpDepth, bmpImageoffset;
/************* HARDWARE SPI ENABLE/DISABLE */
// we want to reuse the pins for the SD card and the TFT - to save 2 pins. this means we have to
// enable the SPI hardware interface whenever accessing the SD card and then disable it when done
int8_t saved_spimode;
void disableSPI(void) {
saved_spimode = SPCR;
SPCR = 0;
}
void enableSPI(void) {
SPCR = saved_spimode;
}
/******************************************/
uint16_t identifier;
void setup()
{
Serial.begin(9600);
tft.reset();
// find the TFT display
identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else if (identifier == 0x7575) {
Serial.println("Found HX8347G");
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(PSTR("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(PSTR("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(PSTR("Found HX8347G LCD driver"));
} else {
Serial.print("Unknown driver chip ");
Serial.print(PSTR("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
while (1);
}
Serial.println(PSTR("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(PSTR(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(PSTR("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(PSTR("If using the breakout board, it should NOT be #defined!"));
Serial.println(PSTR("Also if using the breakout, double-check that all wiring"));
Serial.println(PSTR("matches the tutorial."));
return;
}
tft.begin(identifier);
// the image is a landscape, so get into landscape mode
tft.setRotation(1);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
return;
}
Serial.println("SD OK!");
bmpFile = SD.open("tiger.bmp");
Serial.println("OK!");
spi_save = SPCR;
if (! bmpFile) {
Serial.println("didnt find image");
while (1);
}
if (! bmpReadHeader(bmpFile)) {
Serial.println("bad bmp");
return;
}
Serial.print("image size ");
Serial.print(bmpWidth, DEC);
Serial.print(", ");
Serial.println(bmpHeight, DEC);
disableSPI(); // release SPI so we can use those pins to draw
bmpdraw(bmpFile, 0, 0);
// disable the SD card interface after we are done!
disableSPI();
bmpDraw("woof.bmp", 0, 0);
delay(1000);
}
void loop()
{
for(int i = 0; i<4; i++) {
tft.setRotation(i);
tft.fillScreen(0);
for(int j=0; j <= 200; j += 50) {
bmpDraw("miniwoof.bmp", j, j);
}
delay(1000);
}
}
/*********************************************/
// This procedure reads a bitmap and draws it to the screen
// its sped up by reading many pixels worth of data at a time
// instead of just one pixel at a time. increading the buffer takes
// more RAM but makes the drawing a little faster. 20 pixels' worth
// is probably a good place
// 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(File f, int x, int y) {
void bmpDraw(char *filename, int x, int 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 in buffer (R+G+B per pixel)
uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit 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();
uint8_t lcdidx = 0;
boolean first = true;
enableSPI(); // enable the hardware SPI to talk to the SD card
bmpFile.seek(bmpImageoffset);
disableSPI(); // release it so we can use those pins
uint32_t time = millis();
uint16_t p;
uint8_t g, b;
int i, j;
uint8_t sdbuffer[3 * BUFFPIXEL]; // 3 * pixels to buffer
uint8_t buffidx = 3*BUFFPIXEL;
Serial.print("rotation = "); Serial.println(tft.getRotation(), DEC);
for (i=0; i< bmpHeight; i++) {
// bitmaps are stored with the BOTTOM line first so we have to move 'up'
if((x >= tft.width()) || (y >= tft.height())) return;
if ((identifier == 0x9325) || (identifier == 0x9328)) {
if (tft.getRotation() == 3) {
tft.writeRegister16(ILI932X_ENTRY_MOD, 0x1028);
tft.goTo(x+i, y);
} else if (tft.getRotation() == 2) {
tft.writeRegister16(ILI932X_ENTRY_MOD, 0x1020);
tft.goTo(x+bmpWidth, y+i);
} else if (tft.getRotation() == 1) {
tft.writeRegister16(ILI932X_ENTRY_MOD, 0x1018);
tft.goTo(x+bmpHeight-1-i, y);
} else if (tft.getRotation() == 0) {
tft.writeRegister16(ILI932X_ENTRY_MOD, 0x1030);
tft.goTo(x, y+bmpHeight-i);
}
} else if (identifier == 0x7575) {
if (tft.getRotation() == 3) {
tft.writeRegister8(HX8347G_MEMACCESS, 0x20);
tft.setAddrWindow(x, y+i, 319, y+i);
} else if (tft.getRotation() == 2) {
tft.writeRegister8(HX8347G_MEMACCESS, 0x40);
tft.goTo(x, y+i);
} else if (tft.getRotation() == 1) {
tft.writeRegister8(HX8347G_MEMACCESS, 0xA0);
tft.setAddrWindow(x, y+bmpHeight-1-i, 319, y+bmpHeight-i);
} else if (tft.getRotation() == 0) {
tft.writeRegister8(HX8347G_MEMACCESS, 0x0);
tft.goTo(x, y+bmpHeight-i-1);
}
}
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
SPCR = spi_save;
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print("File not found");
return;
}
for (j=0; j<bmpWidth; j++) {
// read more pixels
if (buffidx >= 3*BUFFPIXEL) {
enableSPI(); // enable the hardware SPI to talk to the SD card
bmpFile.read(sdbuffer, 3*BUFFPIXEL);
disableSPI(); // release it so we can use those pins
buffidx = 0;
}
// convert pixel from 888 to 565
b = sdbuffer[buffidx++]; // blue
g = sdbuffer[buffidx++]; // green
p = sdbuffer[buffidx++]; // red
p >>= 3;
p <<= 6;
g >>= 2;
p |= g;
p <<= 5;
b >>= 3;
p |= b;
// write out the 16 bits of color
tft.writeData(p);
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("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("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("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
SPCR = 0;
tft.setAddrWindow(x, y, x+w-1, y+h-1);
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;
SPCR = spi_save;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each column...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
// Push LCD buffer to the display first
if(lcdidx > 0) {
SPCR = 0;
tft.pushColors(lcdbuffer, lcdidx, first);
lcdidx = 0;
first = false;
}
SPCR = spi_save;
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
lcdbuffer[lcdidx++] = tft.color565(r,g,b);
} // end pixel
} // end scanline
// Write any remaining data to LCD
if(lcdidx > 0) {
SPCR = 0;
tft.pushColors(lcdbuffer, lcdidx, first);
}
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
if (identifier == 0x7575) tft.writeRegister8(HX8347G_MEMACCESS, 0x0);
else tft.writeRegister16(ILI932X_ENTRY_MOD, 0x1030);
Serial.print(millis() - time, DEC);
Serial.println(" ms");
bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
}
boolean bmpReadHeader(File f) {
// read header
uint32_t tmp;
if (read16(f) != 0x4D42) {
// magic bytes missing
return false;
}
// read file size
tmp = read32(f);
Serial.print("size 0x"); Serial.println(tmp, HEX);
// read and ignore creator bytes
read32(f);
bmpImageoffset = read32(f);
Serial.print("offset "); Serial.println(bmpImageoffset, DEC);
// read DIB header
tmp = read32(f);
Serial.print("header size "); Serial.println(tmp, DEC);
bmpWidth = read32(f);
bmpHeight = read32(f);
// 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.
if (read16(f) != 1)
return false;
bmpDepth = read16(f);
Serial.print("bitdepth "); Serial.println(bmpDepth, DEC);
if (read32(f) != 0) {
// compression not supported!
return false;
}
Serial.print("compression "); Serial.println(tmp, DEC);
return true;
}
/*********************************************/
// These read data from the SD card file and convert them to big endian
// (the data is stored in little endian format!)
// LITTLE ENDIAN!
uint16_t read16(File f) {
uint16_t d;
uint8_t b;
b = f.read();
d = f.read();
d <<= 8;
d |= b;
return d;
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
// LITTLE ENDIAN!
uint32_t read32(File f) {
uint32_t d;
uint16_t b;
b = read16(f);
d = read16(f);
d <<= 16;
d |= b;
return d;
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;
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB

+57 -64
View File
@@ -1,20 +1,23 @@
// Paint example specifically for the TFTLCD breakout board.
// If using the Arduino shield, use the tftpaint_shield.pde sketch instead!
// DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
@@ -34,20 +37,19 @@ TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// 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);
@@ -62,50 +64,48 @@ void setup(void) {
tft.reset();
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325");
} else if (identifier == 0x9328) {
Serial.println("Found ILI9328");
} else if (identifier == 0x7575) {
Serial.println("Found HX8347G");
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println("Found ILI9325 LCD driver");
} else if(identifier == 0x9328) {
Serial.println("Found ILI9328 LCD driver");
} else if(identifier == 0x7575) {
Serial.println("Found HX8347G LCD driver");
} else {
Serial.print("Unknown driver chip ");
Serial.print("Unknown LCD driver chip: ");
Serial.println(identifier, HEX);
while (1);
return;
}
tft.begin(identifier);
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(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
currentcolor = RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
currentcolor = RED;
pinMode(13, OUTPUT);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop()
{
digitalWrite(13, HIGH);
Point p = ts.getPoint();
digitalWrite(13, LOW);
// if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
// if sharing pins, you'll need to fix the directions of the touchscreen pins
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
@@ -123,11 +123,10 @@ void loop()
if (p.y < (TS_MINY-5)) {
Serial.println("erase");
// press the bottom of the screen to erase
// press the bottom of the screen to erase
tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
}
// turn from 0->1023 to tft.width
// 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);
/*
@@ -137,33 +136,27 @@ void loop()
*/
if (p.y < BOXSIZE) {
oldcolor = currentcolor;
if (p.x < BOXSIZE) {
currentcolor = RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE*2) {
currentcolor = YELLOW;
} else if (p.x < BOXSIZE*2) {
currentcolor = YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE*3) {
currentcolor = GREEN;
} else if (p.x < BOXSIZE*3) {
currentcolor = GREEN;
tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE*4) {
currentcolor = CYAN;
} else if (p.x < BOXSIZE*4) {
currentcolor = CYAN;
tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE*5) {
currentcolor = BLUE;
} else if (p.x < BOXSIZE*5) {
currentcolor = BLUE;
tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
}
else if (p.x < BOXSIZE*6) {
currentcolor = MAGENTA;
} 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);
@@ -0,0 +1,162 @@
// Paint example specifically for the TFTLCD Arduino shield.
// If using the breakout board, use the tftpaint.pde sketch instead!
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#ifndef USE_ADAFRUIT_SHIELD_PINOUT
#error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif
// These are the pins for the shield!
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#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
// 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;
#define BOXSIZE 40
#define PENRADIUS 4
int oldcolor, currentcolor;
void setup(void) {
Serial.begin(9600);
Serial.println("Paint!");
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println("Found ILI9325 LCD driver");
} else if(identifier == 0x9328) {
Serial.println("Found ILI9328 LCD driver");
} else if(identifier == 0x7575) {
Serial.println("Found HX8347G LCD driver");
} else {
Serial.print("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.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
currentcolor = RED;
pinMode(13, OUTPUT);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop()
{
digitalWrite(13, HIGH);
Point p = ts.getPoint();
digitalWrite(13, LOW);
// 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);
*/
if (p.y < (TS_MINY-5)) {
Serial.println("erase");
// press the bottom of the screen to erase
tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
}
// 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);
Serial.println(")");
*/
if (p.y < BOXSIZE) {
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())) {
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}
}
}
+284
View File
@@ -0,0 +1,284 @@
#ifndef _pin_magic_
#define _pin_magic_
// This header file serves two purposes:
//
// 1) Isolate non-portable MCU port- and pin-specific identifiers and
// operations so the library code itself remains somewhat agnostic
// (PORTs and pin numbers are always referenced through macros).
//
// 2) GCC doesn't always respect the "inline" keyword, so this is a
// ham-fisted manner of forcing the issue to minimize function calls.
// This sometimes makes the library a bit bigger than before, but fast++.
// However, because they're macros, we need to be SUPER CAREFUL about
// parameters -- for example, write8(x) may expand to multiple PORT
// writes that all refer to x, so it needs to be a constant or fixed
// variable and not something like *ptr++ (which, after macro
// expansion, may increment the pointer repeatedly and run off into
// la-la land). Macros also give us fune-grained control over which
// operations are inlined on which boards (balancing speed against
// available program space).
// When using the TFT shield, control and data pins exist in set physical
// locations, but the ports and bitmasks corresponding to each vary among
// boards. A separate set of pin definitions is given for each supported
// board type.
// When using the TFT breakout board, control pins are configurable but
// the data pins are still fixed -- making every data pin configurable
// would be much too slow. The data pin layouts are not the same between
// the shield and breakout configurations -- for the latter, pins were
// chosen to keep the tutorial wiring manageable more than making optimal
// use of ports and bitmasks. So there's a second set of pin definitions
// given for each supported board.
// Shield pin usage:
// LCD Data Bit : 7 6 5 4 3 2 1 0
// Digital pin #: 7 6 13 4 11 10 9 8
// Uno port/pin : PD7 PD6 PB5 PD4 PB3 PB2 PB1 PB0
// Mega port/pin: PH4 PH3 PB7 PG5 PB5 PB4 PH6 PH5
// Leo port/pin : PE6 PD7 PC7 PD4 PB7 PB6 PB5 PB4
// Breakout pin usage:
// LCD Data Bit : 7 6 5 4 3 2 1 0
// Uno dig. pin : 7 6 5 4 3 2 9 8
// Uno port/pin : PD7 PD6 PD5 PD4 PD3 PD2 PB1 PB0
// Mega dig. pin: 29 28 27 26 25 24 23 22
// Mega port/pin: PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 (one contiguous PORT)
// Leo dig. pin : 7 6 5 4 3 2 9 8
// Leo port/pin : PE6 PD7 PC6 PD4 PD0 PD1 PB5 PB4
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined (__AVR_ATmega328__) || defined(__AVR_ATmega8__)
// Arduino Uno, Duemilanove, etc.
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
// LCD control lines:
// RD (read), WR (write), CD (command/data), CS (chip select)
#define RD_PORT PORTC
#define WR_PORT PORTC
#define CD_PORT PORTC
#define CS_PORT PORTC
#define RD_MASK B00000001
#define WR_MASK B00000010
#define CD_MASK B00000100
#define CS_MASK B00001000
// These are macros for I/O operations...
// Write 8-bit value to LCD data lines
#define write8inline(d) { \
PORTD = (PORTD & B00101111) | ((d) & B11010000); \
PORTB = (PORTB & B11010000) | ((d) & B00101111); \
WR_STROBE; } // STROBEs are defined later
// Read 8-bit value from LCD data lines
#define read8inline() (RD_STROBE, (PIND & B11010000) | (PINB & B00101111))
// These set the PORT directions as required before the write and read
// operations. Because write operations are much more common than reads,
// the data-reading functions in the library code set the PORT(s) to
// input before a read, and restore them back to the write state before
// returning. This avoids having to set it for output inside every
// drawing method. The default state has them initialized for writes.
#define setWriteDirInline() { DDRD |= B11010000; DDRB |= B00101111; }
#define setReadDirInline() { DDRD &= ~B11010000; DDRB &= ~B00101111; }
#else // Uno w/Breakout board
#define write8inline(d) { \
PORTD = (PORTD & B00000011) | ((d) & B11111100); \
PORTB = (PORTB & B11111100) | ((d) & B00000011); \
WR_STROBE; }
#define read8inline() (RD_STROBE, (PIND& B11111100)|(PINB& B00000011))
#define setWriteDirInline() { DDRD|= B11111100; DDRB|= B00000011; }
#define setReadDirInline() { DDRD&=~B11111100; DDRB&=~B00000011; }
#endif
// As part of the inline control, macros reference other macros...if any
// of these are left undefined, an equivalent function version (non-inline)
// is declared later. The Uno has a moderate amount of program space, so
// only write8() is inlined -- that one provides the most performance
// benefit, but also generates the most bloat.
#define write8 write8inline
#elif defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
// Arduino Mega, ADK, etc.
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
#define RD_PORT PORTF
#define WR_PORT PORTF
#define CD_PORT PORTF
#define CS_PORT PORTF
#define RD_MASK B00000001
#define WR_MASK B00000010
#define CD_MASK B00000100
#define CS_MASK B00001000
#define write8inline(d) { \
PORTH = (PORTH & B10000111)|(((d) & B11000000)>>3)|(((d) & B00000011)<<5); \
PORTB = (PORTB & B01001111)|(((d) & B00101100)<<2); \
PORTG = (PORTG & B11011111)|(((d) & B00010000)<<1); \
WR_STROBE; }
#define read8inline() (RD_STROBE, \
((PINH & B00011000) << 3) | ((PINB & B10110000) >> 2) | \
((PING & B00100000) >> 1) | ((PINH & B01100000) >> 5))
#define setWriteDirInline() { \
DDRH |= B01111000; DDRB |= B10110000; DDRG |= B00100000; }
#define setReadDirInline() { \
DDRH &= ~B01111000; DDRB &= ~B10110000; DDRG &= ~B00100000; }
// Strobe is wonky on Mega w/shield. Haven't worked out the underlying
// reason, but an interim kludge is just to use inverted levels. ???
#define RD_STROBE RD_IDLE, RD_ACTIVE
#else // Mega w/Breakout board
#define write8inline(d) { PORTA = (d); WR_STROBE; }
#define read8inline() (RD_STROBE, PINA)
#define setWriteDirInline() DDRA = 0xff
#define setReadDirInline() DDRA = 0
#endif
// All of the functions are inlined on the Arduino Mega. When using the
// breakout board, the macro versions aren't appreciably larger than the
// function equivalents, and they're super simple and fast. When using
// the shield, the macros become pretty complicated...but this board has
// so much code space, the macros are used anyway. If you need to free
// up program space, some macros can be removed, at a minor cost in speed.
#define write8 write8inline
#define read8 read8inline
#define setWriteDir setWriteDirInline
#define setReadDir setReadDirInline
#define writeRegister8 writeRegister8inline
#define writeRegister16 writeRegister16inline
#define writeRegisterPair writeRegisterPairInline
#elif defined(__AVR_ATmega32U4__)
// Arduino Leonardo
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
#define RD_PORT PORTF
#define WR_PORT PORTF
#define CD_PORT PORTF
#define CS_PORT PORTF
#define RD_MASK B10000000
#define WR_MASK B01000000
#define CD_MASK B00100000
#define CS_MASK B00010000
#define write8inline(d) { \
PORTE = (PORTE & B10111111) | (((d) & B10000000)>>1); \
PORTD = (PORTD & B01101111) | (((d) & B01000000)<<1) | ((d) & B00010000); \
PORTC = (PORTC & B01111111) | (((d) & B00100000)<<2); \
PORTB = (PORTB & B00001111) | (((d) & B00001111)<<4); \
WR_STROBE; }
#define read8inline() (RD_STROBE, \
(((PINE & B01000000) << 1) | ((PIND & B10000000) >> 1) | \
((PINC & B10000000) >> 2) | ((PINB & B11110000) >> 4) | \
(PIND & B00010000)))
#define setWriteDirInline() { \
DDRE |= B01000000; DDRD |= B10010000; \
DDRC |= B10000000; DDRB |= B11110000; }
#define setReadDirInline() { \
DDRE &= ~B01000000; DDRD &= ~B10010000; \
DDRC &= ~B10000000; DDRB &= ~B11110000; }
#else // Leonardo w/Breakout board
#define write8inline(d) { \
uint8_t dr1 = (d) >> 1, dl1 = (d) << 1; \
PORTE = (PORTE & B10111111) | (dr1 & B01000000); \
PORTD = (PORTD & B01101100) | (dl1 & B10000000) | (((d) & B00001000)>>3) | \
(dr1 & B00000010) | ((d) & B00010000); \
PORTC = (PORTC & B10111111) | (dl1 & B01000000); \
PORTB = (PORTB & B11001111) |(((d) & B00000011)<<4); \
WR_STROBE; }
#define read8inline() (RD_STROBE, \
(((PINE & B01000000) | (PIND & B00000010)) << 1) | \
(((PINC & B01000000) | (PIND & B10000000)) >> 1) | \
((PIND & B00000001)<<3) | ((PINB & B00110000)>>4) | (PIND & B00010000))
#define setWriteDirInline() { \
DDRE |= B01000000; DDRD |= B10010011; \
DDRC |= B01000000; DDRB |= B00110000; }
#define setReadDirInline() { \
DDRE &= ~B01000000; DDRD &= ~B10010011; \
DDRC &= ~B01000000; DDRB &= ~B00110000; }
#endif
// On the Leonardo, only the write8() macro is used -- though even that
// might be excessive given the code size and available program space
// on this board. You may need to disable this to get any sizable
// program to compile.
#define write8 write8inline
#else
#error "Board type unsupported / not recognized"
#endif
// Stuff common to all Arduino board types:
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
// Control signals are ACTIVE LOW (idle is HIGH)
// Command/Data: LOW = command, HIGH = data
// These are single-instruction operations and always inline
#define RD_ACTIVE RD_PORT &= ~RD_MASK
#define RD_IDLE RD_PORT |= RD_MASK
#define WR_ACTIVE WR_PORT &= ~WR_MASK
#define WR_IDLE WR_PORT |= WR_MASK
#define CD_COMMAND CD_PORT &= ~CD_MASK
#define CD_DATA CD_PORT |= CD_MASK
#define CS_ACTIVE CS_PORT &= ~CS_MASK
#define CS_IDLE CS_PORT |= CS_MASK
#else // Breakout board
// When using the TFT breakout board, control pins are configurable.
#define RD_ACTIVE *rdPort &= rdPinUnset
#define RD_IDLE *rdPort |= rdPinSet
#define WR_ACTIVE *wrPort &= wrPinUnset
#define WR_IDLE *wrPort |= wrPinSet
#define CD_COMMAND *cdPort &= cdPinUnset
#define CD_DATA *cdPort |= cdPinSet
#define CS_ACTIVE *csPort &= csPinUnset
#define CS_IDLE *csPort |= csPinSet
#endif
// Data read and write strobes, ~2 instructions and always inline
#ifndef RD_STROBE
#define RD_STROBE RD_ACTIVE, RD_IDLE
#endif
#define WR_STROBE { WR_ACTIVE; WR_IDLE; }
// These higher-level operations are usually functionalized,
// except on Mega where's there's gobs and gobs of program space.
// Set value of TFT register: 8-bit address, 8-bit value
#define writeRegister8inline(a, d) { \
CD_COMMAND; write8(a); CD_DATA; write8(d); }
// Set value of TFT register: 16-bit address, 16-bit value
// See notes at top about macro expansion, hence hi & lo temp vars
#define writeRegister16inline(a, d) { \
uint8_t hi, lo; \
hi = (a) >> 8; lo = (a); CD_COMMAND; write8(hi); write8(lo); \
hi = (d) >> 8; lo = (d); CD_DATA ; write8(hi); write8(lo); }
// Set value of 2 TFT registers: Two 8-bit addresses (hi & lo), 16-bit value
#define writeRegisterPairInline(aH, aL, d) { \
uint8_t hi = (d) >> 8, lo = (d); \
CD_COMMAND; write8(aH); CD_DATA; write8(hi); \
CD_COMMAND; write8(aL); CD_DATA; write8(lo); }
#endif // _pin_magic_