tests/linux/python_cwiid/runtime.py
changeset 287 5b3083695c8c
child 301 87c925eaaa3a
equal deleted inserted replaced
286:a2a8a52b0d4f 287:5b3083695c8c
       
     1 #!/usr/bin/env python
       
     2 ################################################################################
       
     3 #                                                                              #
       
     4 #   This program is free software: you can redistribute it and/or modify       #
       
     5 #   it under the terms of the GNU General Public License as published by       #
       
     6 #   the Free Software Foundation, either version 3 of the License, or          #
       
     7 #   (at your option) any later version.                                        #
       
     8 #                                                                              #
       
     9 #   This program is distributed in the hope that it will be useful,            #
       
    10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
       
    11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
       
    12 #   GNU General Public License for more details.                               #
       
    13 #                                                                              #
       
    14 #   You should have received a copy of the GNU General Public License          #
       
    15 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
       
    16 #                                                                              #
       
    17 ################################################################################
       
    18 
       
    19 import cwiid
       
    20 
       
    21 ## Configuration
       
    22 wiimote_hwaddr = '' # Use your address to speed up the connection proccess
       
    23 #wiimote_hwaddr = '00:19:1D:5D:5D:DC'
       
    24 
       
    25 last_point = (0,0)
       
    26 btA = 0
       
    27 btB = 0
       
    28 
       
    29 def cback(messages):
       
    30     '''Wiimote callback managing method
       
    31     Recieves a message list, each element is different, see the libcwiid docs'''
       
    32     global btA, btB, last_point
       
    33     #print "wiimote callback"
       
    34     for msg in messages:
       
    35         if msg[0] == cwiid.MESG_IR:
       
    36             # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4))
       
    37             for p in msg[1]:
       
    38                 if p:
       
    39                     pos = p['pos'][0], p['pos'][1] # point is mirrored
       
    40                     #s = max(p['size'], 1)
       
    41                         
       
    42                     last_point = tuple(pos)
       
    43                     #print "last_point",last_point
       
    44         elif msg[0] == cwiid.MESG_BTN:
       
    45             # msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*)
       
    46             if msg[1] & cwiid.BTN_A:
       
    47                 btA = 1
       
    48                 #print "btA = 1"
       
    49             else:
       
    50                 btA = 0
       
    51                 #print "btA = 0"
       
    52                 
       
    53             if msg[1] & cwiid.BTN_B:
       
    54                 btB = 1
       
    55                 #print "btB = 1"
       
    56             else:
       
    57                 btB = 0
       
    58                 #print "btB = 0"
       
    59         #elif msg[0] == cwiid.MESG_STATUS:
       
    60         #    # msg is of the form (cwiid.MESG_BTN, { 'status' : value, ... })
       
    61         #    print msg[1]
       
    62 
       
    63 try:
       
    64 #if False:
       
    65     wm = cwiid.Wiimote(wiimote_hwaddr)
       
    66     if wm is not None:
       
    67         # each message will contain info about ir and buttons
       
    68         wm.rpt_mode = cwiid.RPT_IR | cwiid.RPT_BTN # | cwiid.RPT_STATUS
       
    69         # tell cwiid to use the callback interface and allways send button events
       
    70         wm.enable(cwiid.FLAG_MESG_IFC
       
    71                   #| cwiid.FLAG_NONBLOCK
       
    72                   | cwiid.FLAG_REPEAT_BTN)
       
    73 
       
    74         # specify wich function will manage messages AFTER the other settings
       
    75         wm.mesg_callback = cback
       
    76 
       
    77         # quick check on the wiimote
       
    78         print "Got Wiimote!"
       
    79         st = wm.state
       
    80         for e in st:
       
    81             print str(e).ljust(8), ">", st[e]
       
    82 except:
       
    83 #else:
       
    84     print "Error with wiimote " + str(wiimote_hwaddr)
       
    85             
       
    86 def _runtime_cleanup():
       
    87     print "_runtime_cleanup() Called"
       
    88     runing = 0
       
    89     if wm is not None:
       
    90         wm.close()