source: rtos_arduino/trunk/arduino_lib/libraries/TFT/src/utility/Adafruit_GFX.h@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 11.4 KB
Line 
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).
6
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 Processing-like API written by Enrico Gueli for Officine Arduino.
13 BSD license, check license.txt for more information.
14 All text above must be included in any redistribution.
15 ******************************************************************/
16
17#ifndef _ADAFRUIT_GFX_H
18#define _ADAFRUIT_GFX_H
19
20#if ARDUINO >= 100
21 #include "Arduino.h"
22 #include "Print.h"
23#else
24 #include "WProgram.h"
25#endif
26
27/*
28 * This library can work with or without the presence of an SD
29 * reading library (to load images). At the moment, only the
30 * Arduino SD library is supported; it is included in
31 * standard Arduino libraries.
32 *
33 * The presence of the SD library is detected by looking at the
34 * __SD_H__ preprocessor variable, defined into
35 * Arduino SD library to avoid double inclusion. This means
36 * that in order to use the image-related API of Adafruit_GFX,
37 * SD.h *must* be included before Adafruit_GFX.
38 *
39 * The bottom part of this include file contains the actual image
40 * loading code; if it was in a separate .cpp file, there were no
41 * way to check if the SD library was present or not.
42 *
43 * A partial solution was to include SD.h anyway, see if that works
44 * (i.e. it is found in the include search path) and act accordingly.
45 * But this solution relied on the preprocessor to issue only a
46 * warning when an include file is not found. Avr-gcc, used for
47 * Arduino 8-bit MCUs, does that, but the standard gcc-4.4, used for
48 * Arduino Due, issues a fatal error and stops compilation.
49 *
50 * The best solution so far is to put the code here. It works if this
51 * include is used only in one .cpp file in the build (this is the
52 * case of most Arduino sketches); if used in multiple .cpp files,
53 * the linker may complain about duplicate definitions.
54 *
55 */
56
57#if defined(__SD_H__) // Arduino SD library
58 #include "PImage.h"
59#else
60 #warning "The SD library was not found. loadImage() and image() won't be supported."
61#endif
62
63#define swap(a, b) { int16_t t = a; a = b; b = t; }
64
65/* TODO
66enum RectMode {
67 CORNER,
68 CORNERS,
69 RADIUS,
70 CENTER
71};
72*/
73
74typedef uint16_t color;
75
76class Adafruit_GFX : public Print {
77 public:
78
79 Adafruit_GFX(int16_t w, int16_t h); // Constructor
80
81 // This MUST be defined by the subclass
82 virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
83
84
85
86 // These MAY be overridden by the subclass to provide device-specific
87 // optimized code. Otherwise 'generic' versions are used.
88 virtual void
89 drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
90 uint16_t color),
91 drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
92 drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
93 drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
94 uint16_t color),
95 fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
96 uint16_t color),
97 fillScreen(uint16_t color),
98 invertDisplay(boolean i);
99
100// These exist only with Adafruit_GFX (no subclass overrides)
101 void
102 drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
103 drawCircleHelper(int16_t x0, int16_t y0,
104 int16_t r, uint8_t cornername, uint16_t color),
105 fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
106 fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
107 uint8_t cornername, int16_t delta, uint16_t color),
108
109 drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
110 int16_t x2, int16_t y2, uint16_t color),
111 fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
112 int16_t x2, int16_t y2, uint16_t color),
113 drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
114 int16_t radius, uint16_t color),
115 fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
116 int16_t radius, uint16_t color),
117
118 drawBitmap(int16_t x, int16_t y,
119 const uint8_t *bitmap, int16_t w, int16_t h,
120 uint16_t color),
121 drawChar(int16_t x, int16_t y, unsigned char c,
122 uint16_t color, uint16_t bg, uint8_t size),
123 setCursor(int16_t x, int16_t y),
124 setTextColor(uint16_t c),
125 setTextColor(uint16_t c, uint16_t bg),
126 setTextSize(uint8_t s),
127 setTextWrap(boolean w),
128 setRotation(uint8_t r);
129
130#if ARDUINO >= 100
131 virtual size_t write(uint8_t);
132#else
133 virtual void write(uint8_t);
134#endif
135
136 int16_t
137 height(void),
138 width(void);
139
140
141 uint8_t getRotation(void);
142
143
144 /*
145 * Processing-like graphics primitives
146 */
147
148 /// transforms a color in 16-bit form given the RGB components.
149 /// The default implementation makes a 5-bit red, a 6-bit
150 /// green and a 5-bit blue (MSB to LSB). Devices that use
151 /// different scheme should override this.
152 virtual uint16_t newColor(uint8_t red, uint8_t green, uint8_t blue);
153
154
155 void
156 // http://processing.org/reference/background_.html
157 background(uint8_t red, uint8_t green, uint8_t blue),
158 background(color c),
159
160 // http://processing.org/reference/fill_.html
161 fill(uint8_t red, uint8_t green, uint8_t blue),
162 fill(color c),
163
164 // http://processing.org/reference/noFill_.html
165 noFill(),
166
167 // http://processing.org/reference/stroke_.html
168 stroke(uint8_t red, uint8_t green, uint8_t blue),
169 stroke(color c),
170
171 // http://processing.org/reference/noStroke_.html
172 noStroke(),
173
174 text(const char * text, int16_t x, int16_t y),
175 textWrap(const char * text, int16_t x, int16_t y),
176
177 textSize(uint8_t size),
178
179 // similar to ellipse() in Processing, but with
180 // a single radius.
181 // http://processing.org/reference/ellipse_.html
182 circle(int16_t x, int16_t y, int16_t r),
183 point(int16_t x, int16_t y),
184 line(int16_t x1, int16_t y1, int16_t x2, int16_t y2),
185 quad(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, int16_t x4, int16_t y4),
186rect(int16_t x, int16_t y, int16_t width, int16_t height),
187 rect(int16_t x, int16_t y, int16_t width, int16_t height, int16_t radius),
188 triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3);
189
190 /* TODO
191 void rectMode(RectMode mode);
192
193 void pushStyle();
194 void popStyle();
195 */
196
197#if defined(__SD_H__) // Arduino SD library
198 PImage loadImage(const char * fileName) { return PImage::loadImage(fileName); }
199
200 void image(PImage & img, uint16_t x, uint16_t y);
201#endif
202
203 protected:
204 int16_t
205 WIDTH, HEIGHT; // this is the 'raw' display w/h - never changes
206 int16_t
207 _width, _height, // dependent on rotation
208 cursor_x, cursor_y;
209 uint16_t
210 textcolor, textbgcolor;
211 uint8_t
212 textsize,
213 rotation;
214 boolean
215 wrap; // If set, 'wrap' text at right edge of display
216
217 /*
218 * Processing-style graphics state
219 */
220
221 color strokeColor;
222 bool useStroke;
223 color fillColor;
224 bool useFill;
225};
226
227#if defined(__SD_H__) // Arduino SD library
228
229#define BUFFPIXEL 20
230
231void Adafruit_GFX::image(PImage & img, uint16_t x, uint16_t y) {
232 int w, h, row, col;
233 uint8_t r, g, b;
234 uint32_t pos = 0;
235 uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
236 uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
237
238 // Crop area to be loaded
239 w = img._bmpWidth;
240 h = img._bmpHeight;
241 if((x+w-1) >= width()) w = width() - x;
242 if((y+h-1) >= height()) h = height() - y;
243
244 /*
245 // Set TFT address window to clipped image bounds
246 setAddrWindow(x, y, x+w-1, y+h-1);
247 */
248
249 for (row=0; row<h; row++) { // For each scanline...
250 // Seek to start of scan line. It might seem labor-
251 // intensive to be doing this on every line, but this
252 // method covers a lot of gritty details like cropping
253 // and scanline padding. Also, the seek only takes
254 // place if the file position actually needs to change
255 // (avoids a lot of cluster math in SD library).
256 if(img._flip) // Bitmap is stored bottom-to-top order (normal BMP)
257 pos = img._bmpImageoffset + (img._bmpHeight - 1 - row) * img._rowSize;
258 else // Bitmap is stored top-to-bottom
259 pos = img._bmpImageoffset + row * img._rowSize;
260 if(img._bmpFile.position() != pos) { // Need seek?
261 img._bmpFile.seek(pos);
262 buffidx = sizeof(sdbuffer); // Force buffer reload
263 }
264
265 for (col=0; col<w; col++) { // For each pixel...
266 // Time to read more pixel data?
267 if (buffidx >= sizeof(sdbuffer)) { // Indeed
268 img._bmpFile.read(sdbuffer, sizeof(sdbuffer));
269 buffidx = 0; // Set index to beginning
270 }
271
272 // Convert pixel from BMP to TFT format, push to display
273 b = sdbuffer[buffidx++];
274 g = sdbuffer[buffidx++];
275 r = sdbuffer[buffidx++];
276 //pushColor(tft.Color565(r,g,b));
277 drawPixel(x + col, y + row, newColor(r, g, b));
278
279 } // end pixel
280 } // end scanline
281
282}
283
284
285
286
287// These read 16- and 32-bit types from the SD card file.
288// BMP data is stored little-endian, Arduino is little-endian too.
289// May need to reverse subscript order if porting elsewhere.
290
291uint16_t PImage::read16(File f) {
292 uint16_t result;
293 ((uint8_t *)&result)[0] = f.read(); // LSB
294 ((uint8_t *)&result)[1] = f.read(); // MSB
295 return result;
296}
297
298uint32_t PImage::read32(File f) {
299 uint32_t result;
300 ((uint8_t *)&result)[0] = f.read(); // LSB
301 ((uint8_t *)&result)[1] = f.read();
302 ((uint8_t *)&result)[2] = f.read();
303 ((uint8_t *)&result)[3] = f.read(); // MSB
304 return result;
305}
306
307
308PImage PImage::loadImage(const char * fileName) {
309 File bmpFile;
310 int bmpWidth, bmpHeight; // W+H in pixels
311 uint8_t bmpDepth; // Bit depth (currently must be 24)
312 uint32_t bmpImageoffset; // Start of image data in file
313 uint32_t rowSize; // Not always = bmpWidth; may have padding
314 bool flip = true; // BMP is stored bottom-to-top
315
316
317 // Open requested file on SD card
318 if ((bmpFile = SD.open(fileName)) == NULL) {
319 Serial.print(F("loadImage: file not found: "));
320 Serial.println(fileName);
321 return PImage(); // load error
322 }
323
324
325
326 // Parse BMP header
327 if(read16(bmpFile) != 0x4D42) { // BMP signature
328 Serial.println(F("loadImage: file doesn't look like a BMP"));
329 return PImage();
330 }
331
332 Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
333 (void)read32(bmpFile); // Read & ignore creator bytes
334 bmpImageoffset = read32(bmpFile); // Start of image data
335 Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
336 // Read DIB header
337 Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
338 bmpWidth = read32(bmpFile);
339 bmpHeight = read32(bmpFile);
340 if(read16(bmpFile) != 1) { // # planes -- must be '1'
341 Serial.println(F("loadImage: invalid n. of planes"));
342 return PImage();
343 }
344
345 bmpDepth = read16(bmpFile); // bits per pixel
346 Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
347 if((bmpDepth != 24) || (read32(bmpFile) != 0)) { // 0 = uncompressed {
348 Serial.println(F("loadImage: invalid pixel format"));
349 return PImage();
350 }
351
352 Serial.print(F("Image size: "));
353 Serial.print(bmpWidth);
354 Serial.print('x');
355 Serial.println(bmpHeight);
356
357 // BMP rows are padded (if needed) to 4-byte boundary
358 rowSize = (bmpWidth * 3 + 3) & ~3;
359
360 // If bmpHeight is negative, image is in top-down order.
361 // This is not canon but has been observed in the wild.
362 if(bmpHeight < 0) {
363 bmpHeight = -bmpHeight;
364 flip = false;
365 }
366
367 return PImage(bmpFile, bmpWidth, bmpHeight, bmpDepth, bmpImageoffset, rowSize, flip);
368}
369
370#endif
371
372#endif // _ADAFRUIT_GFX_H
Note: See TracBrowser for help on using the repository browser.