Skip to content

Commit 61a6ef0

Browse files
committed
zephyr display driver
1 parent 3ad5f7e commit 61a6ef0

File tree

4 files changed

+296
-2
lines changed

4 files changed

+296
-2
lines changed

src/Arduino_GigaDisplay.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
#define _ARDUINO_GIGADISPLAY_H_
33

44
#include "GigaDisplayRGB.h"
5-
5+
#ifdef __ZEPHYR__
6+
#include "GigaDisplay.h"
7+
#endif
68
#endif

src/GigaDisplay.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* Copyright 2025 Arduino SA
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*
17+
* Camera driver.
18+
*/
19+
#ifdef __ZEPHYR__
20+
21+
#include "Arduino.h"
22+
#include "Arduino_GigaDisplay.h"
23+
24+
#include <zephyr/kernel.h>
25+
#include <zephyr/device.h>
26+
#include <zephyr/drivers/display.h>
27+
28+
Display::Display() : gdev(NULL){
29+
30+
}
31+
32+
bool Display::begin(DisplayPixelFormat pixformat, int rotation) {
33+
34+
int ret = 0;
35+
36+
#if DT_HAS_CHOSEN(zephyr_display)
37+
this->gdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
38+
#endif
39+
40+
if (!this->gdev || !device_is_ready(this->gdev)) {
41+
Serial.println("\t<err> Zephy Display Not Ready!...");
42+
return false;
43+
}
44+
45+
// Get capabilities
46+
struct display_capabilities capabilities = {0};
47+
display_get_capabilities(this->gdev, &capabilities);
48+
49+
uint8_t format_spec = 0;
50+
51+
switch (pixformat) {
52+
case DISPLAY_RGB565:
53+
//Serial.println("Set RGB565");
54+
if (capabilities.current_pixel_format != PIXEL_FORMAT_RGB_565) {
55+
ret = display_set_pixel_format(this->gdev, PIXEL_FORMAT_RGB_565);
56+
}
57+
format_spec = 1;
58+
break;
59+
case DISPLAY_RGB888:
60+
//Serial.println("Set RGB888");
61+
if (capabilities.current_pixel_format != PIXEL_FORMAT_RGB_888) {
62+
ret = display_set_pixel_format(this->gdev, PIXEL_FORMAT_RGB_888);
63+
}
64+
format_spec = 1;
65+
break;
66+
default:
67+
break;
68+
}
69+
70+
if(format_spec == 0) {
71+
Serial.println("\t<err> The specified format is not supported");
72+
return false;
73+
}
74+
75+
if (ret) {
76+
Serial.println("\t<err> Unable to set display format");
77+
return false;
78+
}
79+
80+
81+
display_orientation orientation;
82+
switch(rotation){
83+
case 0:
84+
orientation = DISPLAY_ORIENTATION_NORMAL;
85+
break;
86+
case 1:
87+
orientation = DISPLAY_ORIENTATION_ROTATED_90;
88+
break;
89+
case 2:
90+
orientation = DISPLAY_ORIENTATION_ROTATED_180;
91+
break;
92+
case 3:
93+
orientation = DISPLAY_ORIENTATION_ROTATED_270;
94+
break;
95+
default:
96+
orientation = DISPLAY_ORIENTATION_NORMAL;
97+
break;
98+
}
99+
//Rotation not supported
100+
//Serial.print("Orientation: "); Serial.println(orientation);
101+
ret = display_set_orientation(this->gdev, orientation);
102+
Serial.println(ret);
103+
if(ret) {
104+
Serial.println("\t<err> Failed to set display rotation");
105+
//return false;
106+
}
107+
108+
display_get_capabilities(this->gdev, &capabilities);
109+
110+
printk("- Capabilities:\n");
111+
printk(" x_resolution = %u, y_resolution = %u\n, supported_pixel_formats = %u\n"
112+
" current_pixel_format = %u, current_orientation = %u\n",
113+
capabilities.x_resolution, capabilities.y_resolution,
114+
capabilities.supported_pixel_formats, capabilities.current_pixel_format,
115+
capabilities.current_orientation);
116+
117+
_height = capabilities.y_resolution;
118+
_width = capabilities.x_resolution;
119+
120+
return true;
121+
}
122+
123+
int Display::write8(const uint16_t x,
124+
const uint16_t y,
125+
const void *buf) {
126+
127+
128+
// printk("Display::write8(%u %u %p) %p %x\n", x, y, buf,
129+
// &((struct display_driver_api *)gdev->api)->write, *((uint32_t*)(&((struct display_driver_api *)gdev->api)->write)));
130+
131+
return display_write(this->gdev, x, y, this->buf_desc, buf);
132+
133+
}
134+
135+
void Display::setFrameDesc(uint16_t w, uint16_t h, uint16_t pitch, uint32_t buf_size) {
136+
this->buf_desc->buf_size = buf_size;
137+
this->buf_desc->width = w; /** Number of pixels between consecutive rows in the data buffer */
138+
this->buf_desc->height = h; /** Data buffer row width in pixels */
139+
this->buf_desc->pitch = pitch; /** Data buffer row height in pixels */
140+
141+
}
142+
143+
void Display::startFrameBuffering() {
144+
145+
this->buf_desc->frame_incomplete = false;
146+
147+
}
148+
149+
void Display::endFrameBuffering() {
150+
151+
this->buf_desc->frame_incomplete = true;
152+
153+
}
154+
155+
int Display::setBlanking(bool on) {
156+
int ret = 0;
157+
if(on) {
158+
ret = display_blanking_on(this->gdev);
159+
} else {
160+
ret = display_blanking_off(this->gdev);
161+
}
162+
if(ret < 0) {
163+
return false;
164+
}
165+
return true;
166+
}
167+
168+
void* Display::getFrameBuffer() {
169+
void* fb = display_get_framebuffer(this->gdev);
170+
return fb;
171+
}
172+
173+
#endif //__ZEPHYR__

