src/video/fbcon/SDL_fbelo.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 1895 c121d94672cb
child 2669 e27bdcc80744
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include <unistd.h>
slouken@0
    25
#include <sys/time.h>
slouken@0
    26
#include <ctype.h>
slouken@0
    27
slouken@1358
    28
#include "SDL_stdinc.h"
slouken@0
    29
#include "SDL_fbvideo.h"
slouken@0
    30
#include "SDL_fbelo.h"
slouken@0
    31
slouken@0
    32
/*
slouken@0
    33
	calibration default values
slouken@0
    34
	values are read from the following environment variables:
slouken@0
    35
slouken@0
    36
	SDL_ELO_MIN_X
slouken@0
    37
	SDL_ELO_MAX_X
slouken@0
    38
	SDL_ELO_MIN_Y
slouken@0
    39
	SDL_ELO_MAX_Y
slouken@0
    40
*/
slouken@0
    41
slouken@0
    42
static int ELO_MIN_X = 400;
slouken@0
    43
static int ELO_MAX_X = 3670;
slouken@0
    44
static int ELO_MIN_Y = 500;
slouken@0
    45
static int ELO_MAX_Y = 3540;
slouken@0
    46
slouken@0
    47
#define ELO_SNAP_SIZE 6
slouken@1895
    48
#define ELO_TOUCH_BYTE		'T'
slouken@0
    49
#define ELO_ID			'I'
slouken@0
    50
#define ELO_MODE		'M'
slouken@0
    51
#define ELO_PARAMETER		'P'
slouken@0
    52
#define ELO_REPORT		'B'
slouken@1895
    53
#define ELO_ACK			'A'
slouken@0
    54
slouken@0
    55
#define ELO_INIT_CHECKSUM	0xAA
slouken@0
    56
slouken@1895
    57
#define ELO_BTN_PRESS		0x01
slouken@0
    58
#define ELO_STREAM		0x02
slouken@0
    59
#define ELO_BTN_RELEASE		0x04
slouken@0
    60
slouken@0
    61
#define ELO_TOUCH_MODE		0x01
slouken@0
    62
#define ELO_STREAM_MODE		0x02
slouken@0
    63
#define ELO_UNTOUCH_MODE	0x04
slouken@0
    64
#define ELO_RANGE_CHECK_MODE	0x40
slouken@0
    65
#define ELO_TRIM_MODE		0x02
slouken@0
    66
#define ELO_CALIB_MODE		0x04
slouken@0
    67
#define ELO_SCALING_MODE	0x08
slouken@0
    68
#define ELO_TRACKING_MODE	0x40
slouken@0
    69
slouken@0
    70
#define ELO_SERIAL_MASK		0xF8
slouken@0
    71
slouken@0
    72
#define ELO_SERIAL_IO		'0'
slouken@0
    73
slouken@0
    74
#define ELO_MAX_TRIALS	3
slouken@0
    75
#define ELO_MAX_WAIT		100000
slouken@0
    76
#define ELO_UNTOUCH_DELAY	5
slouken@0
    77
#define ELO_REPORT_DELAY	1
slouken@0
    78
slouken@0
    79
/*	eloParsePacket
slouken@0
    80
*/
slouken@1895
    81
int
slouken@1895
    82
eloParsePacket(unsigned char *mousebuf, int *dx, int *dy, int *button_state)
slouken@1895
    83
{
slouken@1895
    84
    static int elo_button = 0;
slouken@1895
    85
    static int last_x = 0;
slouken@1895
    86
    static int last_y = 0;
slouken@1895
    87
    int x, y;
slouken@0
    88
slouken@1895
    89
    /* Check if we have a touch packet */
slouken@1895
    90
    if (mousebuf[1] != ELO_TOUCH_BYTE) {
slouken@1895
    91
        return 0;
slouken@1895
    92
    }
slouken@0
    93
slouken@1895
    94
    x = ((mousebuf[4] << 8) | mousebuf[3]);
slouken@1895
    95
    y = ((mousebuf[6] << 8) | mousebuf[5]);
slouken@0
    96
slouken@1895
    97
    if ((SDL_abs(x - last_x) > ELO_SNAP_SIZE)
slouken@1895
    98
        || (SDL_abs(y - last_y) > ELO_SNAP_SIZE)) {
slouken@1895
    99
        *dx = ((mousebuf[4] << 8) | mousebuf[3]);
slouken@1895
   100
        *dy = ((mousebuf[6] << 8) | mousebuf[5]);
slouken@1895
   101
    } else {
slouken@1895
   102
        *dx = last_x;
slouken@1895
   103
        *dy = last_y;
slouken@1895
   104
    }
slouken@0
   105
slouken@1895
   106
    last_x = *dx;
slouken@1895
   107
    last_y = *dy;
slouken@0
   108
slouken@1895
   109
    if ((mousebuf[2] & 0x07) == ELO_BTN_PRESS) {
slouken@1895
   110
        elo_button = 1;
slouken@1895
   111
    }
slouken@1895
   112
    if ((mousebuf[2] & 0x07) == ELO_BTN_RELEASE) {
slouken@1895
   113
        elo_button = 0;
slouken@1895
   114
    }
slouken@0
   115
slouken@1895
   116
    *button_state = elo_button;
slouken@1895
   117
    return 1;
slouken@0
   118
}
slouken@0
   119
