Changeset 130

Show
Ignore:
Timestamp:
06/28/07 18:25:39 (2 years ago)
Author:
dsmith
Message:

ir_ptr rewrite

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r128 r130  
     12007-06-27 L. Donnie Smith <cwiid@abstrakraft.org> 
     2        wminput 
     3        * rewrote ir_ptr plugin 
     4 
     5        libcwiid 
     6        * changed version 
     7 
    182007-06-18 L. Donnie Smith <cwiid@abstrakraft.org> 
    29        wminput 
  • trunk/wminput/plugins/ir_ptr/ir_ptr.c

    r125 r130  
    1616 * 
    1717 *  ChangeLog: 
     18 *  2007-06-28 L. Donnie Smith <cwiid@abstrakraft.org> 
     19 *  * Converted to single source 
     20 *  * Added deadspace at edges 
     21 * 
    1822 *  2007-06-01 L. Donnie Smith <cwiid@abstrakraft.org> 
    1923 *  * updated for wmplugin_exec prototype change (&mesg->mesg) 
     
    4549#include "wmplugin.h" 
    4650 
     51#define DEBOUNCE_THRESHOLD      50 
     52#define NEW_AMOUNT      0.3 
     53#define OLD_AMOUNT      (1.0 - NEW_AMOUNT) 
     54#define X_EDGE  50 
     55#define Y_EDGE  50 
     56 
    4757cwiid_wiimote_t *wiimote; 
    4858 
    49 struct cursor { 
    50         unsigned char valid; 
    51         uint16_t pos[2]; 
    52 }; 
    53 int a_debounce, b_debounce; 
    54  
    55 /* static objects are initialized to 0 by default */ 
    56 static int a_index = -1, b_index = -1; 
    57 static struct cwiid_ir_src a, b, prev_a, prev_b; 
    58  
    59 static unsigned char info_init = 0; 
    6059static struct wmplugin_info info; 
    6160static struct wmplugin_data data; 
    62  
    63 static struct cursor c, prev_c; 
    6461 
    6562wmplugin_info_t wmplugin_info; 
     
    6865 
    6966struct wmplugin_info *wmplugin_info() { 
     67        static unsigned char info_init = 0; 
     68 
    7069        if (!info_init) { 
    7170                info.button_count = 0; 
     
    7372                info.axis_info[0].name = "X"; 
    7473                info.axis_info[0].type = WMPLUGIN_ABS; 
    75                 info.axis_info[0].max  = 1024
    76                 info.axis_info[0].min  = 0
     74                info.axis_info[0].max  = CWIID_IR_X_MAX - X_EDGE
     75                info.axis_info[0].min  = X_EDGE
    7776                info.axis_info[0].fuzz = 0; 
    7877                info.axis_info[0].flat = 0; 
    7978                info.axis_info[1].name = "Y"; 
    8079                info.axis_info[1].type = WMPLUGIN_ABS; 
    81                 info.axis_info[1].max  = 768
    82                 info.axis_info[1].min  = 0
     80                info.axis_info[1].max  = CWIID_IR_Y_MAX - Y_EDGE
     81                info.axis_info[1].min  = Y_EDGE
    8382                info.axis_info[1].fuzz = 0; 
    8483                info.axis_info[1].flat = 0; 
     
    104103struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[]) 
    105104{ 
     105        static int src_index = -1; 
     106        static int debounce = 0; 
     107        static uint8_t old_flag; 
     108 
    106109        int i; 
    107         uint8_t flags; 
    108         static uint8_t old_flags; 
     110        uint8_t flag; 
    109111        struct cwiid_ir_mesg *ir_mesg; 
    110112 
     
    120122        } 
    121123 
    122         /* update history */ 
    123         prev_a = a; 
    124         prev_b = b; 
    125         prev_c = c
    126  
    127         /* invalidate a & b indices if sources are no longer present */ 
    128         if ((a_index != -1) && (!ir_mesg->src[a_index].valid)) { 
    129                 a_index = -1; 
     124        /* invalidate src index if source is no longer present */ 
     125        if ((src_index != -1) && !ir_mesg->src[src_index].valid) { 
     126               if (debounce > DEBOUNCE_THRESHOLD) { 
     127                       src_index = -1
     128                } 
     129               else { 
     130                       debounce++; 
     131                } 
    130132        } 
    131         if ((b_index != -1) && (!ir_mesg->src[b_index].valid))
    132                 b_index = -1
     133        else
     134                debounce = 0
    133135        } 
    134136 
    135         /* of not set, pick largest available source for a & b */ 
    136         if (a_index == -1) { 
     137        /* of not set, pick largest available source */ 
     138        if (src_index == -1) { 
    137139                for (i=0; i < CWIID_IR_SRC_COUNT; i++) { 
    138                         if ((ir_mesg->src[i].valid) && (i != b_index)) { 
    139                                 if ((a_index == -1) || 
    140                                   (ir_mesg->src[i].size > ir_mesg->src[a_index].size)) { 
    141                                         a_index = i; 
    142                                 } 
    143                         } 
    144                 } 
    145         } 
    146         /* if there is no current src_b, pick the largest valid one */ 
    147         if (b_index == -1) { 
    148                 for (i=0; i < CWIID_IR_SRC_COUNT; i++) { 
    149                         if ((ir_mesg->src[i].valid) && (i != a_index)) { 
    150                                 if ((b_index == -1) || 
    151                                   (ir_mesg->src[i].size > ir_mesg->src[b_index].size)) { 
    152                                         b_index = i; 
     140                        if (ir_mesg->src[i].valid) { 
     141                                if ((src_index == -1) || 
     142                                  (ir_mesg->src[i].size > ir_mesg->src[src_index].size)) { 
     143                                        src_index = i; 
    153144                                } 
    154145                        } 
     
    156147        } 
    157148 
    158 #define DEBOUNCE_THRESHOLD 50 
     149        /* LEDs */ 
     150        switch (src_index) { 
     151        case 0: 
     152                flag = CWIID_LED1_ON; 
     153                break; 
     154        case 1: 
     155                flag = CWIID_LED2_ON; 
     156                break; 
     157        case 2: 
     158                flag = CWIID_LED3_ON; 
     159                break; 
     160        case 3: 
     161                flag = CWIID_LED4_ON; 
     162                break; 
     163        default: 
     164                flag = 0; 
     165                break; 
     166        } 
     167        if (flag != old_flag) { 
     168                cwiid_set_led(wiimote, flag); 
     169                old_flag = flag; 
     170        } 
    159171 
    160         /* set a & b, mirror the x coordinates */ 
    161         if (a_index == -1) { 
    162                 a_debounce++; 
    163                 if( a_debounce > DEBOUNCE_THRESHOLD ) { 
    164                         a.valid = 0; 
     172        if ((src_index == -1) || !ir_mesg->src[src_index].valid) { 
     173                data.axes[0].valid = data.axes[1].valid = 0; 
     174        } 
     175        else { 
     176                data.axes[0].valid = data.axes[1].valid = 1; 
     177                data.axes[0].value = NEW_AMOUNT * (CWIID_IR_X_MAX - 
     178                                         ir_mesg->src[src_index].pos[CWIID_X]) 
     179                                   + OLD_AMOUNT * data.axes[0].value; 
     180                data.axes[1].value = NEW_AMOUNT * ir_mesg->src[src_index].pos[CWIID_Y] 
     181                                   + OLD_AMOUNT * data.axes[1].value; 
     182 
     183                if (data.axes[0].value > CWIID_IR_X_MAX - X_EDGE) { 
     184                        data.axes[0].value = CWIID_IR_X_MAX - X_EDGE; 
    165185                } 
    166                 else { 
    167                         a = prev_a; 
     186                else if (data.axes[0].value < X_EDGE) { 
     187                        data.axes[0].value = X_EDGE; 
     188                } 
     189                if (data.axes[1].value > CWIID_IR_Y_MAX - Y_EDGE) { 
     190                        data.axes[1].value = CWIID_IR_Y_MAX - Y_EDGE; 
     191                } 
     192                else if (data.axes[1].value < Y_EDGE) { 
     193                        data.axes[1].value = Y_EDGE; 
    168194                } 
    169195        } 
    170         else { 
    171                 a = ir_mesg->src[a_index]; 
    172                 a.pos[CWIID_X] = CWIID_IR_X_MAX - a.pos[CWIID_X]; 
    173                 a_debounce = 0; 
    174         } 
    175         if (b_index == -1) { 
    176                 b_debounce++; 
    177                 if( b_debounce > DEBOUNCE_THRESHOLD ) { 
    178                         b.valid = 0; 
    179                 } 
    180                 else { 
    181                         b = prev_b; 
    182                 } 
    183         } 
    184         else { 
    185                 b = ir_mesg->src[b_index]; 
    186                 b.pos[CWIID_X] = CWIID_IR_X_MAX - b.pos[CWIID_X]; 
    187                 b_debounce = 0; 
    188         } 
    189  
    190         /* if both sources are valid, calculate the center */ 
    191         if (a.valid && b.valid) { 
    192                 c.valid = 1; 
    193                 for (i=0; i < 2; i++) { 
    194                         c.pos[i] = (a.pos[i] + b.pos[i])/2; 
    195                 } 
    196         } 
    197         /* if either source is valid, use best guess */ 
    198         else if (a.valid) { 
    199                 /* if a isn't new, and we have a previous center, 
    200                  * assume source-center relationship holds */ 
    201                 if (prev_a.valid && prev_c.valid) { 
    202                         c.valid = 1; 
    203                         for (i=0; i < 2; i++) { 
    204                                 c.pos[i] = a.pos[i] + (prev_c.pos[i] - prev_a.pos[i]); 
    205                         } 
    206                 } 
    207                 /* if a is new or we don't have a previous center, 
    208                  * use a as the center */ 
    209                 else { 
    210                         c.valid = 1; 
    211                         for (i=0; i < 2; i++) { 
    212                                 c.pos[i] = a.pos[i]; 
    213                         } 
    214                 } 
    215         } 
    216         else if (b.valid) { 
    217                 /* if b isn't new, and we have a previous center, 
    218                  * assume source-center relationship holds */ 
    219                 if (prev_b.valid && prev_c.valid) { 
    220                         c.valid = 1; 
    221                         for (i=0; i < 2; i++) { 
    222                                 c.pos[i] = b.pos[i] + (prev_c.pos[i] - prev_b.pos[i]); 
    223                         } 
    224                 } 
    225                 /* if b is new or we don't have a previous center, 
    226                  * use b as the center */ 
    227                 else { 
    228                         c.valid = 1; 
    229                         for (i=0; i < 2; i++) { 
    230                                 c.pos[i] = b.pos[i]; 
    231                         } 
    232                 } 
    233         } 
    234         /* no sources, no guesses */ 
    235         else { 
    236                 c.valid = 0; 
    237         } 
    238  
    239         /* LEDs */ 
    240         flags = 0; 
    241         if ((a_index == 1) || (b_index == 1)) { 
    242                 flags |= CWIID_LED1_ON; 
    243         } 
    244         else if ((a_index == 2) || (b_index == 2)) { 
    245                 flags |= CWIID_LED2_ON; 
    246         } 
    247         else if ((a_index == 3) || (b_index == 3)) { 
    248                 flags |= CWIID_LED3_ON; 
    249         } 
    250         else if ((a_index == 4) || (b_index == 4)) { 
    251                 flags |= CWIID_LED4_ON; 
    252         } 
    253         if (flags != old_flags) { 
    254                 cwiid_set_led(wiimote, flags); 
    255         } 
    256         old_flags = flags; 
    257  
    258         data.axes[0].valid = data.axes[1].valid = c.valid; 
    259  
    260 #define NEW_AMOUNT 0.6 
    261 #define OLD_AMOUNT (1.0-NEW_AMOUNT) 
    262  
    263         data.axes[0].value = c.pos[CWIID_X]*NEW_AMOUNT + 
    264                              data.axes[0].value*OLD_AMOUNT; 
    265         data.axes[1].value = c.pos[CWIID_Y]*NEW_AMOUNT + 
    266                              data.axes[1].value*OLD_AMOUNT; 
    267196 
    268197        return &data;