root/wmdemo/main.c @ a339960844acef82f06e6c4ba82575fafc4e50ac

Revision a339960844acef82f06e6c4ba82575fafc4e50ac, 6.8 KB (checked in by dsmith <dsmith@…>, 6 years ago)

added wiimote_set_err

git-svn-id: http://abstrakraft.org/cwiid/svn/trunk@27 918edb2d-ff29-0410-9de2-eb38e7f22bc7

  • Property mode set to 100644
Line 
1#include <stdarg.h>
2#include <stdio.h>
3
4#include <wiimote.h>
5
6/* This is a sample program written to demonstrate basic CWiid libwiimote
7 * usage, until _actual_ documentation can be written.  It's quick and dirty
8 * has a horrible interface, but it's sparce enough to pick out the important
9 * parts easily.  For examples of read and write code, see wmgui.  Speaker
10 * support is "experimental" (read: bad) enough to be disabled.  The beginnings
11 * of a speaker output function are in libwiimote source. */
12/* Note: accelerometer (including nunchuk) and IR outputs produce a
13 * lot of data - the purpose of this program is demonstration, not good
14 * interface, and it shows. */
15
16wiimote_mesg_callback_t wiimote_callback;
17
18#define toggle_bit(bf,b)        \
19        (bf) = ((bf) & b)               \
20               ? ((bf) & ~(b))  \
21               : ((bf) | (b))
22
23void set_led_state(wiimote_t *wiimote, unsigned char led_state);
24void set_rpt_mode(wiimote_t *wiimote, unsigned char rpt_mode);
25
26wiimote_err_t err;
27void err(int id, const char *s, ...)
28{
29        va_list ap;
30
31        va_start(ap, s);
32        printf("%d:", id);
33        vprintf(s, ap);
34        printf("\n");
35        va_end(ap);
36}
37
38int main(int argc, char *argv[])
39{
40        bdaddr_t bdaddr;        /* bluetooth device address */
41        wiimote_t *wiimote;     /* wiimote handle */
42        int wiimote_id;         /* wiimote id: useful for handling multiple wiimotes
43                               with a single callback */
44        unsigned char led_state = 0;
45        unsigned char rpt_mode = 0;
46        unsigned char rumble = 0;
47        int exit = 0;
48
49        wiimote_set_err(err);
50
51        /* Connect to any wiimote */
52        bdaddr = *BDADDR_ANY;
53        /* Connect to address in string WIIMOTE_BDADDR */
54        /* bdaddr = str2ba(WIIMOTE_BDADDR); */
55
56        /* Connect to the wiimote */
57        printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
58        if (!(wiimote = wiimote_connect(bdaddr, wiimote_callback, &wiimote_id))) {
59                fprintf(stderr, "Unable to connect to wiimote\n");
60                return -1;
61        }
62
63        /* Menu */
64        printf("1: toggle LED 1\n"
65               "2: toggle LED 2\n"
66               "3: toggle LED 3\n"
67               "4: toggle LED 4\n"
68               "a: toggle accelerometer output\n"
69               "b: toggle button output\n"
70               "e: toggle extension output\n"
71               "i: toggle ir output\n"
72               "r: toggle rumble\n"
73               "s: toggle status output\n"
74               "x: exit\n");
75
76        while (!exit) {
77                switch (getchar()) {
78                case '1':
79                        toggle_bit(led_state, WIIMOTE_LED1_ON);
80                        set_led_state(wiimote, led_state);
81                        break;
82                case '2':
83                        toggle_bit(led_state, WIIMOTE_LED2_ON);
84                        set_led_state(wiimote, led_state);
85                        break;
86                case '3':
87                        toggle_bit(led_state, WIIMOTE_LED3_ON);
88                        set_led_state(wiimote, led_state);
89                        break;
90                case '4':
91                        toggle_bit(led_state, WIIMOTE_LED4_ON);
92                        set_led_state(wiimote, led_state);
93                        break;
94                case 'a':
95                        toggle_bit(rpt_mode, WIIMOTE_RPT_ACC);
96                        set_rpt_mode(wiimote, rpt_mode);
97                        break;
98                case 'b':
99                        toggle_bit(rpt_mode, WIIMOTE_RPT_BTN);
100                        set_rpt_mode(wiimote, rpt_mode);
101                        break;
102                case 'e':
103                        /* WIIMOTE_RPT_EXT is actually
104                         * WIIMOTE_RPT_NUNCHUK | WIIMOTE_RPT_CLASSIC */
105                        toggle_bit(rpt_mode, WIIMOTE_RPT_EXT);
106                        set_rpt_mode(wiimote, rpt_mode);
107                        break;
108                case 'i':
109                        /* libwiimote picks the highest quality IR mode available with the
110                         * other options selected (not including as-yet-undeciphered
111                         * interleaved mode */
112                        toggle_bit(rpt_mode, WIIMOTE_RPT_IR);
113                        set_rpt_mode(wiimote, rpt_mode);
114                        break;
115                case 'r':
116                        toggle_bit(rumble, 1);
117                        if (wiimote_command(wiimote, WIIMOTE_CMD_RUMBLE, rumble)) {
118                                fprintf(stderr, "Error setting rumble\n");
119                        }
120                        break;
121                case 's':
122                        toggle_bit(rpt_mode, WIIMOTE_RPT_STATUS);
123                        set_rpt_mode(wiimote, rpt_mode);
124                        break;
125                case 'x':
126                        exit = -1;
127                        break;
128                case '\n':
129                        break;
130                default:
131                        fprintf(stderr, "invalid option\n");
132                }
133        }
134
135        if (wiimote_disconnect(wiimote)) {
136                fprintf(stderr, "Error on wiimote disconnect\n");
137                return -1;
138        }
139
140        return 0;
141}
142
143void set_led_state(wiimote_t *wiimote, unsigned char led_state)
144{
145        if (wiimote_command(wiimote, WIIMOTE_CMD_LED, led_state)) {
146                fprintf(stderr, "Error setting LEDs \n");
147        }
148}
149       
150void set_rpt_mode(wiimote_t *wiimote, unsigned char rpt_mode)
151{
152        if (wiimote_command(wiimote, WIIMOTE_CMD_RPT_MODE, rpt_mode)) {
153                fprintf(stderr, "Error setting report mode\n");
154        }
155}
156
157/* Prototype wiimote_callback with wiimote_callback_t, define it with the actual
158 * type - this will cause a compile error (rather than some undefined bizarre
159 * behavior) if wiimote_callback_t changes */
160/* wiimote_mesg_callback_t has undergone a few changes lately, hopefully this
161 * will be the last.  Some programs need to know which messages were received
162 * simultaneously (e.g. for correlating acceperometer and IR data), and the
163 * sequence number mechanism used previously proved cumbersome, so we just
164 * pass an array of messages, all of which were received at the same time.
165 * The id is to distinguish between multiple wiimotes using the same callback.
166 * */
167void wiimote_callback(int id, int mesg_count, union wiimote_mesg *mesg[])
168{
169        int i, j;
170        int valid_source;
171
172        for (i=0; i < mesg_count; i++)
173        {
174                switch (mesg[i]->type) {
175                case WIIMOTE_MESG_STATUS:
176                        printf("Status Report: battery=%d extension=",
177                               mesg[i]->status_mesg.battery);
178                        switch (mesg[i]->status_mesg.extension) {
179                        case WIIMOTE_EXT_NONE:
180                                printf("none");
181                                break;
182                        case WIIMOTE_EXT_NUNCHUK:
183                                printf("Nunchuk");
184                                break;
185                        case WIIMOTE_EXT_CLASSIC:
186                                printf("Classic Controller");
187                                break;
188                        default:
189                                printf("Unknown Extension");
190                                break;
191                        }
192                        printf("\n");
193                        break;
194                case WIIMOTE_MESG_BTN:
195                        printf("Button Report: %.4X\n", mesg[i]->btn_mesg.buttons);
196                        break;
197                case WIIMOTE_MESG_ACC:
198                        printf("Acc Report: x=%d, y=%d, z=%d\n", mesg[i]->acc_mesg.x,
199                                                                 mesg[i]->acc_mesg.y,
200                                                                 mesg[i]->acc_mesg.z);
201                        break;
202                case WIIMOTE_MESG_IR:
203                        printf("IR Report: ");
204                        valid_source = 0;
205                        for (j = 0; j < WIIMOTE_IR_SRC_COUNT; j++) {
206                                if (mesg[i]->ir_mesg.src[j].valid) {
207                                        valid_source = 1;
208                                        printf("(%d,%d) ", mesg[i]->ir_mesg.src[j].x,
209                                                           mesg[i]->ir_mesg.src[j].y);
210                                }
211                        }
212                        if (!valid_source) {
213                                printf("no sources detected");
214                        }
215                        printf("\n");
216                        break;
217                case WIIMOTE_MESG_NUNCHUK:
218                        printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
219                               "acc.z=%d\n", mesg[i]->nunchuk_mesg.buttons,
220                               mesg[i]->nunchuk_mesg.stick_x,
221                               mesg[i]->nunchuk_mesg.stick_y, mesg[i]->nunchuk_mesg.acc_x,
222                               mesg[i]->nunchuk_mesg.acc_y, mesg[i]->nunchuk_mesg.acc_z);
223                        break;
224                case WIIMOTE_MESG_CLASSIC:
225                        printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
226                               "l=%d r=%d\n", mesg[i]->classic_mesg.buttons,
227                               mesg[i]->classic_mesg.l_stick_x,
228                               mesg[i]->classic_mesg.l_stick_y,
229                               mesg[i]->classic_mesg.r_stick_x,
230                               mesg[i]->classic_mesg.r_stick_y,
231                               mesg[i]->classic_mesg.l, mesg[i]->classic_mesg.r);
232                        break;
233                default:
234                        printf("Unknown Report");
235                        break;
236                }
237        }
238}
239
Note: See TracBrowser for help on using the browser.