summaryrefslogtreecommitdiff
path: root/src/hal/components/lcd.c
blob: 96f74cae1531fe9315133142a778cb8306b68d64 (plain)
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

//
//    Copyright (C) 2013 Andy Pugh

//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
//

/* A component to stream HAL data to an LCD in a user-defined format */

#include "rtapi.h"
#include "rtapi_app.h"
#include "rtapi_string.h"
#include "hal.h"
#include "rtapi_math.h"
#include <linux/types.h>

#ifdef SIM
#include <stdio.h>
#include <stdlib.h>
#endif

/* module information */
MODULE_AUTHOR("Andy Pugh");
MODULE_DESCRIPTION("Hal-to-text component for Mesa 7i73 and similar");
MODULE_LICENSE("GPL");

#define MAX_CHAN 16
#define MAX_ENTRY 20

typedef struct {
    void **args;
    int num_args;
    char *fmt;
    int length;
}lcd_page_t;

typedef struct {
    lcd_page_t *pages;
    int num_pages;
    hal_u32_t *page_num;
    hal_u32_t last_page;
    hal_u32_t *out;
    hal_float_t *contrast;
    hal_float_t last_contrast;
    char buff[MAX_ENTRY + 1];
    int c_ptr;
    int f_ptr;
    int a_ptr;
    unsigned int dp;
}lcd_inst_t;

typedef struct {
    lcd_inst_t *insts;
    int num_insts;
}lcd_t;

static int comp_id;
static lcd_t *lcd;
static void write(void *arg, long period);
static void write_one(lcd_inst_t *inst);

static int parse_fmt(char *in, int *ptr, char *out, void *val, char dp);

char *digits = "0123456789ABCDEF";

char *fmt_strings[MAX_CHAN];
RTAPI_MP_ARRAY_STRING(fmt_strings, MAX_CHAN, "screen formatting scancodes")

#ifndef do_div
# define do_div(n,base) ({					 \
	    __u32 __base = (base);				 \
	    __u32 __rem;					 \
	    __rem = ((__u64)(n)) % __base;			 \
	    (n) = ((__u64)(n)) / __base;			 \
	    __rem;						 \
	})
#endif

