Skip to content

Commit 0f7e9b4

Browse files
committed
ugfx/driver/ST7789: code clean up
1 parent 3d72757 commit 0f7e9b4

File tree

1 file changed

+100
-89
lines changed

1 file changed

+100
-89
lines changed

components/ugfx/drivers/gdisp/ST7789/gdisp_lld_ST7789.c

Lines changed: 100 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#if GFX_USE_GDISP
1111

12-
#if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
12+
#if defined(GDISP_SCREEN_WIDTH) || defined(GDISP_SCREEN_HEIGHT)
1313
#if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
1414
#warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
1515
#elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
@@ -29,32 +29,30 @@
2929
/* Driver local definitions. */
3030
/*===========================================================================*/
3131

32-
#ifndef GDISP_SCREEN_HEIGHT
33-
#define GDISP_SCREEN_HEIGHT ST7789_SCREEN_HEIGHT
34-
#endif
3532
#ifndef GDISP_SCREEN_WIDTH
3633
#define GDISP_SCREEN_WIDTH ST7789_SCREEN_WIDTH
3734
#endif
35+
#ifndef GDISP_SCREEN_HEIGHT
36+
#define GDISP_SCREEN_HEIGHT ST7789_SCREEN_HEIGHT
37+
#endif
3838
#ifndef GDISP_INITIAL_CONTRAST
3939
#define GDISP_INITIAL_CONTRAST 100
4040
#endif
4141
#ifndef GDISP_INITIAL_BACKLIGHT
42-
#define GDISP_INITIAL_BACKLIGHT 255
42+
#define GDISP_INITIAL_BACKLIGHT 0
4343
#endif
4444

45-
#define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
45+
#define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER << 0)
4646

4747
#include "ST7789.h"
4848

4949
LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
50-
g->priv = gfxAlloc(GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH * 2);
50+
g->priv = gfxAlloc(GDISP_SCREEN_WIDTH*GDISP_SCREEN_HEIGHT*2);
5151
if (g->priv == NULL) {
5252
gfxHalt("GDISP ST7789: Failed to allocate private memory");
5353
}
5454

55-
for (int i=0; i<GDISP_SCREEN_HEIGHT*GDISP_SCREEN_WIDTH*2; i++) {
56-
*((uint8_t *)g->priv + i) = 0x00;
57-
}
55+
memset(g->priv, 0x00, GDISP_SCREEN_WIDTH*GDISP_SCREEN_HEIGHT*2);
5856

5957
// Initialise the board interface
6058
init_board(g);
@@ -94,7 +92,7 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
9492
write_data(g, 0xA1);
9593
write_cmd(g, ST7789_INVON); // 12: Invert display, no args, no delay
9694
write_cmd(g, ST7789_MADCTL); // 13: Memory access control (directions), 1 arg:
97-
write_data(g, 0x70);
95+
write_data(g, 0x00);
9896
write_cmd(g, ST7789_COLMOD); // 14: Set color mode, 1 arg, no delay:
9997
write_data(g, 0x05);
10098
write_cmd(g, ST7789_PVGAMCTRL); // 15: Positive voltage gamma control, 14 args, no delay:
@@ -129,26 +127,27 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
129127
write_data(g, 0x23);
130128
write_cmd(g, ST7789_NORON); // 17: Normal display on, no args, no delay
131129
write_cmd(g, ST7789_CASET); // 18: Set column address, 4 args, no delay:
130+
write_data(g, 0x00);
131+
write_data(g, 0x34);
132+
write_data(g, 0x00);
133+
write_data(g, 0xBA);
134+
write_cmd(g, ST7789_RASET); // 19: Set row address, 4 args, no delay:
132135
write_data(g, 0x00);
133136
write_data(g, 0x28);
134137
write_data(g, 0x01);
135138
write_data(g, 0x17);
136-
write_cmd(g, ST7789_RASET); // 19: Set row address, 4 args, no delay:
137-
write_data(g, 0x00);
138-
write_data(g, 0x35);
139-
write_data(g, 0x00);
140-
write_data(g, 0xBB);
141139
write_cmd(g, ST7789_RAMWR); // 20: Set write ram, N args, no delay:
142-
write_buff(g, (uint8_t *)g->priv, GDISP_SCREEN_HEIGHT*GDISP_SCREEN_WIDTH*2);
140+
write_buff(g, (uint8_t *)g->priv, GDISP_SCREEN_WIDTH*GDISP_SCREEN_HEIGHT*2);
143141
write_cmd(g, ST7789_DISPON); // 21: Main screen turn on, no args, no delay
144142

