graphlcd-base/glcddrivers/simlcd.c
author root@rika
Wed, 06 Feb 2008 17:32:55 +0000
changeset 4 df6a40031aa5
permissions -rw-r--r--
added graphlcd-base
     1 /*
     2  * GraphLCD driver library
     3  *
     4  * simlcd.c  -  SimLCD driver class
     5  *              Output goes to a file instead of lcd.
     6  *              Use SimLCD tool to view this file.
     7  *
     8  * This file is released under the GNU General Public License. Refer
     9  * to the COPYING file distributed with this package.
    10  *
    11  * (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
    12  */
    13 
    14 #include <stdio.h>
    15 #include <syslog.h>
    16 
    17 #include "common.h"
    18 #include "config.h"
    19 #include "simlcd.h"
    20 
    21 
    22 namespace GLCD
    23 {
    24 
    25 cDriverSimLCD::cDriverSimLCD(cDriverConfig * config)
    26 :   config(config)
    27 {
    28     oldConfig = new cDriverConfig(*config);
    29 }
    30 
    31 cDriverSimLCD::~cDriverSimLCD()
    32 {
    33     delete oldConfig;
    34 }
    35 
    36 int cDriverSimLCD::Init()
    37 {
    38     width = config->width;
    39     if (width <= 0)
    40         width = 240;
    41     height = config->height;
    42     if (height <= 0)
    43         height = 128;
    44 
    45     for (unsigned int i = 0; i < config->options.size(); i++)
    46     {
    47         if (config->options[i].name == "")
    48         {
    49         }
    50     }
    51 
    52     // setup lcd array
    53     LCD = new unsigned char *[(width + 7) / 8];
    54     if (LCD)
    55     {
    56         for (int x = 0; x < (width + 7) / 8; x++)
    57         {
    58             LCD[x] = new unsigned char[height];
    59             memset(LCD[x], 0, height);
    60         }
    61     }
    62 
    63     *oldConfig = *config;
    64 
    65     // clear display
    66     Clear();
    67 
    68     syslog(LOG_INFO, "%s: SIMLCD initialized.\n", config->name.c_str());
    69     return 0;
    70 }
    71 
    72 int cDriverSimLCD::DeInit()
    73 {
    74     // free lcd array
    75     if (LCD)
    76     {
    77         for (int x = 0; x < (width + 7) / 8; x++)
    78         {
    79             delete[] LCD[x];
    80         }
    81         delete[] LCD;
    82     }
    83 
    84     return 0;
    85 }
    86 
    87 int cDriverSimLCD::CheckSetup()
    88 {
    89     if (config->width != oldConfig->width ||
    90         config->height != oldConfig->height)
    91     {
    92         DeInit();
    93         Init();
    94         return 0;
    95     }
    96 
    97     if (config->upsideDown != oldConfig->upsideDown ||
    98         config->invert != oldConfig->invert)
    99     {
   100         oldConfig->upsideDown = config->upsideDown;
   101         oldConfig->invert = config->invert;
   102         return 1;
   103     }
   104     return 0;
   105 }
   106 
   107 void cDriverSimLCD::Clear()
   108 {
   109     for (int x = 0; x < (width + 7) / 8; x++)
   110         memset(LCD[x], 0, height);
   111 }
   112 
   113 void cDriverSimLCD::Set8Pixels(int x, int y, unsigned char data)
   114 {
   115     if (x >= width || y >= height)
   116         return;
   117 
   118     if (!config->upsideDown)
   119     {
   120         // normal orientation
   121         LCD[x / 8][y] = LCD[x / 8][y] | data;
   122     }
   123     else
   124     {
   125         // upside down orientation
   126         x = width - 1 - x;
   127         y = height - 1 - y;
   128         LCD[x / 8][y] = LCD[x / 8][y] | ReverseBits(data);
   129     }
   130 }
   131 
   132 void cDriverSimLCD::Refresh(bool refreshAll)
   133 {
   134     FILE * fp = NULL;
   135     int x;
   136     int y;
   137     int i;
   138     unsigned char c;
   139 
   140     if (CheckSetup() > 0)
   141         refreshAll = true;
   142 
   143     fp = fopen("/tmp/simlcd.sem", "r");
   144     if (!fp || refreshAll)
   145     {
   146         if (fp)
   147             fclose(fp);
   148         fp = fopen("/tmp/simlcd.dat", "w");
   149         if (fp)
   150         {
   151             for (y = 0; y < height; y++)
   152             {
   153                 for (x = 0; x < (width + 7) / 8; x++)
   154                 {
   155                     c = LCD[x][y] ^ (config->invert ? 0xff : 0x00);
   156                     for (i = 0; i < 8; i++)
   157                     {
   158                         if (c & 0x80)
   159                         {
   160                             fprintf(fp,"#");
   161                         }
   162                         else
   163                         {
   164                             fprintf(fp,".");
   165                         }
   166                         c = c << 1;
   167                     }
   168                 }
   169                 fprintf(fp,"\n");
   170             }
   171             fclose(fp);
   172         }
   173 
   174         fp = fopen("/tmp/simlcd.sem", "w");
   175         fclose(fp);
   176     }
   177     else
   178     {
   179         fclose(fp);
   180     }
   181 }
   182 
   183 } // end of namespace