Skip to content

Commit e2183be

Browse files
committed
Getting ArduinoGraphics support
1 parent 61a6ef0 commit e2183be

File tree

4 files changed

+206
-22
lines changed

4 files changed

+206
-22
lines changed

examples/basic/ArduinoLogoDrawing/ArduinoLogoDrawing.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77
Note: This example is also embedded in the Mbed Core:
88
https://github.com/arduino/ArduinoCore-mbed/blob/main/libraries/Arduino_H7_Video/
99
*/
10-
10+
#ifdef __MBED__
1111
#include "Arduino_H7_Video.h"
1212
#include "ArduinoGraphics.h"
13-
1413
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
1514
//Arduino_H7_Video Display(1024, 768, USBCVideo);
15+
#elif defined(__ZEPHYR__)
16+
#include "Arduino_GigaDisplay.h"
17+
#include "ArduinoGraphics.h"
18+
Display Display(800, 480);
19+
#endif
1620

1721
void setup() {
22+
while(!Serial && millis() < 5000);
1823
Display.begin();
19-
24+
2025
Display.beginDraw();
2126
Display.background(255, 255, 255);
2227
Display.clear();

examples/basic/SimpleText/SimpleText.ino

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
This example code is in the public domain.
1616
*/
1717

18+
#ifdef __MBED__
1819
#include "Arduino_H7_Video.h"
1920
#include "ArduinoGraphics.h"
20-
2121
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
22+
//Arduino_H7_Video Display(1024, 768, USBCVideo);
23+
#elif defined(__ZEPHYR__)
24+
#include "Arduino_GigaDisplay.h"
25+
#include "ArduinoGraphics.h"
26+
Display Display(800, 480);
27+
#endif
2228

2329
void setup() {
2430
Display.begin();

src/GigaDisplay.cpp

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@
2525
#include <zephyr/device.h>
2626
#include <zephyr/drivers/display.h>
2727

28-
Display::Display() : gdev(NULL){
2928

29+
#ifdef HAS_ARDUINOGRAPHICS
30+
Display::Display(int width, int height) : ArduinoGraphics(width, height), gdev(NULL)
31+
#else
32+
Display::Display(int width, int height) : gdev(NULL)
33+
#endif
34+
{
35+
_height = height;
36+
_width = width;
37+
_rotated = (width >= height) ? true : false;
38+
printk("height: %d, width: %d, rotated: %d\n", _height, _width, _rotated);
3039
}
3140

32-
bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
41+
bool Display::begin(DisplayPixelFormat pixformat) {
3342

3443
int ret = 0;
3544

@@ -38,7 +47,7 @@ bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
3847
#endif
3948

4049
if (!this->gdev || !device_is_ready(this->gdev)) {
41-
Serial.println("\t<err> Zephy Display Not Ready!...");
50+
printk("\t<err> Zephy Display Not Ready!...");
4251
return false;
4352
}
4453

@@ -77,7 +86,7 @@ bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
7786
return false;
7887
}
7988

80-
89+
/*
8190
display_orientation orientation;
8291
switch(rotation){
8392
case 0:
@@ -97,14 +106,15 @@ bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
97106
break;
98107
}
99108
//Rotation not supported
100-
//Serial.print("Orientation: "); Serial.println(orientation);
109+
Serial.print("Orientation: "); Serial.println(orientation);
101110
ret = display_set_orientation(this->gdev, orientation);
102111
Serial.println(ret);
103112
if(ret) {
104113
Serial.println("\t<err> Failed to set display rotation");
105114
//return false;
106115
}
107-
116+
*/
117+
108118
display_get_capabilities(this->gdev, &capabilities);
109119

110120
printk("- Capabilities:\n");
@@ -113,9 +123,39 @@ bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
113123
capabilities.x_resolution, capabilities.y_resolution,
114124
capabilities.supported_pixel_formats, capabilities.current_pixel_format,
115125
capabilities.current_orientation);
116-
126+
#ifndef HAS_ARDUINOGRAPHICS
117127
_height = capabilities.y_resolution;
118128
_width = capabilities.x_resolution;
129+
#endif
130+
131+
#ifdef HAS_ARDUINOGRAPHICS
132+
#ifdef CONFIG_SHARED_MULTI_HEAP
133+
void* ptrFB = getFrameBuffer();
134+
if (ptrFB == nullptr){
135+
printk("Memory not allocated successfully." );
136+
while(1){}
137+
}
138+
// Cast the void pointer to an int pointer to use it
139+
buffer = static_cast<uint16_t*>(ptrFB);
140+
//buffer = (uint16_t*)shared_multi_heap_aligned_alloc(SMH_REG_ATTR_EXTERNAL, 16, (this->width() * this-> height() * sizeof(uint16_t)));
141+
#else
142+
SDRAM.begin();
143+
buffer = (uint16_t*)SDRAM.malloc(this->width() * this-> height() * sizeof(uint16_t));
144+
#endif
145+
sizeof_framebuffer = width() * height() * sizeof(uint16_t);
146+
setFrameDesc(width(), height(), width(), sizeof_framebuffer);
147+
Serial.print("Buffer: 0x"); Serial.println((uint32_t)buffer, HEX);
148+
149+
// turn on the display backlight
150+
pinMode(74, OUTPUT);
151+
digitalWrite(74, HIGH);
152+
153+
if (!ArduinoGraphics::begin()) {
154+
return 1; /* Unknown err */
155+
}
156+
157+
textFont(Font_5x7);
158+
#endif //arduinoGraphics
119159

120160
return true;
121161
}
@@ -170,4 +210,78 @@ void* Display::getFrameBuffer() {
170210
return fb;
171211
}
172212

213+
214+
#ifdef HAS_ARDUINOGRAPHICS
215+
void Display::beginDraw() {
216+
ArduinoGraphics::beginDraw();
217+
startFrameBuffering();
218+
lcdClear(0);
219+
}
220+
221+
void Display::endDraw() {
222+
ArduinoGraphics::endDraw();
223+
224+
endFrameBuffering();
225+
}
226+
227+
void Display::clear(){
228+
uint32_t bg = ArduinoGraphics::background();
229+
uint32_t x_size, y_size;
230+
231+
if(_rotated) {
232+
x_size = (height() <= getDisplayXSize())? height() : getDisplayXSize();
233+
y_size = (width() <= getDisplayYSize())? width() : getDisplayYSize();
234+
} else {
235+
x_size = (width() <= getDisplayXSize())? width() : getDisplayXSize();
236+
y_size = (height() <= getDisplayYSize())? height() : getDisplayYSize();
237+
}
238+
239+
//lcdFillArea((void *)(dsi_getCurrentFrameBuffer()), x_size, y_size, bg);
240+
lcdClear(bg);
241+
}
242+
243+
void Display::set(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
244+
uint32_t x_rot, y_rot;
245+
246+
if ((x < 0) || (y < 0) || (x >= _width) || (y >= _height))
247+
return;
248+
249+
if (_rotated) {
250+
x_rot = ((height()-1) - y);
251+
y_rot = x;
252+
253+
//if (x_rot >= height() || y_rot >= width())
254+
// return;
255+
} else {
256+
x_rot = x;
257+
y_rot = y;
258+
259+
//if (x_rot >= width() || y_rot >= height())
260+
// return;
261+
}
262+
263+
if (x_rot >= getDisplayXSize() || y_rot >= getDisplayYSize())
264+
return;
265+
//printk("%u, %u, %u, %u, %u\n", x, y, x_rot, y_rot, x_rot + (width() * y_rot));
266+
267+
uint16_t color = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
268+
//uint32_t color = (uint32_t)((uint32_t)(r << 16) | (uint32_t)(g << 8) | (uint32_t)(b << 0));
269+
buffer[(x_rot + (getDisplayXSize() * y_rot)) ] = color;
270+
271+
}
272+
273+
void Display::lcdClear(uint16_t Color) {
274+
/* Clear the LCD */
275+
uint8_t hi = Color >> 8, lo = Color & 0xFF;
276+
if (hi == lo) {
277+
memset(buffer, lo, width() * height() * 2);
278+
} else {
279+
uint32_t i, pixels = width() * height();
280+
for (i = 0; i < pixels; i++)
281+
buffer[i] = Color;
282+
}
283+
284+
}
285+
#endif //HAS_ARDUINOGRAPHICS
286+
173287
#endif //__ZEPHYR__

src/GigaDisplay.h

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
#ifdef __ZEPHYR__
2222

2323
#include "Arduino.h"
24+
25+
#if __has_include ("HasIncludeArduinoGraphics.h")
26+
#include "SDRAM.h"
27+
#include "ArduinoGraphics.h"
28+
#define HAS_ARDUINOGRAPHICS
29+
30+
static uint32_t lcd_x_size = 480;
31+
static uint32_t lcd_y_size = 800;
32+
#endif
33+
34+
2435
/**
2536
* @enum DisplayPixelFormat
2637
* @brief Display pixel format enumeration.
@@ -60,24 +71,21 @@ enum DisplayPixelFormat {
6071
* @class Display
6172
* @brief The main class for controlling a camera.
6273
*/
63-
class Display {
64-
private:
65-
const struct device *gdev;
66-
struct display_buffer_descriptor *buf_desc;
67-
68-
protected:
69-
int16_t _height, _width;
70-
74+
class Display
75+
#ifdef HAS_ARDUINOGRAPHICS
76+
: public ArduinoGraphics
77+
#endif
78+
{
7179
public:
7280
/**
7381
* @brief Construct a new Camera object.
7482
*/
75-
Display();
83+
Display(int width = 800, int height = 480);
7684

7785
/**
7886
* @brief Initialize the display
7987
*/
80-
bool begin(DisplayPixelFormat pixformat = DISPLAY_RGB565, int rotation = 0);
88+
bool begin(DisplayPixelFormat pixformat = DISPLAY_RGB565);
8189

8290
/**
8391
* @brief a frame.
@@ -110,7 +118,58 @@ class Display {
110118
int16_t width(void) { return _width; }
111119
int16_t height(void) { return _height; }
112120

113-
121+
122+
#ifdef HAS_ARDUINOGRAPHICS
123+
/**
124+
* @brief Clear the display.
125+
*/
126+
void clear();
127+
128+
/**
129+
* @brief Begin drawing operations on the display.
130+
*/
131+
virtual void beginDraw();
132+
133+
/**
134+
* @brief End drawing operations on the display.
135+
*/
136+
virtual void endDraw();
137+
138+
/**
139+
* @brief Set the color of the pixel at the specified coordinates.
140+
*
141+
* @param x The x-coordinate of the pixel.
142+
* @param y The y-coordinate of the pixel.
143+
* @param r The red component of the color.
144+
* @param g The green component of the color.
145+
* @param b The blue component of the color.
146+
*/
147+
virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b);
148+
149+
uint32_t getDisplayXSize(){
150+
return lcd_x_size;
151+
}
152+
153+
uint32_t getDisplayYSize(){
154+
return lcd_y_size;
155+
}
156+
157+
void lcdClear(uint16_t Color);
158+
159+
#endif
160+
161+
162+
private:
163+
const struct device *gdev;
164+
struct display_buffer_descriptor *buf_desc;
165+
#ifdef HAS_ARDUINOGRAPHICS
166+
uint16_t *buffer = nullptr;
167+
uint32_t sizeof_framebuffer;
168+
#endif
169+
protected:
170+
int16_t _height, _width;
171+
bool _rotated = false;
172+
114173
};
115174

116175
#endif // __GIGA_DISPLAY_H__

0 commit comments

Comments
 (0)