-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdi2clib.c
435 lines (335 loc) · 10.9 KB
/
lcdi2clib.c
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
/********************************************************************************
* lcdi2clib.c
* This file is part of lcdi2c - RaspberryPi library
* for LCD display with the HITACHI HD44780 and compatible controller,
* I2C interface with PCF8574 or/and PCF8574A I/O expander
* Copyright (C) 2021 Boguslaw Kempny kempny@stanpol.com.pl
*
*********************************************************************************
* lcdi2c is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* stepmotor 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. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
*
*********************************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <lcdi2c.h>
#include "pthread.h"
#define DATA 1 // Mode - Sending data
#define CMD 0 // Mode - Sending command
#define ENABLE 0b00000100 // Enable bit
#define MAXDISPL 16 // how many displays we handle
int konvline[4]={0x80, 0xC0, 0x94, 0xd4};
unsigned char byte;
struct dispdef parm[MAXDISPL];
int dispfree(int fdlcd);
void lcdLocI(int fdlcd, int col, int line);
void lcdCharI(int fdlcd, char val);
unsigned char cmd_datac;
//*****************************************
// Display one line of text, truncated on the right if necessary
void lcdLine(int fdlcd, int x, int y, char* text)
{
int i, j;
if ( dispfree(fdlcd) ) return;
lcdLocI (fdlcd, x, y) ;
j = ((strlen(text) + x) < parm[fdlcd].cols) ? strlen(text) : (parm[fdlcd].cols - x);
for(i=0;i<j;i++)
lcdCharI(fdlcd, text[i]);
parm[fdlcd].free_disp=0;
}
//*****************************************
// Display one line of text, truncated on the right if necessary
// For internal use of library, display is already locked
void lcdLineI(int fdlcd, int x, int y, char* text)
{
int i, j;
lcdLocI (fdlcd, x, y) ;
j = ((strlen(text) + x) < parm[fdlcd].cols) ? strlen(text) : (parm[fdlcd].cols - x);
for(i=0;i<j;i++)
lcdCharI(fdlcd, text[i]);
}
//*****************************************
// Display block of text, wrap lines, truncated at the
// end of display if necessary
void lcdBlock(int fdlcd, int x, int y, char* text)
{
int i, j;
if ( dispfree(fdlcd) ) return;
j = (strlen(text) + x + (parm[fdlcd].cols * y ) < parm[fdlcd].cols * parm[fdlcd].rows) ? strlen(text) : (parm[fdlcd].cols * parm[fdlcd].rows - (y * parm[fdlcd].cols) - x);
for(i=0;i<j;i++)
{
lcdLocI(fdlcd, (x + i) % parm[fdlcd].cols, ((x + (y * parm[fdlcd].cols) + i) / parm[fdlcd].cols));
lcdCharI(fdlcd, text[i]);
}
parm[fdlcd].free_disp=0;
}
/**********************************************************/
void *run(void *ptr)
{
int fdlcd;
int i;
fdlcd = (int)ptr;
parm[fdlcd].pos = 0;
parm[fdlcd].move = 1;
while(parm[fdlcd].move)
{
if (! dispfree(fdlcd) )
{
if((parm[fdlcd].move_mode == 0)) // at end start from begining
{
lcdLineI(fdlcd, 0, parm[fdlcd].line, &parm[fdlcd].text[parm[fdlcd].pos]);
if (parm[fdlcd].pos > strlen(parm[fdlcd].text) - parm[fdlcd].cols)
lcdLineI(fdlcd,
strlen(parm[fdlcd].text) - parm[fdlcd].pos,
parm[fdlcd].line,
" ");
{
parm[fdlcd].pos++;
if (parm[fdlcd].pos == strlen(parm[fdlcd].text))
parm[fdlcd].pos = 0;
}
}
else
if((parm[fdlcd].move_mode == 1)) // append begining of text to end of text
{
lcdLineI(fdlcd, 0, parm[fdlcd].line, &parm[fdlcd].text[parm[fdlcd].pos]);
if(strlen(parm[fdlcd].text) > parm[fdlcd].cols)
{
parm[fdlcd].pos++;
if (parm[fdlcd].pos > strlen(parm[fdlcd].text) - parm[fdlcd].cols -1)
{
lcdLineI(fdlcd, strlen(parm[fdlcd].text) - parm[fdlcd].pos +1,
parm[fdlcd].line, parm[fdlcd].text);
if (parm[fdlcd].pos > strlen(parm[fdlcd].text))
parm[fdlcd].pos = 1; // not 0, the line is already displayed
}
}
}
}
parm[fdlcd].free_disp = 0;
for(i = 0; i < parm[fdlcd].speed; i++)
Delay_mls(100);
}
return(0);
}
/**********************************************************/
// speed - speed of animation, in 1/10 sec increments
// mode - mode of animation
void lcdRun(int fdlcd, int mode, int speed, int line, char* text)
{
pthread_t thread_id;
parm[fdlcd].speed = speed;
parm[fdlcd].move_mode = mode;
parm[fdlcd].line = line;
strncpy(parm[fdlcd].text, text, ANIMLEN);
pthread_create (&thread_id, NULL, run, (void *)fdlcd);
}
/**********************************************************/
void lcdStop(int fdlcd)
{
parm[fdlcd].move = 0;
}
//****************************************
// Wait for display free
int dispfree(int fdlcd)
{
static int j;
for(j=0; j<100; j++)
{
if(parm[fdlcd].free_disp == 0)
{parm[fdlcd].free_disp = 1;
return 0;
}
Delay_mls(10);
}
printf("Display busy\n");
return 1;
}
//*************************************
// Clear screen, cursor home
// Not thread safe
void lcdClr(int fdlcd) {
if ( dispfree(fdlcd) ) return;
send_byte(fdlcd, 0x01, CMD);
Delay_mls(5); // clear screen takes long time to complete
send_byte(fdlcd, 0x02, CMD);
parm[fdlcd].free_disp=0;
}
//*************************************
// Define character
void lcdDefc(int fdlcd, int addr, unsigned char* matrix) {
int i;
send_byte(fdlcd, 0x40 | addr * 8, CMD);
for (i=0; i<8; i++)
send_byte(fdlcd, matrix[i], DATA);
send_byte(fdlcd, 0x80, CMD); //switch to DDRAM addressing
}
//*************************************
// Set cursor to location row/column on LCD
// For internal use of library, display is already locked
void lcdLocI(int fdlcd, int col, int line) {
if(line < 2)
send_byte(fdlcd, konvline[line] + col, CMD);
else
send_byte(fdlcd, konvline[line] + col - (20 - parm[fdlcd].cols), CMD);
}
//*************************************
// Set cursor to location row/column on LCD
void lcdLoc(int fdlcd, int col, int line) {
if ( dispfree(fdlcd) ) return;
if(line < 2)
send_byte(fdlcd, konvline[line] + col, CMD);
else
send_byte(fdlcd, konvline[line] + col - (20 - parm[fdlcd].cols), CMD);
parm[fdlcd].free_disp=0;
}
//*************************************
// Display char at cursor position
// For internal use of library, display is already locked
void lcdCharI(int fdlcd, char val) {
send_byte(fdlcd, val, DATA);
}
//*************************************
// Display char at curor position
void lcdChar(int fdlcd, char val) {
if ( dispfree(fdlcd) ) return;
send_byte(fdlcd, val, DATA);
parm[fdlcd].free_disp=0;
}
//*************************************
// Set display on/off, cursor on/off blinking on/off, backlight on/off
// 1 - ON, 0 - OFF, 99 - unchanged
void lcdSet(int fdlcd, int disp, int cur, int blin, int back) // display, cursor, blink,
// backlight on/off,
{
if ( dispfree(fdlcd) ) return;
if(back !=99)
parm[fdlcd].backl = back << 3;
if(disp != 99)
parm[fdlcd].disp = disp;
if(cur != 99)
parm[fdlcd].cur = cur;
if(cur != 99)
parm[fdlcd].blin = blin;
byte = (parm[fdlcd].disp << 2) | (parm[fdlcd].cur << 1) | parm[fdlcd].blin | parm[fdlcd].backl;
send_byte(fdlcd, byte, CMD);
parm[fdlcd].free_disp=0;
}
//*************************************
void send_byte(int fdlcd, int data, int cmd_data) {
//Send byte to data pins
// bits = the data
// cmd_data = 1 for data, 0 for command
// uses the two half byte writes to LCD
//send high 4 bits
byte = data&0xf0;
send_4(fdlcd, byte, cmd_data);
// send low 4 bits
byte = data << 4 & 0xf0;
send_4(fdlcd, byte, cmd_data);
}
//*************************************
void send_4(int fdlcd, unsigned char byte, int cmd_data)
{
// set RS bit
cmd_datac = cmd_data | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(65);
//set ENABLE bit
cmd_datac= cmd_data | ENABLE | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(65);
//send data
cmd_datac = cmd_data | byte | ENABLE | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(400);
//clear ENABLE bit
cmd_datac = (cmd_data & ~ENABLE) | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(500);
}
//*************************************
void send_8(int fdlcd, int byte, int cmd_data) {
//set RS bit
cmd_datac = cmd_data | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
//set ENABLE bit
cmd_datac = cmd_data | ENABLE | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(65);
//send data
cmd_datac = cmd_data | byte | ENABLE | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(400);
//clear ENABLE bit
cmd_datac = (cmd_data & ~ENABLE) | parm[fdlcd].backl;
write(fdlcd, &cmd_datac, 1);
Delay_ns(500);
}
int displcount =0;
//*************************************
int i2copen(int controller, int device)
{
if (displcount == (MAXDISPL))
{printf("To many displays, only %d are supported\n", MAXDISPL); exit(1);}
int file;
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", controller);
file = open(filename, O_RDWR);
if (file < 0) {
printf("Invalid controller: %s\n", filename);
exit(1);
}
if (ioctl(file, I2C_SLAVE, device) < 0) {
printf("Invalid device: %d\n", I2C_SLAVE);
}
parm[displcount].i2caddr = device;
displcount++;
return(file);
}
//*************************************
// initialise display, not thread safe
void lcdinit(int fdlcd, int rows, int cols) {
// Initialise display
parm[fdlcd].free_disp=0;
send_8(fdlcd, 0x30, CMD);
Delay_mls(5);
send_8(fdlcd, 0x30, CMD);
Delay_mcs(110);
send_8(fdlcd, 0x30, CMD);
Delay_mls(5);
send_8(fdlcd, 0x20, CMD);
send_byte(fdlcd, 0x06, CMD); // Cursor move direction
send_byte(fdlcd, 0x0C, CMD); // display On, cursor Off, blink off
send_byte(fdlcd, 0x28, CMD); // Data length, number of lines, font size
send_byte(fdlcd, 0x01, CMD); // Clear display
Delay_mls(5); // wait to complete clear display
parm[fdlcd].rows = rows;
parm[fdlcd].cols = cols;
parm[fdlcd].disp = 1;
parm[fdlcd].cur = 0;
parm[fdlcd].blin = 0;
parm[fdlcd].backl = 1;
}