Ticket #63: balance.diff

File balance.diff, 17.4 kB (added by TBBle, 4 years ago)

Patch to cwiid to support the Wii Balance Board

  • python/cwiidmodule.c

     
    9696        CWIID_CONST_MACRO(RPT_IR), 
    9797        CWIID_CONST_MACRO(RPT_NUNCHUK), 
    9898        CWIID_CONST_MACRO(RPT_CLASSIC), 
     99        CWIID_CONST_MACRO(RPT_BALANCE), 
    99100        CWIID_CONST_MACRO(RPT_EXT), 
    100101        CWIID_CONST_MACRO(LED1_ON), 
    101102        CWIID_CONST_MACRO(LED2_ON), 
     
    153154        CWIID_CONST_MACRO(MESG_IR), 
    154155        CWIID_CONST_MACRO(MESG_NUNCHUK), 
    155156        CWIID_CONST_MACRO(MESG_CLASSIC), 
     157        CWIID_CONST_MACRO(MESG_BALANCE), 
    156158        CWIID_CONST_MACRO(MESG_ERROR), 
    157159        CWIID_CONST_MACRO(MESG_UNKNOWN), 
    158160        CWIID_CONST_MACRO(EXT_NONE), 
    159161        CWIID_CONST_MACRO(EXT_NUNCHUK), 
    160162        CWIID_CONST_MACRO(EXT_CLASSIC), 
     163        CWIID_CONST_MACRO(EXT_BALANCE), 
    161164        CWIID_CONST_MACRO(EXT_UNKNOWN), 
    162165        CWIID_CONST_MACRO(ERROR_DISCONNECT), 
    163166        CWIID_CONST_MACRO(ERROR_COMM), 
  • python/Wiimote.c

     
    8484static PyObject *Wiimote_get_state(Wiimote *self, void *closure); 
    8585static PyObject *Wiimote_get_acc_cal(Wiimote *self, PyObject *args, 
    8686                                     PyObject *kwds); 
     87static PyObject *Wiimote_get_balance_cal(Wiimote *self); 
    8788 
    8889static PyObject *Wiimote_request_status(Wiimote *self); 
    8990static int Wiimote_set_led(Wiimote *self, PyObject *PyLed, void *closure); 
     
    112113        {"get_acc_cal", (PyCFunction)Wiimote_get_acc_cal, 
    113114         METH_VARARGS | METH_KEYWORDS, 
    114115         "get_acc_cal(extension) -> calibration tuple\n\n" 
    115          "retrieve calibration information"}, 
     116         "retrieve accelerometer calibration information"}, 
     117        {"get_balance_cal", (PyCFunction)Wiimote_get_balance_cal, METH_NOARGS, 
     118         "get_balance_cal() -> calibration tuple\n\n" 
     119         "retrieve Balance Board calibration information"}, 
    116120        {"request_status", (PyCFunction)Wiimote_request_status, METH_NOARGS, 
    117121         "request_status()\n\nrequest status message"}, 
    118122        {"read", (PyCFunction)Wiimote_read, METH_VARARGS | METH_KEYWORDS, 
     
    521525                        Py_DECREF(PyExt); 
    522526                } 
    523527                break; 
     528        case CWIID_EXT_BALANCE: 
     529                if (state.rpt_mode & CWIID_RPT_BALANCE) { 
     530                        PyExt = Py_BuildValue("{s:I,s:I,s:I,s:I}", 
     531                                              "right_top", 
     532                                                state.ext.balance.right_top, 
     533                                              "right_bottom", 
     534                                                state.ext.balance.right_bottom, 
     535                                              "left_top", 
     536                                                state.ext.balance.left_top, 
     537                                              "left_bottom", 
     538                                                state.ext.balance.left_bottom); 
     539 
     540                        if (!PyExt) { 
     541                                Py_DECREF(PyState); 
     542                                return NULL; 
     543                        } 
     544 
     545                        if (PyDict_SetItemString(PyState, "balance", PyExt)) { 
     546                                Py_DECREF(PyState); 
     547                                Py_DECREF(PyExt); 
     548                                return NULL; 
     549                        } 
     550 
     551                        Py_DECREF(PyExt); 
     552                } 
     553                break; 
    524554        default: 
    525555                break; 
    526556        } 
     
    558588        return PyAccCal; 
    559589} 
    560590 
     591static PyObject *Wiimote_get_balance_cal(Wiimote *self) 
     592{ 
     593        struct balance_cal balance_cal; 
     594        PyObject *PyBalCal; 
     595 
     596        if (cwiid_get_balance_cal(self->wiimote, &balance_cal)) { 
     597                PyErr_SetString(PyExc_RuntimeError, 
     598                                "Error getting balance board calibration"); 
     599                return NULL; 
     600        } 
     601 
     602        if (!(PyBalCal = Py_BuildValue("([i,i,i],[i,i,i],[i,i,i],[i,i,i])", 
     603                                       balance_cal.right_top[0], 
     604                                       balance_cal.right_top[1], 
     605                                       balance_cal.right_top[2], 
     606                                       balance_cal.right_bottom[0], 
     607                                       balance_cal.right_bottom[1], 
     608                                       balance_cal.right_bottom[2], 
     609                                       balance_cal.left_top[0], 
     610                                       balance_cal.left_top[1], 
     611                                       balance_cal.left_top[2], 
     612                                       balance_cal.left_bottom[0], 
     613                                       balance_cal.left_bottom[1], 
     614                                       balance_cal.left_bottom[2]))) { 
     615                return NULL; 
     616        } 
     617 
     618        return PyBalCal; 
     619} 
     620 
    561621static PyObject *Wiimote_request_status(Wiimote *self) 
    562622{ 
    563623        if (cwiid_request_status(self->wiimote)) { 
     
    728788 *                               "buttons":buttons}, 
    729789 *          (cwiid.CLASSIC_MESG,{"l_stick":(x,y),"r_stick":(x,y),"l":l,"r":r, 
    730790 *                               "buttons":buttons}, 
     791 *          (cwiid.BALANCE_MESG,{"right_top":right_top, 
     792 *                               "right_bottom":right_bottom, 
     793 *                               "left_top":left_top, 
     794 *                               "left_bottom":left_bottom}, 
    731795 *          (cwiid.ERROR_MESG,error)] 
    732796 */ 
    733797PyObject *ConvertMesgArray(int mesg_count, union cwiid_mesg mesg[]) 
     
    834898                                     "r", mesg[i].classic_mesg.r, 
    835899                                     "buttons", mesg[i].classic_mesg.buttons); 
    836900                        break; 
     901                case CWIID_MESG_BALANCE: 
     902                        mesgVal = Py_BuildValue("{s:I,s:I,s:I,s:I}", 
     903                                     "right_top", 
     904                                       mesg[i].balance_mesg.right_top, 
     905                                     "right_bottom", 
     906                                       mesg[i].balance_mesg.right_bottom, 
     907                                     "left_top", 
     908                                       mesg[i].balance_mesg.left_top, 
     909                                     "left_bottom", 
     910                                       mesg[i].balance_mesg.left_bottom); 
     911                        break; 
    837912                case CWIID_MESG_ERROR: 
    838913                        mesgVal = Py_BuildValue("i", mesg[i].error_mesg.error); 
    839914                        break; 
  • libcwiid/process.c

     
    182182{ 
    183183        struct cwiid_nunchuk_mesg *nunchuk_mesg; 
    184184        struct cwiid_classic_mesg *classic_mesg; 
     185        struct cwiid_balance_mesg *balance_mesg; 
    185186        int i; 
    186187 
    187188        switch (wiimote->state.ext_type) { 
     
    224225                                                  (uint16_t)data[5]); 
    225226                } 
    226227                break; 
     228        case CWIID_EXT_BALANCE: 
     229                if (wiimote->state.rpt_mode & CWIID_RPT_BALANCE) { 
     230                        balance_mesg = &ma->array[ma->count++].balance_mesg; 
     231                        balance_mesg->type = CWIID_MESG_BALANCE; 
     232                        balance_mesg->right_top = ((uint16_t)data[0]<<8 | 
     233                                                   (uint16_t)data[1]); 
     234                        balance_mesg->right_bottom = ((uint16_t)data[2]<<8 | 
     235                                                      (uint16_t)data[3]); 
     236                        balance_mesg->left_top = ((uint16_t)data[4]<<8 | 
     237                                                   (uint16_t)data[5]); 
     238                        balance_mesg->left_bottom = ((uint16_t)data[6]<<8 | 
     239                                                      (uint16_t)data[7]); 
     240                } 
     241                break; 
    227242        } 
    228243 
    229244        return 0; 
  • libcwiid/cwiid_internal.h

     
    7373 
    7474/* Wiimote specific magic numbers */ 
    7575#define WIIMOTE_NAME "Nintendo RVL-CNT-01" 
     76#define WIIBALANCE_NAME "Nintendo RVL-WBC-01" 
    7677#define WIIMOTE_CLASS_0 0x04 
    7778#define WIIMOTE_CLASS_1 0x25 
    7879#define WIIMOTE_CLASS_2 0x00 
     
    114115 
    115116/* Extension Values */ 
    116117#define EXT_NONE        0x2E 
    117 #define EXT_PARTIAL 0xFF 
    118 #define EXT_NUNCHUK 0x00 
    119 #define EXT_CLASSIC 0x01 
     118#define EXT_PARTIAL     0xFF 
     119#define EXT_NUNCHUK     0x00 
     120#define EXT_CLASSIC     0x01 
     121#define EXT_BALANCE     0x2A 
    120122 
    121123/* IR Enable blocks */ 
    122124#define MARCAN_IR_BLOCK_1       "\x00\x00\x00\x00\x00\x00\x90\x00\xC0" 
  • libcwiid/interface.c

     
    179179 
    180180        return 0; 
    181181} 
     182 
     183int cwiid_get_balance_cal(struct wiimote *wiimote, 
     184                          struct balance_cal *balance_cal) 
     185{ 
     186        unsigned char buf[24]; 
     187 
     188        if (cwiid_read(wiimote, CWIID_RW_REG, 0xa40024, 24, buf)) { 
     189                cwiid_err(wiimote, "Read error (balancecal)"); 
     190                return -1; 
     191        } 
     192        balance_cal->right_top[0] = ((uint16_t)buf[0]<<8 | (uint16_t)buf[1]); 
     193        balance_cal->right_bottom[0] = ((uint16_t)buf[2]<<8 | (uint16_t)buf[3]); 
     194        balance_cal->left_top[0] = ((uint16_t)buf[4]<<8 | (uint16_t)buf[5]); 
     195        balance_cal->left_bottom[0] = ((uint16_t)buf[6]<<8 | (uint16_t)buf[7]); 
     196        balance_cal->right_top[1] = ((uint16_t)buf[8]<<8 | (uint16_t)buf[9]); 
     197        balance_cal->right_bottom[1] = ((uint16_t)buf[10]<<8 | (uint16_t)buf[11]); 
     198        balance_cal->left_top[1] = ((uint16_t)buf[12]<<8 | (uint16_t)buf[13]); 
     199        balance_cal->left_bottom[1] = ((uint16_t)buf[14]<<8 | (uint16_t)buf[15]); 
     200        balance_cal->right_top[2] = ((uint16_t)buf[16]<<8 | (uint16_t)buf[17]); 
     201        balance_cal->right_bottom[2] = ((uint16_t)buf[18]<<8 | (uint16_t)buf[19]); 
     202        balance_cal->left_top[2] = ((uint16_t)buf[20]<<8 | (uint16_t)buf[21]); 
     203        balance_cal->left_bottom[2] = ((uint16_t)buf[22]<<8 | (uint16_t)buf[23]); 
     204 
     205        return 0; 
     206} 
  • libcwiid/state.c

     
    7575                        wiimote->state.ext.classic.r = mesg->classic_mesg.r; 
    7676                        wiimote->state.ext.classic.buttons = mesg->classic_mesg.buttons; 
    7777                        break; 
     78                case CWIID_MESG_BALANCE: 
     79                        wiimote->state.ext.balance.right_top = mesg->balance_mesg.right_top; 
     80                        wiimote->state.ext.balance.right_bottom = mesg->balance_mesg.right_bottom; 
     81                        wiimote->state.ext.balance.left_top = mesg->balance_mesg.left_top; 
     82                        wiimote->state.ext.balance.left_bottom = mesg->balance_mesg.left_bottom; 
     83                        break; 
    7884                case CWIID_MESG_ERROR: 
    7985                        wiimote->state.error = mesg->error_mesg.error; 
    8086                        break; 
     
    166172                        rpt_type = RPT_EXT21; 
    167173                }        
    168174        } 
     175        else if ((rpt_mode & CWIID_RPT_EXT) && 
     176          wiimote->state.ext_type == CWIID_EXT_BALANCE) { 
     177                rpt_type = RPT_BTN_EXT8; 
     178        } 
    169179        else { 
    170180                if (rpt_mode & CWIID_RPT_IR) { 
    171181                        rpt_type = RPT_BTN_ACC_IR12; 
     
    223233          (CWIID_RPT_CLASSIC & ~rpt_mode & wiimote->state.rpt_mode)) { 
    224234                memset(&wiimote->state.ext, 0, sizeof wiimote->state.ext); 
    225235        } 
     236        else if ((wiimote->state.ext_type == CWIID_EXT_BALANCE) && 
     237          (CWIID_RPT_BALANCE & ~rpt_mode & wiimote->state.rpt_mode)) { 
     238                memset(&wiimote->state.ext, 0, sizeof wiimote->state.ext); 
     239        } 
    226240 
    227241        wiimote->state.rpt_mode = rpt_mode; 
    228242 
  • libcwiid/cwiid.h

     
    7575#define CWIID_RPT_IR            0x08 
    7676#define CWIID_RPT_NUNCHUK       0x10 
    7777#define CWIID_RPT_CLASSIC       0x20 
    78 #define CWIID_RPT_EXT           (CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC) 
     78#define CWIID_RPT_BALANCE       0x40 
     79#define CWIID_RPT_EXT           (CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC | CWIID_RPT_BALANCE) 
    7980 
    8081/* LED flags */ 
    8182#define CWIID_LED1_ON   0x01 
     
    165166        CWIID_MESG_IR, 
    166167        CWIID_MESG_NUNCHUK, 
    167168        CWIID_MESG_CLASSIC, 
     169        CWIID_MESG_BALANCE, 
    168170        CWIID_MESG_ERROR, 
    169171        CWIID_MESG_UNKNOWN 
    170172}; 
     
    173175        CWIID_EXT_NONE, 
    174176        CWIID_EXT_NUNCHUK, 
    175177        CWIID_EXT_CLASSIC, 
     178        CWIID_EXT_BALANCE, 
    176179        CWIID_EXT_UNKNOWN 
    177180}; 
    178181 
     
    187190        uint8_t one[3]; 
    188191}; 
    189192 
     193struct balance_cal { 
     194        uint16_t right_top[3]; 
     195        uint16_t right_bottom[3]; 
     196        uint16_t left_top[3]; 
     197        uint16_t left_bottom[3]; 
     198}; 
     199 
    190200/* Message Structs */ 
    191201struct cwiid_status_mesg { 
    192202        enum cwiid_mesg_type type; 
     
    231241        uint16_t buttons; 
    232242}; 
    233243 
     244struct cwiid_balance_mesg { 
     245        enum cwiid_mesg_type type; 
     246        uint16_t right_top; 
     247        uint16_t right_bottom; 
     248        uint16_t left_top; 
     249        uint16_t left_bottom; 
     250}; 
     251 
    234252struct cwiid_error_mesg { 
    235253        enum cwiid_mesg_type type; 
    236254        enum cwiid_error error; 
     
    244262        struct cwiid_ir_mesg ir_mesg; 
    245263        struct cwiid_nunchuk_mesg nunchuk_mesg; 
    246264        struct cwiid_classic_mesg classic_mesg; 
     265        struct cwiid_balance_mesg balance_mesg; 
    247266        struct cwiid_error_mesg error_mesg; 
    248267}; 
    249268 
     
    262281        uint16_t buttons; 
    263282}; 
    264283 
     284struct balance_state { 
     285        uint16_t right_top; 
     286        uint16_t right_bottom; 
     287        uint16_t left_top; 
     288        uint16_t left_bottom; 
     289}; 
     290 
    265291union ext_state { 
    266292        struct nunchuk_state nunchuk; 
    267293        struct classic_state classic; 
     294        struct balance_state balance; 
    268295}; 
    269296 
    270297struct cwiid_state { 
     
    325352int cwiid_get_state(cwiid_wiimote_t *wiimote, struct cwiid_state *state); 
    326353int cwiid_get_acc_cal(struct wiimote *wiimote, enum cwiid_ext_type ext_type, 
    327354                      struct acc_cal *acc_cal); 
     355int cwiid_get_balance_cal(struct wiimote *wiimote, 
     356                          struct balance_cal *balance_cal); 
    328357 
    329358/* Operations */ 
    330359int cwiid_command(cwiid_wiimote_t *wiimote, enum cwiid_command command, 
  • libcwiid/bluetooth.c

     
    131131 
    132132                /* Filter by name */ 
    133133                if (!(flags & BT_NO_WIIMOTE_FILTER) && 
    134                   strncmp((*bdinfo)[bdinfo_count].name, WIIMOTE_NAME, BT_NAME_LEN)) { 
     134                  strncmp((*bdinfo)[bdinfo_count].name, WIIMOTE_NAME, BT_NAME_LEN) && 
     135                  strncmp((*bdinfo)[bdinfo_count].name, WIIBALANCE_NAME, BT_NAME_LEN)) { 
    135136                        continue; 
    136137                } 
    137138 
  • libcwiid/thread.c

     
    216216                                        case EXT_CLASSIC: 
    217217                                                status_mesg->ext_type = CWIID_EXT_CLASSIC; 
    218218                                                break; 
     219                                        case EXT_BALANCE: 
     220                                                status_mesg->ext_type = CWIID_EXT_BALANCE; 
     221                                                break; 
    219222                                        default: 
    220223                                                status_mesg->ext_type = CWIID_EXT_UNKNOWN; 
    221224                                                break; 
  • wmdemo/wmdemo.py

     
    2626 
    2727        #Connect to address given on command-line, if present 
    2828        print 'Put Wiimote in discoverable mode now (press 1+2)...' 
     29        global wiimote 
    2930        if len(sys.argv) > 1: 
    3031                wiimote = cwiid.Wiimote(sys.argv[1]) 
    3132        else: 
     
    9192 
    9293def print_state(state): 
    9394        print 'Report Mode:', 
    94         for r in ['STATUS', 'BTN', 'ACC', 'IR', 'NUNCHUK', 'CLASSIC']: 
     95        for r in ['STATUS', 'BTN', 'ACC', 'IR', 'NUNCHUK', 'CLASSIC', 'BALANCE']: 
    9596                if state['rpt_mode'] & eval('cwiid.RPT_' + r): 
    9697                        print r, 
    9798        print 
     
    142143                  (state['classic']['buttons'], 
    143144                   state['classic']['l_stick'], state['classic']['r_stick'], 
    144145                   state['classic']['l'], state['classic']['r']) 
     146        elif state['ext_type'] == cwiid.EXT_BALANCE: 
     147                print 'Balance: right_top=%d right_bottom=%d left_top=%d left_bottom=%d' % \ 
     148                  (state['balance']['right_top'], state['balance']['right_bottom'], 
     149                   state['balance']['left_top'], state['balance']['left_bottom']) 
    145150 
    146151def callback(mesg_list): 
    147152        for mesg in mesg_list: 
     
    154159                                print 'Nunchuk' 
    155160                        elif mesg[1]['ext_type'] == cwiid.EXT_CLASSIC: 
    156161                                print 'Classic Controller' 
     162                        elif mesg[1]['ext_type'] == cwiid.EXT_BALANCE: 
     163                                print 'Balance Board' 
    157164                        else: 
    158165                                print 'Unknown Extension' 
    159166 
     
    190197                           state['classic']['l_stick'], state['classic']['r_stick'], 
    191198                           state['classic']['l'], state['classic']['r']) 
    192199                elif mesg[0] ==  cwiid.MESG_ERROR: 
     200                        print "Error message received" 
     201                        global wiimote 
    193202                        wiimote.close() 
    194203                        exit(-1) 
    195204                else: 
  • wmdemo/wmdemo.c

     
    124124                        break; 
    125125                case 'e': 
    126126                        /* CWIID_RPT_EXT is actually 
    127                          * CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC */ 
     127                         * CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC | CWIID_RPT_BALANCE */ 
    128128                        toggle_bit(rpt_mode, CWIID_RPT_EXT); 
    129129                        set_rpt_mode(wiimote, rpt_mode); 
    130130                        break; 
     
    215215        if (state->rpt_mode & CWIID_RPT_IR) printf(" IR"); 
    216216        if (state->rpt_mode & CWIID_RPT_NUNCHUK) printf(" NUNCHUK"); 
    217217        if (state->rpt_mode & CWIID_RPT_CLASSIC) printf(" CLASSIC"); 
     218        if (state->rpt_mode & CWIID_RPT_BALANCE) printf(" BALANCE"); 
    218219        printf("\n"); 
    219220         
    220221        printf("Active LEDs:"); 
     
    272273                       state->ext.classic.r_stick[CWIID_Y], 
    273274                       state->ext.classic.l, state->ext.classic.r); 
    274275                break; 
     276        case CWIID_EXT_BALANCE: 
     277                printf("Balance: right_top=%d right_bottom=%d " 
     278                       "left_top=%d left_bottom=%d\n", 
     279                       state->ext.balance.right_top, 
     280                       state->ext.balance.right_bottom, 
     281                       state->ext.balance.left_top, 
     282                       state->ext.balance.left_bottom); 
     283                break; 
    275284        } 
    276285} 
    277286 
     
    290299{ 
    291300        int i, j; 
    292301        int valid_source; 
     302        struct balance_cal balance_cal; 
    293303 
    294304        for (i=0; i < mesg_count; i++) 
    295305        { 
     
    307317                        case CWIID_EXT_CLASSIC: 
    308318                                printf("Classic Controller"); 
    309319                                break; 
     320                        case CWIID_EXT_BALANCE: 
     321                                if(cwiid_get_balance_cal(wiimote, &balance_cal)) { 
     322                                        printf("Balance board: Failed to fetch calibration data"); 
     323                                } 
     324                                else { 
     325                                        printf("Balance board: right_top=(%d,%d,%d), " 
     326                                                   "right_bottom=(%d,%d,%d), left_top=(%d,%d,%d), " 
     327                                                   "left_bottom=(%d,%d,%d)", 
     328                                                   balance_cal.right_top[0], 
     329                                                   balance_cal.right_top[1], 
     330                                                   balance_cal.right_top[2], 
     331                                                   balance_cal.right_bottom[0], 
     332                                                   balance_cal.right_bottom[1], 
     333                                                   balance_cal.right_bottom[2], 
     334                                                   balance_cal.left_top[0], 
     335                                                   balance_cal.left_top[1], 
     336                                                   balance_cal.left_top[2], 
     337                                                   balance_cal.left_bottom[0], 
     338                                                   balance_cal.left_bottom[1], 
     339                                                   balance_cal.left_bottom[2]); 
     340                                } 
     341                                break; 
    310342                        default: 
    311343                                printf("Unknown Extension"); 
    312344                                break; 
     
    355387                               mesg[i].classic_mesg.r_stick[CWIID_Y], 
    356388                               mesg[i].classic_mesg.l, mesg[i].classic_mesg.r); 
    357389                        break; 
     390                case CWIID_MESG_BALANCE: 
     391                        printf("Balance Report: right_top=%d right_bottom=%d " 
     392                               "left_top=%d left_bottom=%d\n", 
     393                               mesg[i].balance_mesg.right_top, 
     394                               mesg[i].balance_mesg.right_bottom, 
     395                               mesg[i].balance_mesg.left_top, 
     396                               mesg[i].balance_mesg.left_bottom); 
     397                        break; 
    358398                case CWIID_MESG_ERROR: 
    359399                        if (cwiid_close(wiimote)) { 
    360400                                fprintf(stderr, "Error on wiimote disconnect\n");