Skip to content

Commit bbd757e

Browse files
committed
Initial commit
0 parents  commit bbd757e

File tree

4 files changed

+636
-0
lines changed

4 files changed

+636
-0
lines changed

Arduino_GigaDisplay_GFX.cpp

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
2+
#include "Arduino_GigaDisplay_GFX.h"
3+
#include "platform/mbed_critical.h"
4+
5+
GigaDisplay_GFX::GigaDisplay_GFX() : Adafruit_GFX(480, 800) {
6+
7+
}
8+
9+
GigaDisplay_GFX::~GigaDisplay_GFX(void) {
10+
if (buffer)
11+
free(buffer);
12+
}
13+
14+
//rtos::Semaphore refresh_sem(1);
15+
16+
void GigaDisplay_GFX::refresh_if_needed() {
17+
while (1) {
18+
rtos::ThisThread::flags_wait_any(0x1);
19+
//refresh_sem.acquire();
20+
dsi_lcdDrawImage((void *) this->getBuffer(), (void *)(dsi_getActiveFrameBuffer()), 480, 800, DMA2D_INPUT_RGB565);
21+
//dsi_lcdDrawImage((void *) this->getBuffer(), (void *)(dsi_getActiveFrameBuffer()), this->width(), this->height(), DMA2D_INPUT_RGB565);
22+
//refresh_sem.release();
23+
delay(10);
24+
}
25+
}
26+
27+
28+
void GigaDisplay_GFX::begin() {
29+
display = new Arduino_H7_Video(480, 800, GigaDisplayShield);
30+
display->begin();
31+
buffer = (uint16_t*)ea_malloc(this->width() * this-> height() * 2);
32+
_refresh_thd = new rtos::Thread(osPriorityHigh);
33+
_refresh_thd->start(mbed::callback(this, &GigaDisplay_GFX::refresh_if_needed));
34+
//buffer = (uint16_t*)dsi_getActiveFrameBuffer();
35+
}
36+
37+
void GigaDisplay_GFX::startWrite() {
38+
//refresh_sem.acquire();
39+
}
40+
41+
void GigaDisplay_GFX::endWrite() {
42+
//refresh_sem.release();
43+
_refresh_thd->flags_set(0x1);
44+
}
45+
46+
void GigaDisplay_GFX::drawPixel(int16_t x, int16_t y, uint16_t color) {
47+
if (hasBuffer()) {
48+
if ((x < 0) || (y < 0) || (x >= _width) || (y >= _height))
49+
return;
50+
51+
int16_t t;
52+
switch (rotation) {
53+
case 1:
54+
t = x;
55+
x = WIDTH - 1 - y;
56+
y = t;
57+
break;
58+
case 2:
59+
x = WIDTH - 1 - x;
60+
y = HEIGHT - 1 - y;
61+
break;
62+
case 3:
63+
t = x;
64+
x = y;
65+
y = HEIGHT - 1 - t;
66+
break;
67+
}
68+
69+
buffer[x + y * WIDTH] = color;
70+
}
71+
}
72+
73+
uint16_t GigaDisplay_GFX::getPixel(int16_t x, int16_t y) {
74+
int16_t t;
75+
switch (rotation) {
76+
case 1:
77+
t = x;
78+
x = WIDTH - 1 - y;
79+
y = t;
80+
break;
81+
case 2:
82+
x = WIDTH - 1 - x;
83+
y = HEIGHT - 1 - y;
84+
break;
85+
case 3:
86+
t = x;
87+
x = y;
88+
y = HEIGHT - 1 - t;
89+
break;
90+
}
91+
return getRawPixel(x, y);
92+
}
93+
94+
uint16_t GigaDisplay_GFX::getRawPixel(int16_t x, int16_t y) {
95+
if ((x < 0) || (y < 0) || (x >= WIDTH) || (y >= HEIGHT))
96+
return 0;
97+
if (hasBuffer()) {
98+
return buffer[x + y * WIDTH];
99+
}
100+
return 0;
101+
}
102+
103+
void GigaDisplay_GFX::fillScreen(uint16_t color) {
104+
if (hasBuffer()) {
105+
uint8_t hi = color >> 8, lo = color & 0xFF;
106+
if (hi == lo) {
107+
memset(buffer, lo, WIDTH * HEIGHT * 2);
108+
} else {
109+
uint32_t i, pixels = WIDTH * HEIGHT;
110+
for (i = 0; i < pixels; i++)
111+
buffer[i] = color;
112+
}
113+
}
114+
}
115+
116+
void GigaDisplay_GFX::byteSwap(void) {
117+
if (hasBuffer()) {
118+
uint32_t i, pixels = WIDTH * HEIGHT;
119+
for (i = 0; i < pixels; i++)
120+
buffer[i] = __builtin_bswap16(buffer[i]);
121+
}
122+
}
123+
124+
void GigaDisplay_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h,
125+
uint16_t color) {
126+
if (h < 0) { // Convert negative heights to positive equivalent
127+
h *= -1;
128+
y -= h - 1;
129+
if (y < 0) {
130+
h += y;
131+
y = 0;
132+
}
133+
}
134+
135+
// Edge rejection (no-draw if totally off canvas)
136+
if ((x < 0) || (x >= width()) || (y >= height()) || ((y + h - 1) < 0)) {
137+
return;
138+
}
139+
140+
if (y < 0) { // Clip top
141+
h += y;
142+
y = 0;
143+
}
144+
if (y + h > height()) { // Clip bottom
145+
h = height() - y;
146+
}
147+
148+
if (getRotation() == 0) {
149+
drawFastRawVLine(x, y, h, color);
150+
} else if (getRotation() == 1) {
151+
int16_t t = x;
152+
x = WIDTH - 1 - y;
153+
y = t;
154+
x -= h - 1;
155+
drawFastRawHLine(x, y, h, color);
156+
} else if (getRotation() == 2) {
157+
x = WIDTH - 1 - x;
158+
y = HEIGHT - 1 - y;
159+
160+
y -= h - 1;
161+
drawFastRawVLine(x, y, h, color);
162+
} else if (getRotation() == 3) {
163+
int16_t t = x;
164+
x = y;
165+
y = HEIGHT - 1 - t;
166+
drawFastRawHLine(x, y, h, color);
167+
}
168+
}
169+
170+
void GigaDisplay_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w,
171+
uint16_t color) {
172+
if (w < 0) { // Convert negative widths to positive equivalent
173+
w *= -1;
174+
x -= w - 1;
175+
if (x < 0) {
176+
w += x;
177+
x = 0;
178+
}
179+
}
180+
181+
// Edge rejection (no-draw if totally off canvas)
182+
if ((y < 0) || (y >= height()) || (x >= width()) || ((x + w - 1) < 0)) {
183+
return;
184+
}
185+
186+
if (x < 0) { // Clip left
187+
w += x;
188+
x = 0;
189+
}
190+
if (x + w >= width()) { // Clip right
191+
w = width() - x;
192+
}
193+
194+
if (getRotation() == 0) {
195+
drawFastRawHLine(x, y, w, color);
196+
} else if (getRotation() == 1) {
197+
int16_t t = x;
198+
x = WIDTH - 1 - y;
199+
y = t;
200+
drawFastRawVLine(x, y, w, color);
201+
} else if (getRotation() == 2) {
202+
x = WIDTH - 1 - x;
203+
y = HEIGHT - 1 - y;
204+
205+
x -= w - 1;
206+
drawFastRawHLine(x, y, w, color);
207+
} else if (getRotation() == 3) {
208+
int16_t t = x;
209+
x = y;
210+
y = HEIGHT - 1 - t;
211+
y -= w - 1;
212+
drawFastRawVLine(x, y, w, color);
213+
}
214+
}
215+
216+
void GigaDisplay_GFX::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
217+
uint16_t color) {
218+
// x & y already in raw (rotation 0) coordinates, no need to transform.
219+
uint16_t *buffer_ptr = buffer + y * WIDTH + x;
220+
for (int16_t i = 0; i < h; i++) {
221+
(*buffer_ptr) = color;
222+
buffer_ptr += WIDTH;
223+
}
224+
}
225+
226+
void GigaDisplay_GFX::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
227+
uint16_t color) {
228+
// x & y already in raw (rotation 0) coordinates, no need to transform.
229+
uint32_t buffer_index = y * WIDTH + x;
230+
for (uint32_t i = buffer_index; i < buffer_index + w; i++) {
231+
buffer[i] = color;
232+
}
233+
}

