]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/drivers/mou_mt.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / drivers / mou_mt.c
1 /*
2  *  MicroTouch touch panel driver for MicroTouch capacitive and resistive
3  *  controllers at RS232 port.
4  *
5  *  We expect the Microtouch Controller configured to 9600 Baud, 8 data bits,
6  *  1 stop bit, no parity
7  *
8  *  Written by Holger Waechtler <hwaechtler@users.sourceforge.net>
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <termios.h>
16 #include "device.h"
17
18 static int      MT_Open(MOUSEDEVICE *pmd);
19 static void     MT_Close(void);
20 static int      MT_GetButtonInfo(void);
21 static void     MT_GetDefaultAccel(int *pscale,int *pthresh);
22 static int      MT_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp);
23
24 MOUSEDEVICE mousedev = {
25         MT_Open,
26         MT_Close,
27         MT_GetButtonInfo,
28         MT_GetDefaultAccel,
29         MT_Read,
30         NULL
31 };
32
33 static int mt_fd;
34
35
36 #define CMD(fd,x) \
37    do { \
38       write (fd, "\001" x "\r", strlen(x) + 2); \
39       tcdrain(fd); \
40    } while (0)
41
42
43
44 /*
45  * Open up the mouse device.
46  */
47 static int
48 MT_Open(MOUSEDEVICE *pmd)
49 {
50    char *devname = "/dev/ttyS0";
51    struct termios termios;
52
53    if ((mt_fd = open (devname, O_RDWR | O_NONBLOCK)) < 0) {
54       fprintf (stderr, "error opening '%s'\n", devname);
55       exit (-1);
56    }
57
58    tcgetattr (mt_fd, &termios);
59
60    cfsetispeed (&termios, B9600);
61    cfsetospeed (&termios, B9600);
62
63    cfmakeraw (&termios);
64    termios.c_cflag &= ~CBAUD;
65    termios.c_cflag = B9600;
66    termios.c_cflag |= CS8 | CREAD;
67    termios.c_oflag = 0;
68    termios.c_oflag &= ~(OCRNL | ONLRET);
69
70    tcsetattr (mt_fd, TCSAFLUSH, &termios);
71
72    CMD(mt_fd,"R");
73    CMD(mt_fd,"AD");
74    CMD(mt_fd,"PN812");
75    CMD(mt_fd,"FT");
76    CMD(mt_fd,"MS");
77    CMD(mt_fd,"PL");
78
79   /*
80    *  This is a sort of hack, but our embedded PPC was to slow to read all
81    *  responses from the Microtouch controller. To get out of this we simply
82    *  discard them ...
83    */
84    usleep (250000);
85    tcflush (mt_fd, TCIOFLUSH);
86
87    return 0;
88 }
89
90 /*
91  * Close the mouse device.
92  */
93 static void
94 MT_Close(void)
95 {
96    if (mt_fd > 0)
97       close (mt_fd);
98
99    mt_fd = 0;
100 }
101
102 /*
103  * Get mouse buttons supported
104  */
105 static int
106 MT_GetButtonInfo(void)
107 {
108    return MWBUTTON_L;
109 }
110
111
112 /*
113  * doesn't makes sense for a touch panel ...
114  */
115 static void
116 MT_GetDefaultAccel(int *pscale,int *pthresh)
117 {
118    *pscale = 3;
119    *pthresh = 5;
120 }
121
122
123
124 char buf [5];
125 int bytes_in_buf = 0;
126
127
128 /*
129  * Attempt to read bytes from the mouse and interpret them.
130  * Returns -1 on error, 0 if either no bytes were read or not enough
131  * was read for a complete state, or 1 if the new state was read.
132  * When a new state is read, the current buttons and x and y deltas
133  * are returned.  This routine does not block.
134  */
135 static int
136 MT_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp)
137 {
138    if (bytes_in_buf == 5) {
139       bytes_in_buf = 0;
140
141       *dx = ((buf[1] & 0x7f) | ((buf[2] & 0x7f) << 7));
142       *dy = ((buf[3] & 0x7f) | ((buf[4] & 0x7f) << 7));
143       *dx = (*dx * scrdev.xvirtres) >> 14;
144       *dy = scrdev.yvirtres - ((*dy * scrdev.yvirtres) >> 14);
145       *dz = 1;
146
147       if (!(buf[0] & (1 << 6)))
148          *bp = 0;
149       else
150          *bp = MWBUTTON_L;
151
152       return 2;
153    }
154
155    if (read (mt_fd, buf + bytes_in_buf, 1) == 1)
156       bytes_in_buf++;
157
158    if ((buf[bytes_in_buf-1] & 0x80) != 0) {
159       buf [0] = buf [bytes_in_buf-1];
160       bytes_in_buf = 1;
161    }       
162
163    return 0;
164 }
165