forked from majestik666/Beagle_GPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeagle_GPIO_KS0108.cc
497 lines (392 loc) · 11.8 KB
/
Beagle_GPIO_KS0108.cc
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
/******************************
** Beagle Bone GPIO Library **
** **
** Francois Sugny **
** 01/07/12 **
** **
** v1.0 **
******************************/
//=======================================================
//=======================================================
#include <stdlib.h>
#include <string.h>
#include "Beagle_GPIO_KS0108.hh"
//=======================================================
//=======================================================
Beagle_GPIO_KS0108::Beagle_GPIO_KS0108(
Beagle_GPIO * gpio,
unsigned short _pin_RS,
unsigned short _pin_RW,
unsigned short _pin_E,
unsigned short _pin_DB0,
unsigned short _pin_DB1,
unsigned short _pin_DB2,
unsigned short _pin_DB3,
unsigned short _pin_DB4,
unsigned short _pin_DB5,
unsigned short _pin_DB6,
unsigned short _pin_DB7,
unsigned short _pin_CS1,
unsigned short _pin_CS2)
{
if (!gpio || !gpio->isActive())
{
GPIO_ERROR( "Invalid or Inactive GPIO Module" );
m_gpio = NULL;
return;
}
m_gpio = gpio;
m_pin_RS = _pin_RS;
m_pin_RW = _pin_RW;
m_pin_E = _pin_E;
m_pin_DB0 = _pin_DB0;
m_pin_DB1 = _pin_DB1;
m_pin_DB2 = _pin_DB2;
m_pin_DB3 = _pin_DB3;
m_pin_DB4 = _pin_DB4;
m_pin_DB5 = _pin_DB5;
m_pin_DB6 = _pin_DB6;
m_pin_DB7 = _pin_DB7;
m_pin_CS1 = _pin_CS1;
m_pin_CS2 = _pin_CS2;
// Initialize pins on the board
gpio->configurePin( m_pin_RS, Beagle_GPIO::kOUTPUT );
gpio->configurePin( m_pin_RW, Beagle_GPIO::kOUTPUT );
gpio->configurePin( m_pin_E, Beagle_GPIO::kOUTPUT );
gpio->configurePin( m_pin_CS1, Beagle_GPIO::kOUTPUT );
gpio->configurePin( m_pin_CS2, Beagle_GPIO::kOUTPUT );
m_gpio->writePin( m_pin_RS, 0);
m_gpio->writePin( m_pin_RW, 0);
m_gpio->writePin( m_pin_E, 0);
m_gpio->writePin( m_pin_CS1, 1);
m_gpio->writePin( m_pin_CS2, 1);
setDataPortAsOutput();
m_gpio->writePin( m_pin_DB0, 0);
m_gpio->writePin( m_pin_DB1, 0);
m_gpio->writePin( m_pin_DB2, 0);
m_gpio->writePin( m_pin_DB3, 0);
m_gpio->writePin( m_pin_DB4, 0);
m_gpio->writePin( m_pin_DB5, 0);
m_gpio->writePin( m_pin_DB6, 0);
m_gpio->writePin( m_pin_DB7, 0);
}
//=======================================================
//=======================================================
Beagle_GPIO_KS0108::~Beagle_GPIO_KS0108()
{
GPIO_PRINT( "Closing LCD Screen" );
m_gpio = NULL;
}
//=======================================================
//=======================================================
// Initialize Screen
void Beagle_GPIO_KS0108::initScreen()
{
GPIO_PRINT( "Initializing KS0108 Screen" );
// Wait 20ms
usleep( 20000 );
for ( int i=0; i<2; ++i )
writeCommand( 0x3E, i );
usleep( 25000 );
for ( int i=0; i<2; ++i )
writeCommand( 0x3F, i );
// Wait 200ms
usleep( 200000 );
for ( int i=0; i<2; ++i )
writeCommand( 0xC0, i );
m_gpio->writePin( m_pin_RS, 0 );
m_gpio->writePin( m_pin_RW, 0 );
m_gpio->writePin( m_pin_E, 0 );
m_gpio->writePin( m_pin_CS1, 0 );
m_gpio->writePin( m_pin_CS2, 0 );
writeDataPort( 0xC0 );
usleep(10000);
m_gpio->writePin( m_pin_E, 1 );
usleep(50000);
m_gpio->writePin( m_pin_E, 0 );
usleep(50000);
writeDataPort( 0xB8 );
usleep(10000);
m_gpio->writePin( m_pin_E, 1 );
usleep(50000);
m_gpio->writePin( m_pin_E, 0 );
usleep(50000);
writeDataPort( 0x40 );
usleep(10000);
m_gpio->writePin( m_pin_E, 1 );
usleep(50000);
m_gpio->writePin( m_pin_E, 0 );
usleep(50000);
writeDataPort( 0x3F );
usleep(10000);
m_gpio->writePin( m_pin_E, 1 );
usleep(50000);
m_gpio->writePin( m_pin_E, 0 );
usleep(50000);
// Wait 200ms
usleep( 200000 );
GPIO_PRINT( "KS0108 Screen Initialized" );
for (unsigned char i=0;i<64;++i)
writeData( i );
while(1);
}
//=======================================================
//=======================================================
// Clear Screen
void Beagle_GPIO_KS0108::clearScreen()
{
for ( int j=0; j<64/8; ++j )
{
goToPosition( 0, 8*j );
for ( int i=0; i<128/8; ++i )
{
writeData( 0x00 );
m_screenX++;
}
}
goToPosition(0,0);
}
//=======================================================
//=======================================================
// Write a single pixel
void Beagle_GPIO_KS0108::setPixel( unsigned char _x, unsigned char _y, unsigned char _c )
{
unsigned char tmp;
goToPosition( _x, _y );
tmp = readData();
if ( _c )
tmp |= ( 1 << ( _y % 8 ) );
else
tmp &= ~( 1 << ( _y % 8 ) );
writeData( tmp );
}
//=======================================================
//=======================================================
// Enable Controller
void Beagle_GPIO_KS0108::enableController( unsigned char _controller )
{
std::cout << "Enable controller " << (int)(_controller) << "\n";
switch( _controller)
{
case 0: m_gpio->writePin( m_pin_CS1, 0 ); m_gpio->writePin( m_pin_CS2, 1 ); break;
case 1: m_gpio->writePin( m_pin_CS1, 1 ); m_gpio->writePin( m_pin_CS2, 0 ); break;
};
}
//=======================================================
//=======================================================
// Disable Controller
void Beagle_GPIO_KS0108::disableController( unsigned char _controller )
{
switch( _controller)
{
case 0: m_gpio->writePin( m_pin_CS1, 1 ); m_gpio->writePin( m_pin_CS2, 0 ); break;
case 1: m_gpio->writePin( m_pin_CS1, 0 ); m_gpio->writePin( m_pin_CS2, 1 ); break;
};
}
//=======================================================
//=======================================================
// Set Data Port as Output
void Beagle_GPIO_KS0108::setDataPortAsOutput()
{
m_gpio->configurePin( m_pin_DB0, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB1, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB2, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB3, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB4, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB5, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB6, Beagle_GPIO::kOUTPUT );
m_gpio->configurePin( m_pin_DB7, Beagle_GPIO::kOUTPUT );
}
//=======================================================
//=======================================================
// Set Data Port as Input
void Beagle_GPIO_KS0108::setDataPortAsInput()
{
m_gpio->configurePin( m_pin_DB0, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB1, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB2, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB3, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB4, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB5, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB6, Beagle_GPIO::kINPUT );
m_gpio->configurePin( m_pin_DB7, Beagle_GPIO::kINPUT );
}
//=======================================================
//=======================================================
// Read Data Port
unsigned char Beagle_GPIO_KS0108::readDataPort()
{
unsigned char result = 0x00;
result += m_gpio->readPin( m_pin_DB0 );
result += m_gpio->readPin( m_pin_DB1 ) << 1;
result += m_gpio->readPin( m_pin_DB2 ) << 2;
result += m_gpio->readPin( m_pin_DB3 ) << 3;
result += m_gpio->readPin( m_pin_DB4 ) << 4;
result += m_gpio->readPin( m_pin_DB5 ) << 5;
result += m_gpio->readPin( m_pin_DB6 ) << 6;
result += m_gpio->readPin( m_pin_DB7 ) << 7;
return result;
}
//=======================================================
//=======================================================
// Write Data Port
void Beagle_GPIO_KS0108::writeDataPort( unsigned char _c )
{
m_gpio->writePin( m_pin_DB7, (_c & 0x01) );
m_gpio->writePin( m_pin_DB6, (_c & 0x02) >> 1 );
m_gpio->writePin( m_pin_DB5, (_c & 0x04) >> 2 );
m_gpio->writePin( m_pin_DB4, (_c & 0x08) >> 3 );
m_gpio->writePin( m_pin_DB3, (_c & 0x10) >> 4 );
m_gpio->writePin( m_pin_DB2, (_c & 0x20) >> 5 );
m_gpio->writePin( m_pin_DB1, (_c & 0x40) >> 6 );
m_gpio->writePin( m_pin_DB0, (_c & 0x80) >> 7 );
}
//=======================================================
//=======================================================
// Read screen status
unsigned char Beagle_GPIO_KS0108::readStatus( unsigned char _controller )
{
// Set data port as input
setDataPortAsInput();
// RW = 1
m_gpio->writePin( m_pin_RW, 1 );
// RS = 0
m_gpio->writePin( m_pin_RS, 0 );
// Enable Controller
//enableController( _controller );
// E = 1
m_gpio->writePin( m_pin_E, 1 );
// wait 1ms
usleep(1000);
// Read Data Port
unsigned char status = readDataPort();
// E = 0
m_gpio->writePin( m_pin_E, 0 );
// Disable Controller
//disableController( _controller );
return status;
}
//=======================================================
//=======================================================
// Write a string to screen
void Beagle_GPIO_KS0108::write( const char * _string )
{
int l = strlen( _string );
for ( int i=0; i<l; ++i)
writeChar( _string[i] );
}
//=======================================================
//=======================================================
// Write a character
void Beagle_GPIO_KS0108::writeChar( unsigned char _c )
{
_c -= 32;
// Use Font Array to display character
}
//=======================================================
//=======================================================
// Move to a specific position on screen
void Beagle_GPIO_KS0108::goToPosition( unsigned char _x, unsigned char _y )
{
if ( _x > 127 ) _x = 0;
if ( _y > 63 ) _y = 0;
m_screenX = _x;
m_screenY = _y;
unsigned char controller = 0;
if ( _x > 64 )
{
_x -= 64;
controller++;
}
writeCommand( 0x40 | _x, controller );
writeCommand( 0xB8 | _y/8, 0 );
writeCommand( 0xB8 | _y/8, 1 );
}
//=======================================================
//=======================================================
// Write some data
void Beagle_GPIO_KS0108::writeData( unsigned char _c )
{
// Wait for controller busy flag
//while( readStatus( m_screenX/64 ) & 0x80 );
// Set Data port as output
setDataPortAsOutput();
// RW=0 RS=1
m_gpio->writePin( m_pin_RW, 0 );
m_gpio->writePin( m_pin_RS, 1 );
m_gpio->writePin( m_pin_E, 0 );
// Delay
usleep(1000);
// Write command to Data Port
writeDataPort( _c );
// Enable controller
//enableController( m_screenX / 64 );
// E = 1
m_gpio->writePin( m_pin_E, 1 );
// Delay
usleep(1000);
// E = 0
m_gpio->writePin( m_pin_E, 0 );
// Disable controller
//disableController( m_screenX / 64 );
}
//=======================================================
//=======================================================
// Read some data
unsigned char Beagle_GPIO_KS0108::readData()
{
// Wait for controller busy flag
while( readStatus( m_screenX/64 ) & 0x80 );
// Set Data port as output
setDataPortAsInput();
// Enable controller
enableController( m_screenX / 64 );
// RW=1 RS=1
m_gpio->writePin( m_pin_RW, 1 );
m_gpio->writePin( m_pin_RS, 1 );
// Delay
usleep(1000);
// E = 1
m_gpio->writePin( m_pin_E, 1 );
// Delay
usleep(1000);
// Read actual data
unsigned char result = readDataPort();
// E = 0
m_gpio->writePin( m_pin_E, 0 );
// Disable controller
disableController( m_screenX / 64 );
return result;
}
//=======================================================
//=======================================================
// Write a command
void Beagle_GPIO_KS0108::writeCommand( unsigned char _c, unsigned char _controller )
{
// Wait for controller busy flag
//while( readStatus(_controller) & 0x80 );
// Set Data as output
setDataPortAsOutput();
// RW=0 RS=0
m_gpio->writePin( m_pin_RW, 0 );
m_gpio->writePin( m_pin_RS, 0 );
m_gpio->writePin( m_pin_E, 0 );
// Delay
usleep(1000);
// Enable controller
//enableController( _controller );
// Delay
usleep(1000);
// Write command to Data Port
writeDataPort( _c );
// E = 1
m_gpio->writePin( m_pin_E, 1 );
// Delay
usleep(1000);
// E = 0
m_gpio->writePin( m_pin_E, 0 );
// Disable controller
//disableController( _controller );
}
//=======================================================
//=======================================================