Arduino_GigaDisplay_GFX.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
#ifndef __ARDUINO_GIGADISPLAY_GFX__
3+
#define __ARDUINO_GIGADISPLAY_GFX__
4+
5+
#include "Arduino_H7_Video.h"
6+
#include "Adafruit_GFX.h"
7+
#include "Adafruit_SPITFT.h"
8+
#include "dsi.h"
9+
#include "SDRAM.h"
10+
11+
class GigaDisplay_GFX : public Adafruit_GFX {
12+
public:
13+
GigaDisplay_GFX();
14+
~GigaDisplay_GFX(void);
15+
void begin();
16+
void drawPixel(int16_t x, int16_t y, uint16_t color);
17+
void fillScreen(uint16_t color);
18+
void byteSwap(void);
19+
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
20+
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
21+
uint16_t getPixel(int16_t x, int16_t y);
22+
uint16_t *getBuffer(void) {
23+
return buffer;
24+
}
25+
uint16_t *hasBuffer(void) {
26+
if (!buffer) {
27+
begin();
28+
}
29+
return buffer;
30+
}
31+
32+
void startWrite();
33+
void endWrite();
34+
35+
uint16_t color565(uint8_t red, uint8_t green, uint8_t blue) {
36+
return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
37+
}
38+
39+
protected:
40+
uint16_t getRawPixel(int16_t x, int16_t y);
41+
void drawFastRawVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
42+
void drawFastRawHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
43+
uint16_t *buffer = nullptr; ///< Raster data: no longer private, allow subclass access
44+
45+
private:
46+
Arduino_H7_Video* display;
47+
void refresh_if_needed();
48+
bool need_refresh = false;
49+
uint32_t last_refresh = 0;
50+
rtos::Thread* _refresh_thd;
51+
};
52+
53+
#endif //__ARDUINO_GIGADISPLAY_GFX__

examples/Basic/Basic.ino

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "Arduino_GigaDisplay_GFX.h"
2+
3+
GigaDisplay_GFX display;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
}
8+
9+
void loop() {
10+
Serial.println(millis());
11+
display.fillScreen(0);
12+
display.fillRect(120, 180, 120, 180, 0x8888);
13+
display.print(false);
14+
delay(100);
15+
}

0 commit comments

Comments
 (0)