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__
0 commit comments