Permalink
Browse files

Corrected Adafruit_GFX library integration

  • Loading branch information...
1 parent 5005a67 commit 737712e48e8d0262a000bd209e64448d82da1632 @PaintYourDragon PaintYourDragon committed Apr 3, 2012
View
153 Adafruit_ST7735.cpp
@@ -389,108 +389,47 @@ void Adafruit_ST7735::drawPixel(uint16_t x, uint16_t y, uint16_t color) {
}
-// bresenham's algorithm - thx wikpedia
-void Adafruit_ST7735::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
-uint16_t color) {
-
- uint16_t steep = abs(y1 - y0) > abs(x1 - x0);
- if (steep) {
- swap(x0, y0);
- swap(x1, y1);
- }
-
- if (x0 > x1) {
- swap(x0, x1);
- swap(y0, y1);
- }
-
- uint16_t dx, dy;
- dx = x1 - x0;
- dy = abs(y1 - y0);
-
- int16_t err = dx / 2;
- int16_t ystep;
-
- if (y0 < y1) {
- ystep = 1;
- } else {
- ystep = -1;
- }
-
- for (; x0<=x1; x0++) {
- if (steep) drawPixel(y0, x0, color);
- else drawPixel(x0, y0, color);
- err -= dy;
- if (err < 0) {
- y0 += ystep;
- err += dx;
- }
- }
-}
-
-
-void Adafruit_ST7735::drawVerticalLine(uint8_t x, uint8_t y, uint8_t length,
+void Adafruit_ST7735::drawFastVLine(uint16_t x, uint16_t y, uint16_t h,
uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
- if((y+length-1) >= _height) length = _height-y;
- setAddrWindow(x, y, x, y+length-1);
+ if((y+h-1) >= _height) h = _height-y;
+ setAddrWindow(x, y, x, y+h-1);
uint8_t hi = color >> 8, lo = color;
*rsport |= rspinmask;
*csport &= ~cspinmask;
- while (length--) {
+ while (h--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
}
-void Adafruit_ST7735::drawHorizontalLine(uint8_t x, uint8_t y,
- uint8_t length, uint16_t color) {
+void Adafruit_ST7735::drawFastHLine(uint16_t x, uint16_t y, uint16_t w,
+ uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
- if((x+length-1) >= _width) length = _width-x;
- setAddrWindow(x, y, x+length-1, y);
+ if((x+w-1) >= _width) w = _width-x;
+ setAddrWindow(x, y, x+w-1, y);
uint8_t hi = color >> 8, lo = color;
*rsport |= rspinmask;
*csport &= ~cspinmask;
- while (length--) {
+ while (w--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
}
-void Adafruit_ST7735::drawFastLine(uint8_t x, uint8_t y, uint8_t length,
- uint16_t color, uint8_t vertical) {
- if(vertical) drawVerticalLine( x, y, length, color);
- else drawHorizontalLine(x, y, length, color);
-}
-
-
-// draw a rectangle
-void Adafruit_ST7735::drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint16_t color) {
- drawHorizontalLine(x, y , w, color); // Top edge
- if(h > 1) {
- drawHorizontalLine(x, y+h-1, w, color); // Bottom edge
- if(h > 2) {
- drawVerticalLine(x , y+1, h-2, color); // Left edge
- drawVerticalLine(x+w-1, y+1, h-2, color); // Right edge
- }
- }
-}
-
-
// fill a rectangle
-void Adafruit_ST7735::fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
-uint16_t color) {
+void Adafruit_ST7735::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
+ uint16_t color) {
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
@@ -513,32 +452,12 @@ uint16_t color) {
}
-// draw a string from memory
-void Adafruit_ST7735::drawString(uint16_t x, uint16_t y, char *c,
- uint16_t color, uint8_t size) {
- while (c[0] != 0) {
- drawChar(x, y, c[0], color, 0, size);
- x += size * 6;
- c++;
- if (x + 5 >= _width) {
- y += size * 8;
- x = 0;
- }
- }
-}
-
-
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
-uint8_t Adafruit_ST7735::getRotation() {
- return rotation;
-}
-
-
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
@@ -575,54 +494,8 @@ void Adafruit_ST7735::setRotation(uint8_t m) {
}
-void Adafruit_ST7735::setCursor(uint16_t x, uint16_t y) {
- cursor_x = x;
- cursor_y = y;
-}
-
-
-void Adafruit_ST7735::setTextSize(uint8_t s) {
- textsize = (s > 0) ? s : 1;
-}
-
-
-void Adafruit_ST7735::setTextColor(uint16_t c) {
- textcolor = c;
-}
-
-
-#if ARDUINO >= 100
-size_t Adafruit_ST7735::write(uint8_t c) {
-#else
-void Adafruit_ST7735::write(uint8_t c) {
-#endif
- if (c == '\n') {
- cursor_y += textsize*8;
- cursor_x = 0;
- } else if (c == '\r') {
- // skip em
- } else {
- drawChar(cursor_x, cursor_y, c, textcolor, 0, textsize);
- cursor_x += textsize*6;
- }
-#if ARDUINO >= 100
- return 1;
-#endif
-}
-
-
-void Adafruit_ST7735::goHome(void) {
- setCursor (0,0);
-}
-
-
-uint8_t Adafruit_ST7735::width() {
- return _width;
-}
-
-
-uint8_t Adafruit_ST7735::height() {
- return _height;
+void Adafruit_ST7735::invertDisplay(boolean i) {
+ writecommand(i ? ST7735_INVON : ST7735_INVOFF);
}
View
56 Adafruit_ST7735.h
@@ -96,49 +96,24 @@ class Adafruit_ST7735 : public Adafruit_GFX {
public:
- Adafruit_ST7735(uint8_t CS, uint8_t RS, uint8_t SID,
- uint8_t SCLK, uint8_t RST);
+ Adafruit_ST7735(uint8_t CS, uint8_t RS, uint8_t SID, uint8_t SCLK,
+ uint8_t RST);
Adafruit_ST7735(uint8_t CS, uint8_t RS, uint8_t RST);
void initB(void), // for ST7735B displays
initR(uint8_t options = INITR_GREENTAB), // for ST7735R
- fillScreen(uint16_t color),
+ setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1),
pushColor(uint16_t color),
+ fillScreen(uint16_t color),
drawPixel(uint16_t x, uint16_t y, uint16_t color),
- drawLine(int16_t x, int16_t y, int16_t x1, int16_t y1,
- uint16_t color),
-
- drawVerticalLine(uint8_t x0, uint8_t y0, uint8_t length,
- uint16_t color),
- drawHorizontalLine(uint8_t x0, uint8_t y0, uint8_t length,
- uint16_t color),
- drawFastLine(uint8_t x0, uint8_t y0, uint8_t l, uint16_t color,
- uint8_t flag),
- drawRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
- uint16_t color),
- fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
+ drawFastVLine(uint16_t x, uint16_t y, uint16_t h, uint16_t color),
+ drawFastHLine(uint16_t x, uint16_t y, uint16_t w, uint16_t color),
+ fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
uint16_t color),
- drawString(uint16_t x, uint16_t y, char *c, uint16_t color,
- uint8_t size=1),
- setCursor(uint16_t x, uint16_t y),
- setTextColor(uint16_t c),
- setTextSize(uint8_t s),
- goHome(void),
- writecommand(uint8_t c),
- writedata(uint8_t d),
- setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1),
- setRotation(uint8_t);
- uint8_t width(),
- height(),
- getRotation(void);
+ setRotation(uint8_t r),
+ invertDisplay(boolean i);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
-#if ARDUINO >= 100
- virtual size_t write(uint8_t);
-#else
- virtual void write(uint8_t);
-#endif
-
/* These are not for current use, 8-bit protocol only!
uint8_t readdata(void),
readcommand8(uint8_t);
@@ -149,21 +124,18 @@ class Adafruit_ST7735 : public Adafruit_GFX {
private:
- void drawCircleHelper(uint16_t, uint16_t, uint16_t, uint8_t, uint16_t),
- fillCircleHelper(uint16_t, uint16_t, uint16_t, uint8_t, uint16_t,
- uint16_t),
- spiwrite(uint8_t),
- commonInit(prog_uchar *cmdList),
- commandList(prog_uchar *addr);
+ void spiwrite(uint8_t),
+ writecommand(uint8_t c),
+ writedata(uint8_t d),
+ commandList(prog_uchar *addr),
+ commonInit(prog_uchar *cmdList);
//uint8_t spiread(void);
boolean hwSPI;
volatile uint8_t *dataport, *clkport, *csport, *rsport;
uint8_t _cs, _rs, _rst, _sid, _sclk,
datapinmask, clkpinmask, cspinmask, rspinmask,
- rotation, textsize,
colstart, rowstart; // some displays need this changed
- uint16_t _width, _height, cursor_x, cursor_y, textcolor;
};
#endif
View
2 README.txt
@@ -17,3 +17,5 @@ MIT license, all text above must be included in any redistribution
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_ST7735. Check that the Adafruit_ST7735 folder contains Adafruit_ST7735.cpp and Adafruit_ST7735.
Place the Adafruit_ST7735 library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE
+
+Also requires the Adafruit_GFX library for Arduino.
View
14 examples/graphicstest/graphicstest.pde
@@ -107,9 +107,9 @@ void setup(void) {
}
void loop() {
- tft.writecommand(ST7735_INVON);
+ tft.invertDisplay(true);
delay(500);
- tft.writecommand(ST7735_INVOFF);
+ tft.invertDisplay(false);
delay(500);
}
@@ -148,16 +148,18 @@ void testlines(uint16_t color) {
}
void testdrawtext(char *text, uint16_t color) {
- tft.drawString(0, 0, text, color);
+ tft.setCursor(0, 0);
+ tft.setTextColor(color);
+ tft.print(text);
}
void testfastlines(uint16_t color1, uint16_t color2) {
tft.fillScreen(ST7735_BLACK);
for (uint16_t y=0; y < tft.height(); y+=5) {
- tft.drawHorizontalLine(0, y, tft.width(), color1);
+ tft.drawFastHLine(0, y, tft.width(), color1);
}
for (uint16_t x=0; x < tft.width(); x+=5) {
- tft.drawVerticalLine(x, 0, tft.height(), color2);
+ tft.drawFastVLine(x, 0, tft.height(), color2);
}
}
@@ -247,7 +249,7 @@ void tftPrintTest() {
tft.setTextSize(4);
tft.print(1234.567);
delay(1500);
- tft.goHome(); // go to 0, 0
+ tft.setCursor(0, 0);
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(0);
View
14 examples/graphicstest_highspeed/graphicstest_highspeed.pde
@@ -106,9 +106,9 @@ void setup(void) {
}
void loop() {
- tft.writecommand(ST7735_INVON);
+ tft.invertDisplay(true);
delay(500);
- tft.writecommand(ST7735_INVOFF);
+ tft.invertDisplay(false);
delay(500);
}
@@ -147,16 +147,18 @@ void testlines(uint16_t color) {
}
void testdrawtext(char *text, uint16_t color) {
- tft.drawString(0, 0, text, color);
+ tft.setCursor(0, 0);
+ tft.setTextColor(color);
+ tft.print(text);
}
void testfastlines(uint16_t color1, uint16_t color2) {
tft.fillScreen(ST7735_BLACK);
for (uint16_t y=0; y < tft.height(); y+=5) {
- tft.drawHorizontalLine(0, y, tft.width(), color1);
+ tft.drawFastHLine(0, y, tft.width(), color1);
}
for (uint16_t x=0; x < tft.width(); x+=5) {
- tft.drawVerticalLine(x, 0, tft.height(), color2);
+ tft.drawFastVLine(x, 0, tft.height(), color2);
}
}
@@ -246,7 +248,7 @@ void tftPrintTest() {
tft.setTextSize(4);
tft.print(1234.567);
delay(1500);
- tft.goHome(); // go to 0, 0
+ tft.setCursor(0, 0);
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(0);
View
9 examples/rotationtest/rotationtest.pde
@@ -163,8 +163,8 @@ void rotateFastline(void) {
tft.fillScreen(ST7735_BLACK);
Serial.println(tft.getRotation(), DEC);
- tft.drawHorizontalLine(0, 20, tft.width(), ST7735_RED);
- tft.drawVerticalLine(20, 0, tft.height(), ST7735_BLUE);
+ tft.drawFastHLine(0, 20, tft.width(), ST7735_RED);
+ tft.drawFastVLine(20, 0, tft.height(), ST7735_BLUE);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
@@ -269,7 +269,10 @@ void rotateString(void) {
tft.fillScreen(ST7735_BLACK);
Serial.println(tft.getRotation(), DEC);
- tft.drawString(8, 25, "Adafruit Industries", ST7735_WHITE);
+ tft.setCursor(8, 25);
+ tft.setTextSize(1);
+ tft.setTextColor(ST7735_WHITE);
+ tft.print("Adafruit Industries");
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
View
9 examples/rotationtest_highspeed/rotationtest_highspeed.pde
@@ -162,8 +162,8 @@ void rotateFastline(void) {
tft.fillScreen(ST7735_BLACK);
Serial.println(tft.getRotation(), DEC);
- tft.drawHorizontalLine(0, 20, tft.width(), ST7735_RED);
- tft.drawVerticalLine(20, 0, tft.height(), ST7735_BLUE);
+ tft.drawFastHLine(0, 20, tft.width(), ST7735_RED);
+ tft.drawFastVLine(20, 0, tft.height(), ST7735_BLUE);
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();
@@ -268,7 +268,10 @@ void rotateString(void) {
tft.fillScreen(ST7735_BLACK);
Serial.println(tft.getRotation(), DEC);
- tft.drawString(8, 25, "Adafruit Industries", ST7735_WHITE);
+ tft.setCursor(8, 25);
+ tft.setTextSize(1);
+ tft.setTextColor(ST7735_WHITE);
+ tft.print("Adafruit Industries");
while (!Serial.available());
Serial.read(); Serial.read(); Serial.read();

0 comments on commit 737712e

Please sign in to comment.