diff options
author | Jakob Odersky <jodersky@gmail.com> | 2014-10-25 15:08:17 +0200 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2014-10-25 15:08:17 +0200 |
commit | fa91766c740a238d17b9cd556b34c8ee054486cb (patch) | |
tree | 435bc4d959b2abeb08a6890153ea9efbe87d85cb | |
parent | 4dfb70b68137b23747d321702ee5dc720287b73e (diff) | |
download | maverick-fa91766c740a238d17b9cd556b34c8ee054486cb.tar.gz maverick-fa91766c740a238d17b9cd556b34c8ee054486cb.tar.bz2 maverick-fa91766c740a238d17b9cd556b34c8ee054486cb.zip |
implement generic controller
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | controller.c | 135 | ||||
-rw-r--r-- | controller.h | 40 | ||||
-rw-r--r-- | generic.c | 41 | ||||
-rw-r--r-- | main.c | 97 |
6 files changed, 194 insertions, 141 deletions
@@ -3,5 +3,7 @@ *.sublime-workspace # general files +controller +controller-static *.o *~ @@ -1,17 +1,25 @@ TARGET=controller -all: $(TARGET) $(TARGET)-static +CC=gcc +CFLAGS=-std=gnu99 -Wall -O2 + +LD=gcc +LDFLAGS=-O2 +SOURCES=$(wildcard *.c) +OBJECTS=$(addsuffix .o, $(basename $(SOURCES))) -$(TARGET): $(TARGET).o - gcc -Wall -o $@ $< `sdl2-config --libs` -$(TARGET)-static: $(TARGET).o - gcc -Wall -o $@ $< `sdl2-config --static-libs` +all: $(TARGET) $(TARGET)-static + +$(TARGET): $(OBJECTS) + $(LD) $(LDFLAGS) -o $@ $^ `sdl2-config --libs` +$(TARGET)-static: $(OBJECTS) + $(LD) $(LDFLAGS) -o $@ $^ `sdl2-config --static-libs` %.o: %.c - gcc -Wall `sdl2-config --cflags` -o $@ -c $< + $(CC) $(CFLAGS) `sdl2-config --cflags` -o $@ -c $< clean: rm -f *.o diff --git a/controller.c b/controller.c deleted file mode 100644 index c5be026..0000000 --- a/controller.c +++ /dev/null @@ -1,135 +0,0 @@ -#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; -}
\ No newline at end of file diff --git a/controller.h b/controller.h new file mode 100644 index 0000000..0f81c66 --- /dev/null +++ b/controller.h @@ -0,0 +1,40 @@ +#ifndef CONTROLLER_H +#define CONTROLLER_H + +#include <stdbool.h> +#include <stddef.h> + +#define AXIS_MIN -32768 +#define AXIS_MAX 32767 +#define CHANNELS 5 + +typedef unsigned char channel_t; + +inline channel_t axis_to_channel(int value) { + long centered = (long) value - AXIS_MIN; + return (channel_t) (centered * 255 / (AXIS_MAX - AXIS_MIN)); +} + +inline channel_t channel_clamp(channel_t channel, channel_t min, channel_t max) { + channel_t upper = (channel < max) ? channel : max; + return (min < upper) ? upper : min; +} + +inline void channel_increase(channel_t* channels, size_t channel_index, channel_t step, channel_t min, channel_t max) { + channel_t current = channels[channel_index]; + channel_t next = channel_clamp(current + step, min, max); + channels[channel_index] = next; +} + +inline void channel_decrease(channel_t* channels, size_t channel_index, channel_t step, channel_t min, channel_t max) { + channel_t current = channels[channel_index]; + channel_t next = channel_clamp(current - step, min, max); + channels[channel_index] = next; +} + +void channel_reset(channel_t* channels); +void event_axis(channel_t* channels, int axis, int value); +void event_button(channel_t* channels, int button, bool value); + + +#endif
\ No newline at end of file diff --git a/generic.c b/generic.c new file mode 100644 index 0000000..8c32a97 --- /dev/null +++ b/generic.c @@ -0,0 +1,41 @@ +#include "controller.h" + +#define CHANNEL_THROTTLE 0 + +#define THROTTLE_MIN 12 +#define THROTTLE_MAX 255 + +void channel_reset(channel_t* channels) { + channels[0] = THROTTLE_MIN; + channels[1] = 127; + channels[2] = 127; + channels[3] = 127; + channels[4] = 127; +} +void event_axis(channel_t* channels, int axis, int value) { + channels[axis+1] = axis_to_channel(value); +} +void event_button(channel_t* channels, int button, bool value) { + if (value) { + switch(button) { + case 0: + channel_reset(channels); + break; + case 4: + channel_decrease(channels, CHANNEL_THROTTLE, 1, THROTTLE_MIN, THROTTLE_MAX); + break; + case 5: + channel_increase(channels, CHANNEL_THROTTLE, 1, THROTTLE_MIN, THROTTLE_MAX); + break; + case 6: + channel_decrease(channels, CHANNEL_THROTTLE, 10, THROTTLE_MIN, THROTTLE_MAX); + break; + case 7: + channel_increase(channels, CHANNEL_THROTTLE, 10, THROTTLE_MIN, THROTTLE_MAX); + break; + default: + break; + } + + } +}
\ No newline at end of file @@ -0,0 +1,97 @@ +#include <stdio.h> +#include <SDL.h> +#include "controller.h" + +static int run(int joystick_id, int timeout, bool debug); +static void print_channels(channel_t* channels, bool debug); + +int main(int argc, char *argv[]) { + + //initialize joystick library + if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { + fprintf(stderr, "Couldn't initialize SDL joystick subsystem: %s\n", SDL_GetError()); + exit(1); + } + + run(0, 500, true); + + + return 0; +} + +static int run(int joystick_id, int timeout, bool debug) { + + channel_t channels[CHANNELS]; + channel_reset(channels); + + //pointer to joystick in use + SDL_Joystick* joystick = NULL; + + //event loop + SDL_Event e; + bool cont = true; + while (cont) { + + if(SDL_WaitEventTimeout(&e, timeout)) { + switch( e.type ) { + + case SDL_JOYDEVICEADDED: + if (e.jdevice.which == joystick_id) { + fprintf(stderr, "Joystick %d connected\n", e.jdevice.which); + joystick=SDL_JoystickOpen(e.jdevice.which); + if(joystick) { + fprintf(stderr, "Opened joystick %d\n", e.jdevice.which); + 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", e.jdevice.which); + } + } + break; + + case SDL_JOYDEVICEREMOVED: + if (e.jdevice.which == joystick_id) { + if (SDL_JoystickGetAttached(joystick)) { + SDL_JoystickClose(joystick); + } + fprintf(stderr, "Joystick %d disconnected\n", e.jdevice.which); + } + break; + + case SDL_JOYAXISMOTION: + event_axis(channels, e.jaxis.axis, e.jaxis.value); + break; + + case SDL_JOYBUTTONDOWN: + event_button(channels, e.jbutton.button, true); + break; + + case SDL_JOYBUTTONUP: + event_button(channels, e.jbutton.button, false); + break; + + case SDL_QUIT: + cont = false; + break; + + default: + fprintf(stderr,"Unsupported event type: %d\n", e.type); + break; + } + + } + print_channels(channels, debug); + } + return 0; +} + +void print_channels(channel_t* channels, bool debug) { + for(size_t i=0; i < CHANNELS; ++i) { + fprintf(stdout, "%u\n", channels[i]); + fprintf(stderr, "channel %zu: %u\n", i, channels[i]); + } + fflush(stdout); + fflush(stderr); +}
\ No newline at end of file |