int rtapi_app_main(void){
    int i, f, f1, k, p;
    int retval;
    
    if (!fmt_strings[0]){
        rtapi_print_msg(RTAPI_MSG_ERR, "The LCD component requires at least one valid format string");
        return -EINVAL;
    }

    comp_id = hal_init("lcd");
    if (comp_id < 0) {
        rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: hal_init() failed\n");
        return -1;
    }
    
    // allocate shared memory for data
    lcd = hal_malloc(sizeof(lcd_t));
    if (lcd == 0) {
        rtapi_print_msg(RTAPI_MSG_ERR,
                        "lcd component: Out of Memory\n");
        hal_exit(comp_id);
        return -1;
    }
    
    // Count the instances. Very unlikely to be more than one, but...
    for (lcd->num_insts = 0; fmt_strings[lcd->num_insts];lcd->num_insts++){}
    lcd->insts = hal_malloc(lcd->num_insts * sizeof(lcd_inst_t));
    
    for (i = 0; i < lcd->num_insts; i++){
        lcd_inst_t *inst = &lcd->insts[i];
        inst->num_pages = 1;
        
        // count the pages demarked by | chars.
        for (f = 0; fmt_strings[i][f]; f++){
            if (fmt_strings[i][f] =='|') inst->num_pages++;
        }
        inst->pages = hal_malloc(inst->num_pages * sizeof(lcd_page_t));
        
        //second pass
        f1 = k = p = 0;
        for (f = 0; fmt_strings[i][f]; f++){
            if (fmt_strings[i][f] =='%') {
                int type = parse_fmt(fmt_strings[i], &f, NULL, NULL, 0);
                if (type > 0) {
                    inst->pages[p].num_args++;
                }
            }
            
            if (fmt_strings[i][f + 1] =='|' || fmt_strings[i][f + 1] == 0) {
                inst->pages[p].fmt = hal_malloc(f - f1 + 2);
                retval = snprintf(inst->pages[p].fmt,
                                  f - f1 + 2, "%s",
                                  fmt_strings[i] + f1);
                inst->pages[p].length = f - f1 + 2;
                inst->pages[p].args = hal_malloc(inst->pages[p].num_args
                                                 * sizeof(hal_float_t));
                f1 = f + 2;
                {
                    int a = -1, s = -1;
                    lcd_page_t page = inst->pages[p];
                    
                    while (page.fmt[++s]){
                        
                        if (page.fmt[s] == '%'){
                            int type = parse_fmt(page.fmt, &s, NULL, NULL, 0);
                            a++;
                            switch (type){
                                case 'f':
                                    retval = hal_pin_float_newf(HAL_IN,
                                                                (hal_float_t**)&(inst->pages[p].args[a]), comp_id,
                                                                "lcd.%02i.page.%02i.arg.%02i",
                                                                i, p, a);
                                    if (retval != 0) {
                                        return retval;
                                    }
                                    break;
                                case 'u':
                                case 'c':
                                    retval = hal_pin_u32_newf(HAL_IN,
                                                              (hal_u32_t **)&(inst->pages[p].args[a]), comp_id,
                                                              "lcd.%02i.page.%02i.arg.%02i",
                                                              i, p, a);
                                    if (retval != 0) {
                                        return retval;
                                    }
                                    
                                    break;
                                case 's':
                                    retval = hal_pin_s32_newf(HAL_IN,
                                                              (hal_s32_t **)&(inst->pages[p].args[a]), comp_id,
                                                              "lcd.%02i.page.%02i.arg.%02i",
                                                              i, p, a);
                                    if (retval != 0) {
                                        return retval;
                                    }
                                    break;
                                case 'b':
                                    retval = hal_pin_bit_newf(HAL_IN,
                                                              (hal_bit_t **)&(inst->pages[p].args[a]), comp_id,
                                                              "lcd.%02i.page.%02i.arg.%02i",
                                                              i, p, a);
                                    if (retval != 0) {
                                        return retval;
                                    }
                                    break;
                            }
                        }
                    }
                }
                p++; // increment the page index
            }
        }
    }
    retval = hal_export_funct("lcd", write, lcd, 1, 0, comp_id); //needs fp?
    if (retval < 0) {
        rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: function export failed\n");
        return -1;
    }
    
    for (i = 0; i < lcd->num_insts; i++){
        retval = hal_pin_u32_newf(HAL_IN, &(lcd->insts[i].page_num), comp_id,
                                  "lcd.%02i.page_num", i);
        if (retval != 0) {
            return retval;
        }
        lcd->insts[i].last_page = 0xFFFF; // force screen refresh
        retval = hal_pin_u32_newf(HAL_OUT, &(lcd->insts[i].out), comp_id,
                                  "lcd.%02i.out",i);
        if (retval != 0) {
            return retval;
        }
        retval = hal_pin_float_newf(HAL_IN, &(lcd->insts[i].contrast), comp_id,
                                    "lcd.%02i.contrast",i);
        if (retval != 0) {
            return retval;
        }
        retval = hal_param_u32_newf(HAL_RW, &(lcd->insts[i].dp), comp_id,
                                    "lcd.%02i.decimal-separator",i);
        lcd->insts[i].dp = '.';
        if (retval != 0) {
            return retval;
        }
        lcd->insts[i].f_ptr = 0;
        lcd->insts[i].buff[0] = 0x11; // turn off cursor. More init can be added
        lcd->insts[i].buff[0] = 0;
        lcd->insts[i].c_ptr = 0;
    }
    return 0;
}

void write(void *arg, long period){
    lcd_t *lcd;
    int i;
    
    lcd = arg;

    for (i = 0; i < lcd->num_insts; i++){
        
        lcd_inst_t *inst = &lcd->insts[i];
        write_one(inst);
    }
}

