-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtxorb.c
556 lines (451 loc) · 12.5 KB
/
mtxorb.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
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
/**
* Matrix Orbital character display user-space driver for Linux
*
* Copyright (c) 2021 Frank Thaule
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/termios.h>
#include <sys/poll.h>
#include <sys/errno.h>
#include "mtxorb.h"
#define MAX_WIDTH 40
#define MAX_HEIGHT 4
#define MAX_CELLWIDTH 5
#define MAX_CELLHEIGHT 8
#define MAX_CC 8 /* max. number of custom characters */
#define IS_LCD_TYPE (p->device->type == MTXORB_LCD)
#define IS_LKD_TYPE (p->device->type == MTXORB_LKD)
#define IS_VFD_TYPE (p->device->type == MTXORB_VFD)
#define IS_VKD_TYPE (p->device->type == MTXORB_VKD)
/* Alias for ignoring the warning -Wunused-result for write() */
#define Write(fd, buf, n) ((void)!write(fd, buf, n))
enum mtxorb_cc_mode
{
cc_hbar,
cc_vbar,
cc_bignum,
cc_custom
};
struct mtxorb_priv
{
int fd; /* File descriptor */
struct termios oldtio;
/* Keep track of hbar, vbar, num, custom chars
* mode, so we don't re-initialize on subsequent
* calls.
*/
enum mtxorb_cc_mode current_cc_mode;
struct mtxorb_device_info *device;
};
static void mtxorb_set_key_auto_tx(MTXORB *handle, enum mtxorb_onoff on);
static int mtxorb_validate_device_info(const struct mtxorb_device_info *info);
MTXORB *mtxorb_open(const char *portname, int baudrate, const struct mtxorb_device_info *info)
{
struct mtxorb_priv *p;
struct termios oldtio, newtio;
int fd;
int speed;
if ((info == NULL) || (mtxorb_validate_device_info(info) != 0))
{
errno = EINVAL;
return NULL;
}
switch (baudrate)
{
case 9600:
speed = B9600;
break;
case 19200:
speed = B19200;
break;
case 38400:
speed = B38400;
break;
case 57600:
speed = B57600;
break;
default:
errno = EINVAL;
return NULL;
}
if ((fd = open(portname, O_RDWR | O_NOCTTY)) == -1)
return NULL;
if (flock(fd, LOCK_EX | LOCK_NB) == -1)
return NULL;
/* Save current port settings */
tcgetattr(fd, &oldtio);
/* 8-N-1, blocking read by default unless using poll or select */
memset(&newtio, 0, sizeof(struct termios));
newtio.c_cflag = speed | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
/* Flush input buffer and apply new settings */
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &newtio) == -1)
return NULL;
/* Allocate memory for the new handler */
p = malloc(sizeof(struct mtxorb_priv));
if (p == NULL)
return NULL;
p->fd = fd;
p->device = (struct mtxorb_device_info *)info;
memcpy(&p->oldtio, &oldtio, sizeof(struct termios));
mtxorb_clear(p);
mtxorb_home(p);
mtxorb_set_key_auto_tx(p, MTXORB_ON);
return p;
}
void mtxorb_close(MTXORB *handle)
{
struct mtxorb_priv *p = handle;
if (p == NULL)
return;
if (p->fd != -1)
{
/* Release lock */
flock(p->fd, LOCK_UN);
/* Restore old port settings */
tcsetattr(p->fd, TCSANOW, &p->oldtio);
/* Close the port */
close(p->fd);
}
free(p);
}
/* ----- Text functions ----- */
void mtxorb_home(MTXORB *handle)
{
mtxorb_gotoxy(handle, 0, 0);
}
void mtxorb_clear(MTXORB *handle)
{
struct mtxorb_priv *p = handle;
Write(p->fd, "\xFE"
"X",
2);
}
void mtxorb_putc(MTXORB *handle, char c)
{
struct mtxorb_priv *p = handle;
if (c == '\xFE')
c = ' ';
Write(p->fd, &c, 1);
}
void mtxorb_puts(MTXORB *handle, const char *s)
{
struct mtxorb_priv *p = handle;
char *c;
/* Avoid creating an intermediate buffer,
* so check and send bytes one by one */
c = (char *)s;
for (; *c != '\0'; ++c)
{
/* Replace command prefix char with space */
if (*c == '\xFE')
Write(p->fd, " ", 1);
else
Write(p->fd, c, 1);
}
}
void mtxorb_write(MTXORB *handle, const void *buf, size_t nbytes)
{
struct mtxorb_priv *p = handle;
Write(p->fd, buf, nbytes);
}
ssize_t mtxorb_read(MTXORB *handle, void *buf, size_t nbytes, int timeout)
{
struct mtxorb_priv *p = handle;
struct pollfd fds[1];
fds[0].fd = p->fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
poll(fds, 1, timeout);
if (fds[0].revents == 0)
return 0;
return read(fds[0].fd, buf, nbytes);
}
void mtxorb_gotoxy(MTXORB *handle, int x, int y)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 'G', 0, 0};
if ((x >= 0) && (x < p->device->width))
out[2] = x + 1;
if ((y >= 0) && (y < p->device->height))
out[3] = y + 1;
Write(p->fd, out, 4);
}
void mtxorb_set_cursor_block(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0};
out[1] = (on == MTXORB_ON) ? 'S' : 'T';
Write(p->fd, out, 2);
}
void mtxorb_set_cursor_uline(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0};
out[1] = (on == MTXORB_ON) ? 'J' : 'K';
Write(p->fd, out, 2);
}
void mtxorb_set_auto_scroll(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0};
out[1] = (on == MTXORB_ON) ? 'Q' : 'R';
Write(p->fd, out, 2);
}
void mtxorb_set_auto_line_wrap(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0};
out[1] = (on == MTXORB_ON) ? 'C' : 'D';
Write(p->fd, out, 2);
}
/* ----- Special characters functions ----- */
void mtxorb_set_custom_char(MTXORB *handle, int id, const char *data)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 'N', 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char mask = (1 << p->device->cellwidth) - 1;
int i;
if ((data == NULL) ||
(id < 0) || (id >= MAX_CC))
return;
out[2] = id;
for (i = 0; i < p->device->cellheight; i++)
out[i + 3] = data[i] & mask;
Write(p->fd, out, 11);
p->current_cc_mode = cc_custom;
}
void mtxorb_hbar(MTXORB *handle, int x, int y, int len, enum mtxorb_dir dir)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0, 0, 0, 0, 0};
if ((x < 0) || (x >= p->device->width) ||
(y < 0) || (y >= p->device->height) ||
(len < 0) || (len > 100))
return;
/* Initialize the bar, replacing custom characters
* currently present in memory bank 0.
*/
if (p->current_cc_mode != cc_hbar)
{
out[1] = 'h';
Write(p->fd, out, 2);
p->current_cc_mode = cc_hbar;
}
/* Place the bar */
out[1] = '\x7C';
out[2] = x + 1;
out[3] = y + 1;
out[4] = (dir == MTXORB_LEFT) ? 1 : 0;
out[5] = len;
Write(p->fd, out, 6);
}
void mtxorb_vbar(MTXORB *handle, int x, int len, enum mtxorb_vbar_style style)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0, 0, 0};
if ((x < 0) || (x >= p->device->width) ||
(len < 0) || (len > 32))
return;
/* Initialize the bar, replacing custom characters currently present
* in memory. */
if (p->current_cc_mode != cc_vbar)
{
out[1] = (style == MTXORB_WIDE) ? 'v' : 'h';
Write(p->fd, out, 2);
p->current_cc_mode = cc_vbar;
}
/* Place the bar */
out[1] = '=';
out[2] = x + 1;
out[3] = len;
Write(p->fd, out, 4);
}
void mtxorb_bignum(MTXORB *handle, int x, int y, int digit, enum mtxorb_bignum_style style)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0, 0, 0, 0};
if ((x < 0) || (x >= p->device->width) ||
(digit < 0) || (digit > 9))
return;
/* Initialize the bar, replacing all custom characters
* currently present. */
if (p->current_cc_mode != cc_bignum)
{
out[1] = (style == MTXORB_LARGE) ? 'n' : 'm';
Write(p->fd, out, 2);
p->current_cc_mode = cc_bignum;
}
/* Place the digit */
if (style == MTXORB_LARGE)
{
out[1] = '#';
out[2] = x + 1;
out[3] = digit;
Write(p->fd, out, 4);
}
else
{
if ((y < 0) || (y >= p->device->height))
return;
/* Medium sized digit */
out[1] = 'o';
out[2] = y + 1;
out[3] = x + 1;
out[4] = digit;
Write(p->fd, out, 5);
}
}
/* ----- Display functions ----- */
void mtxorb_backlight_off(MTXORB *handle)
{
struct mtxorb_priv *p = handle;
Write(p->fd, "\xFE"
"F",
2);
}
void mtxorb_set_contrast(MTXORB *handle, int value)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 'P', 0};
if ((value < 0) || (value > 255))
return;
if (IS_LCD_TYPE || IS_LKD_TYPE)
{
out[2] = value;
Write(p->fd, out, 3);
}
}
void mtxorb_set_brightness(MTXORB *handle, int value)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0, 0};
if ((value < 0) || (value > 255))
return;
if (IS_VFD_TYPE || IS_VKD_TYPE)
{
if (value > 3)
value = 3;
out[1] = 'Y';
}
else
out[1] = '\x99';
out[2] = value;
Write(p->fd, out, 3);
}
void mtxorb_set_bg_color(MTXORB *handle, int r, int g, int b)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', '\x82', 0, 0, 0};
if (IS_LKD_TYPE)
{
out[2] = r & 0xFF;
out[3] = g & 0xFF;
out[4] = b & 0xFF;
Write(p->fd, out, 5);
}
}
/* ----- General Purpose Output functions ----- */
void mtxorb_set_output(MTXORB *handle, enum mtxorb_gpo_flags flags)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0, 0};
int i;
if (IS_LKD_TYPE || IS_VKD_TYPE)
{
for (i = 0; i < 6; i++)
{
out[1] = (flags & (1 << i)) ? 'W' : 'V';
out[2] = i + 1;
Write(p->fd, out, 3);
}
}
else
{
/* Only one output on LCD/VFD displays */
out[1] = (flags) ? 'W' : 'V';
Write(p->fd, out, 2);
}
}
/* ----- Keypad functions ----- */
void mtxorb_keypad_backlight_off(MTXORB *handle)
{
struct mtxorb_priv *p = handle;
if (IS_LKD_TYPE)
Write(p->fd, "\xFE"
"\x9B",
2);
}
void mtxorb_set_keypad_brightness(MTXORB *handle, int value)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', '\x9C', 0};
if (IS_LKD_TYPE)
{
if ((value < 0) || (value > 255))
return;
out[2] = value;
Write(p->fd, out, 3);
}
}
void mtxorb_set_key_auto_repeat(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', '\x7E', 0};
if (IS_LKD_TYPE || IS_VKD_TYPE)
{
out[2] = (on == MTXORB_ON) ? 1 : 0;
Write(p->fd, out, 3);
}
}
void mtxorb_set_key_debounce_time(MTXORB *handle, int value)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 'U', 0};
if ((value < 0) || (value > 255))
return;
if (IS_LKD_TYPE || IS_VKD_TYPE)
{
out[2] = value;
Write(p->fd, out, 3);
}
}
/* ------ Internal functions ----- */
static void mtxorb_set_key_auto_tx(MTXORB *handle, enum mtxorb_onoff on)
{
struct mtxorb_priv *p = handle;
unsigned char out[] = {'\xFE', 0};
if (IS_LKD_TYPE || IS_VKD_TYPE)
{
out[1] = (on == MTXORB_ON) ? 'A' : 'O';
Write(p->fd, out, 2);
}
}
static int mtxorb_validate_device_info(const struct mtxorb_device_info *info)
{
if ((info->width < 0) || (info->width > MAX_WIDTH) ||
(info->height < 0) || (info->height > MAX_HEIGHT))
return -1;
else if ((info->cellwidth < 0) || (info->cellwidth > MAX_CELLWIDTH) ||
(info->cellheight < 0) || (info->cellheight > MAX_CELLHEIGHT))
return -1;
else if ((info->type != MTXORB_LCD) && (info->type != MTXORB_LKD) &&
(info->type != MTXORB_VFD) && (info->type != MTXORB_VKD))
return -1;
return 0;
}