src/GigaDisplay.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2025 Arduino SA
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
#ifndef __GIGA_DISPLAY_H__
19+
#define __GIGA_DISPLAY_H__
20+
21+
#ifdef __ZEPHYR__
22+
23+
#include "Arduino.h"
24+
/**
25+
* @enum DisplayPixelFormat
26+
* @brief Display pixel format enumeration.
27+
*
28+
* The different formats use different numbers of bits per pixel:
29+
* - Grayscale (8-bit)
30+
* - RGB565 (16-bit)
31+
*/
32+
enum DisplayPixelFormat {
33+
DISPLAY_RGB565, /**< RGB565 format (16-bit). */
34+
DISPLAY_RGB888 /**< RGB888 format (16-bit). */
35+
};
36+
37+
// Color definitions
38+
39+
#define RGB565_BLACK 0x0000 /* 0, 0, 0 */
40+
#define RGB565_NAVY 0x000F /* 0, 0, 128 */
41+
#define RGB565_DARKGREEN 0x03E0 /* 0, 128, 0 */
42+
#define RGB565_DARKCYAN 0x03EF /* 0, 128, 128 */
43+
#define RGB565_MAROON 0x7800 /* 128, 0, 0 */
44+
#define RGB565_PURPLE 0x780F /* 128, 0, 128 */
45+
#define RGB565_OLIVE 0x7BE0 /* 128, 128, 0 */
46+
#define RGB565_LIGHTGREY 0xC618 /* 192, 192, 192 */
47+
#define RGB565_DARKGREY 0x7BEF /* 128, 128, 128 */
48+
#define RGB565_BLUE 0x001F /* 0, 0, 255 */
49+
#define RGB565_GREEN 0x07E0 /* 0, 255, 0 */
50+
#define RGB565_CYAN 0x07FF /* 0, 255, 255 */
51+
#define RGB565_RED 0xF800 /* 255, 0, 0 */
52+
#define RGB565_MAGENTA 0xF81F /* 255, 0, 255 */
53+
#define RGB565_YELLOW 0xFFE0 /* 255, 255, 0 */
54+
#define RGB565_WHITE 0xFFFF /* 255, 255, 255 */
55+
#define RGB565_ORANGE 0xFD20 /* 255, 165, 0 */
56+
#define RGB565_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
57+
#define RGB565_PINK 0xF81F
58+
59+
/**
60+
* @class Display
61+
* @brief The main class for controlling a camera.
62+
*/
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+
71+
public:
72+
/**
73+
* @brief Construct a new Camera object.
74+
*/
75+
Display();
76+
77+
/**
78+
* @brief Initialize the display
79+
*/
80+
bool begin(DisplayPixelFormat pixformat = DISPLAY_RGB565, int rotation = 0);
81+
82+
/**
83+
* @brief a frame.
84+
*
85+
* @param fb Reference to a FrameBuffer object to store the frame data.
86+
* @param timeout Time in milliseconds to wait for a frame (default: 5000).
87+
* @return true if the frame is successfully captured, otherwise false.
88+
*/
89+
//bool grabFrame(FrameBuffer &fb, uint32_t timeout = 5000);
90+
91+
92+
/**
93+
*
94+
*
95+
*
96+
*
97+
*/
98+
int write8(const uint16_t x,
99+
const uint16_t y,
100+
const void *buf);
101+
102+
void setFrameDesc(uint16_t w, uint16_t h, uint16_t pitch, uint32_t buf_size);
103+
void startFrameBuffering();
104+
void endFrameBuffering();
105+
106+
int setBlanking(bool on);
107+
108+
void* getFrameBuffer();
109+
110+
int16_t width(void) { return _width; }
111+
int16_t height(void) { return _height; }
112+
113+
114+
};
115+
116+
#endif // __GIGA_DISPLAY_H__
117+
118+
#endif // __ZEPHYR__

src/GigaDisplayRGB.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class GigaDisplayBacklight {
4747
}
4848
private:
4949
mbed::Ticker* ticker;
50-
mbed::DigitalOut* pin;
50+
mbed::DigitalOut* pin
5151
int intensity;
5252
};
5353
#endif //MBED
54+
5455
#endif

0 commit comments

Comments
 (0)