tests/linux/python_cwiid/runtime.py
author lbessard
Wed, 25 Feb 2009 16:39:11 +0100
changeset 317 5378fe2fb4bc
parent 301 87c925eaaa3a
permissions -rw-r--r--
Fix size of ComboBoxes
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
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    20
import sys
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    21
from threading import Thread
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    22
## Configuration
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    23
wm = None
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    24
wiimote_hwaddr = '' # Use your address to speed up the connection proccess
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    25
#wiimote_hwaddr = '00:19:1D:5D:5D:DC'
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    26
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    27
last_point = (0,0)
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    28
btA = 0
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    29
btB = 0
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    30
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    31
def cback(messages):
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    32
    '''Wiimote callback managing method
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    33
    Recieves a message list, each element is different, see the libcwiid docs'''
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    34
    global btA, btB, last_point
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    35
    #print messages
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    36
    #print "wiimote callback"
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    37
    for msg in messages:
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    38
#        if msg[0] == cwiid.MESG_IR:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    39
#            # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4))
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    40
#            for p in msg[1]:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    41
#                if p:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    42
#                    pos = p['pos'][0], p['pos'][1] # point is mirrored
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    43
#                    #s = max(p['size'], 1)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    44
#                        
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    45
#                    last_point = tuple(pos)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    46
#                    #print "last_point",last_point
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    47
        if msg[0] == cwiid.MESG_BTN:
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    48
            # msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*)
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    49
            if msg[1] & cwiid.BTN_A:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    50
                btA = 1
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    51
                #print "btA = 1"
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    52
            else:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    53
                btA = 0
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    54
                #print "btA = 0"
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    55
                
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    56
            if msg[1] & cwiid.BTN_B:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    57
                btB = 1
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    58
                #print "btB = 1"
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    59
            else:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    60
                btB = 0
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    61
        elif msg[0] == cwiid.MESG_NUNCHUK:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    62
               #sbb = msg[1]['buttons']
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    63
               last_point = msg[1]['stick']
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    64
               #ssx = msg[1]['stick'][0]
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    65
               #ssy = msg[1]['stick'][1]
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    66
               #msg[1]['acc'][0]
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    67
               #msg[1]['acc'][1]
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    68
               #msg[1]['acc'][2]
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    69
                #print "btB = 0"
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    70
        #elif msg[0] == cwiid.MESG_STATUS:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    71
        #    # msg is of the form (cwiid.MESG_BTN, { 'status' : value, ... })
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    72
        #    print msg[1]
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    73
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    74
def Connect_Wiimote(frameobj):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    75
    global wm,wiimote_hwaddr
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    76
    try:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    77
    #if False:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    78
        print "Press 1+2 Now !!!!"
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    79
        sys.stdout.flush()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    80
        wm = cwiid.Wiimote(wiimote_hwaddr)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    81
        if wm is not None:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    82
            # each message will contain info about ir and buttons
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    83
            wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT # | cwiid.RPT_STATUS | cwiid.RPT_IR | 
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    84
            # tell cwiid to use the callback interface and allways send button events
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    85
            wm.enable(cwiid.FLAG_MESG_IFC)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    86
                      #| cwiid.FLAG_NONBLOCK
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    87
                      #| cwiid.FLAG_REPEAT_BTN)
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    88
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    89
            # specify wich function will manage messages AFTER the other settings
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    90
            wm.mesg_callback = cback
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
    91
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    92
            # quick check on the wiimote
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    93
            print "Got Wiimote!"
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    94
            frameobj.label_1.SetLabel("Got Wiimote !")
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    95
            st = wm.state
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    96
            for e in st:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    97
                print str(e).ljust(8), ">", st[e]
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    98
    except:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
    99
    #else:
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   100
        print "Error with wiimote " + str(wiimote_hwaddr)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   101
        frameobj.label_1.SetLabel("Wiimote NOK")
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   102
    sys.stdout.flush()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   103
          
287
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
   104
def _runtime_cleanup():
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
   105
    if wm is not None:
5b3083695c8c The must-have test : using wiimote sensors !
etisserant
parents:
diff changeset
   106
        wm.close()
301
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   107
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   108
def start_manu(self,evt):
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   109
    self.label_1.SetLabel("press 1+2 now !!!")
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   110
    wx.CallAfter(Connect_Wiimote, self)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   111
    evt.Skip()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   112
HMIFrame.start_manu = start_manu
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   113
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   114
def _runtime_begin():
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   115
    pass
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   116
    #wx.CallAfter(Connect_Wiimote)
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   117
#Thread(target=Connect_Wiimote).start()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   118
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   119
#create_frame()
87c925eaaa3a Added support for wxglade GUIs.
etisserant
parents: 287
diff changeset
   120
#wx.Yield()