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