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