slouken@0
   120
/*	Convert the raw coordinates from the ELO controller
slouken@0
   121
	to a screen position.
slouken@0
   122
*/
slouken@1895
   123
void
slouken@1895
   124
eloConvertXY(_THIS, int *dx, int *dy)
slouken@1895
   125
{
slouken@1895
   126
    int input_x = *dx;
slouken@1895
   127
    int input_y = *dy;
slouken@1895
   128
    int width = ELO_MAX_X - ELO_MIN_X;
slouken@1895
   129
    int height = ELO_MAX_Y - ELO_MIN_Y;
slouken@0
   130
slouken@1895
   131
    *dx =
slouken@1895
   132
        ((int) cache_vinfo.xres -
slouken@1895
   133
         ((int) cache_vinfo.xres * (input_x - ELO_MIN_X)) / width);
slouken@1895
   134
    *dy = (cache_vinfo.yres * (input_y - ELO_MIN_Y)) / height;
slouken@0
   135
}
slouken@0
   136
slouken@0
   137
slouken@0
   138
/*	eloGetPacket
slouken@0
   139
*/
slouken@1895
   140
int
slouken@1895
   141
eloGetPacket(unsigned char *buffer, int *buffer_p, int *checksum, int fd)
slouken@1895
   142
{
slouken@1895
   143
    int num_bytes;
slouken@1895
   144
    int ok;
slouken@0
   145
slouken@1895
   146
    if (fd == 0) {
slouken@1895
   147
        num_bytes = ELO_PACKET_SIZE;
slouken@1895
   148
    } else {
slouken@1895
   149
        num_bytes = read(fd,
slouken@1895
   150
                         (char *) (buffer + *buffer_p),
slouken@1895
   151
                         ELO_PACKET_SIZE - *buffer_p);
slouken@1895
   152
    }
slouken@0
   153
slouken@1895
   154
    if (num_bytes < 0) {
slouken@0
   155
#ifdef DEBUG_MOUSE
slouken@1895
   156
        fprintf(stderr,
slouken@1895
   157
                "System error while reading from Elographics touchscreen.\n");
slouken@0
   158
#endif
slouken@1895
   159
        return 0;
slouken@1895
   160
    }
slouken@0
   161
slouken@1895
   162
    while (num_bytes) {
slouken@1895
   163
        if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) {
slouken@1895
   164
            SDL_memcpy(&buffer[0], &buffer[1], num_bytes - 1);
slouken@1895
   165
        } else {
slouken@1895
   166
            if (*buffer_p < ELO_PACKET_SIZE - 1) {
slouken@1895
   167
                *checksum = *checksum + buffer[*buffer_p];
slouken@1895
   168
                *checksum = *checksum % 256;
slouken@1895
   169
            }
slouken@1895
   170
            (*buffer_p)++;
slouken@1895
   171
        }
slouken@1895
   172
        num_bytes--;
slouken@1895
   173
    }
slouken@0
   174
slouken@1895
   175
    if (*buffer_p == ELO_PACKET_SIZE) {
slouken@1895
   176
        ok = (*checksum == buffer[ELO_PACKET_SIZE - 1]);
slouken@1895
   177
        *checksum = ELO_INIT_CHECKSUM;
slouken@1895
   178
        *buffer_p = 0;
slouken@0
   179
slouken@1895
   180
        if (!ok) {
slouken@1895
   181
            return 0;
slouken@1895
   182
        }
slouken@0
   183
slouken@1895
   184
        return 1;
slouken@1895
   185
    } else {
slouken@1895
   186
        return 0;
slouken@1895
   187
    }
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
/* eloSendPacket
slouken@0
   191
*/
slouken@0
   192
slouken@1895
   193
int
slouken@1895
   194
