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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/*
* GraphLCD driver library
*
* t6963c.c - T6963C driver class
*
* low level routines based on lcdproc 0.5 driver, (c) 2001 Manuel Stahl
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* (c) 2003-2004 Andreas Regel <andreas.regel AT powarman.de>
* (c) 2011 Wolfgang Astleitner <mrwastl AT users.sourceforge.net>
*/
#include <syslog.h>
#include <cstring>
#include <cstdlib>
#include "common.h"
#include "config.h"
#include "port.h"
#include "t6963c.h"
namespace GLCD
{
// T6963 commands
const unsigned char kSetCursorPointer = 0x21;
const unsigned char kSetOffsetRegister = 0x22;
const unsigned char kSetAddressPointer = 0x24;
const unsigned char kSetTextHomeAddress = 0x40;
const unsigned char kSetTextArea = 0x41;
const unsigned char kSetGraphicHomeAddress = 0x42;
const unsigned char kSetGraphicArea = 0x43;
const unsigned char kSetMode = 0x80;
const unsigned char kSetDisplayMode = 0x90;
const unsigned char kSetCursorPattern = 0xA0;
const unsigned char kDataWriteInc = 0xC0;
const unsigned char kDataReadInc = 0xC1;
const unsigned char kDataWriteDec = 0xC2;
const unsigned char kDataReadDec = 0xC3;
const unsigned char kDataWrite = 0xC4;
const unsigned char kDataRead = 0xC5;
const unsigned char kAutoWrite = 0xB0;
const unsigned char kAutoRead = 0xB1;
const unsigned char kAutoReset = 0xB2;
// T6963 Parameters
const unsigned char kModeOr = 0x00;
const unsigned char kModeXor = 0x01;
const unsigned char kModeAnd = 0x03;
const unsigned char kModeTextAttribute = 0x04;
const unsigned char kModeInternalCG = 0x00;
const unsigned char kModeExternalCG = 0x08;
const unsigned char kTextAttributeNormal = 0x00;
const unsigned char kTextAttributeInverse = 0x05;
const unsigned char kTextAttributeNoDisplay = 0x03;
const unsigned char kTextAttributeBlink = 0x08;
const unsigned char kDisplayModeBlink = 0x01;
const unsigned char kDisplayModeCursor = 0x02;
const unsigned char kDisplayModeText = 0x04;
const unsigned char kDisplayModeGraphic = 0x08;
const unsigned short kGraphicBase = 0x0000;
const unsigned short kTextBase = 0x1500;
const unsigned short kCGRAMBase = 0x1800;
// T6963 Wirings
static const std::string kWiringStandard = "Standard";
static const std::string kWiringWindows = "Windows";
static const std::string kWiringSerial = "Serial";
const unsigned char kStandardWRHI = 0x00; // 01 / nSTRB
const unsigned char kStandardWRLO = 0x01; //
const unsigned char kStandardRDHI = 0x00; // 17 / nSELECT
const unsigned char kStandardRDLO = 0x08; //
const unsigned char kStandardCEHI = 0x00; // 14 / nLINEFEED
const unsigned char kStandardCELO = 0x02; //
const unsigned char kStandardCDHI = 0x04; // 16 / INIT
const unsigned char kStandardCDLO = 0x00; //
const unsigned char kWindowsWRHI = 0x04; // 16 / INIT
const unsigned char kWindowsWRLO = 0x00; //
const unsigned char kWindowsRDHI = 0x00; // 14 / nLINEFEED
const unsigned char kWindowsRDLO = 0x02; //
const unsigned char kWindowsCEHI = 0x00; // 01 / nSTRB
const unsigned char kWindowsCELO = 0x01; //
const unsigned char kWindowsCDHI = 0x00; // 17 / nSELECT
const unsigned char kWindowsCDLO = 0x08; //
const unsigned char kSerialWRHI = 0x01; // 01 / nSTRB
const unsigned char kSerialWRLO = 0x00; //
const unsigned char kSerialRDHI = 0x08; // 17 / nSELECT
const unsigned char kSerialRDLO = 0x00; //
const unsigned char kSerialCEHI = 0x02; // 14 / nLINEFEED
const unsigned char kSerialCELO = 0x00; //
const unsigned char kSerialCDHI = 0x00; // 16 / INIT
const unsigned char kSerialCDLO = 0x04; //
cDriverT6963C::cDriverT6963C(cDriverConfig * config)
: cDriver(config)
{
port = new cParallelPort();
//width = config->width;
//height = config->height;
refreshCounter = 0;
displayMode = 0;
bidirectLPT = 1;
autoWrite = false;
serial = 0;
}
cDriverT6963C::~cDriverT6963C()
{
delete port;
}
int cDriverT6963C::Init()
{
int x;
width = config->width;
if (width <= 0)
width = 240;
height = config->height;
if (height <= 0)
height = 128;
// default values
FS = 6;
WRHI = kStandardWRHI;
WRLO = kStandardWRLO;
RDHI = kStandardRDHI;
RDLO = kStandardRDLO;
CEHI = kStandardCEHI;
CELO = kStandardCELO;
CDHI = kStandardCDHI;
CDLO = kStandardCDLO;
useAutoMode = true;
useStatusCheck = true;
for (unsigned int i = 0; i < config->options.size(); i++)
{
if (config->options[i].name == "FontSelect")
{
int fontSelect = atoi(config->options[i].value.c_str());
if (fontSelect == 6)
FS = 6;
else if (fontSelect == 8)
FS = 8;
else
syslog(LOG_ERR, "%s error: font select %d not supported, using default (%d)!\n",
config->name.c_str(), fontSelect, FS);
}
else if (config->options[i].name == "Wiring")
{
if (config->options[i].value == kWiringStandard)
{
WRHI = kStandardWRHI;
WRLO = kStandardWRLO;
RDHI = kStandardRDHI;
RDLO = kStandardRDLO;
CEHI = kStandardCEHI;
CELO = kStandardCELO;
CDHI = kStandardCDHI;
CDLO = kStandardCDLO;
}
else if (config->options[i].value == kWiringWindows)
{
WRHI = kWindowsWRHI;
WRLO = kWindowsWRLO;
RDHI = kWindowsRDHI;
RDLO = kWindowsRDLO;
CEHI = kWindowsCEHI;
CELO = kWindowsCELO;
CDHI = kWindowsCDHI;
CDLO = kWindowsCDLO;
}
else if (config->options[i].value == kWiringSerial)
{
serial = 1;
WRHI = kSerialWRHI;
WRLO = kSerialWRLO;
RDHI = kSerialRDHI;
RDLO = kSerialRDLO;
CEHI = kSerialCEHI;
CELO = kSerialCELO;
CDHI = kSerialCDHI;
CDLO = kSerialCDLO;
}
else
syslog(LOG_ERR, "%s error: wiring %s not supported, using default (Standard)!\n",
config->name.c_str(), config->options[i].value.c_str());
}
else if (config->options[i].name == "AutoMode")
{
if (config->options[i].value == "yes")
useAutoMode = true;
else if (config->options[i].value == "no")
useAutoMode = false;
else
syslog(LOG_ERR, "%s error: unknown auto mode setting %s, using default (%s)!\n",
config->name.c_str(), config->options[i].value.c_str(), useAutoMode ? "yes" : "no");
}
else if (config->options[i].name == "StatusCheck")
{
if (config->options[i].value == "yes")
useStatusCheck = true;
else if (config->options[i].value == "no")
useStatusCheck = false;
else
syslog(LOG_ERR, "%s error: unknown status check setting %s, using default (%s)!\n",
config->name.c_str(), config->options[i].value.c_str(), useStatusCheck ? "yes" : "no");
}
}
// setup lcd array (wanted state)
newLCD = new unsigned char*[(width + (FS - 1)) / FS];
if (newLCD)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
newLCD[x] = new unsigned char[height];
memset(newLCD[x], 0, height);
}
}
// setup lcd array (current state)
oldLCD = new unsigned char*[(width + (FS - 1)) / FS];
if (oldLCD)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
oldLCD[x] = new unsigned char[height];
memset(oldLCD[x], 0, height);
}
}
if (config->device == "")
{
// use DirectIO
if (port->Open(config->port) != 0)
return -1;
uSleep(10);
}
else
{
// use ppdev
if (port->Open(config->device.c_str()) != 0)
return -1;
}
// disable chip
// disable reading from LCD
// disable writing to LCD
// command/status mode
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
port->SetDirection(kForward); // make 8-bit parallel port an output port
// Test ECP mode
if (bidirectLPT == 1)
{
syslog(LOG_DEBUG, "%s: Testing ECP mode...\n", config->name.c_str());
int i = 0;
int ecp_input;
port->SetDirection(kReverse);
for (int i = 0; i < 100; i++)
{
T6963CSetControl(WRHI | CEHI | CDHI | RDHI); // wr, ce, cd, rd
T6963CSetControl(WRHI | CELO | CDHI | RDLO);
T6963CSetControl(WRHI | CELO | CDHI | RDLO);
T6963CSetControl(WRHI | CELO | CDHI | RDLO);
ecp_input = port->ReadData();
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
if ((ecp_input & 0x03) == 0x03)
break;
}
port->SetDirection(kForward);
if (i >= 100)
{
syslog(LOG_DEBUG, "%s: ECP mode not working! -> is now disabled\n", config->name.c_str());
bidirectLPT = 0;
}
else
syslog(LOG_DEBUG, "%s: working!\n", config->name.c_str());
}
T6963CCommandWord(kSetGraphicHomeAddress, kGraphicBase);
if (width % FS == 0)
T6963CCommandWord(kSetGraphicArea, width / FS);
else
T6963CCommandWord(kSetGraphicArea, width / FS + 1);
T6963CCommand(kSetMode | kModeOr | kModeInternalCG);
T6963CDisplayMode(kDisplayModeText, false);
T6963CDisplayMode(kDisplayModeGraphic, true);
T6963CDisplayMode(kDisplayModeCursor, false);
T6963CDisplayMode(kDisplayModeBlink, false);
port->Release();
*oldConfig = *config;
// clear display
Clear();
syslog(LOG_INFO, "%s: T6963 initialized.\n", config->name.c_str());
return 0;
}
int cDriverT6963C::DeInit()
{
int x;
// free lcd array (wanted state)
if (newLCD)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
delete[] newLCD[x];
}
delete[] newLCD;
}
// free lcd array (current state)
if (oldLCD)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
delete[] oldLCD[x];
}
delete[] oldLCD;
}
if (port->Close() != 0)
return -1;
return 0;
}
int cDriverT6963C::CheckSetup()
{
if (config->device != oldConfig->device ||
config->port != oldConfig->port ||
config->width != oldConfig->width ||
config->height != oldConfig->height)
{
DeInit();
Init();
return 0;
}
if (config->upsideDown != oldConfig->upsideDown ||
config->invert != oldConfig->invert)
{
oldConfig->upsideDown = config->upsideDown;
oldConfig->invert = config->invert;
return 1;
}
return 0;
}
void cDriverT6963C::Clear()
{
for (int x = 0; x < (width + (FS - 1)) / FS; x++)
memset(newLCD[x], 0, height);
}
void cDriverT6963C::SetPixel(int x, int y, uint32_t data)
{
if (x >= width || y >= height)
return;
if (FS == 6)
{
int pos = x % 6;
if (config->upsideDown)
{
x = width - 1 - x;
y = height - 1 - y;
} else {
pos = 5 - pos; // reverse bit position
}
// columns_per_line = (width + FS6-1) / FS6 (FS6 == 6 bits per byte used)
//int cols = (width + 6 - 1 ) / 6;
int col = x / 6;
if (data == GRAPHLCD_White)
newLCD[col][y] |= (1 << pos);
else
newLCD[col][y] &= ( 0x3F ^ (1 << pos) );
}
else
{
int pos = x % 8;
if (config->upsideDown)
{
x = width - 1 - x;
y = height - 1 - y;
} else {
pos = 7 - pos; // reverse bit position
}
if (data == GRAPHLCD_White)
newLCD[x / 8][y] |= (1 << pos);
else
newLCD[x / 8][y] &= ( 0xFF ^ (1 << pos) );
}
}
#if 0
void cDriverT6963C::Set8Pixels(int x, int y, unsigned char data)
{
if (x >= width || y >= height)
return;
if (FS == 6)
{
unsigned char data1 = 0;
unsigned char data2 = 0;
unsigned char data3 = 0;
if (!config->upsideDown)
{
// normal orientation
x = x - (x % 8);
data1 = data >> (2 + (x % 6));
if (x % 6 == 5)
{
data2 = data >> 1;
data3 = data << 5;
}
else
data2 = data << (4 - (x % 6));
newLCD[x / 6][y] |= data1;
if (x / 6 + 1 < (width + 5) / 6)
newLCD[x / 6 + 1][y] |= data2;
if (x / 6 + 2 < (width + 5) / 6)
if (x % 6 == 5)
newLCD[x / 6 + 2][y] |= data3;
}
else
{
// upside down orientation
x = width - 1 - x;
y = height - 1 - y;
x = x - (x % 8);
data = ReverseBits(data);
data1 = data >> (2 + (x % 6));
if (x % 6 == 5)
{
data2 = data >> 1;
data3 = data << 5;
}
else
data2 = data << (4 - (x % 6));
newLCD[x / 6][y] |= data1;
if (x / 6 + 1 < (width + 5) / 6)
newLCD[x / 6 + 1][y] |= data2;
if (x / 6 + 2 < (width + 5) / 6)
if (x % 6 == 5)
newLCD[x / 6 + 2][y] |= data3;
}
}
else
{
if (!config->upsideDown)
{
newLCD[x / 8][y] |= data;
}
else
{
x = width - 1 - x;
y = height - 1 - y;
newLCD[x / 8][y] |= ReverseBits(data);
}
}
}
#endif
void cDriverT6963C::Refresh(bool refreshAll)
{
int x,y;
int addr = 0;
if (CheckSetup() == 1)
refreshAll = true;
if (config->refreshDisplay > 0)
{
refreshCounter = (refreshCounter + 1) % config->refreshDisplay;
if (!refreshAll && !refreshCounter)
refreshAll = true;
}
port->Claim();
if (refreshAll)
{
// draw all
T6963CCommandWord(kSetAddressPointer, kGraphicBase);
if (useAutoMode)
{
T6963CCommand(kAutoWrite);
autoWrite = true;
}
for (y = 0; y < height; y++)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
if (autoWrite)
T6963CData((newLCD[x][y]) ^ (config->invert ? 0xff : 0x00));
else
T6963CCommandByte(kDataWriteInc, (newLCD[x][y]) ^ (config->invert ? 0xff : 0x00));
oldLCD[x][y] = newLCD[x][y];
}
}
if (autoWrite)
{
T6963CCommand(kAutoReset);
autoWrite = false;
}
// and reset RefreshCounter
refreshCounter = 0;
}
else
{
// draw only the changed bytes
bool cs = false;
for (y = 0; y < height; y++)
{
for (x = 0; x < (width + (FS - 1)) / FS; x++)
{
if (oldLCD[x][y] != newLCD[x][y])
{
if (!cs)
{
if (width % FS == 0)
addr = (y * (width / FS)) + x;
else
addr = (y * (width / FS + 1)) + x;
T6963CCommandWord(kSetAddressPointer, kGraphicBase + addr);
if (useAutoMode)
{
T6963CCommand(kAutoWrite);
autoWrite = true;
}
cs = true;
}
if (autoWrite)
T6963CData((newLCD[x][y]) ^ (config->invert ? 0xff : 0x00));
else
T6963CCommandByte(kDataWriteInc, (newLCD[x][y]) ^ (config->invert ? 0xff : 0x00));
oldLCD[x][y] = newLCD[x][y];
}
else
{
if (autoWrite)
{
T6963CCommand(kAutoReset);
autoWrite = false;
}
cs = false;
}
}
}
if (autoWrite)
{
T6963CCommand(kAutoReset);
autoWrite = false;
}
}
port->Release();
}
void cDriverT6963C::T6963CSetControl(unsigned char flags)
{
unsigned char status = port->ReadControl();
status &= 0xF0; // mask 4 bits
status |= flags; // add new flags
port->WriteControl(status);
}
void cDriverT6963C::T6963CDSPReady()
{
int input = 0;
port->SetDirection(kReverse);
if (bidirectLPT == 1)
{
for (int i = 0; i < 10; i++)
{
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
T6963CSetControl(WRHI | CELO | CDHI | RDLO);
input = port->ReadData();
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
if (!autoWrite && (input & 3) == 3)
break;
if (autoWrite && (input & 8) == 8)
break;
}
}
else
{
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
T6963CSetControl(WRHI | CELO | CDHI | RDLO);
T6963CSetControl(WRHI | CEHI | CDHI | RDHI);
}
port->SetDirection(kForward);
}
void cDriverT6963C::T6963CData(unsigned char data)
{
if (serial)
{
T6963CSetControl(WRLO | CEHI | CDLO | RDLO);
for (int i = 128; i; i>>=1)
{
if (data & i)
{
T6963CSetControl(WRLO | CEHI | CDHI | RDLO);
T6963CSetControl(WRHI | CEHI | CDHI | RDLO);
}
else
{
T6963CSetControl(WRLO | CEHI | CDLO | RDLO);
T6963CSetControl(WRHI | CEHI | CDLO | RDLO);
}
}
T6963CSetControl(WRLO | CEHI | CDLO | RDLO); // CD down (data)
T6963CSetControl(WRLO | CELO | CDLO | RDLO); // CE down
T6963CSetControl(WRLO | CEHI | CDLO | RDLO); // CE up
}
else
{
if (useStatusCheck)
T6963CDSPReady();
T6963CSetControl(WRHI | CEHI | CDLO | RDHI); // CD down (data)
T6963CSetControl(WRLO | CELO | CDLO | RDHI); // CE & WR down
port->WriteData(data);
T6963CSetControl(WRHI | CEHI | CDLO | RDHI); // CE & WR up again
T6963CSetControl(WRHI | CEHI | CDHI | RDHI); // CD up again
}
}
void cDriverT6963C::T6963CCommand(unsigned char cmd)
{
if (serial)
{
syslog(LOG_DEBUG, "Serial cmd out: ");
T6963CSetControl(WRLO | CEHI | CDLO | RDLO);
for (int i = 128; i; i>>=1)
{
if (cmd & i)
{
T6963CSetControl(WRLO | CEHI | CDHI | RDLO);
T6963CSetControl(WRHI | CEHI | CDHI | RDLO);
}
else
{
T6963CSetControl(WRLO | CEHI | CDLO | RDLO);
T6963CSetControl(WRHI | CEHI | CDLO | RDLO);
}
}
T6963CSetControl(WRLO | CEHI | CDHI | RDLO); // CD up (command)
T6963CSetControl(WRLO | CELO | CDHI | RDLO); // CE down
T6963CSetControl(WRLO | CEHI | CDHI | RDLO); // CE up
T6963CSetControl(WRLO | CEHI | CDLO | RDLO); // CD down
}
else
{
if (useStatusCheck)
T6963CDSPReady();
T6963CSetControl(WRHI | CEHI | CDHI | RDHI); // CD up (command)
T6963CSetControl(WRLO | CELO | CDHI | RDHI); // CE & WR down
port->WriteData(cmd);
T6963CSetControl(WRHI | CEHI | CDHI | RDHI); // CE & WR up again
T6963CSetControl(WRHI | CEHI | CDLO | RDHI); // CD down again
}
}
void cDriverT6963C::T6963CCommandByte(unsigned char cmd, unsigned char data)
{
T6963CData(data);
T6963CCommand(cmd);
}
void cDriverT6963C::T6963CCommand2Bytes(unsigned char cmd, unsigned char data1, unsigned char data2)
{
T6963CData(data1);
T6963CData(data2);
T6963CCommand(cmd);
}
void cDriverT6963C::T6963CCommandWord(unsigned char cmd, unsigned short data)
{
T6963CData(data % 256);
T6963CData(data >> 8);
T6963CCommand(cmd);
}
void cDriverT6963C::T6963CDisplayMode(unsigned char mode, bool enable)
{
if (enable)
displayMode |= mode;
else
displayMode &= ~mode;
T6963CCommand(kSetDisplayMode | displayMode);
}
}
|