/
Hardware_ks0108.cxx
488 lines (401 loc) · 11.8 KB
/
Hardware_ks0108.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//*******************************************************************************
//*
//* ks0108 Arduino drivers
//*
//* (C) by Mark Sproul
//* This code is derived from other works. Credit is given below
//* This is open source like the rest of the arduino source code
//*
//* this code is derived from the ks0108.cpp Arduino library
//*
//*******************************************************************************
//* ks0108.cpp - Arduino library support for ks0108 and compatable graphic LCDs
//* Copyright (c)2008 Michael Margolis All right reserved
//* mailto:memargolis@hotmail.com?subject=KS0108_Library
//*
//* The high level functions of this library are based on version 1.1 of ks0108 graphics routines
//* written and copyright by Fabian Maximilian Thiele. His sitelink is dead but
//* you can obtain a copy of his original work here:
//* http://www.scienceprog.com/wp-content/uploads/2007/07/glcd_ks0108.zip
//*
//* Code changes include conversion to an Arduino C++ library, rewriting the low level routines
//* to read busy status flag and support a wider range of displays, adding more flexibility
//* in port addressing and improvements in I/O speed. The interface has been made more Arduino friendly
//* and some convenience functions added.
//*
//* This library is distributed in the hope that it will be useful,
//* but WITHOUT ANY WARRANTY; without even the implied warranty of
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//*
//* Version: 1.0 - May 8 2008 - first release
//* Version: 1.1 - Nov 7 2008 - restructured low level code to adapt to panel speed
//* - moved chip and panel configuration into seperate header files
//* - added fixed width system font
//* Version: 2 - May 26 2009 - second release
//* - added support for Mega and Sanguino, improved panel speed tolerance, added bitmap support
//*
//*******************************************************************************
//* <MLS> is Mark Sproul msproul -at- jove.rutgers.edu
//* http://coewww.rutgers.edu/~msproul/
//*******************************************************************************
//* Detailed edit history
//*******************************************************************************
//* Jan 28, 2010 <MLS> Started on UnivGraphicsLib
//* Jun 20, 2011 <MLS> UGL working on KS0108 board on mega
//*******************************************************************************
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(__AVR__)
#include <avr/pgmspace.h>
#endif
#include "HardwareSerial.h"
#include "UnivGraphicsHW_Defs.h"
#include "UnivGraphicsLib.h"
#include "UnivGraphicsHardware.h" //* this is the routines that the implementer provides
#ifdef _GRAPHICS_HW_KS0108_
//* this slows down the drawing so you can debug things.
//#define GLCD_DEBUG
#define ksSOURCE
#include "ks0108.h"
//*******************************************************************************
//* private globals for ks0108
static lcdCoord gCoord;
static boolean gInverted;
#pragma mark -
#pragma mark ks0108 private routines
#ifndef __MWERKS__
#define EN_DELAY() asm volatile( "ldi r24, %0 \n\t" "subi r24, 0x1 \n\t" "and r24, r24 \n\t" "brne .-6 \n\t" ::"M" (EN_DELAY_VALUE) : "r24" )
#endif
static void KS0108_WriteCommand(uint8_t cmd, uint8_t chip);
void KS0108_WriteData(uint8_t data);
//***********************************************************************************
__inline__ void KS0108_SelectChip(uint8_t chip)
{
//static uint8_t prevchip;
if (gChipSelect[chip] & 1)
{
fastWriteHigh(CSEL1);
}
else
{
fastWriteLow(CSEL1);
}
if (gChipSelect[chip] & 2)
{
fastWriteHigh(CSEL2);
}
else
{
fastWriteLow(CSEL2);
}
}
//***********************************************************************************
void KS0108_WaitReady( uint8_t chip)
{
// wait until LCD busy bit goes to zero
KS0108_SelectChip(chip);
lcdDataDir(0x00);
fastWriteLow(D_I);
fastWriteHigh(R_W);
fastWriteHigh(EN);
EN_DELAY();
while(LCD_DATA_IN_HIGH & LCD_BUSY_FLAG)
{
; //do nothin
}
fastWriteLow(EN);
}
//***********************************************************************************
__inline__ void KS0108_Enable(void)
{
EN_DELAY();
fastWriteHigh(EN); // EN high level width min 450 ns
EN_DELAY();
fastWriteLow(EN);
//EN_DELAY(); // some displays may need this delay at the end of the enable pulse
}
//***********************************************************************************
void KS0108_GotoXY(uint8_t x, uint8_t y)
{
uint8_t chip, cmd;
if ( (x > DISPLAY_WIDTH-1) || (y > DISPLAY_HEIGHT-1) ) // exit if coordinates are not legal
return;
gCoord.x = x; // save new coordinates
gCoord.y = y;
if (y/8 != gCoord.page)
{
gCoord.page = y/8;
cmd = LCD_SET_PAGE | gCoord.page; // set y address on all chips
for (chip=0; chip < DISPLAY_WIDTH/CHIP_WIDTH; chip++)
{
KS0108_WriteCommand(cmd, chip);
}
}
chip = gCoord.x/CHIP_WIDTH;
x = x % CHIP_WIDTH;
cmd = LCD_SET_ADD | x;
KS0108_WriteCommand(cmd, chip); // set x address on active chip
}
//***********************************************************************************
uint8_t KS0108_DoReadData(uint8_t first)
{
uint8_t data, chip;
chip = gCoord.x / CHIP_WIDTH;
KS0108_WaitReady(chip);
if (first)
{
if (gCoord.x % CHIP_WIDTH == 0 && chip > 0)
{ // todo , remove this test and call GotoXY always?
KS0108_GotoXY(gCoord.x, gCoord.y);
KS0108_WaitReady(chip);
}
}
fastWriteHigh(D_I); // D/I = 1
fastWriteHigh(R_W); // R/W = 1
fastWriteHigh(EN); // EN high level width: min. 450ns
EN_DELAY();
#ifdef LCD_DATA_NIBBLES
data = (LCD_DATA_IN_LOW & 0x0F) | (LCD_DATA_IN_HIGH & 0xF0);
#else
data = LCD_DATA_IN_LOW; // low and high nibbles on same port so read all 8 bits at once
#endif
fastWriteLow(EN);
if (first == 0)
{
KS0108_GotoXY(gCoord.x, gCoord.y);
}
if (gInverted)
{
data = ~data;
}
return data;
}
//***********************************************************************************
inline uint8_t KS0108_ReadData(void)
{
KS0108_DoReadData(1); // dummy read
return KS0108_DoReadData(0); // "real" read
gCoord.x++;
}
//***********************************************************************************
static void KS0108_WriteCommand(uint8_t cmd, uint8_t chip)
{
if (gCoord.x % CHIP_WIDTH == 0 && chip > 0)
{ // todo , ignore address 0???
EN_DELAY();
}
KS0108_WaitReady(chip);
fastWriteLow(D_I); // D/I = 0
fastWriteLow(R_W); // R/W = 0
lcdDataDir(0xFF);
EN_DELAY();
lcdDataOut(cmd);
KS0108_Enable(); // enable
EN_DELAY();
EN_DELAY();
lcdDataOut(0x00);
}
//***********************************************************************************
void KS0108_WriteData(uint8_t data)
{
uint8_t displayData, yOffset, chip;
//showHex("wrData",data);
//showXY("wr", this->Coord.x,this->Coord.y);
#ifdef LCD_CMD_PORT
uint8_t cmdPort;
#endif
#ifdef GLCD_DEBUG
volatile uint16_t i;
for (i=0; i<5000; i++);
#endif
if (gCoord.x >= DISPLAY_WIDTH)
return;
chip = gCoord.x/CHIP_WIDTH;
KS0108_WaitReady(chip);
if (gCoord.x % CHIP_WIDTH == 0 && chip > 0)
{ // todo , ignore address 0???
KS0108_GotoXY(gCoord.x, gCoord.y);
}
fastWriteHigh(D_I); // D/I = 1
fastWriteLow(R_W); // R/W = 0
lcdDataDir(0xFF); // data port is output
yOffset = gCoord.y%8;
if (yOffset != 0)
{
// first page
#ifdef LCD_CMD_PORT
cmdPort = LCD_CMD_PORT; // save command port
#endif
displayData = KS0108_ReadData();
#ifdef LCD_CMD_PORT
LCD_CMD_PORT = cmdPort; // restore command port
#else
fastWriteHigh(D_I); // D/I = 1
fastWriteLow(R_W); // R/W = 0
SelectChip(chip);
#endif
lcdDataDir(0xFF); // data port is output
displayData |= data << yOffset;
if (gInverted)
displayData = ~displayData;
lcdDataOut( displayData); // write data
KS0108_Enable(); // enable
// second page
KS0108_GotoXY(gCoord.x, gCoord.y+8);
displayData = KS0108_ReadData();
#ifdef LCD_CMD_PORT
LCD_CMD_PORT = cmdPort; // restore command port
#else
fastWriteHigh(D_I); // D/I = 1
fastWriteLow(R_W); // R/W = 0
SelectChip(chip);
#endif
lcdDataDir(0xFF); // data port is output
displayData |= data >> (8-yOffset);
if (gInverted)
displayData = ~displayData;
lcdDataOut(displayData); // write data
KS0108_Enable(); // enable
KS0108_GotoXY(gCoord.x + 1, gCoord.y - 8);
}
else
{
// just this code gets executed if the write is on a single page
if (gInverted)
{
data = ~data;
}
EN_DELAY();
lcdDataOut(data); // write data
KS0108_Enable(); // enable
gCoord.x++;
//showXY("WrData",gCoord.x, gCoord.y);
}
}
//***********************************************************************************
void KS0108_ClearPage(uint8_t page, uint8_t color)
{
for (uint8_t x=0; x < DISPLAY_WIDTH; x++)
{
KS0108_GotoXY(x, page * 8);
KS0108_WriteData(color);
}
}
//***********************************************************************************
void KS0108_ClearScreen(uint8_t color)
{
uint8_t page;
for ( page = 0; page < 8; page++)
{
KS0108_GotoXY(0, page * 8);
KS0108_ClearPage(page, color);
}
}
//***********************************************************************************
void KS0108_SetDot(uint8_t xLoc, uint8_t yLoc, uint8_t color)
{
if ((xLoc >= 0) && (xLoc < gWidth) && (yLoc >= 0) && (yLoc < gHeight))
{
uint8_t data;
KS0108_GotoXY(xLoc, yLoc - yLoc % 8); // read data from display memory
data = KS0108_ReadData();
if (color == BLACK)
{
data |= 0x01 << (yLoc % 8); // set dot
}
else
{
data &= ~(0x01 << (yLoc % 8)); // clear dot
}
KS0108_WriteData(data); // write data back to display
}
}
#pragma mark -
#pragma mark Graphics Hardware Routines
//*******************************************************************************
//* returns TRUE if succesfull
boolean GrahicsHW_CheckIfPresent(void)
{
return(true);
}
//*******************************************************************************
boolean GrahicsHW_Init(void)
{
#ifdef _DEBUG_VIA_SERIAL_
Serial.println("KS0108_Init");
#if defined(__AVR_ATmega1280__)
Serial.println("__AVR_ATmega1280__");
#endif
#endif
pinMode(D_I, OUTPUT);
pinMode(R_W, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(CSEL1, OUTPUT);
pinMode(CSEL2, OUTPUT);
delay(10);
fastWriteLow(D_I);
fastWriteLow(R_W);
fastWriteLow(EN);
gCoord.x = 0;
gCoord.y = 0;
gCoord.page = 0;
gInverted = false;
for (uint8_t chip=0; chip < DISPLAY_WIDTH/CHIP_WIDTH; chip++)
{
delay(10);
KS0108_WriteCommand(LCD_ON, chip); // power on
delay(2);
KS0108_WriteCommand(LCD_DISP_START, chip); // display start line = 0
}
// delay(50);
delay(150);
#ifdef _MONOCHROME_BLACK_ON_WHITE_
KS0108_ClearScreen(WHITE);
#else
KS0108_ClearScreen(BLACK);
#endif
KS0108_GotoXY(0, 0);
gWidth = DISPLAY_WIDTH;
gHeight = DISPLAY_HEIGHT;
return(true);
}
//*******************************************************************************
void GrahicsHW_EraseScreen(void)
{
#ifdef _MONOCHROME_BLACK_ON_WHITE_
KS0108_ClearScreen(WHITE);
#else
KS0108_ClearScreen(BLACK);
#endif
}
//*******************************************************************************
void GrahicsHW_Reset(void)
{
}
//*******************************************************************************
void GrahicsHW_SetPixel(short xLoc, short yLoc, unsigned char theColorByte)
{
uint8_t pixelColor;
if ((xLoc > gWidth) || (yLoc > gHeight))
{
return;
}
if ((xLoc < 0) || (yLoc < 0))
{
return;
}
pixelColor = (theColorByte ? BLACK : WHITE);
KS0108_SetDot(xLoc, yLoc, pixelColor);
}
//*******************************************************************************
short GrahicsHW_GetWidth(void)
{
return(gWidth);
}
//*******************************************************************************
short GrahicsHW_GetHeight(void)
{
return(gHeight);
}
#endif // _GRAPHICS_HW_KS0108_