Pārlūkot izejas kodu

changed fastvhline to point to drawline, added fillScreen, a fake constructor and rotation stuff

ladyada 13 gadi atpakaļ
vecāks
revīzija
17a6c8b28a
2 mainītis faili ar 70 papildinājumiem un 18 dzēšanām
  1. 53 10
      Adafruit_GFX.cpp
  2. 17 8
      Adafruit_GFX.h

+ 53 - 10
Adafruit_GFX.cpp

@@ -18,6 +18,18 @@ All text above must be included in any redistribution
 #include "glcdfont.c"
 #include <avr/pgmspace.h>
 
+void Adafruit_GFX::constructor(uint16_t w, uint16_t h) {
+  _width = WIDTH = w;
+  _height = HEIGHT = h;
+
+  rotation = 0;    
+  cursor_y = cursor_x = 0;
+  textsize = 1;
+  textcolor = 0xFFFF;
+  textbgcolor = 0x0000;
+}
+
+
 // draw a circle outline
 void Adafruit_GFX::drawCircle(uint16_t x0, uint16_t y0, uint16_t r, 
 			      uint16_t color) {
@@ -46,7 +58,6 @@ void Adafruit_GFX::drawCircle(uint16_t x0, uint16_t y0, uint16_t r,
     drawPixel(x0 - x, y0 + y, color);
     drawPixel(x0 + x, y0 - y, color);
     drawPixel(x0 - x, y0 - y, color);
-    
     drawPixel(x0 + y, y0 + x, color);
     drawPixel(x0 - y, y0 + x, color);
     drawPixel(x0 + y, y0 - x, color);
@@ -175,7 +186,6 @@ void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
 void Adafruit_GFX::drawRect(uint16_t x, uint16_t y, 
 			    uint16_t w, uint16_t h, 
 			    uint16_t color) {
-  Serial.println('[]');
   drawFastHLine(x, y, w, color);
   drawFastHLine(x, y+h-1, w, color);
   drawFastVLine(x, y, h, color);
@@ -185,18 +195,14 @@ void Adafruit_GFX::drawRect(uint16_t x, uint16_t y,
 void Adafruit_GFX::drawFastVLine(uint16_t x, uint16_t y, 
 				 uint16_t h, uint16_t color) {
   // stupidest version - update in subclasses if desired!
-  for (uint16_t j=y; j<y+h; j++) {
-    drawPixel(x, j, color);
-  }
+  drawLine(x, y, x, y+h-1, color);
 }
 
 
 void Adafruit_GFX::drawFastHLine(uint16_t x, uint16_t y, 
 				 uint16_t w, uint16_t color) {
   // stupidest version - update in subclasses if desired!
-  for (uint16_t i=x; i<x+w; i++) {
-    drawPixel(i, y, color);
-  }
+  drawLine(x, y, x+w-1, y, color);
 }
 
 void Adafruit_GFX::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
@@ -207,6 +213,11 @@ void Adafruit_GFX::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
   }
 }
 
+
+void Adafruit_GFX::fillScreen(uint16_t color) {
+  fillRect(0, 0, _width, _height, color);
+}
+
 // draw a rounded rectangle!
 void Adafruit_GFX::drawRoundRect(uint16_t x, uint16_t y, uint16_t w,
 uint16_t h, uint16_t r, uint16_t color) {
@@ -403,6 +414,38 @@ void Adafruit_GFX::setTextColor(uint16_t c) {
 }
 
  void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
-  textcolor = c;
-  textbgcolor = b; 
+   textcolor = c;
+   textbgcolor = b; 
+ }
+
+uint8_t Adafruit_GFX::getRotation(void) {
+  rotation %= 4;
+  return rotation;
+}
+
+void Adafruit_GFX::setRotation(uint8_t x) {
+  x %= 4;  // cant be higher than 3
+  rotation = x;
+  switch (x) {
+  case 0:
+  case 2:
+    _width = WIDTH;
+    _height = HEIGHT;
+    break;
+  case 1:
+  case 3:
+    _width = HEIGHT;
+    _height = WIDTH;
+    break;
+ }
+}
+
+
+// return the size of the display which depends on the rotation!
+ uint16_t Adafruit_GFX::width(void) { 
+   return _width; 
 }
+ 
+ uint16_t Adafruit_GFX::height(void) { 
+   return _height; 
+ }

+ 17 - 8
Adafruit_GFX.h

@@ -25,20 +25,26 @@ All text above must be included in any redistribution
 
 #define swap(a, b) { int16_t t = a; a = b; b = t; }
 
-class Adafruit_GFX : public Print{
+class Adafruit_GFX : public Print {
  public:
+
+  //Adafruit_GFX();
+  // i have no idea why we have to formally call the constructor. kinda sux
+  void constructor(uint16_t w, uint16_t h);
+
   // this must be defined by the subclass
   virtual void drawPixel(uint16_t x, uint16_t y, uint16_t color);
 
   // these are 'generic' drawing functions, so we can share them!
-  void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 
+  virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 
 		uint16_t color);
   virtual void drawFastVLine(uint16_t x, uint16_t y, uint16_t h, uint16_t color);
   virtual void drawFastHLine(uint16_t x, uint16_t y, uint16_t w, uint16_t color);
-  void drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
+  virtual void drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
 		uint16_t color);
-  void fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
+  virtual void fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
 		uint16_t color);
+  virtual void fillScreen(uint16_t color);
 
   void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, 
 		  uint16_t color);
@@ -73,15 +79,18 @@ class Adafruit_GFX : public Print{
   void setTextColor(uint16_t c, uint16_t bg);
   void setTextSize(uint8_t s);
 
+  uint16_t height(void);
+  uint16_t width(void);
 
-  // return the size of the display
-  uint16_t width() { return WIDTH; }
-  uint16_t height() { return HEIGHT; }
+  void setRotation(uint8_t r);
+  uint8_t getRotation(void);
 
  protected:
-  uint16_t WIDTH, HEIGHT;
+  uint16_t WIDTH, HEIGHT;       // this is the 'raw' display w/h - never changes
+  uint16_t _width, _height;     // dependant on rotation
   uint16_t cursor_x, cursor_y, textcolor, textbgcolor;
   uint8_t textsize;
+  uint8_t rotation;
 };
 
 #endif