1 | /*Begining of Auto generated code by Atmel studio */
|
2 | #include <Arduino.h>
|
3 |
|
4 | /*End of auto generated code by Atmel studio */
|
5 |
|
6 | /***************************************************
|
7 | This is our Bitmap drawing example for the Adafruit HX8357 Breakout
|
8 | ----> http://www.adafruit.com/products/2050
|
9 |
|
10 | Check out the links above for our tutorials and wiring diagrams
|
11 | These displays use SPI to communicate, 4 or 5 pins are required to
|
12 | interface (RST is optional)
|
13 | Adafruit invests time and resources providing this open source code,
|
14 | please support Adafruit and open-source hardware by purchasing
|
15 | products from Adafruit!
|
16 |
|
17 | Written by Limor Fried/Ladyada for Adafruit Industries.
|
18 | MIT license, all text above must be included in any redistribution
|
19 | ****************************************************/
|
20 |
|
21 |
|
22 | #include <Adafruit_GFX.h> // Core graphics library
|
23 | #include "Adafruit_HX8357.h"
|
24 | #include <SPI.h>
|
25 | #include <SD.h>
|
26 |
|
27 | //Beginning of Auto generated function prototypes by Atmel Studio
|
28 | void setup(void );
|
29 | void bmpDraw(char filename, uint8_t x, uint16_t y);
|
30 | uint16_t read16(File f);
|
31 | uint32_t read32(File f);
|
32 |
|
33 | // TFT display and SD card will share the hardware SPI interface.
|
34 | // Hardware SPI pins are specific to the Arduino board type and
|
35 | // cannot be remapped to alternate pins. For Arduino Uno,
|
36 | // Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
|
37 |
|
38 | #define TFT_DC 47
|
39 | #define TFT_CS 46
|
40 |
|
41 | // Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
|
42 | Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
|
43 |
|
44 | #define SD_CS 44
|
45 |
|
46 | void setup(void) {
|
47 | Serial.begin(9600);
|
48 |
|
49 | tft.begin(HX8357D);
|
50 | tft.fillScreen(HX8357_BLUE);
|
51 |
|
52 | Serial.print("Initializing SD card...");
|
53 | if (!SD.begin(SD_CS)) {
|
54 | Serial.println("failed!");
|
55 | }
|
56 | Serial.println("OK!");
|
57 |
|
58 | bmpDraw("jumpers.bmp", 0, 0); //Hier tritt der Fehler auf
|
59 | }
|
60 |
|
61 | void loop() {
|
62 | }
|
63 |
|
64 | // This function opens a Windows Bitmap (BMP) file and
|
65 | // displays it at the given coordinates. It's sped up
|
66 | // by reading many pixels worth of data at a time
|
67 | // (rather than pixel by pixel). Increasing the buffer
|
68 | // size takes more of the Arduino's precious RAM but
|
69 | // makes loading a little faster. 20 pixels seems a
|
70 | // good balance.
|
71 |
|
72 | #define BUFFPIXEL 20
|
73 |
|
74 | void bmpDraw(char *filename, uint8_t x, uint16_t y) {
|
75 |
|
76 | File bmpFile;
|
77 | int bmpWidth, bmpHeight; // W+H in pixels
|
78 | uint8_t bmpDepth; // Bit depth (currently must be 24)
|
79 | uint32_t bmpImageoffset; // Start of image data in file
|
80 | uint32_t rowSize; // Not always = bmpWidth; may have padding
|
81 | uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
|
82 | uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
|
83 | boolean goodBmp = false; // Set to true on valid header parse
|
84 | boolean flip = true; // BMP is stored bottom-to-top
|
85 | int w, h, row, col;
|
86 | uint8_t r, g, b;
|
87 | uint32_t pos = 0, startTime = millis();
|
88 |
|
89 | if((x >= tft.width()) || (y >= tft.height())) return;
|
90 |
|
91 | Serial.println();
|
92 | Serial.print(F("Loading image '"));
|
93 | Serial.print(filename);
|
94 | Serial.println('\'');
|
95 |
|
96 | // Open requested file on SD card
|
97 | if ((bmpFile = SD.open(filename)) == NULL) {
|
98 | Serial.print(F("File not found"));
|
99 | return;
|
100 | }
|
101 |
|
102 | // Parse BMP header
|
103 | if(read16(bmpFile) == 0x4D42) { // BMP signature
|
104 | Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
|
105 | (void)read32(bmpFile); // Read & ignore creator bytes
|
106 | bmpImageoffset = read32(bmpFile); // Start of image data
|
107 | Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
|
108 | // Read DIB header
|
109 | Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
|
110 | bmpWidth = read32(bmpFile);
|
111 | bmpHeight = read32(bmpFile);
|
112 | if(read16(bmpFile) == 1) { // # planes -- must be '1'
|
113 | bmpDepth = read16(bmpFile); // bits per pixel
|
114 | Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
|
115 | if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
|
116 |
|
117 | goodBmp = true; // Supported BMP format -- proceed!
|
118 | Serial.print(F("Image size: "));
|
119 | Serial.print(bmpWidth);
|
120 | Serial.print('x');
|
121 | Serial.println(bmpHeight);
|
122 |
|
123 | // BMP rows are padded (if needed) to 4-byte boundary
|
124 | rowSize = (bmpWidth * 3 + 3) & ~3;
|
125 |
|
126 | // If bmpHeight is negative, image is in top-down order.
|
127 | // This is not canon but has been observed in the wild.
|
128 | if(bmpHeight < 0) {
|
129 | bmpHeight = -bmpHeight;
|
130 | flip = false;
|
131 | }
|
132 |
|
133 | // Crop area to be loaded
|
134 | w = bmpWidth;
|
135 | h = bmpHeight;
|
136 | if((x+w-1) >= tft.width()) w = tft.width() - x;
|
137 | if((y+h-1) >= tft.height()) h = tft.height() - y;
|
138 |
|
139 | // Set TFT address window to clipped image bounds
|
140 | tft.setAddrWindow(x, y, x+w-1, y+h-1);
|
141 |
|
142 | for (row=0; row<h; row++) { // For each scanline...
|
143 |
|
144 | // Seek to start of scan line. It might seem labor-
|
145 | // intensive to be doing this on every line, but this
|
146 | // method covers a lot of gritty details like cropping
|
147 | // and scanline padding. Also, the seek only takes
|
148 | // place if the file position actually needs to change
|
149 | // (avoids a lot of cluster math in SD library).
|
150 | if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
151 | pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
|
152 | else // Bitmap is stored top-to-bottom
|
153 | pos = bmpImageoffset + row * rowSize;
|
154 | if(bmpFile.position() != pos) { // Need seek?
|
155 | bmpFile.seek(pos);
|
156 | buffidx = sizeof(sdbuffer); // Force buffer reload
|
157 | }
|
158 |
|
159 | for (col=0; col<w; col++) { // For each pixel...
|
160 | // Time to read more pixel data?
|
161 | if (buffidx >= sizeof(sdbuffer)) { // Indeed
|
162 | bmpFile.read(sdbuffer, sizeof(sdbuffer));
|
163 | buffidx = 0; // Set index to beginning
|
164 | }
|
165 |
|
166 | // Convert pixel from BMP to TFT format, push to display
|
167 | b = sdbuffer[buffidx++];
|
168 | g = sdbuffer[buffidx++];
|
169 | r = sdbuffer[buffidx++];
|
170 | tft.pushColor(tft.color565(r,g,b));
|
171 | } // end pixel
|
172 | } // end scanline
|
173 | Serial.print(F("Loaded in "));
|
174 | Serial.print(millis() - startTime);
|
175 | Serial.println(" ms");
|
176 | } // end goodBmp
|
177 | }
|
178 | }
|
179 |
|
180 | bmpFile.close();
|
181 | if(!goodBmp) Serial.println(F("BMP format not recognized."));
|
182 | }
|
183 |
|
184 | // These read 16- and 32-bit types from the SD card file.
|
185 | // BMP data is stored little-endian, Arduino is little-endian too.
|
186 | // May need to reverse subscript order if porting elsewhere.
|
187 |
|
188 | uint16_t read16(File &f) {
|
189 | uint16_t result;
|
190 | ((uint8_t *)&result)[0] = f.read(); // LSB
|
191 | ((uint8_t *)&result)[1] = f.read(); // MSB
|
192 | return result;
|
193 | }
|
194 |
|
195 | uint32_t read32(File &f) {
|
196 | uint32_t result;
|
197 | ((uint8_t *)&result)[0] = f.read(); // LSB
|
198 | ((uint8_t *)&result)[1] = f.read();
|
199 | ((uint8_t *)&result)[2] = f.read();
|
200 | ((uint8_t *)&result)[3] = f.read(); // MSB
|
201 | return result;
|
202 | }
|