Skip to content

Commit 953b7d4

Browse files
committed
ugfx/driver/ST7735: code clean up
1 parent 0f7e9b4 commit 953b7d4

File tree

1 file changed

+99
-88
lines changed

1 file changed

+99
-88
lines changed

components/ugfx/drivers/gdisp/ST7735/gdisp_lld_ST7735.c

Lines changed: 99 additions & 88 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 ST7735_SCREEN_HEIGHT
34-
#endif
3532
#ifndef GDISP_SCREEN_WIDTH
3633
#define GDISP_SCREEN_WIDTH ST7735_SCREEN_WIDTH
3734
#endif
35+
#ifndef GDISP_SCREEN_HEIGHT
36+
#define GDISP_SCREEN_HEIGHT ST7735_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 "ST7735.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 ST7735: 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);
@@ -105,7 +103,7 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
105103
write_data(g, 0x06);
106104
write_cmd(g, ST7735_INVON); // 13: Invert display, no args, no delay
107105
write_cmd(g, ST7735_MADCTL); // 14: Memory access control (directions), 1 arg:
108-
write_data(g, 0x68);
106+
write_data(g, 0xC8);
109107
write_cmd(g, ST7735_COLMOD); // 15: Set color mode, 1 arg, no delay:
110108
write_data(g, 0x05);
111109
write_cmd(g, ST7735_GAMCTRP1); // 16: Magical unicorn dust, 16 args, no delay:
@@ -145,25 +143,26 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
145143
write_cmd(g, ST7735_NORON); // 18: Normal display on, no args, no delay
146144
write_cmd(g, ST7735_CASET); // 19: Set column address, 4 args, no delay:
147145
write_data(g, 0x00);
148-
write_data(g, 0x01);
146+
write_data(g, 0x1A);
149147
write_data(g, 0x00);
150-
write_data(g, 0xA0);
148+
write_data(g, 0x69);
151149
write_cmd(g, ST7735_RASET); // 20: Set row address, 4 args, no delay:
152150
write_data(g, 0x00);
153-
write_data(g, 0x1A);
151+
write_data(g, 0x01);
154152
write_data(g, 0x00);
155-
write_data(g, 0x69);
153+
write_data(g, 0xA0);
156154
write_cmd(g, ST7735_RAMWR); // 21: Set write ram, N args, no delay:
157-
write_buff(g, (uint8_t *)g->priv, GDISP_SCREEN_HEIGHT*GDISP_SCREEN_WIDTH*2);
155+
write_buff(g, (uint8_t *)g->priv, GDISP_SCREEN_WIDTH*GDISP_SCREEN_HEIGHT*2);
158156
write_cmd(g, ST7735_DISPON); // 22: Main screen turn on, no args, no delay
159157

160158
/* Initialise the GDISP structure */
161-
g->g.Width = GDISP_SCREEN_HEIGHT;
162-
g->g.Height = GDISP_SCREEN_WIDTH;
159+
g->g.Width = GDISP_SCREEN_WIDTH;
160+
g->g.Height = GDISP_SCREEN_HEIGHT;
163161
g->g.Orientation = GDISP_ROTATE_0;
164162
g->g.Powermode = powerOn;
165163
g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
166164
g->g.Contrast = GDISP_INITIAL_CONTRAST;
165+
167166
return TRUE;
168167
}
169168

@@ -178,120 +177,132 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
178177
#endif
179178