eloSendPacket(unsigned char *packet, int fd)
slouken@0
   195
{
slouken@1895
   196
    int i, result;
slouken@1895
   197
    int sum = ELO_INIT_CHECKSUM;
slouken@0
   198
slouken@1895
   199
    packet[0] = ELO_START_BYTE;
slouken@1895
   200
    for (i = 0; i < ELO_PACKET_SIZE - 1; i++) {
slouken@1895
   201
        sum += packet[i];
slouken@1895
   202
        sum &= 0xFF;
slouken@1895
   203
    }
slouken@1895
   204
    packet[ELO_PACKET_SIZE - 1] = sum;
slouken@0
   205
slouken@1895
   206
    result = write(fd, packet, ELO_PACKET_SIZE);
slouken@1895
   207
slouken@1895
   208
    if (result != ELO_PACKET_SIZE) {
slouken@0
   209
#ifdef DEBUG_MOUSE
slouken@1895
   210
        printf("System error while sending to Elographics touchscreen.\n");
slouken@0
   211
#endif
slouken@1895
   212
        return 0;
slouken@1895
   213
    } else {
slouken@1895
   214
        return 1;
slouken@1895
   215
    }
slouken@0
   216
}
slouken@0
   217
slouken@0
   218
slouken@0
   219
/*	eloWaitForInput
slouken@0
   220
 */
slouken@1895
   221
int
slouken@1895
   222
eloWaitForInput(int fd, int timeout)
slouken@0
   223
{
slouken@1895
   224
    fd_set readfds;
slouken@1895
   225
    struct timeval to;
slouken@1895
   226
    int r;
slouken@0
   227
slouken@1895
   228
    FD_ZERO(&readfds);
slouken@1895
   229
    FD_SET(fd, &readfds);
slouken@1895
   230
    to.tv_sec = 0;
slouken@1895
   231
    to.tv_usec = timeout;
slouken@0
   232
slouken@1895
   233
    r = select(FD_SETSIZE, &readfds, NULL, NULL, &to);
slouken@1895
   234
    return r;
slouken@0
   235
}
slouken@0
   236
slouken@0
   237
/*	eloWaitReply
slouken@0
   238
 */
slouken@1895
   239
int
slouken@1895
   240
eloWaitReply(unsigned char type, unsigned char *reply, int fd)
slouken@1895
   241
{
slouken@1895
   242
    int ok;
slouken@1895
   243
    int i, result;
slouken@1895
   244
    int reply_p = 0;
slouken@1895
   245
    int sum = ELO_INIT_CHECKSUM;
slouken@0
   246
slouken@1895
   247
    i = ELO_MAX_TRIALS;
slouken@1895
   248
    do {
slouken@1895
   249
        ok = 0;
slouken@0
   250
slouken@1895
   251
        result = eloWaitForInput(fd, ELO_MAX_WAIT);
slouken@0
   252
slouken@1895
   253
        if (result > 0) {
slouken@1895
   254
            ok = eloGetPacket(reply, &reply_p, &sum, fd);
slouken@0
   255
slouken@1895
   256
            if (ok && reply[1] != type && type != ELO_PARAMETER) {
slouken@0
   257
#ifdef DEBUG_MOUSE
slouken@1895
   258
                fprintf(stderr, "Wrong reply received\n");
slouken@0
   259
#endif
slouken@1895
   260
                ok = 0;
slouken@1895
   261
            }
slouken@1895
   262
        } else {
slouken@0
   263
#ifdef DEBUG_MOUSE
slouken@1895
   264
            fprintf(stderr, "No input!\n");
slouken@0
   265
#endif
slouken@1895
   266
        }
slouken@0
   267
slouken@1895
   268
        if (result == 0) {
slouken@1895
   269
            i--;
slouken@1895
   270
        }
slouken@1895
   271
    }
slouken@1895
   272
    while (!ok && (i > 0));
slouken@0
   273
slouken@1895
   274
    return ok;
slouken@0
   275
}
slouken@0
   276
slouken@0
   277
slouken@0
   278
/*	eloWaitAck
slouken@0
   279
 */
slouken@0
   280
slouken@1895
   281
int
slouken@1895
   282
