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