static void write_one(lcd_inst_t *inst){

    int retval;
    char c1, c2;
    
    if (inst->buff[inst->c_ptr] != 0){
        *inst->out = inst->buff[inst->c_ptr++];
        return;
    }
    
    inst->c_ptr = 0;
    inst->buff[0] = 0;
    
    if (*inst->page_num != inst->last_page){
        inst->last_page = *inst->page_num;
        if (*inst->page_num >= inst->num_pages) return; // should this error?
        *inst->out = 0x11; //cursor off
        inst->buff[0] = 0x1A; //dummy
        inst->buff[1] = 0; //end
        inst->c_ptr = 0;
        inst->f_ptr = 0;
        inst->a_ptr = 0;
        return;
    }    
    
    if (inst->f_ptr > inst->pages[*inst->page_num].length){
        *inst->out = 0x18; // clear line
        inst->buff[0] = 0x1E; // home
        inst->buff[1] = 0; //end
        inst->c_ptr = 0;
        inst->f_ptr = 0;
        inst->a_ptr = 0;
        return;
    }
    
    if (*inst->contrast != inst->last_contrast){
        int c = *inst->contrast * 159.0 + 0x20;
        if (c > 0xBF) c = 0xBF;
        if (c < 0x20) c = 0x20;
        inst->last_contrast = *inst->contrast;
        *inst->out = 0x1B;
        inst->buff[0] = 'C';
        inst->buff[1] = c;
        inst->buff[2] = 0;
        inst->c_ptr = 0;
        return;
    }
    
    switch (inst->pages[*inst->page_num].fmt[inst->f_ptr]){
        case '\\': //escape chars
            c1 = inst->pages[*inst->page_num].fmt[++inst->f_ptr];
            switch (c1){
                case 'n':
                case 'N':
                    inst->f_ptr++;
                    inst->buff[0] = 0x18; //erase to end
                    inst->buff[1] = 0x0A; //CR
                    inst->buff[2] = 0x0D; //LF
                    inst->buff[3] = 0;
                    break;
                case 't':
                case 'T':
                    inst->f_ptr++;
                    inst->buff[0] = ' ';
                    inst->buff[1] = ' ';
                    inst->buff[2] = ' ';
                    inst->buff[3] = ' ';
                    inst->buff[4] = 0;
                    break;
                case '\\':
                    inst->f_ptr++;
                    inst->buff[0] = '\\';
                    inst->buff[1] = 0;
                    
                default: //check for hex
                    c2 = inst->pages[*inst->page_num].fmt[++inst->f_ptr];
                    inst->f_ptr++;
                    c1 &= 0xDF; c2 &= 0xDF; //upper case
                    if (strchr(digits, c1) && strchr(digits, c2)){
                        inst->buff[0] = (16 * (strchr(digits, c1) - digits) 
                                               + (strchr(digits, c2) - digits));
                        inst->buff[1] = 0;
                    }
            }
            *inst->out = inst->buff[0];
            inst->c_ptr = 1;
            return;
        case '%':
            retval = parse_fmt(inst->pages[*inst->page_num].fmt,
                               &inst->f_ptr,
                               inst->buff,
                               inst->pages[*inst->page_num].args[inst->a_ptr++],
                               (char)inst->dp);
            if (retval >= 0) {
                *inst->out = inst->buff[0];
                inst->c_ptr = 1;
                inst->f_ptr++;
                return;
            }
        default:
            *inst->out = inst->pages[*inst->page_num].fmt[inst->f_ptr++];
    }
}

int num_digits_baseN(__u64 val, int base){
    int n = 1;
    __u64 m = 1;
    while ((m *= base) <= val){
        n += 1;
    }
    return n;
}

