aboutsummaryrefslogtreecommitdiff
path: root/controller.c
blob: c5be026e5d85afa85f097273babdb7866eac66ab (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdbool.h>
#include "SDL.h"

void joystick_open(int id, SDL_Joystick* joystick) {

    joystick=SDL_JoystickOpen(id);
  
    if(joystick) {
        fprintf(stderr, "Opened joystick %d\n", id);
        fprintf(stderr, "Name: %s\n", SDL_JoystickName(joystick));
        fprintf(stderr, "Number of axes: %d\n", SDL_JoystickNumAxes(joystick));
        fprintf(stderr, "Number of buttons: %d\n", SDL_JoystickNumButtons(joystick));
        fprintf(stderr, "Number of balls: %d\n", SDL_JoystickNumBalls(joystick));
    } else {
        fprintf(stderr, "Couldn't open joystick %d\n", id);
    }
}

void joystick_close(SDL_Joystick* joystick) {
    if (SDL_JoystickGetAttached(joystick)) {
        SDL_JoystickClose(joystick);
    }
}

int main(int argc, char *argv[]) { 

    //initialize joystick library
    if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
        fprintf(stderr, "Couldn't initialize SDL joystick: %s\n", SDL_GetError());
        exit(1);
    }

    //id of controlling joystick
    int joystick_id = 0;

    int throttle = 0;

    //pointer to joystick in use
    SDL_Joystick* joystick = NULL;

    //event loop
    SDL_Event e;
    bool cont = true;
    while (cont) {

        while( SDL_WaitEventTimeout(&e, 500)) {
            switch( e.type ) {

            case SDL_JOYDEVICEADDED:
                if (e.jdevice.which == joystick_id) {
                    fprintf(stderr, "Joystick connected\n");
                    joystick_open(joystick_id, joystick);
                }
                break;

            case SDL_JOYDEVICEREMOVED:
                if (e.jdevice.which == joystick_id) {
                    fprintf(stderr, "Joystick disconnected\n");
                    joystick_close(joystick);
                }
                break;

            case SDL_JOYAXISMOTION:
                fprintf(stderr, "axis event\n");
                fprintf(stderr, "%d\n", e.jaxis.value);
                break;
            case SDL_JOYHATMOTION:
                fprintf(stderr, "hat event\n");
                break;

            case SDL_JOYBUTTONDOWN:
                switch (e.jbutton.button) {
                case 0:
                    throttle = 12;
                    break;
                case 4:
                    throttle -= 1;
                    break;
                case 5:
                    throttle += 1;
                    break;
                case 6:
                    throttle -= 25;
                    break;
                case 7:
                    throttle += 25;
                    break;
                default:
                    break;
                }

                fprintf(stderr, "button down\n");
                break;

            case SDL_JOYBUTTONUP:
                fprintf(stderr, "button up\n");
                break;

            case SDL_QUIT:
                cont = false;
                break;

            default:
                fprintf(stderr,"Unsupported event type: %d\n", e.type);
                break;
            }

            if (throttle < 0) {
                throttle = 0;
            }
            if (throttle > 255) {
                throttle = 255;
            }
            unsigned char tc = (unsigned char) throttle;
            fprintf(stderr, "%u\n", tc);
            printf("%u\n", tc);
            fflush(stdout);

        }

        unsigned char tc = (unsigned char) throttle;
        //fwrite(&tc, 1, 1, stdout);
        //fflush(stdout);
        printf("%u\n", tc);
        fflush(stdout);

        

        //print_control_data(&c);
        //printf("--\n");
    }

    return 0;
}