graphlcd-base/glcddrivers/image.c
author root@rika
Thu, 23 Apr 2009 20:55:41 +0200
changeset 33 7a0c4b0354ba
parent 4 df6a40031aa5
permissions -rw-r--r--
updated documentation
     1 /*
     2  * GraphLCD driver library
     3  *
     4  * image.c  -  Image output device
     5  *             Output goes to a image file instead of LCD.
     6  *
     7  * This file is released under the GNU General Public License. Refer
     8  * to the COPYING file distributed with this package.
     9  *
    10  * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
    11  */
    12 
    13 #include <stdio.h>
    14 #include <syslog.h>
    15 
    16 #include "common.h"
    17 #include "config.h"
    18 #include "image.h"
    19 
    20 
    21 namespace GLCD
    22 {
    23 
    24 cDriverImage::cDriverImage(cDriverConfig * config)
    25 :   config(config)
    26 {
    27     oldConfig = new cDriverConfig(*config);
    28 }
    29 
    30 cDriverImage::~cDriverImage()
    31 {
    32     delete oldConfig;
    33 }
    34 
    35 int cDriverImage::Init()
    36 {
    37     width = config->width;
    38     if (width <= 0)
    39         width = 240;
    40     height = config->height;
    41     if (height <= 0)
    42         height = 128;
    43     lineSize = (width + 7) / 8;
    44 
    45     for (unsigned int i = 0; i < config->options.size(); i++)
    46     {
    47         if (config->options[i].name == "")
    48         {
    49         }
    50     }
    51 
    52     newLCD = new unsigned char[lineSize * height];
    53     if (newLCD)
    54         memset(newLCD, 0, lineSize * height);
    55     oldLCD = new unsigned char[lineSize * height];
    56     if (oldLCD)
    57         memset(oldLCD, 0, lineSize * height);
    58 
    59     counter = 0;
    60 
    61     *oldConfig = *config;
    62 
    63     // clear display
    64     Clear();
    65 
    66     syslog(LOG_INFO, "%s: image driver initialized.\n", config->name.c_str());
    67     return 0;
    68 }
    69 
    70 int cDriverImage::DeInit()
    71 {
    72     if (newLCD)
    73         delete[] newLCD;
    74     if (oldLCD)
    75         delete[] oldLCD;
    76     return 0;
    77 }
    78 
    79 int cDriverImage::CheckSetup()
    80 {
    81     if (config->width != oldConfig->width ||
    82         config->height != oldConfig->height)
    83     {
    84         DeInit();
    85         Init();
    86         return 0;
    87     }
    88 
    89     if (config->upsideDown != oldConfig->upsideDown ||
    90         config->invert != oldConfig->invert)
    91     {
    92         oldConfig->upsideDown = config->upsideDown;
    93         oldConfig->invert = config->invert;
    94         return 1;
    95     }
    96     return 0;
    97 }
    98 
    99 void cDriverImage::Clear()
   100 {
   101     memset(newLCD, 0, lineSize * height);
   102 }
   103 
   104 void cDriverImage::Set8Pixels(int x, int y, unsigned char data)
   105 {
   106     if (x >= width || y >= height)
   107         return;
   108 
   109     if (!config->upsideDown)
   110     {
   111         // normal orientation
   112         newLCD[lineSize * y + x / 8] |= data;
   113     }
   114     else
   115     {
   116         // upside down orientation
   117         x = width - 1 - x;
   118         y = height - 1 - y;
   119         newLCD[lineSize * y + x / 8] |= ReverseBits(data);
   120     }
   121 }
   122 
   123 void cDriverImage::Refresh(bool refreshAll)
   124 {
   125     int i;
   126     bool refresh;
   127     char fileName[256];
   128     char str[32];
   129     FILE * fp;
   130     unsigned char c;
   131 
   132     refresh = false;
   133     if (CheckSetup() > 0)
   134         refresh = true;
   135 
   136     for (i = 0; i < lineSize * height; i++)
   137     {
   138         if (newLCD[i] != oldLCD[i])
   139         {
   140             refresh = true;
   141             break;
   142         }
   143     }
   144 
   145     if (refresh)
   146     {
   147         sprintf(fileName, "%s/%s%05d.%s", "/tmp", "lcd", counter, "pbm");
   148         fp = fopen(fileName, "wb");
   149         if (fp)
   150         {
   151             sprintf(str, "P4\n%d %d\n", width, height);
   152             fwrite(str, strlen(str), 1, fp);
   153             for (i = 0; i < lineSize * height; i++)
   154             {
   155                 c = newLCD[i] ^ (config->invert ? 0xff : 0x00);
   156                 fwrite(&c, 1, 1, fp);
   157                 oldLCD[i] = newLCD[i];
   158             }
   159             fclose(fp);
   160         }
   161         counter++;
   162         if (counter > 99999)
   163             counter = 0;
   164     }
   165 }
   166 
   167 } // end of namespace