static int parse_fmt(char *in, int *ptr, char *out, void *val, char dp){
    /*parse val into the format in in pointed to by ptr.
     if out is null, then simply return the type of the format
     as u, s or f. a return value of -1 indicates an invalid pointer
     on exit ptr will point after the end of the format*/
    
    const double pow10[] = {1,10,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,
        1e12,1e13,1e14,1e15,1e16,1e17,1e18,1e19,1e20};
    
    char fill = ' ';

    int d = 0; // dot found
    char sgn = ' ';
    int c = -1, m = -1; // Length and Mantissa sig. figs
    int base = 10;
    if (in[*ptr] != '%') return -EINVAL;
    (*ptr)++;
    while (in[*ptr]){
        switch (in[*ptr]){
            case '%':
                if (out != NULL) out[0] = '%';
                return 't';
            case '.':
                if (d) return -EINVAL;
                d = 1;
                dp = '.';
                break;
            case ' ':
                fill = ' ';
                break;
            case '+':
                sgn = '+';
                break;
            case '0':
                if (c < 0 && d == 0){
                    fill = '0';
                    break;
                }
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                if (d){
                    if (m > 0){
                        m = m * 10 + (in[*ptr] - '0');
                    }
                    else
                    {
                        m = (in[*ptr] - '0');
                    }
                }
                else
                {
                    if (c > 0){
                        c = c * 10 + (in[*ptr] - '0');
                    }
                    else
                    {
                        c = (in[*ptr] - '0');
                    }
                }
                
                if (c > MAX_ENTRY) c = MAX_ENTRY;
                
                break;
                        
            case 'x':
            case 'X':
                base = 16;
            case 'o':
                if (base == 10) base = 8;
            case 'u':
                if (out == NULL || val == NULL) return 'u';
            {
                unsigned int tmp;
                int i;
                unsigned int v = *((hal_u32_t*)val);

                if (c < 1) c = num_digits_baseN(v, base);

                tmp = v;
                for (i = c - 1; i >= 0; i--){
                    if (tmp != 0 || i == (c-1)){
                        out[i] = digits[tmp % base];
                    }
                    else
                    {
                        out[i] = fill;
                    }
                    tmp /= base;
                }
                if (tmp !=0){ // it didn't fit
                    for (i = 0; i < c; i++){out[i] = '*';}
                }
                out[c] = 0; //terminate string
            }
                return 'u';
                break;
            case 'd':
            case 'i':
                if (out == NULL || val == NULL) return 's';
            {
                int tmp, s = 0;
                int i;
                int v = *((hal_s32_t*)val);
                
                if (sgn == '+') s = 1;
                if (v < 0) {s = 1; sgn = '-'; v = -v;}
                
                if (c < 1) c = num_digits_baseN(v, base) + s;
 
                tmp = abs(v);
                for (i = c - 1; i >= s; i--){
                    if (tmp != 0 || i == c - 1){
                        out[i] = digits[tmp % 10];
                        tmp /= 10;
                    }
                    else
                    {
                        if (s && fill == ' '){
                            out[i] = sgn;
                            s = 0;
                        }
                        else
                        {
                            out[i] = fill;
                        }
                    }
                }
                if (tmp !=0){ // it didn't fit
                    for (i = 0; i < c; i++){out[i] = '*';}
                }
                if (s) out[0] = sgn;
                
                out[c] = 0;
                
                return 's';
            }
            case 'f':
            case 'F': //The reason for all this messing about, no %f format in kernel code!!!!
                if (out == NULL || val == NULL) return 'f';
            {
                int i;
                double v = *((hal_float_t*)val);
                __u64 tmp = 0; //enough bits for 9 decimal digits.
                int s = 0;
                
                if (sgn != ' ') s = 1;
                if (v < 0) {s = 1; sgn = '-'; v = -v;}
                
                if (m < 0) m = 4;
                
                if (c < 1) c = num_digits_baseN(v, base) + m + 1 + s;
                
                if (c > MAX_ENTRY){ // then it won't fit
                    tmp = 2;
                    goto overflow;
                }
                
                tmp = v * pow10[m] + 0.5;
                
                for (i = c - 1; i > c - m - 1 ; i--){
                    out[i] = digits[do_div(tmp, 10)];
                }
                
                if (m > 0) {
                    out[i] = dp;
                }
                else
                {
                    m = -1; // shuffle down into decimal point
                }
                
                for (i = c - m - 2; i >= s ; i--){
                    if (tmp > 0 || i == c - m - 2){
                        out[i] = digits[do_div(tmp, 10)];
                    }
                    else
                    {
                        if (sgn != ' ' && fill == ' '){
                            s = 0;
                            out[i] = sgn;
                            sgn = ' ';
                        }
                        else
                        {
                            out[i] = fill;
                        }
                    }
                }
                
                if (s){
                    out[0] = sgn;
                }
                
            overflow:
                if (tmp > 0){ // it didn't fit
                    for (i = 1; i < c; i++){out[i] = '*';}
                }
                
                out[c] = 0; //terminate the string
                
                return 'f';
            }
            case 'c':
                if (out == NULL || val == NULL) return 'c';
            {
                int i;
                unsigned char v = *((hal_u32_t*)val);
                
                if (c == 0) c = 1;
                for (i = 0; i < c; i++){
                    out[i] = (v > ' ')? v : ' ';
                }
                out[i] = 0;
                return 'c';
            }
            case 'b':
                if (out == NULL || val == NULL) return 'b';
            {
                char c1, c2;
                char bt = 'E', bf = 'E'; // a hint that there is a format error
                int v = *((hal_bit_t*)val);
                if (in[++(*ptr)] == '\\'){
                    c1 = in[++(*ptr)];
                    if (c1 > '9') c1 &= 0xDF;
                    c2 = in[++(*ptr)];
                    if (c2 > '9') c2 &= 0xDF;
                    if (strchr(digits, c1) && strchr(digits, c2)){
                        bt = (16 * (strchr(digits, c1) - digits) 
                              + (strchr(digits, c2) - digits));
                    }
                } 
                else
                {
                    bt = in[*ptr];
                }
                if (in[++(*ptr)] == '\\'){
                    c1 = in[++(*ptr)] & 0xDF;
                    c2 = in[++(*ptr)] & 0xDF;
                    if (strchr(digits, c1) && strchr(digits, c2)){
                        bf = (16 * (strchr(digits, c1) - digits) 
                              + (strchr(digits, c2) - digits));
                    }
                } 
                else
                {
                    bf = in[*ptr];
                }
                out[0] = (v)?bt:bf;
                out[1] = 0;
                return 'b';
            }
        }
        (*ptr)++;
    }
    return -EINVAL;
}

void rtapi_app_exit(void)
{
    hal_exit(comp_id);
}