-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoB.h
166 lines (159 loc) · 4.5 KB
/
TwoB.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#define ALIGN_LEFT 0
#define ALIGN_TOP 0
#define ALIGN_RIGHT 65535
#define ALIGN_BOTTOM 65534
#define ALIGN_CENTER 65533 // Left <--> Right
#define ALIGN_MIDDLE 65532 // Top <--> Bottom
uint16_t pWidth, pHeight;
// These two variables are global, so you can pick up the values, should you need them, after loading the image.
void displayImage(uint16_t *colors, uint16_t zWidth, uint16_t zHeight, uint16_t XX, uint16_t YY, Adafruit_ILI9341 tft) {
SerialUSB.print("Image size: ");
SerialUSB.print(zWidth);
SerialUSB.print(" x ");
SerialUSB.println(zHeight);
uint16_t pXX = XX, pYY = YY;
if (XX == ALIGN_RIGHT) {
pXX = 320 - zWidth;
} else if (XX == ALIGN_CENTER) {
pXX = (320 - zWidth) / 2;
}
if (YY == ALIGN_BOTTOM) {
pYY = 240 - zHeight;
} else if (YY == ALIGN_MIDDLE) {
pYY = (240 - zHeight) / 2;
}
uint16_t px, py, ix, color;
ix = 0;
for (py = 0; py < zHeight; py++) {
tft.startWrite();
tft.setAddrWindow(pXX, py + pYY, zWidth, 1);
for (px = 0; px < zWidth; px++) {
color = colors[ix++];
// SPI1.transfer((uint8_t)(color >> 8));
// SPI1.transfer((uint8_t)(color & 0xFF));
tft.spiWrite((uint8_t)(color >> 8));
tft.spiWrite((uint8_t)(color & 0xFF));
}
tft.endWrite();
}
}
bool preloadImage(char *fName, uint16_t *pColors) {
SdFile binFile(fName, O_RDONLY);
// check for open error
if (!binFile.isOpen()) {
SerialUSB.print("Couldn't open file ");
SerialUSB.println(fName);
return false;
}
char header[6];
if (binFile.read(&header, 6) != 6) {
SerialUSB.println("Couldn't read header from file.");
binFile.close();
return false;
}
if (header[0] != '2' || header[1] != 'B') {
SerialUSB.println("Wrong header!");
binFile.close();
return false;
}
pWidth = header[2] + header[3] * 256;
pHeight = header[4] + header[5] * 256;
SerialUSB.print("Image size: ");
SerialUSB.print(pWidth);
SerialUSB.print(" x ");
SerialUSB.println(pHeight);
uint16_t px, py, nBytes, ix, nx = 0;
nBytes = pWidth * 2;
char mb[nBytes];
for (py = 0; py < pHeight; py++) {
if (binFile.read(&mb, nBytes) != nBytes) {
SerialUSB.println("Failed reading from file!");
binFile.close();
return false;
}
ix = 0;
for (px = 0; px < pWidth; px++) {
pColors[nx++] = mb[ix + 1] * 256 + mb[ix];
ix += 2;
}
}
binFile.close();
return true;
}
void displayImage(char *fName, uint16_t XX, uint16_t YY, Adafruit_ILI9341 tft) {
char mb[640];
SdFile binFile(fName, O_RDONLY);
// check for open error
if (!binFile.isOpen()) {
SerialUSB.print("Couldn't open file ");
SerialUSB.println(fName);
return;
}
if (binFile.read(&mb, 6) != 6) {
SerialUSB.println("Couldn't read header from file.");
binFile.close();
return;
}
if (mb[0] != '2' || mb[1] != 'B') {
SerialUSB.println("Wrong header!");
binFile.close();
return;
}
pWidth = mb[2] + mb[3] * 256;
pHeight = mb[4] + mb[5] * 256;
SerialUSB.print("Image size: ");
SerialUSB.print(pWidth);
SerialUSB.print(" x ");
SerialUSB.println(pHeight);
uint16_t pXX = XX, pYY = YY;
if (XX == ALIGN_RIGHT) {
pXX = 320 - pWidth;
} else if (XX == ALIGN_CENTER) {
pXX = (320 - pWidth) / 2;
}
if (YY == ALIGN_BOTTOM) {
pYY = 240 - pHeight;
} else if (YY == ALIGN_MIDDLE) {
pYY = (240 - pHeight) / 2;
}
uint16_t px, py, nBytes, ix;
nBytes = pWidth * 2;
for (py = 0; py < pHeight; py++) {
if (binFile.read(&mb, nBytes) != nBytes) {
SerialUSB.println("Failed reading from file!");
binFile.close();
return;
}
ix = 0;
tft.startWrite();
tft.setAddrWindow(pXX, py + pYY, pWidth, 1);
for (px = 0; px < pWidth; px++) {
// SPI1.transfer(mb[ix + 1]);
// SPI1.transfer(mb[ix]);
tft.spiWrite(mb[ix + 1]);
tft.spiWrite(mb[ix]);
ix += 2;
}
tft.endWrite();
}
binFile.close();
}
#define ILI9341_RAMREAD 0x2E // Memory Read
#define ILI9341_NOP 0
void readRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t *pColors, Adafruit_ILI9341 tft) {
// Does not work...
uint16_t ix = 0;
for (uint16_t i = 0; i < h; i++) {
for (uint16_t n = 0; n < w; n++) {
tft.startWrite();
tft.setAddrWindow(x + n, y + i, 1, 1);
tft.writeCommand(ILI9341_RAMREAD); // read from RAM
uint8_t red = tft.spiRead(); // dummy read
red = tft.spiRead();
uint8_t green = tft.spiRead();
uint8_t blue = tft.spiRead();
pColors[ix++] = tft.color565(red, green, blue);
tft.endWrite();
}
}
}