180179
#if GDISP_HARDWARE_STREAM_WRITE
181-
static int16_t stream_write_x = 0;
182-
static int16_t stream_write_cx = 0;
183-
static int16_t stream_write_y = 0;
184-
static int16_t stream_write_cy = 0;
180+
static uint8_t write_x = 0;
181+
static uint8_t write_cx = 0;
182+
static uint8_t write_y = 0;
183+
static uint8_t write_cy = 0;
185184
LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
186-
stream_write_x = g->p.x;
187-
stream_write_cx = g->p.cx;
188-
stream_write_y = g->p.y;
189-
stream_write_cy = g->p.cy;
185+
write_x = g->p.x;
186+
write_cx = g->p.cx;
187+
write_y = g->p.y;
188+
write_cy = g->p.cy;
190189
}
191190
LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
191+
uint16_t pos = 0;
192+
switch (g->g.Orientation) {
193+
case GDISP_ROTATE_0:
194+
default:
195+
pos = write_y * g->g.Width + write_x;
196+
break;
197+
case GDISP_ROTATE_90:
198+
pos = (g->g.Width - write_x - 1) * g->g.Height + write_y;
199+
break;
200+
case GDISP_ROTATE_180:
201+
pos = (g->g.Height - write_y - 1) * g->g.Width + (g->g.Width - write_x - 1);
202+
break;
203+
case GDISP_ROTATE_270:
204+
pos = write_x * g->g.Height + (g->g.Height - write_y - 1);
205+
break;
206+
}
192207
LLDCOLOR_TYPE c = gdispColor2Native(g->p.color);
193-
*((uint8_t *)g->priv + (stream_write_x + stream_write_y * g->g.Width) * 2 + 0) = c >> 8;
194-
*((uint8_t *)g->priv + (stream_write_x + stream_write_y * g->g.Width) * 2 + 1) = c;
195-
stream_write_x++;
196-
if (--stream_write_cx <= 0) {
197-
stream_write_x = g->p.x;
198-
stream_write_cx = g->p.cx;
199-
stream_write_y++;
200-
if (--stream_write_cy <= 0) {
201-
stream_write_y = g->p.y;
202-
stream_write_cy = g->p.cy;
208+
*((uint8_t *)g->priv + pos * 2 + 0) = c >> 8;
209+
*((uint8_t *)g->priv + pos * 2 + 1) = c;
210+
write_x++;
211+
if (--write_cx == 0) {
212+
write_x = g->p.x;
213+
write_cx = g->p.cx;
214+
write_y++;
215+
if (--write_cy == 0) {
216+
write_y = g->p.y;
217+
write_cy = g->p.cy;
203218
}
204219
}
205220
}
206221
LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
207-
stream_write_x = 0;
208-
stream_write_cx = 0;
209-
stream_write_y = 0;
210-
stream_write_cy = 0;
211222
g->flags |= GDISP_FLG_NEEDFLUSH;
212223
}
213224
#endif
214225

215226
#if GDISP_HARDWARE_STREAM_READ
216-
static int16_t stream_read_x = 0;
217-
static int16_t stream_read_cx = 0;
218-
static int16_t stream_read_y = 0;
219-
static int16_t stream_read_cy = 0;
227+
static uint8_t read_x = 0;
228+
static uint8_t read_cx = 0;
229+
static uint8_t read_y = 0;
230+
static uint8_t read_cy = 0;
220231
LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
221-
stream_read_x = g->p.x;
222-
stream_read_cx = g->p.cx;
223-
stream_read_y = g->p.y;
224-
stream_read_cy = g->p.cy;
232+
read_x = g->p.x;
233+
read_cx = g->p.cx;
234+
read_y = g->p.y;
235+
read_cy = g->p.cy;
225236
}
226237
LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
227-
LLDCOLOR_TYPE c = (*((uint8_t *)g->priv + (stream_read_x + stream_read_y * g->g.Width) * 2 + 0) << 8)
228-
| (*((uint8_t *)g->priv + (stream_read_x + stream_read_y * g->g.Width) * 2 + 1));
229-
stream_read_x++;
230-
if (--stream_read_cx <= 0) {
231-
stream_read_x = g->p.x;
232-
stream_read_cx = g->p.cx;
233-
stream_read_y++;
234-
if (--stream_read_cy <= 0) {
235-
stream_read_y = g->p.y;
236-
stream_read_cy = g->p.cy;
238+
uint16_t pos = 0;
239+
switch (g->g.Orientation) {
240+
case GDISP_ROTATE_0:
241+
default:
242+
pos = read_y * g->g.Width + read_x;
243+
break;
244+
case GDISP_ROTATE_90:
245+
pos = (g->g.Width - read_x - 1) * g->g.Height + read_y;
246+
break;
247+
case GDISP_ROTATE_180:
248+
pos = (g->g.Height - read_y - 1) * g->g.Width + (g->g.Width - read_x - 1);
249+
break;
250+
case GDISP_ROTATE_270:
251+
pos = read_x * g->g.Height + (g->g.Height - read_y - 1);
252+
break;
253+
}
254+
LLDCOLOR_TYPE c = (*((uint8_t *)g->priv + pos * 2 + 0) << 8)
255+
| (*((uint8_t *)g->priv + pos * 2 + 1));
256+
read_x++;
257+
if (--read_cx == 0) {
258+
read_x = g->p.x;
259+
read_cx = g->p.cx;
260+
read_y++;
261+
if (--read_cy == 0) {
262+
read_y = g->p.y;
263+
read_cy = g->p.cy;
237264
}
238265
}
239266
return c;
240267
}
241-
LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
242-
stream_read_x = 0;
243-
stream_read_cx = 0;
244-
stream_read_y = 0;
245-
stream_read_cy = 0;
246-
}
268+
LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {}
247269
#endif
248270

