1- /* *****************************************************************
2- This is the core graphics library for all our displays, providing
3- basic graphics primitives (points, lines, circles, etc.). It needs
4- to be paired with a hardware-specific library for each display
5- device we carry (handling the lower-level functions).
1+ /*
2+ This is the core graphics library for all our displays, providing a common
3+ set of graphics primitives (points, lines, circles, etc.). It needs to be
4+ paired with a hardware-specific library for each display device we carry
5+ (to handle the lower-level functions).
6+
7+ Adafruit invests time and resources providing this open source code, please
8+ support Adafruit & open-source hardware by purchasing products from Adafruit!
69
7- Adafruit invests time and resources providing this open
8- source code, please support Adafruit and open-source hardware
9- by purchasing products from Adafruit!
10-
11- Written by Limor Fried/Ladyada for Adafruit Industries.
12- BSD license, check license.txt for more information.
13- All text above must be included in any redistribution.
14- ******************************************************************/
10+ Copyright (c) 2013 Adafruit Industries. All rights reserved.
11+
12+ Redistribution and use in source and binary forms, with or without
13+ modification, are permitted provided that the following conditions are met:
14+
15+ - Redistributions of source code must retain the above copyright notice,
16+ this list of conditions and the following disclaimer.
17+ - Redistributions in binary form must reproduce the above copyright notice,
18+ this list of conditions and the following disclaimer in the documentation
19+ and/or other materials provided with the distribution.
20+
21+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+ POSSIBILITY OF SUCH DAMAGE.
32+ */
1533
1634#include " Adafruit_GFX.h"
1735#include " glcdfont.c"
18- #include < avr/pgmspace.h >
19-
20- void Adafruit_GFX::constructor ( int16_t w, int16_t h) {
21- _width = WIDTH = w;
22- _height = HEIGHT = h;
36+ #ifdef __AVR__
37+ # include < avr/pgmspace.h >
38+ # else
39+ # define pgm_read_byte ( addr ) (*( const unsigned char *)(addr))
40+ # endif
2341
24- rotation = 0 ;
25- cursor_y = cursor_x = 0 ;
26- textsize = 1 ;
42+ Adafruit_GFX::Adafruit_GFX (int16_t w, int16_t h) {
43+ _width = WIDTH = w;
44+ _height = HEIGHT = h;
45+ rotation = 0 ;
46+ cursor_y = cursor_x = 0 ;
47+ textsize = 1 ;
2748 textcolor = textbgcolor = 0xFFFF ;
28- wrap = true ;
49+ wrap = true ;
2950}
3051
31-
32- // draw a circle outline
33- void Adafruit_GFX::drawCircle (int16_t x0, int16_t y0, int16_t r,
34- uint16_t color) {
52+ // Draw a circle outline
53+ void Adafruit_GFX::drawCircle (int16_t x0, int16_t y0, int16_t r,
54+ uint16_t color) {
3555 int16_t f = 1 - r;
3656 int16_t ddF_x = 1 ;
3757 int16_t ddF_y = -2 * r;
3858 int16_t x = 0 ;
3959 int16_t y = r;
4060
41- drawPixel (x0, y0+r, color);
42- drawPixel (x0, y0-r, color);
43- drawPixel (x0+r, y0, color);
44- drawPixel (x0-r, y0, color);
61+ drawPixel (x0 , y0+r, color);
62+ drawPixel (x0 , y0-r, color);
63+ drawPixel (x0+r, y0 , color);
64+ drawPixel (x0-r, y0 , color);
4565
4666 while (x<y) {
4767 if (f >= 0 ) {
@@ -61,7 +81,6 @@ void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
6181 drawPixel (x0 - y, y0 + x, color);
6282 drawPixel (x0 + y, y0 - x, color);
6383 drawPixel (x0 - y, y0 - x, color);
64-
6584 }
6685}
6786
@@ -101,15 +120,15 @@ void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
101120 }
102121}
103122
104- void Adafruit_GFX::fillCircle (int16_t x0, int16_t y0, int16_t r,
123+ void Adafruit_GFX::fillCircle (int16_t x0, int16_t y0, int16_t r,
105124 uint16_t color) {
106125 drawFastVLine (x0, y0-r, 2 *r+1 , color);
107126 fillCircleHelper (x0, y0, r, 3 , 0 , color);
108127}
109128
110- // used to do circles and roundrects!
129+ // Used to do circles and roundrects
111130void Adafruit_GFX::fillCircleHelper (int16_t x0, int16_t y0, int16_t r,
112- uint8_t cornername, int16_t delta, uint16_t color) {
131+ uint8_t cornername, int16_t delta, uint16_t color) {
113132
114133 int16_t f = 1 - r;
115134 int16_t ddF_x = 1 ;
@@ -138,9 +157,9 @@ void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
138157 }
139158}
140159
141- // bresenham 's algorithm - thx wikpedia
142- void Adafruit_GFX::drawLine (int16_t x0, int16_t y0,
143- int16_t x1, int16_t y1,
160+ // Bresenham 's algorithm - thx wikpedia
161+ void Adafruit_GFX::drawLine (int16_t x0, int16_t y0,
162+ int16_t x1, int16_t y1,
144163 uint16_t color) {
145164 int16_t steep = abs (y1 - y0) > abs (x1 - x0);
146165 if (steep) {
@@ -180,59 +199,56 @@ void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
180199 }
181200}
182201
183-
184- // draw a rectangle
185- void Adafruit_GFX::drawRect (int16_t x, int16_t y,
186- int16_t w, int16_t h,
202+ // Draw a rectangle
203+ void Adafruit_GFX::drawRect (int16_t x, int16_t y,
204+ int16_t w, int16_t h,
187205 uint16_t color) {
188206 drawFastHLine (x, y, w, color);
189207 drawFastHLine (x, y+h-1 , w, color);
190208 drawFastVLine (x, y, h, color);
191209 drawFastVLine (x+w-1 , y, h, color);
192210}
193211
194- void Adafruit_GFX::drawFastVLine (int16_t x, int16_t y,
212+ void Adafruit_GFX::drawFastVLine (int16_t x, int16_t y,
195213 int16_t h, uint16_t color) {
196- // stupidest version - update in subclasses if desired!
214+ // Update in subclasses if desired!
197215 drawLine (x, y, x, y+h-1 , color);
198216}
199217
200-
201- void Adafruit_GFX::drawFastHLine (int16_t x, int16_t y,
218+ void Adafruit_GFX::drawFastHLine (int16_t x, int16_t y,
202219 int16_t w, uint16_t color) {
203- // stupidest version - update in subclasses if desired!
220+ // Update in subclasses if desired!
204221 drawLine (x, y, x+w-1 , y, color);
205222}
206223
207- void Adafruit_GFX::fillRect (int16_t x, int16_t y, int16_t w, int16_t h,
224+ void Adafruit_GFX::fillRect (int16_t x, int16_t y, int16_t w, int16_t h,
208225 uint16_t color) {
209- // stupidest version - update in subclasses if desired!
226+ // Update in subclasses if desired!
210227 for (int16_t i=x; i<x+w; i++) {
211- drawFastVLine (i, y, h, color);
228+ drawFastVLine (i, y, h, color);
212229 }
213230}
214231
215-
216232void Adafruit_GFX::fillScreen (uint16_t color) {
217233 fillRect (0 , 0 , _width, _height, color);
218234}
219235
220- // draw a rounded rectangle!
236+ // Draw a rounded rectangle
221237void Adafruit_GFX::drawRoundRect (int16_t x, int16_t y, int16_t w,
222238 int16_t h, int16_t r, uint16_t color) {
223239 // smarter version
224240 drawFastHLine (x+r , y , w-2 *r, color); // Top
225241 drawFastHLine (x+r , y+h-1 , w-2 *r, color); // Bottom
226- drawFastVLine ( x , y+r , h-2 *r, color); // Left
227- drawFastVLine ( x+w-1 , y+r , h-2 *r, color); // Right
242+ drawFastVLine (x , y+r , h-2 *r, color); // Left
243+ drawFastVLine (x+w-1 , y+r , h-2 *r, color); // Right
228244 // draw four corners
229245 drawCircleHelper (x+r , y+r , r, 1 , color);
230246 drawCircleHelper (x+w-r-1 , y+r , r, 2 , color);
231247 drawCircleHelper (x+w-r-1 , y+h-r-1 , r, 4 , color);
232248 drawCircleHelper (x+r , y+h-r-1 , r, 8 , color);
233249}
234250
235- // fill a rounded rectangle!
251+ // Fill a rounded rectangle
236252void Adafruit_GFX::fillRoundRect (int16_t x, int16_t y, int16_t w,
237253 int16_t h, int16_t r, uint16_t color) {
238254 // smarter version
@@ -243,18 +259,18 @@ void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
243259 fillCircleHelper (x+r , y+r, r, 2 , h-2 *r-1 , color);
244260}
245261
246- // draw a triangle!
262+ // Draw a triangle
247263void Adafruit_GFX::drawTriangle (int16_t x0, int16_t y0,
248- int16_t x1, int16_t y1,
264+ int16_t x1, int16_t y1,
249265 int16_t x2, int16_t y2, uint16_t color) {
250266 drawLine (x0, y0, x1, y1, color);
251267 drawLine (x1, y1, x2, y2, color);
252268 drawLine (x2, y2, x0, y0, color);
253269}
254270
255- // fill a triangle!
271+ // Fill a triangle
256272void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
257- int16_t x1, int16_t y1,
273+ int16_t x1, int16_t y1,
258274 int16_t x2, int16_t y2, uint16_t color) {
259275
260276 int16_t a, b, y, last;
@@ -330,7 +346,7 @@ void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
330346 }
331347}
332348
333- void Adafruit_GFX::drawBitmap (int16_t x, int16_t y,
349+ void Adafruit_GFX::drawBitmap (int16_t x, int16_t y,
334350 const uint8_t *bitmap, int16_t w, int16_t h,
335351 uint16_t color) {
336352
@@ -345,15 +361,14 @@ void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
345361 }
346362}
347363
348-
349364#if ARDUINO >= 100
350365size_t Adafruit_GFX::write (uint8_t c) {
351366#else
352367void Adafruit_GFX::write (uint8_t c) {
353368#endif
354369 if (c == ' \n ' ) {
355370 cursor_y += textsize*8 ;
356- cursor_x = 0 ;
371+ cursor_x = 0 ;
357372 } else if (c == ' \r ' ) {
358373 // skip em
359374 } else {
@@ -369,7 +384,7 @@ void Adafruit_GFX::write(uint8_t c) {
369384#endif
370385}
371386
372- // draw a character
387+ // Draw a character
373388void Adafruit_GFX::drawChar (int16_t x, int16_t y, unsigned char c,
374389 uint16_t color, uint16_t bg, uint8_t size) {
375390
@@ -397,7 +412,7 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
397412 drawPixel (x+i, y+j, bg);
398413 else { // big size
399414 fillRect (x+i*size, y+j*size, size, size, bg);
400- }
415+ }
401416 }
402417 line >>= 1 ;
403418 }
@@ -409,60 +424,55 @@ void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
409424 cursor_y = y;
410425}
411426
412-
413427void Adafruit_GFX::setTextSize (uint8_t s) {
414428 textsize = (s > 0 ) ? s : 1 ;
415429}
416430
417-
418431void Adafruit_GFX::setTextColor (uint16_t c) {
419- textcolor = c;
420- textbgcolor = c;
421- // for 'transparent' background, we'll set the bg
432+ // For 'transparent' background, we'll set the bg
422433 // to the same as fg instead of using a flag
434+ textcolor = textbgcolor = c;
423435}
424436
425- void Adafruit_GFX::setTextColor (uint16_t c, uint16_t b) {
426- textcolor = c;
427- textbgcolor = b;
428- }
437+ void Adafruit_GFX::setTextColor (uint16_t c, uint16_t b) {
438+ textcolor = c;
439+ textbgcolor = b;
440+ }
429441
430442void Adafruit_GFX::setTextWrap (boolean w) {
431443 wrap = w;
432444}
433445
434446uint8_t Adafruit_GFX::getRotation (void ) {
435- rotation %= 4 ;
436447 return rotation;
437448}
438449
439450void Adafruit_GFX::setRotation (uint8_t x) {
440- x %= 4 ; // cant be higher than 3
441- rotation = x;
442- switch (x) {
443- case 0 :
444- case 2 :
445- _width = WIDTH;
451+ rotation = (x & 3 );
452+ switch (rotation) {
453+ case 0 :
454+ case 2 :
455+ _width = WIDTH;
446456 _height = HEIGHT;
447457 break ;
448- case 1 :
449- case 3 :
450- _width = HEIGHT;
458+ case 1 :
459+ case 3 :
460+ _width = HEIGHT;
451461 _height = WIDTH;
452462 break ;
453463 }
454464}
455465
456- void Adafruit_GFX::invertDisplay (boolean i) {
457- // do nothing, can be subclassed
458- }
459-
460-
461- // return the size of the display which depends on the rotation!
462- int16_t Adafruit_GFX::width (void ) {
463- return _width;
466+ // Return the size of the display (per current rotation)
467+ int16_t Adafruit_GFX::width (void ) {
468+ return _width;
464469}
465470
466- int16_t Adafruit_GFX::height (void ) {
467- return _height;
471+ int16_t Adafruit_GFX::height (void ) {
472+ return _height;
468473}
474+
475+ void Adafruit_GFX::invertDisplay (boolean i) {
476+ // Do nothing, must be subclassed if supported
477+ }
478+
0 commit comments