eloWaitAck(int fd)
slouken@1895
   283
{
slouken@1895
   284
    unsigned char packet[ELO_PACKET_SIZE];
slouken@1895
   285
    int i, nb_errors;
slouken@0
   286
slouken@1895
   287
    if (eloWaitReply(ELO_ACK, packet, fd)) {
slouken@1895
   288
        for (i = 0, nb_errors = 0; i < 4; i++) {
slouken@1895
   289
            if (packet[2 + i] != '0') {
slouken@1895
   290
                nb_errors++;
slouken@1895
   291
            }
slouken@1895
   292
        }
slouken@1895
   293
slouken@1895
   294
        if (nb_errors != 0) {
slouken@0
   295
#ifdef DEBUG_MOUSE
slouken@1895
   296
            fprintf(stderr,
slouken@1895
   297
                    "Elographics acknowledge packet reports %d errors\n",
slouken@1895
   298
                    nb_errors);
slouken@0
   299
#endif
slouken@1895
   300
        }
slouken@1895
   301
        return 1;
slouken@1895
   302
    } else {
slouken@1895
   303
        return 0;
slouken@1895
   304
    }
slouken@0
   305
}
slouken@0
   306
slouken@0
   307
slouken@0
   308
/*	eloSendQuery --
slouken@0
   309
*/
slouken@1895
   310
int
slouken@1895
   311
eloSendQuery(unsigned char *request, unsigned char *reply, int fd)
slouken@1895
   312
{
slouken@1895
   313
    int ok;
slouken@0
   314
slouken@1895
   315
    if (eloSendPacket(request, fd)) {
slouken@1895
   316
        ok = eloWaitReply(toupper(request[1]), reply, fd);
slouken@1895
   317
        if (ok) {
slouken@1895
   318
            ok = eloWaitAck(fd);
slouken@1895
   319
        }
slouken@1895
   320
        return ok;
slouken@1895
   321
    } else {
slouken@1895
   322
        return 0;
slouken@1895
   323
    }
slouken@0
   324
}
slouken@0
   325
slouken@0
   326
slouken@0
   327
/*	eloSendControl
slouken@0
   328
*/
slouken@1895
   329
int
slouken@1895
   330
eloSendControl(unsigned char *control, int fd)
slouken@1895
   331
{
slouken@1895
   332
    if (eloSendPacket(control, fd)) {
slouken@1895
   333
        return eloWaitAck(fd);
slouken@1895
   334
    } else {
slouken@1895
   335
        return 0;
slouken@1895
   336
    }
slouken@0
   337
}
slouken@0
   338
slouken@0
   339
/*	eloInitController
slouken@0
   340
*/
slouken@1895
   341
int
slouken@1895
   342