145143
/* Initialise the GDISP structure */
146-
g->g.Width = GDISP_SCREEN_HEIGHT;
147-
g->g.Height = GDISP_SCREEN_WIDTH;
144+
g->g.Width = GDISP_SCREEN_WIDTH;
145+
g->g.Height = GDISP_SCREEN_HEIGHT;
148146
g->g.Orientation = GDISP_ROTATE_0;
149147
g->g.Powermode = powerOn;
150148
g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
151149
g->g.Contrast = GDISP_INITIAL_CONTRAST;
150+
152151
return TRUE;
153152
}
154153

@@ -163,120 +162,132 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
163162
#endif
164163

165164
#if GDISP_HARDWARE_STREAM_WRITE
166-
static int16_t stream_write_x = 0;
167-
static int16_t stream_write_cx = 0;
168-
static int16_t stream_write_y = 0;
169-
static int16_t stream_write_cy = 0;
165+
static uint8_t write_x = 0;
166+
static uint8_t write_cx = 0;
167+
static uint8_t write_y = 0;
168+
static uint8_t write_cy = 0;
170169
LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
171-
stream_write_x = g->p.x;
172-
stream_write_cx = g->p.cx;
173-
stream_write_y = g->p.y;
174-
stream_write_cy = g->p.cy;
170+
write_x = g->p.x;
171+
write_cx = g->p.cx;
172+
write_y = g->p.y;
173+
write_cy = g->p.cy;
175174
}
176175
LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
176+
uint16_t pos = 0;
177+
switch (g->g.Orientation) {
178+
case GDISP_ROTATE_0:
179+
default:
180+
pos = write_y * g->g.Width + write_x;
181+
break;
182+
case GDISP_ROTATE_90:
183+
pos = (g->g.Width - write_x - 1) * g->g.Height + write_y;
184+
break;
185+
case GDISP_ROTATE_180:
186+
pos = (g->g.Height - write_y - 1) * g->g.Width + (g->g.Width - write_x - 1);
187+
break;
188+
case GDISP_ROTATE_270:
189+
pos = write_x * g->g.Height + (g->g.Height - write_y - 1);
190+
break;
191+
}
177192
LLDCOLOR_TYPE c = gdispColor2Native(g->p.color);
178-
*((uint8_t *)g->priv + (stream_write_x + stream_write_y * g->g.Width) * 2 + 0) = c >> 8;
179-
*((uint8_t *)g->priv + (stream_write_x + stream_write_y * g->g.Width) * 2 + 1) = c;
180-
stream_write_x++;
181-
if (--stream_write_cx <= 0) {
182-
stream_write_x = g->p.x;
183-
stream_write_cx = g->p.cx;
184-
stream_write_y++;
185-
if (--stream_write_cy <= 0) {
186-
stream_write_y = g->p.y;
187-
stream_write_cy = g->p.cy;
193+
*((uint8_t *)g->priv + pos * 2 + 0) = c >> 8;
194+
*((uint8_t *)g->priv + pos * 2 + 1) = c;
195+
write_x++;
196+
if (--write_cx == 0) {
197+
write_x = g->p.x;
198+
write_cx = g->p.cx;
199+
write_y++;
200+
if (--write_cy == 0) {
201+
write_y = g->p.y;
202+
write_cy = g->p.cy;
188203
}
189204
}
190205
}
191206
LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
192-
stream_write_x = 0;
193-
stream_write_cx = 0;
194-
stream_write_y = 0;
195-
stream_write_cy = 0;
196207
g->flags |= GDISP_FLG_NEEDFLUSH;
197208
}
198209
#endif
199210

