Ticket #87: nunchuk_mouse-plugin.patch

File nunchuk_mouse-plugin.patch, 7.2 kB (added by jonaskoelker, 3 years ago)

nunchuk_mouse plugin

  • configure.ac

     
    144144        [wminput/plugins/ir_ptr/Makefile] 
    145145        [wminput/plugins/acc/Makefile] 
    146146        [wminput/plugins/nunchuk_acc/Makefile] 
     147        [wminput/plugins/nunchuk_mouse/Makefile] 
    147148        [wminput/plugins/led/Makefile] 
    148149        [wminput/plugins/nunchuk_stick2btn/Makefile] 
    149150        [lswm/Makefile] 
  • wminput/plugins/Makefile.in

     
    22 
    33include @top_builddir@/defs.mak 
    44 
    5 PLUGINS = ir_ptr acc nunchuk_acc led nunchuk_stick2btn 
     5PLUGINS = ir_ptr acc nunchuk_acc led nunchuk_stick2btn nunchuk_mouse 
    66 
    77all install clean distclean uninstall: TARGET += $(MAKECMDGOALS) 
    88 
  • wminput/plugins/nunchuk_mouse/Makefile.in

     
     1#Copyright (C) 2009 Jonas Kölker <jonaskoelker@gnu.org> 
     2 
     3include @top_builddir@/defs.mak 
     4 
     5PLUGIN_NAME = nunchuk_mouse 
     6SOURCES = nunchuk_mouse.c 
     7CFLAGS += -I@top_builddir@/wminput -I@top_builddir@/libcwiid 
     8LDLIBS += -lm 
     9INST_DIR = $(CWIID_PLUGINS_DIR) 
     10 
     11include $(COMMON)/include/plugin.mak 
     12 
     13distclean: clean 
     14        rm Makefile 
     15 
     16.PHONY: distclean 
  • wminput/plugins/nunchuk_mouse/nunchuk_mouse.c

     
     1/* Copyright (C) 2009 Jonas Kölker <jonaskoelker@gnu.org> 
     2 * 
     3 *  This program is free software; you can redistribute it and/or 
     4 *  modify it under the terms of the GNU General Public License as 
     5 *  published by the Free Software Foundation; either version 2 of the 
     6 *  License, or (at your option) any later version. 
     7 * 
     8 *  This program is distributed in the hope that it will be useful, 
     9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
     10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     11 *  General Public License for more details. 
     12 * 
     13 *  You should have received a copy of the GNU General Public License 
     14 *  along with this program; if not, write to the Free Software 
     15 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
     16 *  02110-1301 USA 
     17 */ 
     18 
     19#include <math.h> 
     20 
     21#include "wmplugin.h" 
     22#include <cwiid.h> 
     23#include "nunchuk_mouse.h" 
     24 
     25static void process_nunchuk(struct cwiid_nunchuk_mesg *mesg) 
     26{ 
     27    /* st(x|y): stick value along direction */ 
     28        const uint8_t stx = mesg->stick[CWIID_X]; 
     29        const uint8_t sty = mesg->stick[CWIID_Y]; 
     30    /* d(x|y)(n|p): "delta x, direction=negative", etc. */ 
     31    const int dxn = (center_x - DEADZONE) - stx; 
     32    const int dxp = stx - (center_x + DEADZONE); 
     33    const int dyn = (center_y - DEADZONE) - sty; 
     34    const int dyp = sty - (center_y + DEADZONE); 
     35 
     36    /* TODO: with -REL_Y, we go down slower than we go up. */ 
     37    data.buttons = 0; 
     38    /* += (vs =): to accumulate the result of multiple events. */ 
     39    data.axes[X].value += 
     40        (dxn >= 0)? -(dxn * RANGE / x_neg_range): 
     41        (dxp >= 0)? +(dxp * RANGE / x_pos_range): 0; 
     42    data.axes[Y].value += 
     43        (dyn >= 0)? -(dyn * RANGE / y_neg_range): 
     44        (dyp >= 0)? +(dyp * RANGE / y_pos_range): 0; 
     45} 
     46 
     47static void calibrate_joystick() { 
     48    int ret; 
     49    uint8_t buf[6]; 
     50 
     51    /* http://abstrakraft.org/cwiid/ticket/40 */ 
     52    ret = cwiid_read(wiimote, CWIID_RW_REG | CWIID_RW_DECODE, 
     53                     0xA40028, sizeof buf, buf); 
     54    if (ret) wmplugin_err(plugin_id, "joystick calibration error"); 
     55 
     56    center_x = buf[OFF_X + OFF_CENTER]; 
     57    x_neg_range = (center_x - DEADZONE) - buf[OFF_X - OFF_MIN]; 
     58    x_pos_range = buf[OFF_X + OFF_MAX] - (center_x + DEADZONE); 
     59 
     60    center_y = buf[OFF_Y + OFF_CENTER]; 
     61    y_neg_range = (center_y - DEADZONE) - buf[OFF_Y - OFF_MIN]; 
     62    y_pos_range = buf[OFF_Y + OFF_MAX] - (center_y + DEADZONE); 
     63} 
     64 
     65struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[]) 
     66{ 
     67        int i; 
     68        enum cwiid_ext_type ext_type = CWIID_EXT_NONE; 
     69        struct wmplugin_data *ret = NULL; 
     70 
     71    data.axes[X].value = data.axes[Y].value = 0; 
     72        for (i=0; i < mesg_count; i++) { 
     73                switch (mesg[i].type) { 
     74                case CWIID_MESG_STATUS: 
     75                        if ((mesg[i].status_mesg.ext_type == CWIID_EXT_NUNCHUK) && 
     76                          (ext_type != CWIID_EXT_NUNCHUK)) { 
     77              calibrate_joystick(); 
     78                        } 
     79                        ext_type = mesg[i].status_mesg.ext_type; 
     80                        break; 
     81                case CWIID_MESG_NUNCHUK: 
     82                        process_nunchuk(&mesg[i].nunchuk_mesg); 
     83            ret = &data; 
     84            break; 
     85                default: 
     86                        break; 
     87                } 
     88        } 
     89    return ret; 
     90} 
     91 
     92int wmplugin_init(int id, cwiid_wiimote_t *arg_wiimote) 
     93{ 
     94        plugin_id = id; 
     95        wiimote = arg_wiimote; 
     96        data.buttons = 0; 
     97        data.axes[0].valid = 1; 
     98        data.axes[1].valid = 1; 
     99        if (wmplugin_set_rpt_mode(id, CWIID_RPT_STATUS | CWIID_RPT_NUNCHUK)) { 
     100                return -1; 
     101        } 
     102 
     103        return 0; 
     104} 
     105 
     106struct wmplugin_info *wmplugin_info() { 
     107    if (info_init) return &info; 
     108        info.button_count = 0; 
     109        info.axis_count = 2; 
     110 
     111    info.axis_info[X].name = "X"; 
     112        info.axis_info[X].type = WMPLUGIN_REL; 
     113        info.axis_info[X].min  = -RANGE; 
     114        info.axis_info[X].max  = +RANGE; 
     115        info.axis_info[X].fuzz = 0; 
     116        info.axis_info[X].flat = 0; 
     117 
     118    info.axis_info[Y].name = "Y"; 
     119        info.axis_info[Y].type = WMPLUGIN_REL; 
     120        info.axis_info[Y].min  = -RANGE; 
     121        info.axis_info[Y].max  = +RANGE; 
     122        info.axis_info[Y].fuzz = 0; 
     123        info.axis_info[Y].flat = 0; 
     124 
     125    info_init = 1; 
     126        return &info; 
     127} 
  • wminput/plugins/nunchuk_mouse/nunchuk_mouse.h

     
     1/* Button flags */ 
     2#define STICK_KEY_UP            0x0001 
     3#define STICK_KEY_DOWN          0x0002 
     4#define STICK_KEY_RIGHT         0x0004 
     5#define STICK_KEY_LEFT          0x0008 
     6 
     7/* array indices */ 
     8#define X                   0 
     9#define Y                   1 
     10#define OFF_X               0 
     11#define OFF_Y               3 
     12#define OFF_MAX             0 
     13#define OFF_MIN             1 
     14#define OFF_CENTER          2 
     15 
     16/* misc */ 
     17#define DEADZONE            10 
     18#define RANGE               10 /* TODO: make this a parameter */ 
     19 
     20/* calibration data.  Since the center isn't guaranteed to be the 
     21 * average of min and max, we distinguish (as in the cases are not 
     22 * symmetric) between being below center vs. being above center. */ 
     23static uint8_t center_x, center_y; 
     24static uint8_t x_neg_range, x_pos_range, y_neg_range, y_pos_range; 
     25 
     26/* in data, accumulate mouse motions for all the events in each 
     27 * batch.  After the batch, return it to wminput. */ 
     28static struct wmplugin_data data; 
     29 
     30/* bookkeeping info */ 
     31static unsigned char info_init = 0; 
     32static cwiid_wiimote_t *wiimote; 
     33static int plugin_id; 
     34static struct wmplugin_info info; 
     35 
     36/* function declarations */ 
     37wmplugin_info_t wmplugin_info; 
     38wmplugin_init_t wmplugin_init; 
     39wmplugin_exec_t wmplugin_exec; 
     40static void process_nunchuk(struct cwiid_nunchuk_mesg *mesg); 
     41static void calibrate_joystick(void);