/
controller.c
101 lines (79 loc) · 2.78 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _XOPEN_SOURCE 500 /* usleep */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wiiuse.h> // XXX
#include "button_event.h"
#include "controller.h"
#include "debug.h"
#include "ending.h"
#include "highlevel_command.h"
#include "rqueue.h"
#include "wiimotes.h"
/* - - - - - - - - - - - - - - - - - - - - */
static void handle_button(struct button_event *b, rqueue_t hlcommandsq);
static void flash_leds(int wiimote_num, rqueue_t hlcommandsq);
static void rumble(int wiimote_num, rqueue_t hlcommandsq);
/* - - - - - - - - - - - - - - - - - - - - */
void *
controller_run(void *v)
{
struct controller_run_args *c_args = v;
ending_t ending = c_args->ending;
rqueue_t buttonsq = c_args->buttonsq;
rqueue_t hlcommandsq = c_args->hlcommandsq;
struct button_event *b;
while (1) {
while (!rqueue_is_empty(buttonsq)) {
b = rqueue_remove(buttonsq);
handle_button(b, hlcommandsq);
button_event_free(b);
}
if (ending_get(ending)) {
break;
}
usleep(10000);
}
print_info("controller exiting");
return NULL;
}
/* - - - - - - - - - - - - - - - - - - - - */
static void
handle_button(struct button_event *b, rqueue_t hlcommandsq)
{
const char *button_name;
switch (b->button) {
case WIIMOTE_BUTTON_A: button_name = "A"; break;
case WIIMOTE_BUTTON_B: button_name = "B"; break;
case WIIMOTE_BUTTON_UP: button_name = "UP"; break;
case WIIMOTE_BUTTON_DOWN: button_name = "DOWN"; break;
case WIIMOTE_BUTTON_LEFT: button_name = "LEFT"; break;
case WIIMOTE_BUTTON_RIGHT: button_name = "RIGHT"; break;
case WIIMOTE_BUTTON_MINUS: button_name = "MINUS"; break;
case WIIMOTE_BUTTON_PLUS: button_name = "PLUS"; break;
case WIIMOTE_BUTTON_ONE: button_name = "ONE"; break;
case WIIMOTE_BUTTON_TWO: button_name = "TWO"; break;
case WIIMOTE_BUTTON_HOME: button_name = "HOME"; break;
default: button_name = "<unknown>"; break;
}
print_info("button pressed: wiimote %d, button %s", b->id, button_name);
switch (b->button) {
case WIIMOTE_BUTTON_A: flash_leds(b->id, hlcommandsq); break;
case WIIMOTE_BUTTON_B: rumble(b->id, hlcommandsq); break;
}
}
static void flash_leds(int wiimote_num, rqueue_t hlcommandsq)
{
struct highlevel_command *hlc;
hlc = highlevel_command_new(wiimote_num, HIGHLEVEL_COMMAND_FLASH);
hlc->parameters.flash.pattern_num = 0;
rqueue_add(hlcommandsq, hlc);
}
static void rumble(int wiimote_num, rqueue_t hlcommandsq)
{
struct highlevel_command *hlc;
hlc = highlevel_command_new(wiimote_num, HIGHLEVEL_COMMAND_RUMBLE);
hlc->parameters.rumble.pattern_num = 0;
rqueue_add(hlcommandsq, hlc);
}