200211
#if GDISP_HARDWARE_STREAM_READ
201-
static int16_t stream_read_x = 0;
202-
static int16_t stream_read_cx = 0;
203-
static int16_t stream_read_y = 0;
204-
static int16_t stream_read_cy = 0;
212+
static uint8_t read_x = 0;
213+
static uint8_t read_cx = 0;
214+
static uint8_t read_y = 0;
215+
static uint8_t read_cy = 0;
205216
LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
206-
stream_read_x = g->p.x;
207-
stream_read_cx = g->p.cx;
208-
stream_read_y = g->p.y;
209-
stream_read_cy = g->p.cy;
217+
read_x = g->p.x;
218+
read_cx = g->p.cx;
219+
read_y = g->p.y;
220+
read_cy = g->p.cy;
210221
}
211222
LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
212-
LLDCOLOR_TYPE c = (*((uint8_t *)g->priv + (stream_read_x + stream_read_y * g->g.Width) * 2 + 0) << 8)
213-
| (*((uint8_t *)g->priv + (stream_read_x + stream_read_y * g->g.Width) * 2 + 1));
214-
stream_read_x++;
215-
if (--stream_read_cx <= 0) {
216-
stream_read_x = g->p.x;
217-
stream_read_cx = g->p.cx;
218-
stream_read_y++;
219-
if (--stream_read_cy <= 0) {
220-
stream_read_y = g->p.y;
221-
stream_read_cy = g->p.cy;
223+
uint16_t pos = 0;
224+
switch (g->g.Orientation) {
225+
case GDISP_ROTATE_0:
226+
default:
227+
pos = read_y * g->g.Width + read_x;
228+
break;
229+
case GDISP_ROTATE_90:
230+
pos = (g->g.Width - read_x - 1) * g->g.Height + read_y;
231+
break;
232+
case GDISP_ROTATE_180:
233+
pos = (g->g.Height - read_y - 1) * g->g.Width + (g->g.Width - read_x - 1);
234+
break;
235+
case GDISP_ROTATE_270:
236+
pos = read_x * g->g.Height + (g->g.Height - read_y - 1);
237+
break;
238+
}
239+
LLDCOLOR_TYPE c = (*((uint8_t *)g->priv + pos * 2 + 0) << 8)
240+
| (*((uint8_t *)g->priv + pos * 2 + 1));
241+
read_x++;
242+
if (--read_cx == 0) {
243+
read_x = g->p.x;
244+
read_cx = g->p.cx;
245+
read_y++;
246+
if (--read_cy == 0) {
247+
read_y = g->p.y;
248+
read_cy = g->p.cy;
222249
}
223250
}
224251
return c;
225252
}
226-
LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
227-
stream_read_x = 0;
228-
stream_read_cx = 0;
229-
stream_read_y = 0;
230-
stream_read_cy = 0;
231-
}
253+
LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {}
232254
#endif
233255

234256
#if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
235257
LLDSPEC void gdisp_lld_control(GDisplay *g) {
236-
switch(g->p.x) {
237-
case GDISP_CONTROL_POWER:
238-
if (g->g.Powermode == (powermode_t)g->p.ptr)
239-
return;
240-
switch((powermode_t)g->p.ptr) {
241-
case powerOff:
242-
case powerSleep:
243-
case powerDeepSleep:
244-
case powerOn:
245-
default:
246-
return;
247-
}
248-
g->g.Powermode = (powermode_t)g->p.ptr;
249-
return;
258+
switch (g->p.x) {
250259
case GDISP_CONTROL_ORIENTATION:
251-
if (g->g.Orientation == (orientation_t)g->p.ptr)
260+
if (g->g.Orientation == (orientation_t)g->p.ptr) {
252261
return;
253-
switch((orientation_t)g->p.ptr) {
262+
}
263+
switch ((orientation_t)g->p.ptr) {
254264
case GDISP_ROTATE_0:
255-
g->g.Height = GDISP_SCREEN_HEIGHT;
256265
g->g.Width = GDISP_SCREEN_WIDTH;
266+
g->g.Height = GDISP_SCREEN_HEIGHT;
257267
break;
258268
case GDISP_ROTATE_90:
259-
g->g.Height = GDISP_SCREEN_WIDTH;
260269
g->g.Width = GDISP_SCREEN_HEIGHT;
270+
g->g.Height = GDISP_SCREEN_WIDTH;
261271
break;
262272
case GDISP_ROTATE_180:
263-
g->g.Height = GDISP_SCREEN_HEIGHT;
264273
g->g.Width = GDISP_SCREEN_WIDTH;
274+
g->g.Height = GDISP_SCREEN_HEIGHT;
265275
break;
266276
case GDISP_ROTATE_270:
267-
g->g.Height = GDISP_SCREEN_WIDTH;
268277
g->g.Width = GDISP_SCREEN_HEIGHT;
278+
g->g.Height = GDISP_SCREEN_WIDTH;
269279
break;
270280
default:
271281
return;
272282
}
273283
g->g.Orientation = (orientation_t)g->p.ptr;
274284
return;
275285
case GDISP_CONTROL_BACKLIGHT:
276-
if ((unsigned)g->p.ptr > 255)
277-
g->p.ptr = (void *)255;
278-
g->g.Backlight = (unsigned)g->p.ptr;
279-
set_backlight(g, g->g.Backlight);
286+
if (g->g.Backlight == (uint32_t)g->p.ptr) {
287+
return;
288+
}
289+
set_backlight(g, (uint32_t)g->p.ptr);
290+
g->g.Backlight = (uint32_t)g->p.ptr;
280291
return;
281292
default:
282293
return;

0 commit comments

Comments
 (0)