eloInitController(int fd)
slouken@1895
   343
{
slouken@1895
   344
    unsigned char req[ELO_PACKET_SIZE];
slouken@1895
   345
    unsigned char reply[ELO_PACKET_SIZE];
slouken@1895
   346
    const char *buffer = NULL;
slouken@1895
   347
    int result = 0;
slouken@0
   348
slouken@1895
   349
    struct termios mouse_termios;
slouken@0
   350
slouken@1895
   351
    /* try to read the calibration values */
slouken@1895
   352
    buffer = SDL_getenv("SDL_ELO_MIN_X");
slouken@1895
   353
    if (buffer) {
slouken@1895
   354
        ELO_MIN_X = SDL_atoi(buffer);
slouken@1895
   355
    }
slouken@1895
   356
    buffer = SDL_getenv("SDL_ELO_MAX_X");
slouken@1895
   357
    if (buffer) {
slouken@1895
   358
        ELO_MAX_X = SDL_atoi(buffer);
slouken@1895
   359
    }
slouken@1895
   360
    buffer = SDL_getenv("SDL_ELO_MIN_Y");
slouken@1895
   361
    if (buffer) {
slouken@1895
   362
        ELO_MIN_Y = SDL_atoi(buffer);
slouken@1895
   363
    }
slouken@1895
   364
    buffer = SDL_getenv("SDL_ELO_MAX_Y");
slouken@1895
   365
    if (buffer) {
slouken@1895
   366
        ELO_MAX_Y = SDL_atoi(buffer);
slouken@1895
   367
    }
slouken@0
   368
#ifdef DEBUG_MOUSE
slouken@1895
   369
    fprintf(stderr,
slouken@1895
   370
            "ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n",
slouken@1895
   371
            ELO_MIN_X, ELO_MAX_X, ELO_MIN_Y, ELO_MAX_Y);
slouken@0
   372
#endif
slouken@0
   373
slouken@1895
   374
    /* set comm params */
slouken@1895
   375
    SDL_memset(&mouse_termios, 0, sizeof(mouse_termios));
slouken@1895
   376
    mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
slouken@1895
   377
    mouse_termios.c_cc[VMIN] = 1;
slouken@1895
   378
    result = tcsetattr(fd, TCSANOW, &mouse_termios);
slouken@0
   379
slouken@1895
   380
    if (result < 0) {
slouken@0
   381
#ifdef DEBUG_MOUSE
slouken@1895
   382
        fprintf(stderr, "Unable to configure Elographics touchscreen port\n");
slouken@0
   383
#endif
slouken@1895
   384
        return 0;
slouken@1895
   385
    }
slouken@0
   386
slouken@1895
   387
    SDL_memset(req, 0, ELO_PACKET_SIZE);
slouken@1895
   388
    req[1] = tolower(ELO_PARAMETER);
slouken@1895
   389
    if (!eloSendQuery(req, reply, fd)) {
slouken@0
   390
#ifdef DEBUG_MOUSE
slouken@1895
   391
        fprintf(stderr,
slouken@1895
   392
                "Not at the specified rate or model 2310, will continue\n");
slouken@0
   393
#endif
slouken@1895
   394
    }
slouken@0
   395
slouken@1895
   396
    SDL_memset(req, 0, ELO_PACKET_SIZE);
slouken@1895
   397
    req[1] = tolower(ELO_ID);
slouken@1895
   398
    if (eloSendQuery(req, reply, fd)) {
slouken@0
   399
#ifdef DEBUG_MOUSE
slouken@1895
   400
        fprintf(stderr, "Ok, controller configured!\n");
slouken@0
   401
#endif
slouken@1895
   402
    } else {
slouken@0
   403
#ifdef DEBUG_MOUSE
slouken@1895
   404
        fprintf(stderr,
slouken@1895
   405
                "Unable to ask Elographics touchscreen identification\n");
slouken@0
   406
#endif
slouken@1895
   407
        return 0;
slouken@1895
   408
    }
slouken@0
   409
slouken@1895
   410
    SDL_memset(req, 0, ELO_PACKET_SIZE);
slouken@1895
   411
    req[1] = ELO_MODE;
slouken@1895
   412
    req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE;
slouken@1895
   413
    req[4] = ELO_TRACKING_MODE;
slouken@1895
   414
    if (!eloSendControl(req, fd)) {
slouken@0
   415
#ifdef DEBUG_MOUSE
slouken@1895
   416
        fprintf(stderr,
slouken@1895
   417
                "Unable to change Elographics touchscreen operating mode\n");
slouken@0
   418
#endif
slouken@1895
   419
        return 0;
slouken@1895
   420
    }
slouken@0
   421
slouken@1895
   422
    SDL_memset(req, 0, ELO_PACKET_SIZE);
slouken@1895
   423
    req[1] = ELO_REPORT;
slouken@1895
   424
    req[2] = ELO_UNTOUCH_DELAY;
slouken@1895
   425
    req[3] = ELO_REPORT_DELAY;
slouken@1895
   426
    if (!eloSendControl(req, fd)) {
slouken@0
   427
#ifdef DEBUG_MOUSE
slouken@1895
   428
        fprintf(stderr,
slouken@1895
   429
                "Unable to change Elographics touchscreen reports timings\n");
slouken@0
   430
#endif
slouken@1895
   431
        return 0;
slouken@1895
   432
    }
slouken@0
   433
slouken@1895
   434
    return 1;
slouken@0
   435
}
slouken@4
   436
slouken@1895
   437
int
slouken@1895
   438
eloReadPosition(_THIS, int fd, int *x, int *y, int *button_state, int *realx,
slouken@1895
   439
                int *realy)
slouken@1895
   440
{
slouken@1895
   441
    unsigned char buffer[ELO_PACKET_SIZE];
slouken@1895
   442
    int pointer = 0;
slouken@1895
   443
    int checksum = ELO_INIT_CHECKSUM;
slouken@4
   444
slouken@1895
   445
    while (pointer < ELO_PACKET_SIZE) {
slouken@1895
   446
        if (eloGetPacket(buffer, &pointer, &checksum, fd)) {
slouken@1895
   447
            break;
slouken@4
   448
        }
slouken@1895
   449
    }
slouken@4
   450
slouken@1895
   451
    if (!eloParsePacket(buffer, realx, realy, button_state)) {
slouken@1895
   452
        return 0;
slouken@1895
   453
    }
slouken@1895
   454
slouken@1895
   455
    *x = *realx;
slouken@1895
   456
    *y = *realy;
slouken@4
   457
slouken@1895
   458
    eloConvertXY(this, x, y);
slouken@4
   459
slouken@1895
   460
    return 1;
slouken@4
   461
}
slouken@1895
   462
slouken@1895
   463
/* vi: set ts=4 sw=4 expandtab: */