graphlcd-base/glcddrivers/g15daemon.c
changeset 4 df6a40031aa5
equal deleted inserted replaced
3:d0e62fc47285 4:df6a40031aa5
       
     1 /*
       
     2 * GraphLCD driver library
       
     3 *
       
     4 * g15daemon.c  -  pseudo device for the g15daemon meta driver
       
     5 *                   Output goes to the g15daemon which then displays it
       
     6 *
       
     7 * This file is released under the GNU General Public License. Refer
       
     8 * to the COPYING file distributed with this package.
       
     9 *
       
    10 */
       
    11 
       
    12 #include <fcntl.h>
       
    13 #include <stdio.h>
       
    14 #include <syslog.h>
       
    15 #include <unistd.h>
       
    16 #include <sys/mman.h>
       
    17 #include <sys/ioctl.h>
       
    18 #include <sys/socket.h>
       
    19 #include <poll.h>
       
    20 #include <arpa/inet.h>
       
    21 #include <unistd.h>
       
    22 
       
    23 #include "common.h"
       
    24 #include "config.h"
       
    25 
       
    26 #include "g15daemon.h"
       
    27 
       
    28 #define G15SERVER_PORT 15550
       
    29 #define G15SERVER_ADDR "127.0.0.1"
       
    30 
       
    31 #define G15_WIDTH 160
       
    32 #define G15_HEIGHT 43
       
    33 
       
    34 
       
    35 static int g15_send(int sock, char *buf, int len)
       
    36 {
       
    37     int total = 0;
       
    38     int retval = 0;
       
    39     int bytesleft = len;
       
    40 
       
    41     while (total < len) {
       
    42         retval = send(sock, buf+total, bytesleft, 0);
       
    43         if (retval == -1) {
       
    44             break;
       
    45         }
       
    46         bytesleft -= retval;
       
    47         total += retval;
       
    48     }
       
    49     return retval==-1?-1:0;
       
    50 }
       
    51 
       
    52 static int g15_recv(int sock, char *buf, int len)
       
    53 {
       
    54     int total = 0;
       
    55     int retval = 0;
       
    56     int bytesleft = len;
       
    57 
       
    58     while (total < len) {
       
    59         retval = recv(sock, buf+total, bytesleft, 0);
       
    60         if (retval < 1) {
       
    61             break;
       
    62         }
       
    63         total += retval;
       
    64         bytesleft -= retval;
       
    65     }
       
    66     return total;
       
    67 }
       
    68 
       
    69 static int open_g15_daemon()
       
    70 {
       
    71     int g15screen_fd;
       
    72     struct sockaddr_in serv_addr;
       
    73 
       
    74     char buffer[256];
       
    75 
       
    76     g15screen_fd = socket(AF_INET, SOCK_STREAM, 0);
       
    77     if (g15screen_fd < 0)
       
    78         return -1;
       
    79 
       
    80     memset(&serv_addr, 0, sizeof(serv_addr));
       
    81     serv_addr.sin_family      = AF_INET;
       
    82     inet_aton (G15SERVER_ADDR, &serv_addr.sin_addr);
       
    83     serv_addr.sin_port        = htons(G15SERVER_PORT);
       
    84 
       
    85     if (connect(g15screen_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
       
    86         return -1;
       
    87 
       
    88     memset(buffer,0,256);
       
    89     if (g15_recv(g15screen_fd, buffer, 16)<0)
       
    90         return -1;
       
    91 
       
    92     /* here we check that we're really talking to the g15daemon */
       
    93     if (strcmp(buffer,"G15 daemon HELLO") != 0)
       
    94         return -1;
       
    95 
       
    96     /* we want to use a pixelbuffer */
       
    97     g15_send(g15screen_fd,"GBUF",4);
       
    98 
       
    99     return g15screen_fd;
       
   100 }
       
   101 
       
   102 
       
   103 namespace GLCD
       
   104 {
       
   105 
       
   106 cDriverG15daemon::cDriverG15daemon(cDriverConfig * config)
       
   107 :   config(config),
       
   108     offbuff(0),
       
   109     sockfd(-1)
       
   110 {
       
   111     oldConfig = new cDriverConfig(*config);
       
   112 }
       
   113 
       
   114 cDriverG15daemon::~cDriverG15daemon()
       
   115 {
       
   116     delete oldConfig;
       
   117 }
       
   118 
       
   119 int cDriverG15daemon::Init()
       
   120 {
       
   121     // default values
       
   122     width = config->width;
       
   123     if (width !=G15_WIDTH)
       
   124         width = G15_WIDTH;
       
   125     height = config->height;
       
   126     if (height !=G15_HEIGHT)
       
   127         height = G15_HEIGHT;
       
   128 
       
   129     for (unsigned int i = 0; i < config->options.size(); i++) {
       
   130         if (config->options[i].name == "") {
       
   131         }
       
   132     }
       
   133 
       
   134     screensize = 6880;
       
   135 
       
   136     if ((sockfd = open_g15_daemon())<0)
       
   137         return -1;
       
   138     // reserve memory to draw into
       
   139     offbuff = new char[6880];
       
   140 
       
   141     *oldConfig = *config;
       
   142 
       
   143     // clear display
       
   144     Refresh(true);
       
   145 
       
   146     syslog(LOG_INFO, "%s: g15daemon initialized.\n", config->name.c_str());
       
   147     return 0;
       
   148 }
       
   149 
       
   150 int cDriverG15daemon::DeInit()
       
   151 {
       
   152     if (offbuff);
       
   153     delete[] offbuff;
       
   154     if (-1 != sockfd)
       
   155         close(sockfd);
       
   156 
       
   157     return 0;
       
   158 }
       
   159 
       
   160 int cDriverG15daemon::CheckSetup()
       
   161 {
       
   162     if (config->device != oldConfig->device ||
       
   163         config->port != oldConfig->port ||
       
   164         config->width != oldConfig->width ||
       
   165         config->height != oldConfig->height)
       
   166     {
       
   167         DeInit();
       
   168         Init();
       
   169         return 0;
       
   170     }
       
   171 
       
   172     if (config->upsideDown != oldConfig->upsideDown ||
       
   173         config->invert != oldConfig->invert)
       
   174     {
       
   175         oldConfig->upsideDown = config->upsideDown;
       
   176         oldConfig->invert = config->invert;
       
   177         return 1;
       
   178     }
       
   179     return 0;
       
   180 }
       
   181 
       
   182 void cDriverG15daemon::SetPixel(int x, int y)
       
   183 {
       
   184     if (x >= width || y >= height)
       
   185         return;
       
   186 
       
   187     if (config->upsideDown)
       
   188     {
       
   189         x = width - 1 - x;
       
   190         y = height - 1 - y;
       
   191     }
       
   192 
       
   193     offbuff[x + (width * y)] = 1;
       
   194 }
       
   195 
       
   196 void cDriverG15daemon::Clear()
       
   197 {
       
   198     memset(offbuff, 0, screensize);
       
   199 }
       
   200 
       
   201 void cDriverG15daemon::Set8Pixels(int x, int y, unsigned char data)
       
   202 {
       
   203     int n;
       
   204 
       
   205     x &= 0xFFF8;
       
   206 
       
   207     for (n = 0; n < 8; ++n)
       
   208     {
       
   209         if (data & (0x80 >> n))      // if bit is set
       
   210             SetPixel(x + n, y);
       
   211     }
       
   212 }
       
   213 
       
   214 void cDriverG15daemon::Refresh(bool refreshAll)
       
   215 {
       
   216     g15_send(sockfd, offbuff, screensize);
       
   217 }
       
   218 
       
   219 } // end of namespace