From 71697266ed8fe1429594f83a30bad93b0f82a907 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 13 Mar 2016 22:36:46 +0100 Subject: [PATCH] Simplify and clean up joystick example Signed-off-by: Joachim Nilsson --- README.md | 46 ++++++++++++++++++++++----------------------- examples/joystick.c | 18 ++++++++---------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 5bc3444..d71654a 100644 --- a/README.md +++ b/README.md @@ -234,43 +234,43 @@ to act upon joystick input. #include struct js_event { - uint32_t time; /* event timestamp in milliseconds */ - int16_t value; /* value */ - uint8_t type; /* event type */ - uint8_t number; /* axis/button number */ + uint32_t time; /* event timestamp in milliseconds */ + int16_t value; /* value */ + uint8_t type; /* event type */ + uint8_t number; /* axis/button number */ } e; static void joystick_cb(uev_t *w, void *arg, int events) { - read (w->fd, &e, sizeof(e)); + read(w->fd, &e, sizeof(e)); - switch (e.type) { - case 1: - if (e.value) printf("Button %d pressed\n", e.number); - else printf("Button %d released\n", e.number); - break; + switch (e.type) { + case 1: + printf("Button %d %s\n", e.number, e.value ? "pressed" : "released"); + break; - case 2: - printf("Joystick axis %d moved, value %d!\n", e.number, e.value); - break; - } + case 2: + printf("Joystick axis %d moved, value %d!\n", e.number, e.value); + break; + } } int main(void) { - int fd = open("/dev/input/js1", O_RDONLY, O_NONBLOCK); - uev_t js1_watcher; - uev_ctx_t ctx; + int fd; + uev_t watcher; + uev_ctx_t ctx; - if (fd < 0) - errx(errno, "Cannot find a joystick attached."); + fd = open("/dev/input/js1", O_RDONLY, O_NONBLOCK); + if (fd < 0) + errx(errno, "Cannot find a joystick attached."); - uev_init(&ctx); - uev_io_init(&ctx, &js1_watcher, joystick_cb, NULL, fd, UEV_READ); + uev_init(&ctx); + uev_io_init(&ctx, &watcher, joystick_cb, NULL, fd, UEV_READ); - puts("Starting, press Ctrl-C to exit."); + puts("Starting, press Ctrl-C to exit."); - return uev_run(&ctx, 0); + return uev_run(&ctx, 0); } ``` diff --git a/examples/joystick.c b/examples/joystick.c index 4a0efc8..88e8557 100644 --- a/examples/joystick.c +++ b/examples/joystick.c @@ -1,6 +1,6 @@ /* Simple example of how to use libuEv with Linux joystick API * - * Copyright (c) 2014-2015 Joachim Nilsson + * Copyright (c) 2014-2016 Joachim Nilsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,8 +30,6 @@ #include "uev.h" -#define UNUSED(arg) arg __attribute__ ((unused)) - struct js_event { uint32_t time; /* event timestamp in milliseconds */ int16_t value; /* value */ @@ -39,15 +37,14 @@ struct js_event { uint8_t number; /* axis/button number */ } e; -static void joystick_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events)) +static void joystick_cb(uev_t *w, void *arg, int events) { - if (read (w->fd, &e, sizeof(e)) < 0) + if (read(w->fd, &e, sizeof(e)) < 0) errx(errno, "Failed reading joystick event"); switch (e.type) { case 1: - if (e.value) printf("Button %d pressed\n", e.number); - else printf("Button %d released\n", e.number); + printf("Button %d %s\n", e.number, e.value ? "pressed" : "released"); break; case 2: @@ -58,10 +55,11 @@ static void joystick_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events)) int main(void) { - int fd = open("/dev/input/js1", O_RDONLY, O_NONBLOCK); - uev_t js1_watcher; + int fd; + uev_t js1_watcher; uev_ctx_t ctx; + fd = open("/dev/input/js1", O_RDONLY, O_NONBLOCK); if (fd < 0) errx(errno, "Cannot find a joystick attached."); @@ -75,7 +73,7 @@ int main(void) /** * Local Variables: - * compile-command: "gcc -o joystick joystick.c io.o timer.o signal.o main.o" + * compile-command: "make joystick" * version-control: t * indent-tabs-mode: t * c-file-style: "linux"