249271
#if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
250272
LLDSPEC void gdisp_lld_control(GDisplay *g) {
251-
switch(g->p.x) {
252-
case GDISP_CONTROL_POWER:
253-
if (g->g.Powermode == (powermode_t)g->p.ptr)
254-
return;
255-
switch((powermode_t)g->p.ptr) {
256-
case powerOff:
257-
case powerSleep:
258-
case powerDeepSleep:
259-
case powerOn:
260-
default:
261-
return;
262-
}
263-
g->g.Powermode = (powermode_t)g->p.ptr;
264-
return;
273+
switch (g->p.x) {
265274
case GDISP_CONTROL_ORIENTATION:
266-
if (g->g.Orientation == (orientation_t)g->p.ptr)
275+
if (g->g.Orientation == (orientation_t)g->p.ptr) {
267276
return;
268-
switch((orientation_t)g->p.ptr) {
277+
}
278+
switch ((orientation_t)g->p.ptr) {
269279
case GDISP_ROTATE_0:
270-
g->g.Height = GDISP_SCREEN_HEIGHT;
271280
g->g.Width = GDISP_SCREEN_WIDTH;
281+
g->g.Height = GDISP_SCREEN_HEIGHT;
272282
break;
273283
case GDISP_ROTATE_90:
274-
g->g.Height = GDISP_SCREEN_WIDTH;
275284
g->g.Width = GDISP_SCREEN_HEIGHT;
285+
g->g.Height = GDISP_SCREEN_WIDTH;
276286
break;
277287
case GDISP_ROTATE_180:
278-
g->g.Height = GDISP_SCREEN_HEIGHT;
279288
g->g.Width = GDISP_SCREEN_WIDTH;
289+
g->g.Height = GDISP_SCREEN_HEIGHT;
280290
break;
281291
case GDISP_ROTATE_270:
282-
g->g.Height = GDISP_SCREEN_WIDTH;
283292
g->g.Width = GDISP_SCREEN_HEIGHT;
293+
g->g.Height = GDISP_SCREEN_WIDTH;
284294
break;
285295
default:
286296
return;
287297
}
288298
g->g.Orientation = (orientation_t)g->p.ptr;
289299
return;
290300
case GDISP_CONTROL_BACKLIGHT:
291-
if ((unsigned)g->p.ptr > 255)
292-
g->p.ptr = (void *)255;
293-
g->g.Backlight = (unsigned)g->p.ptr;
294-
set_backlight(g, g->g.Backlight);
301+
if (g->g.Backlight == (uint32_t)g->p.ptr) {
302+
return;
303+
}
304+
set_backlight(g, (uint32_t)g->p.ptr);
305+
g->g.Backlight = (uint32_t)g->p.ptr;
295306
return;
296307
default:
297308
return;

0 commit comments

Comments
 (0)