2 * This file is subject to the terms of the GFX License. If a copy of
3 * the license was not distributed with this file, you can obtain one at:
5 * http://ugfx.org/license.html
12 #define GDISP_DRIVER_VMT GDISPVMT_ST7565_QMK
13 #include "gdisp_lld_config.h"
14 #include "src/gdisp/gdisp_driver.h"
16 #include "board_ST7565.h"
18 /*===========================================================================*/
19 /* Driver local definitions. */
20 /*===========================================================================*/
22 #ifndef GDISP_SCREEN_HEIGHT
23 #define GDISP_SCREEN_HEIGHT 32
25 #ifndef GDISP_SCREEN_WIDTH
26 #define GDISP_SCREEN_WIDTH 128
28 #ifndef GDISP_INITIAL_CONTRAST
29 #define GDISP_INITIAL_CONTRAST 35
31 #ifndef GDISP_INITIAL_BACKLIGHT
32 #define GDISP_INITIAL_BACKLIGHT 100
35 #define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
39 /*===========================================================================*/
40 /* Driver config defaults for backward compatibility. */
41 /*===========================================================================*/
42 #ifndef ST7565_LCD_BIAS
43 #define ST7565_LCD_BIAS ST7565_LCD_BIAS_7
46 #define ST7565_ADC ST7565_ADC_NORMAL
48 #ifndef ST7565_COM_SCAN
49 #define ST7565_COM_SCAN ST7565_COM_SCAN_INC
51 #ifndef ST7565_PAGE_ORDER
52 #define ST7565_PAGE_ORDER 0,1,2,3
55 /*===========================================================================*/
56 /* Driver local functions. */
57 /*===========================================================================*/
63 uint8_t ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8];
66 // Some common routines and macros
67 #define PRIV(g) ((PrivData*)g->priv)
68 #define RAM(g) (PRIV(g)->ram)
70 static GFXINLINE void write_cmd(GDisplay* g, uint8_t cmd) {
71 PRIV(g)->data[PRIV(g)->data_pos++] = cmd;
74 static GFXINLINE void flush_cmd(GDisplay* g) {
75 write_data(g, PRIV(g)->data, PRIV(g)->data_pos);
76 PRIV(g)->data_pos = 0;
79 #define write_cmd2(g, cmd1, cmd2) { write_cmd(g, cmd1); write_cmd(g, cmd2); }
80 #define write_cmd3(g, cmd1, cmd2, cmd3) { write_cmd(g, cmd1); write_cmd(g, cmd2); write_cmd(g, cmd3); }
82 // Some common routines and macros
83 #define delay(us) gfxSleepMicroseconds(us)
84 #define delay_ms(ms) gfxSleepMilliseconds(ms)
86 #define xyaddr(x, y) ((x) + ((y)>>3)*GDISP_SCREEN_WIDTH)
87 #define xybit(y) (1<<((y)&7))
89 /*===========================================================================*/
90 /* Driver exported functions. */
91 /*===========================================================================*/
94 * As this controller can't update on a pixel boundary we need to maintain the
95 * the entire display surface in memory so that we can do the necessary bit
96 * operations. Fortunately it is a small display in monochrome.
97 * 64 * 128 / 8 = 1024 bytes.
100 LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
101 // The private area is the display surface.
102 g->priv = gfxAlloc(sizeof(PrivData));
103 PRIV(g)->buffer2 = false;
104 PRIV(g)->data_pos = 0;
106 // Initialise the board interface
110 setpin_reset(g, TRUE);
111 gfxSleepMilliseconds(20);
112 setpin_reset(g, FALSE);
113 gfxSleepMilliseconds(20);
117 write_cmd(g, ST7565_RESET);
118 write_cmd(g, ST7565_LCD_BIAS);
119 write_cmd(g, ST7565_ADC);
120 write_cmd(g, ST7565_COM_SCAN);
122 write_cmd(g, ST7565_RESISTOR_RATIO | 0x1);
123 write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST);
125 // turn on internal power supply (VC=1, VR=1, VF=1)
126 write_cmd(g, ST7565_POWER_CONTROL | 0x07);
128 write_cmd(g, ST7565_INVERT_DISPLAY);
129 write_cmd(g, ST7565_ALLON_NORMAL);
131 write_cmd(g, ST7565_START_LINE | 0);
132 write_cmd(g, ST7565_RMW);
141 /* Initialise the GDISP structure */
142 g->g.Width = GDISP_SCREEN_WIDTH;
143 g->g.Height = GDISP_SCREEN_HEIGHT;
144 g->g.Orientation = GDISP_ROTATE_0;
145 g->g.Powermode = powerOff;
146 g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
147 g->g.Contrast = GDISP_INITIAL_CONTRAST;
151 #if GDISP_HARDWARE_FLUSH
152 LLDSPEC void gdisp_lld_flush(GDisplay *g) {
155 // Don't flush if we don't need it.
156 if (!(g->flags & GDISP_FLG_NEEDFLUSH))
161 unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0);
162 for (p = 0; p < 4; p++) {
163 write_cmd(g, ST7565_PAGE | (p + dstOffset));
164 write_cmd(g, ST7565_COLUMN_MSB | 0);
165 write_cmd(g, ST7565_COLUMN_LSB | 0);
166 write_cmd(g, ST7565_RMW);
169 write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
172 unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
173 write_cmd(g, ST7565_START_LINE | line);
175 PRIV(g)->buffer2 = !PRIV(g)->buffer2;
178 g->flags &= ~GDISP_FLG_NEEDFLUSH;
182 #if GDISP_HARDWARE_DRAWPIXEL
183 LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
186 switch(g->g.Orientation) {
192 case GDISP_ROTATE_90:
194 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
196 case GDISP_ROTATE_180:
197 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
198 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
200 case GDISP_ROTATE_270:
201 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
205 if (gdispColor2Native(g->p.color) != Black)
206 RAM(g)[xyaddr(x, y)] |= xybit(y);
208 RAM(g)[xyaddr(x, y)] &= ~xybit(y);
209 g->flags |= GDISP_FLG_NEEDFLUSH;
213 #if GDISP_HARDWARE_PIXELREAD
214 LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
217 switch(g->g.Orientation) {
223 case GDISP_ROTATE_90:
225 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
227 case GDISP_ROTATE_180:
228 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
229 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
231 case GDISP_ROTATE_270:
232 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
236 return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
240 LLDSPEC void gdisp_lld_blit_area(GDisplay *g) {
241 uint8_t* buffer = (uint8_t*)g->p.ptr;
242 int linelength = g->p.cx;
243 for (int i = 0; i < g->p.cy; i++) {
244 unsigned dstx = g->p.x;
245 unsigned dsty = g->p.y + i;
246 unsigned srcx = g->p.x1;
247 unsigned srcy = g->p.y1 + i;
248 unsigned srcbit = srcy * g->p.x2 + srcx;
249 for(int j=0; j < linelength; j++) {
250 uint8_t src = buffer[srcbit / 8];
251 uint8_t bit = 7-(srcbit % 8);
252 uint8_t bitset = (src >> bit) & 1;
253 uint8_t* dst = &(RAM(g)[xyaddr(dstx, dsty)]);
258 *dst &= ~xybit(dsty);
264 g->flags |= GDISP_FLG_NEEDFLUSH;
267 #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
268 LLDSPEC void gdisp_lld_control(GDisplay *g) {
270 case GDISP_CONTROL_POWER:
271 if (g->g.Powermode == (powermode_t)g->p.ptr)
273 switch((powermode_t)g->p.ptr) {
279 write_cmd(g, ST7565_DISPLAY_OFF);
286 write_cmd(g, ST7565_DISPLAY_ON);
293 g->g.Powermode = (powermode_t)g->p.ptr;
296 case GDISP_CONTROL_ORIENTATION:
297 if (g->g.Orientation == (orientation_t)g->p.ptr)
299 switch((orientation_t)g->p.ptr) {
300 /* Rotation is handled by the drawing routines */
302 case GDISP_ROTATE_180:
303 g->g.Height = GDISP_SCREEN_HEIGHT;
304 g->g.Width = GDISP_SCREEN_WIDTH;
306 case GDISP_ROTATE_90:
307 case GDISP_ROTATE_270:
308 g->g.Height = GDISP_SCREEN_WIDTH;
309 g->g.Width = GDISP_SCREEN_HEIGHT;
314 g->g.Orientation = (orientation_t)g->p.ptr;
317 case GDISP_CONTROL_CONTRAST:
318 g->g.Contrast = (unsigned)g->p.ptr & 63;
321 write_cmd2(g, ST7565_CONTRAST, g->g.Contrast);
327 #endif // GDISP_NEED_CONTROL
329 #endif // GFX_USE_GDISP