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_ERGODOX
13 #include "drivers/gdisp/st7565ergodox/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 0
31 #ifndef GDISP_INITIAL_BACKLIGHT
32 #define GDISP_INITIAL_BACKLIGHT 100
35 #define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
37 #include "drivers/gdisp/st7565ergodox/st7565.h"
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_DISPLAY_OFF);
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_START_LINE | 0);
124 write_cmd(g, ST7565_RESISTOR_RATIO | 0x6);
126 // turn on voltage converter (VC=1, VR=0, VF=0)
127 write_cmd(g, ST7565_POWER_CONTROL | 0x04);
131 // turn on voltage regulator (VC=1, VR=1, VF=0)
132 write_cmd(g, ST7565_POWER_CONTROL | 0x06);
136 // turn on voltage follower (VC=1, VR=1, VF=1)
137 write_cmd(g, ST7565_POWER_CONTROL | 0x07);
142 write_cmd(g, ST7565_COM_SCAN);
143 write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST*64/101);
144 //write_cmd2(g, ST7565_CONTRAST, 0);
145 write_cmd(g, ST7565_DISPLAY_ON);
146 write_cmd(g, ST7565_ALLON_NORMAL);
147 write_cmd(g, ST7565_INVERT_DISPLAY);
149 write_cmd(g, ST7565_RMW);
158 /* Initialise the GDISP structure */
159 g->g.Width = GDISP_SCREEN_WIDTH;
160 g->g.Height = GDISP_SCREEN_HEIGHT;
161 g->g.Orientation = GDISP_ROTATE_0;
162 g->g.Powermode = powerOn;
163 g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
164 g->g.Contrast = GDISP_INITIAL_CONTRAST;
168 #if GDISP_HARDWARE_FLUSH
169 LLDSPEC void gdisp_lld_flush(GDisplay *g) {
172 // Don't flush if we don't need it.
173 if (!(g->flags & GDISP_FLG_NEEDFLUSH))
178 unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0);
179 for (p = 0; p < 4; p++) {
180 write_cmd(g, ST7565_PAGE | (p + dstOffset));
181 write_cmd(g, ST7565_COLUMN_MSB | 0);
182 write_cmd(g, ST7565_COLUMN_LSB | 0);
183 write_cmd(g, ST7565_RMW);
186 write_data(g, RAM(g) + (p*GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH);
189 unsigned line = (PRIV(g)->buffer2 ? 32 : 0);
190 write_cmd(g, ST7565_START_LINE | line);
192 PRIV(g)->buffer2 = !PRIV(g)->buffer2;
195 g->flags &= ~GDISP_FLG_NEEDFLUSH;
199 #if GDISP_HARDWARE_DRAWPIXEL
200 LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
203 switch(g->g.Orientation) {
209 case GDISP_ROTATE_90:
211 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
213 case GDISP_ROTATE_180:
214 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
215 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
217 case GDISP_ROTATE_270:
218 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
222 if (gdispColor2Native(g->p.color) != Black)
223 RAM(g)[xyaddr(x, y)] |= xybit(y);
225 RAM(g)[xyaddr(x, y)] &= ~xybit(y);
226 g->flags |= GDISP_FLG_NEEDFLUSH;
230 #if GDISP_HARDWARE_PIXELREAD
231 LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
234 switch(g->g.Orientation) {
240 case GDISP_ROTATE_90:
242 y = GDISP_SCREEN_HEIGHT-1 - g->p.x;
244 case GDISP_ROTATE_180:
245 x = GDISP_SCREEN_WIDTH-1 - g->p.x;
246 y = GDISP_SCREEN_HEIGHT-1 - g->p.y;
248 case GDISP_ROTATE_270:
249 x = GDISP_SCREEN_HEIGHT-1 - g->p.y;
253 return (RAM(g)[xyaddr(x, y)] & xybit(y)) ? White : Black;
257 LLDSPEC void gdisp_lld_blit_area(GDisplay *g) {
258 uint8_t* buffer = (uint8_t*)g->p.ptr;
259 int linelength = g->p.cx;
260 for (int i = 0; i < g->p.cy; i++) {
261 unsigned dstx = g->p.x;
262 unsigned dsty = g->p.y + i;
263 unsigned srcx = g->p.x1;
264 unsigned srcy = g->p.y1 + i;
265 unsigned srcbit = srcy * g->p.x2 + srcx;
266 for(int j=0; j < linelength; j++) {
267 uint8_t src = buffer[srcbit / 8];
268 uint8_t bit = 7-(srcbit % 8);
269 uint8_t bitset = (src >> bit) & 1;
270 uint8_t* dst = &(RAM(g)[xyaddr(dstx, dsty)]);
275 *dst &= ~xybit(dsty);
283 #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
284 LLDSPEC void gdisp_lld_control(GDisplay *g) {
286 case GDISP_CONTROL_POWER:
287 if (g->g.Powermode == (powermode_t)g->p.ptr)
289 switch((powermode_t)g->p.ptr) {
295 write_cmd(g, ST7565_DISPLAY_OFF);
302 write_cmd(g, ST7565_DISPLAY_ON);
309 g->g.Powermode = (powermode_t)g->p.ptr;
312 case GDISP_CONTROL_ORIENTATION:
313 if (g->g.Orientation == (orientation_t)g->p.ptr)
315 switch((orientation_t)g->p.ptr) {
316 /* Rotation is handled by the drawing routines */
318 case GDISP_ROTATE_180:
319 g->g.Height = GDISP_SCREEN_HEIGHT;
320 g->g.Width = GDISP_SCREEN_WIDTH;
322 case GDISP_ROTATE_90:
323 case GDISP_ROTATE_270:
324 g->g.Height = GDISP_SCREEN_WIDTH;
325 g->g.Width = GDISP_SCREEN_HEIGHT;
330 g->g.Orientation = (orientation_t)g->p.ptr;
333 case GDISP_CONTROL_CONTRAST:
334 if ((unsigned)g->p.ptr > 100)
335 g->p.ptr = (void *)100;
338 write_cmd2(g, ST7565_CONTRAST, ((((unsigned)g->p.ptr)<<6)/101) & 0x3F);
341 g->g.Contrast = (unsigned)g->p.ptr;
345 #endif // GDISP_NEED_CONTROL
347 #endif // GFX_USE_GDISP