source: npl/mediabox/lcdproc_edwin/src/server/drivers/CFontz.c @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 14.2 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <termios.h>
5#include <fcntl.h>
6#include <string.h>
7#include <errno.h>
8#include "lcd.h"
9#include "CFontz.h"
10#include "drv_base.h"
11
12#include "../render.h"
13
14#include "../../shared/debug.h"
15#include "../../shared/str.h"
16
17static int custom=0;
18typedef enum {
19        hbar = 1,
20        vbar = 2,
21        bign = 4,
22        beat = 8 } custom_type;
23
24
25
26static int fd;
27
28static void CFontz_linewrap(int on);
29static void CFontz_autoscroll(int on);
30static void CFontz_hidecursor();
31static void CFontz_reboot();
32
33// TODO:  Get rid of this variable?
34lcd_logical_driver *CFontz;
35// TODO:  Get the frame buffers working right
36
37/////////////////////////////////////////////////////////////////
38// Opens com port and sets baud correctly...
39//
40int CFontz_init(lcd_logical_driver *driver, char *args)
41{
42   char *argv[64];
43   int argc;
44   struct termios portset;
45   int i;
46   int tmp;
47   int reboot=0;
48   
49   int contrast=140;
50   char device[256] = "/dev/lcd";
51   int speed=B9600;
52   
53   CFontz = driver;
54
55   //debug("CFontz_init: Args(all): %s\n", args);
56   
57   argc = get_args(argv, args, 64);
58
59   /*
60   for(i=0; i<argc; i++)
61   {
62      printf("Arg(%i): %s\n", i, argv[i]);
63   }
64   */
65   
66   for(i=0; i<argc; i++)
67   {
68      //printf("Arg(%i): %s\n", i, argv[i]);
69      if(0 == strcmp(argv[i], "-d")  ||
70         0 == strcmp(argv[i], "--device"))
71      {
72         if(i + 1 > argc) {
73            fprintf(stderr, "CFontz_init: %s requires an argument\n",
74                    argv[i]);
75            return -1;
76         }
77         strcpy(device, argv[++i]);
78      }
79      else if(0 == strcmp(argv[i], "-c")  ||
80         0 == strcmp(argv[i], "--contrast"))
81      {
82         if(i + 1 > argc) {
83            fprintf(stderr, "CFontz_init: %s requires an argument\n",
84                    argv[i]);
85            return -1;
86         }
87         tmp = atoi(argv[++i]);
88         if((tmp < 0) || (tmp > 255)){
89            fprintf(stderr, "CFontz_init: %s argument must between 0 and 255. Using default value.\n",
90                    argv[i]);
91         } else contrast = tmp;
92      }
93      else if(0 == strcmp(argv[i], "-b")  ||
94         0 == strcmp(argv[i], "--brightness"))
95      {
96         if(i + 1 > argc) {
97            fprintf(stderr, "CFontz_init: %s requires an argument\n",
98                    argv[i]);
99            return -1;
100         }
101         tmp = atoi(argv[++i]);
102         if((tmp < 0) || (tmp > 255)){
103            fprintf(stderr, "CFontz_init: %s argument must between 0 and 255. Using default value.\n",
104                    argv[i]);
105         } else backlight_brightness = tmp;
106      }
107      else if(0 == strcmp(argv[i], "-o")  ||
108         0 == strcmp(argv[i], "--off-bright"))
109      {
110         if(i + 1 > argc) {
111            fprintf(stderr, "CFontz_init: %s requires an argument\n",
112                    argv[i]);
113            return -1;
114         }
115         tmp = atoi(argv[++i]);
116         if((tmp < 0) || (tmp > 255)){
117            fprintf(stderr, "CFontz_init: %s argument must between 0 and 255. Using default value.\n",
118                    argv[i]);
119         } else backlight_off_brightness = tmp;
120      }
121      else if(0 == strcmp(argv[i], "-s")  ||
122         0 == strcmp(argv[i], "--speed"))
123      {
124         if(i + 1 > argc) {
125            fprintf(stderr, "CFontz_init: %s requires an argument\n",
126                    argv[i]);
127            return -1;
128         }
129         tmp = atoi(argv[++i]);
130         if(tmp==1200) speed=B1200;
131         else if(tmp==2400) speed=B2400;
132         else if(tmp==9600) speed=B9600;
133         else if(tmp==19200) speed=B19200;
134         else {
135            fprintf(stderr, "CFontz_init: %s argument must be 1200, 2400, or 9600. Using default value.\n",
136                    argv[i]);
137         }
138      }
139      else if(0 == strcmp(argv[i], "-h")  ||
140         0 == strcmp(argv[i], "--help"))
141      {
142         printf("LCDproc CrystalFontz LCD driver\n"
143                "\t-d\t--device\tSelect the output device to use [/dev/lcd]\n"
144                "\t-t\t--type\t\tSelect the LCD type (size) [20x4]\n"
145                "\t-c\t--contrast\tSet the initial contrast [140]\n"
146                "\t-b\t--brightness\tSet the initial brightness [255]\n"
147                "\t-o\t--off-bright\tSet the initial brightness [0]\n"
148                "\t-s\t--speed\t\tSet the communication speed [9600]\n"
149                "\t-r\t--reboot\tReinitialize the LCD's BIOS\n"
150                "\t-h\t--help\t\tShow this help information\n");
151         return -1;
152      }
153      else if(0 == strcmp(argv[i], "-r")  ||
154         0 == strcmp(argv[i], "--reboot"))
155      {
156         printf("LCDd: rebooting CrystalFontz LCD...\n");
157         reboot=1;
158      }
159      else
160      {
161         printf("Invalid parameter: %s\n", argv[i]);
162      }
163     
164   }
165   
166   // Set up io port correctly, and open it...
167   fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
168   if (fd == -1)
169   {
170      fprintf(stderr, "CFontz_init: failed (%s)\n", strerror(errno));
171      return -1;
172   }
173   //else fprintf(stderr, "CFontz_init: opened device %s\n", device);
174   tcgetattr(fd, &portset);
175   // This is necessary in Linux, but does not exist in irix.
176#ifndef IRIX
177   cfmakeraw(&portset);
178#endif
179   cfsetospeed(&portset, speed);
180   cfsetispeed(&portset, speed);
181   tcsetattr(fd, TCSANOW, &portset);
182   
183
184   // Set display-specific stuff..
185   if(reboot)
186   {
187      CFontz_reboot();
188      sleep(4);
189      reboot=0;
190   }
191   sleep(1);
192   CFontz_hidecursor();
193   CFontz_linewrap(1);
194   CFontz_autoscroll(0);
195   CFontz_backlight(backlight_brightness);
196
197   
198   if(!driver->framebuf)
199   {
200      fprintf(stderr, "CFontz_init: No frame buffer.\n");
201      driver->close();
202      return -1;
203   }
204
205
206   // Set the functions the driver supports...
207
208   driver->clear =      (void *)-1;
209   driver->string =     (void *)-1;
210//  driver->chr =        CFontz_chr;
211   driver->chr =        CFontz_chr;
212   driver->vbar =       CFontz_vbar;
213   driver->init_vbar =  CFontz_init_vbar;
214   driver->hbar =       CFontz_hbar;
215   driver->init_hbar =  CFontz_init_hbar;
216   driver->num =        CFontz_num;
217   driver->init_num =   CFontz_init_num;
218
219   driver->init =       CFontz_init;
220   driver->close =      CFontz_close;
221   driver->flush =      CFontz_flush;
222   driver->flush_box =  CFontz_flush_box;
223   driver->contrast =   CFontz_contrast;
224   driver->backlight =  CFontz_backlight;
225   driver->set_char =   CFontz_set_char;
226   driver->icon =       CFontz_icon;
227   driver->draw_frame = CFontz_draw_frame;
228
229   CFontz_contrast(contrast);
230
231   lcd.cellwid = 6;
232   lcd.cellhgt = 8;
233
234   debug("CFontz: foo!\n");
235   
236   return fd;
237}
238
239
240
241/////////////////////////////////////////////////////////////////
242// Clean-up
243//
244void CFontz_close()
245{
246  close (fd);
247
248  if(CFontz->framebuf) free(CFontz->framebuf);
249
250  CFontz->framebuf = NULL;
251}
252
253
254void CFontz_flush()
255{
256   CFontz_draw_frame(lcd.framebuf);
257}
258
259
260
261void CFontz_flush_box(int lft, int top, int rgt, int bot)
262{
263  int y;
264  char out[LCD_MAX_WIDTH];
265 
266 
267//  printf("Flush (%i,%i)-(%i,%i)\n", lft, top, rgt, bot);
268
269  for(y=top; y<=bot; y++)
270    {
271      sprintf(out, "%c%c%c", 17, lft, y);
272      write(fd, out, 4);
273      write(fd, lcd.framebuf+(y*lcd.wid)+lft, rgt-lft+1);
274     
275    }
276 
277 
278}
279
280
281/////////////////////////////////////////////////////////////////
282// Prints a character on the lcd display, at position (x,y).  The
283// upper-left is (1,1), and the lower right should be (20,4).
284//
285void CFontz_chr(int x, int y, char c)
286{
287  y--;
288  x--;
289 
290  if(c < 32  &&  c >= 0) c += 128;
291  lcd.framebuf[(y*lcd.wid) + x] = c;
292}
293
294
295/////////////////////////////////////////////////////////////////
296// Changes screen contrast (0-255; 140 seems good)
297//
298int CFontz_contrast(int contrast)
299{
300   int realcontrast;
301   char out[4];
302   static int status=140;
303
304   if(contrast > 0)
305   {
306      status=contrast;
307      realcontrast = (((int)(status)) * 100) / 255;
308      sprintf(out, "%c%c", 15, realcontrast);
309      write(fd, out, 3);
310   }
311
312   return status;
313}
314
315
316/////////////////////////////////////////////////////////////////
317// Sets the backlight on or off -- can be done quickly for
318// an intermediate brightness...
319//
320void CFontz_backlight(int on)
321{
322  char out[4];
323  if(on)
324  {
325    sprintf(out, "%c%c", 14, (unsigned char)(on * 100 / 255));
326    //sprintf(out, "%c%c", 14, 100);
327  }
328  else
329  {
330    sprintf(out, "%c%c", 14, 0);
331  }
332    write(fd, out, 3);
333}
334
335
336/////////////////////////////////////////////////////////////////
337// Toggle the built-in linewrapping feature
338//
339static void CFontz_linewrap(int on)
340{
341  char out[4];
342  if(on)
343    sprintf(out, "%c", 23);
344  else
345    sprintf(out, "%c", 24);
346  write(fd, out, 1);
347}
348
349/////////////////////////////////////////////////////////////////
350// Toggle the built-in automatic scrolling feature
351//
352static void CFontz_autoscroll(int on)
353{
354  char out[4];
355  if(on)
356    sprintf(out, "%c", 19);
357  else
358    sprintf(out, "%c", 20);
359  write(fd, out, 1);
360}
361
362
363/////////////////////////////////////////////////////////////////
364// Get rid of the blinking curson
365//
366static void CFontz_hidecursor()
367{
368  char out[4];
369  sprintf(out, "%c", 4);
370  write(fd, out, 1);
371}
372
373/////////////////////////////////////////////////////////////////
374// Reset the display bios
375//
376static void CFontz_reboot()
377{
378  char out[4];
379  sprintf(out, "%c", 26);
380  write(fd, out, 1);
381}
382
383
384/////////////////////////////////////////////////////////////////
385// Sets up for vertical bars.  Call before lcd.vbar()
386//
387void CFontz_init_vbar()
388{
389  char a[] = {
390    0,0,0,0,0,0,
391    0,0,0,0,0,0,
392    0,0,0,0,0,0,
393    0,0,0,0,0,0,
394    0,0,0,0,0,0,
395    0,0,0,0,0,0,
396    0,0,0,0,0,0,
397    0,1,1,1,1,1,
398  };
399  char b[] = {
400    0,0,0,0,0,0,
401    0,0,0,0,0,0,
402    0,0,0,0,0,0,
403    0,0,0,0,0,0,
404    0,0,0,0,0,0,
405    0,0,0,0,0,0,
406    0,1,1,1,1,1,
407    0,1,1,1,1,1,
408  };
409  char c[] = {
410    0,0,0,0,0,0,
411    0,0,0,0,0,0,
412    0,0,0,0,0,0,
413    0,0,0,0,0,0,
414    0,0,0,0,0,0,
415    0,1,1,1,1,1,
416    0,1,1,1,1,1,
417    0,1,1,1,1,1,
418  };
419  char d[] = {
420    0,0,0,0,0,0,
421    0,0,0,0,0,0,
422    0,0,0,0,0,0,
423    0,0,0,0,0,0,
424    0,1,1,1,1,1,
425    0,1,1,1,1,1,
426    0,1,1,1,1,1,
427    0,1,1,1,1,1,
428  };
429  char e[] = {
430    0,0,0,0,0,0,
431    0,0,0,0,0,0,
432    0,0,0,0,0,0,
433    0,1,1,1,1,1,
434    0,1,1,1,1,1,
435    0,1,1,1,1,1,
436    0,1,1,1,1,1,
437    0,1,1,1,1,1,
438  };
439  char f[] = {
440    0,0,0,0,0,0,
441    0,0,0,0,0,0,
442    0,1,1,1,1,1,
443    0,1,1,1,1,1,
444    0,1,1,1,1,1,
445    0,1,1,1,1,1,
446    0,1,1,1,1,1,
447    0,1,1,1,1,1,
448  };
449  char g[] = {
450    0,0,0,0,0,0,
451    0,1,1,1,1,1,
452    0,1,1,1,1,1,
453    0,1,1,1,1,1,
454    0,1,1,1,1,1,
455    0,1,1,1,1,1,
456    0,1,1,1,1,1,
457    0,1,1,1,1,1,
458  };
459
460  if(custom!=vbar) {
461    CFontz_set_char(1,a);
462    CFontz_set_char(2,b);
463    CFontz_set_char(3,c);
464    CFontz_set_char(4,d);
465    CFontz_set_char(5,e);
466    CFontz_set_char(6,f);
467    CFontz_set_char(7,g);
468    custom=vbar;
469  }
470}
471
472/////////////////////////////////////////////////////////////////
473// Inits horizontal bars...
474//
475void CFontz_init_hbar()
476{
477
478  char a[] = {
479    1,0,0,0,0,0,
480    1,0,0,0,0,0,
481    1,0,0,0,0,0,
482    1,0,0,0,0,0,
483    1,0,0,0,0,0,
484    1,0,0,0,0,0,
485    1,0,0,0,0,0,
486    1,0,0,0,0,0,
487  };
488  char b[] = {
489    1,1,0,0,0,0,
490    1,1,0,0,0,0,
491    1,1,0,0,0,0,
492    1,1,0,0,0,0,
493    1,1,0,0,0,0,
494    1,1,0,0,0,0,
495    1,1,0,0,0,0,
496    1,1,0,0,0,0,
497  };
498  char c[] = {
499    1,1,1,0,0,0,
500    1,1,1,0,0,0,
501    1,1,1,0,0,0,
502    1,1,1,0,0,0,
503    1,1,1,0,0,0,
504    1,1,1,0,0,0,
505    1,1,1,0,0,0,
506    1,1,1,0,0,0,
507  };
508  char d[] = {
509    1,1,1,1,0,0,
510    1,1,1,1,0,0,
511    1,1,1,1,0,0,
512    1,1,1,1,0,0,
513    1,1,1,1,0,0,
514    1,1,1,1,0,0,
515    1,1,1,1,0,0,
516    1,1,1,1,0,0,
517  };
518  char e[] = {
519    1,1,1,1,1,0,
520    1,1,1,1,1,0,
521    1,1,1,1,1,0,
522    1,1,1,1,1,0,
523    1,1,1,1,1,0,
524    1,1,1,1,1,0,
525    1,1,1,1,1,0,
526    1,1,1,1,1,0,
527  };
528  char f[] = {
529    1,1,1,1,1,1,
530    1,1,1,1,1,1,
531    1,1,1,1,1,1,
532    1,1,1,1,1,1,
533    1,1,1,1,1,1,
534    1,1,1,1,1,1,
535    1,1,1,1,1,1,
536    1,1,1,1,1,1,
537  };
538
539  if(custom!=hbar) {
540    CFontz_set_char(1,a);
541    CFontz_set_char(2,b);
542    CFontz_set_char(3,c);
543    CFontz_set_char(4,d);
544    CFontz_set_char(5,e);
545    CFontz_set_char(6,f);
546    custom=hbar;
547  }
548}
549
550/////////////////////////////////////////////////////////////////
551// Draws a vertical bar...
552//
553void CFontz_vbar(int x, int len)
554{
555  char map[9] = {32, 1, 2, 3, 4, 5, 6, 7, 255 };
556 
557
558  int y;
559  for(y=lcd.hgt; y > 0 && len>0; y--)
560    {
561      if(len >= lcd.cellhgt) CFontz_chr(x, y, 255);
562      else CFontz_chr(x, y, map[len]);
563
564      len -= lcd.cellhgt;
565    }
566 
567}
568
569/////////////////////////////////////////////////////////////////
570// Draws a horizontal bar to the right.
571//
572void CFontz_hbar(int x, int y, int len)
573{
574  char map[7] = { 32, 1, 2, 3, 4, 5, 6  };
575
576  for(; x<=lcd.wid && len>0; x++)
577    {
578      if(len >= lcd.cellwid) CFontz_chr(x,y,map[6]);
579      else CFontz_chr(x, y, map[len]);
580     
581      len -= lcd.cellwid;
582     
583    }
584
585}
586
587
588/////////////////////////////////////////////////////////////////
589// Sets up for big numbers.
590//
591void CFontz_init_num()
592{
593}
594
595
596/////////////////////////////////////////////////////////////////
597// Writes a big number.
598//
599void CFontz_num(int x, int num)
600{
601  char out[5];
602  sprintf(out, "%c%c%c", 28, x, num);
603  write(fd, out, 3);
604}
605
606
607/////////////////////////////////////////////////////////////////
608// Sets a custom character from 0-7...
609//
610// For input, values > 0 mean "on" and values <= 0 are "off".
611//
612// The input is just an array of characters...
613//
614void CFontz_set_char(int n, char *dat)
615{
616  char out[4];
617  int row, col;
618  int letter;
619
620  if(n < 0 || n > 7) return;
621  if(!dat) return;
622
623  sprintf(out, "%c%c", 25, n);
624  write(fd, out, 2);
625
626 
627  for(row=0; row<lcd.cellhgt; row++)
628  {
629    letter = 0;
630    for(col=0; col<lcd.cellwid; col++)
631    {
632      letter <<= 1;
633      letter |= (dat[(row*lcd.cellwid) + col] > 0);
634    }
635    write(fd, &letter, 1);
636  }
637}
638
639
640void CFontz_icon(int which, char dest)
641{
642  char icons[3][6*8] = {
643   {
644     1,1,1,1,1,1,  // Empty Heart
645     1,0,1,0,1,1,
646     0,0,0,0,0,1,
647     0,0,0,0,0,1,
648     0,0,0,0,0,1,
649     1,0,0,0,1,1,
650     1,1,0,1,1,1,
651     1,1,1,1,1,1,
652   },   
653
654   {
655     1,1,1,1,1,1,  // Filled Heart
656     1,0,1,0,1,1,
657     0,1,0,1,0,1,
658     0,1,1,1,0,1,
659     0,1,1,1,0,1,
660     1,0,1,0,1,1,
661     1,1,0,1,1,1,
662     1,1,1,1,1,1,
663   },
664   
665   {
666     0,0,0,0,0,0,  // Ellipsis
667     0,0,0,0,0,0,
668     0,0,0,0,0,0,
669     0,0,0,0,0,0,
670     0,0,0,0,0,0,
671     0,0,0,0,0,0,
672     0,0,0,0,0,0,
673     1,0,1,0,1,0,
674   },
675   
676  };
677 
678  if(custom==bign) custom=beat;
679  CFontz_set_char(dest, &icons[which][0]);
680}
681
682
683/////////////////////////////////////////////////////////////
684// Blasts a single frame onscreen, to the lcd...
685//
686// Input is a character array, sized lcd.wid*lcd.hgt
687//
688void CFontz_draw_frame(char *dat)
689{
690  char out[LCD_MAX_WIDTH * LCD_MAX_HEIGHT];
691  int i;
692 
693  if(!dat) return;
694
695  // Custom characters start at 128, not at 0.
696  /*
697  for(i=0; i<lcd.wid*lcd.hgt; i++)
698  {
699     if(dat[i] < 32  &&  dat[i] >= 0) dat[i] += 128;
700  }
701  */
702 
703  for(i=0; i<lcd.hgt; i++)
704  {
705     sprintf(out, "%c%c%c", 17, 0, i);
706     write(fd, out, 3);
707     write(fd, dat+(lcd.wid*i), lcd.wid);
708  }
709  /*
710    sprintf(out, "%c", 1);
711    write(fd, out, 1);
712    write(fd, dat, lcd.wid*lcd.hgt);
713  */
714
715}
716
717
718
Note: See TracBrowser for help on using the repository browser.