aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2014-10-23 14:11:24 +0200
committerJakob Odersky <jodersky@gmail.com>2014-10-23 14:11:24 +0200
commit4dfb70b68137b23747d321702ee5dc720287b73e (patch)
tree8482273030e41e04c90738f09c0c89ce9d432455
downloadmaverick-4dfb70b68137b23747d321702ee5dc720287b73e.tar.gz
maverick-4dfb70b68137b23747d321702ee5dc720287b73e.tar.bz2
maverick-4dfb70b68137b23747d321702ee5dc720287b73e.zip
initial commit
-rw-r--r--.gitignore7
-rw-r--r--Makefile19
-rw-r--r--controller.c135
3 files changed, 161 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..24b0cb0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+# ide
+*.sublime-project
+*.sublime-workspace
+
+# general files
+*.o
+*~
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..bb9ca92
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+TARGET=controller
+
+all: $(TARGET) $(TARGET)-static
+
+
+$(TARGET): $(TARGET).o
+ gcc -Wall -o $@ $< `sdl2-config --libs`
+
+$(TARGET)-static: $(TARGET).o
+ gcc -Wall -o $@ $< `sdl2-config --static-libs`
+
+
+%.o: %.c
+ gcc -Wall `sdl2-config --cflags` -o $@ -c $<
+
+clean:
+ rm -f *.o
+ rm -f $(TARGET)
+ rm -f $(TARGET)-static \ No newline at end of file
diff --git a/controller.c b/controller.c
new file mode 100644
index 0000000..c5be026
--- /dev/null
+++ b/controller.c
@@ -0,0 +1,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;
+} \ No newline at end of file