Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ligi 1
/* x52pro driver library
2
 * Copyright (C) 2007 Eduard Hasenleithner <eduard@hasenleithner.at>
3
 * Licensed under the LGPL. Please see "COPYING".
4
 */
5
#include <stdio.h>
6
#include <string.h>
7
#include <usb.h>
8
#include "x52.h"
9
 
10
struct x52 {
11
        usb_dev_handle *hdl;
12
};
13
 
14
#define X52PRO_REQUEST 0x91
15
 
16
#define X52PRO_CLEAR1 0xd9
17
#define X52PRO_CLEAR2 0xda
18
#define X52PRO_CLEAR3 0xdc
19
#define X52PRO_WRITE1 0xd1
20
#define X52PRO_WRITE2 0xd2
21
#define X52PRO_WRITE3 0xd4
22
#define X52PRO_SETLED 0xb8
23
#define X52PRO_MFDBRI 0xb1
24
#define X52PRO_LEDBRI 0xb2
25
 
26
#define X52PRO_TIME   0xc0
27
#define X52PRO_DATE   0xc4
28
#define X52PRO_YEAR   0xc8
29
 
30
int write_idx[3] = {
31
        X52PRO_WRITE1,
32
        X52PRO_WRITE2,
33
        X52PRO_WRITE3,
34
};
35
 
36
int clear_idx[3] = {
37
        X52PRO_CLEAR1,
38
        X52PRO_CLEAR2,
39
        X52PRO_CLEAR3,
40
};
41
 
42
int x52_settext(struct x52 *x52, int line, char *text, int length)
43
{
44
        int r;
45
        if (line > 3) return -1;
46
        r = usb_control_msg(x52->hdl,
47
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
48
                X52PRO_REQUEST, 0x00, clear_idx[line], NULL, 0, 1000);
49
        if (r < 0) {
50
                fprintf(stderr, "x52_settext failed at clear command (%s)\n",
51
                        usb_strerror());
52
                return -2;
53
        }
54
 
55
        while (length >= 1) {
56
                int chars;
57
                if (length == 1) chars = (' ' << 8) + *text;
58
                else chars = *(unsigned short*) text;
59
                r = usb_control_msg(x52->hdl,
60
                        USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
61
                        X52PRO_REQUEST, chars, write_idx[line], NULL, 0, 1000);
62
                if (r<0) {
63
                        fprintf(stderr, "x52_settext failed at write %d (%s)\n",
64
                                length, usb_strerror());
65
                        return -2;
66
                }
67
                length -= 2;
68
                text += 2;
69
        }
70
        return 0;
71
}
72
 
73
int x52_setbri(struct x52 *x52, int mfd, int brightness)
74
{
75
        int r;
76
        r = usb_control_msg(x52->hdl,
77
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
78
                X52PRO_REQUEST, brightness, mfd ? X52PRO_MFDBRI : X52PRO_LEDBRI,
79
                NULL, 0, 1000);
80
        if (r < 0) {
81
                fprintf(stderr, "x52_setbri failed (%s)\n", usb_strerror());
82
                return -2;
83
        }
84
        return 0;
85
}
86
 
87
int x52_setled(struct x52 *x52, int led, int on)
88
{
89
        int r;
90
        r = usb_control_msg(x52->hdl,
91
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
92
                X52PRO_REQUEST, on | (led<<8), X52PRO_SETLED,
93
                NULL, 0, 1000);
94
        if (r < 0) {
95
                fprintf(stderr, "x52_setled failed (%s)\n", usb_strerror());
96
                return -2;
97
        }
98
        return 0;
99
}
100
 
101
int x52_settime(struct x52 *x52, int h24, int hour, int minute)
102
{
103
        int r;
104
        r = usb_control_msg(x52->hdl,
105
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
106
                X52PRO_REQUEST, minute | (hour<<8) | (h24?0x8000:0), X52PRO_TIME,
107
                NULL, 0, 1000);
108
        if (r < 0) {
109
                fprintf(stderr, "x52_settime failed (%s)\n", usb_strerror());
110
                return -2;
111
        }
112
        return 0;
113
}
114
 
115
int x52_setdate(struct x52 *x52, int year, int month, int day)
116
{
117
        int r;
118
        r = usb_control_msg(x52->hdl,
119
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
120
                X52PRO_REQUEST, day | (month<<8), X52PRO_DATE,
121
                NULL, 0, 1000);
122
        if (r < 0) {
123
                fprintf(stderr, "x52_setdate failed (%s)\n", usb_strerror());
124
                return -2;
125
        }
126
        r = usb_control_msg(x52->hdl,
127
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
128
                X52PRO_REQUEST, year, X52PRO_YEAR,
129
                NULL, 0, 1000);
130
        if (r < 0) {
131
                fprintf(stderr, "x52_setdate failed for year (%s)\n", usb_strerror());
132
                return -2;
133
        }
134
        return 0;
135
}
136
 
137
int x52_custom(struct x52 *x52, int index, int value)
138
{
139
        int r = usb_control_msg(x52->hdl,
140
                USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_ENDPOINT_OUT,
141
                X52PRO_REQUEST, value, index, NULL, 0, 1000);
142
        if (r < 0) {
143
                fprintf(stderr, "x52_settext failed at clear command (%s)\n",
144
                        usb_strerror());
145
                return -2;
146
        }
147
        return 0;
148
}
149
 
150
struct x52* x52_init(void)
151
{
152
        struct x52 x52, *x52p;
153
 
154
        usb_init();
155
        usb_find_busses();
156
        usb_find_devices();
157
 
158
        struct usb_bus *bus;
159
        struct usb_device *joydev = NULL;
160
        for (bus = usb_busses; bus; bus = bus->next) {
161
                struct usb_device *dev;
162
                for (dev = bus->devices; dev; dev = dev->next) {
163
                        struct usb_device_descriptor *dsc = &dev->descriptor;
164
                        if (dsc->idVendor == 0x6a3 && ((dsc->idProduct == 0x75c)||(dsc->idProduct == 0x762)))
165
                          {
166
                                joydev = dev;
167
                                break;
168
                          }
169
                }
170
                if (joydev) break;
171
        }
172
        if (!joydev) {
173
                fprintf(stderr, "joystick not found\n");
174
                return NULL;
175
        }
176
        fprintf(stderr, "joystick found\n");
177
        x52.hdl = usb_open(joydev);
178
        if (x52.hdl==NULL) {
179
                fprintf(stderr, "joystick open failed\n");
180
                return NULL;
181
        }
182
        x52p = malloc(sizeof(*x52p));
183
        *x52p = x52;
184
        return x52p;
185
}
186
 
187
 
188
void x52_close(struct x52* x52)
189
{
190
        int r;
191
        r = usb_close(x52->hdl);
192
        free(x52);
193
        printf("usb_close: %d\n", r);
194
}