tests/linux/python_cwiid/runtime.py
changeset 301 87c925eaaa3a
parent 287 5b3083695c8c
equal deleted inserted replaced
300:7f7912ae5ee8 301:87c925eaaa3a
    15 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
    15 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
    16 #                                                                              #
    16 #                                                                              #
    17 ################################################################################
    17 ################################################################################
    18 
    18 
    19 import cwiid
    19 import cwiid
    20 
    20 import sys
       
    21 from threading import Thread
    21 ## Configuration
    22 ## Configuration
       
    23 wm = None
    22 wiimote_hwaddr = '' # Use your address to speed up the connection proccess
    24 wiimote_hwaddr = '' # Use your address to speed up the connection proccess
    23 #wiimote_hwaddr = '00:19:1D:5D:5D:DC'
    25 #wiimote_hwaddr = '00:19:1D:5D:5D:DC'
    24 
    26 
    25 last_point = (0,0)
    27 last_point = (0,0)
    26 btA = 0
    28 btA = 0
    28 
    30 
    29 def cback(messages):
    31 def cback(messages):
    30     '''Wiimote callback managing method
    32     '''Wiimote callback managing method
    31     Recieves a message list, each element is different, see the libcwiid docs'''
    33     Recieves a message list, each element is different, see the libcwiid docs'''
    32     global btA, btB, last_point
    34     global btA, btB, last_point
       
    35     #print messages
    33     #print "wiimote callback"
    36     #print "wiimote callback"
    34     for msg in messages:
    37     for msg in messages:
    35         if msg[0] == cwiid.MESG_IR:
    38 #        if msg[0] == cwiid.MESG_IR:
    36             # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4))
    39 #            # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4))
    37             for p in msg[1]:
    40 #            for p in msg[1]:
    38                 if p:
    41 #                if p:
    39                     pos = p['pos'][0], p['pos'][1] # point is mirrored
    42 #                    pos = p['pos'][0], p['pos'][1] # point is mirrored
    40                     #s = max(p['size'], 1)
    43 #                    #s = max(p['size'], 1)
    41                         
    44 #                        
    42                     last_point = tuple(pos)
    45 #                    last_point = tuple(pos)
    43                     #print "last_point",last_point
    46 #                    #print "last_point",last_point
    44         elif msg[0] == cwiid.MESG_BTN:
    47         if msg[0] == cwiid.MESG_BTN:
    45             # msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*)
    48             # msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*)
    46             if msg[1] & cwiid.BTN_A:
    49             if msg[1] & cwiid.BTN_A:
    47                 btA = 1
    50                 btA = 1
    48                 #print "btA = 1"
    51                 #print "btA = 1"
    49             else:
    52             else:
    53             if msg[1] & cwiid.BTN_B:
    56             if msg[1] & cwiid.BTN_B:
    54                 btB = 1
    57                 btB = 1
    55                 #print "btB = 1"
    58                 #print "btB = 1"
    56             else:
    59             else:
    57                 btB = 0
    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]
    58                 #print "btB = 0"
    69                 #print "btB = 0"
    59         #elif msg[0] == cwiid.MESG_STATUS:
    70         #elif msg[0] == cwiid.MESG_STATUS:
    60         #    # msg is of the form (cwiid.MESG_BTN, { 'status' : value, ... })
    71         #    # msg is of the form (cwiid.MESG_BTN, { 'status' : value, ... })
    61         #    print msg[1]
    72         #    print msg[1]
    62 
    73 
    63 try:
    74 def Connect_Wiimote(frameobj):
    64 #if False:
    75     global wm,wiimote_hwaddr
    65     wm = cwiid.Wiimote(wiimote_hwaddr)
    76     try:
    66     if wm is not None:
    77     #if False:
    67         # each message will contain info about ir and buttons
    78         print "Press 1+2 Now !!!!"
    68         wm.rpt_mode = cwiid.RPT_IR | cwiid.RPT_BTN # | cwiid.RPT_STATUS
    79         sys.stdout.flush()
    69         # tell cwiid to use the callback interface and allways send button events
    80         wm = cwiid.Wiimote(wiimote_hwaddr)
    70         wm.enable(cwiid.FLAG_MESG_IFC
    81         if wm is not None:
    71                   #| cwiid.FLAG_NONBLOCK
    82             # each message will contain info about ir and buttons
    72                   | cwiid.FLAG_REPEAT_BTN)
    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)
    73 
    88 
    74         # specify wich function will manage messages AFTER the other settings
    89             # specify wich function will manage messages AFTER the other settings
    75         wm.mesg_callback = cback
    90             wm.mesg_callback = cback
    76 
    91 
    77         # quick check on the wiimote
    92             # quick check on the wiimote
    78         print "Got Wiimote!"
    93             print "Got Wiimote!"
    79         st = wm.state
    94             frameobj.label_1.SetLabel("Got Wiimote !")
    80         for e in st:
    95             st = wm.state
    81             print str(e).ljust(8), ">", st[e]
    96             for e in st:
    82 except:
    97                 print str(e).ljust(8), ">", st[e]
    83 #else:
    98     except:
    84     print "Error with wiimote " + str(wiimote_hwaddr)
    99     #else:
    85             
   100         print "Error with wiimote " + str(wiimote_hwaddr)
       
   101         frameobj.label_1.SetLabel("Wiimote NOK")
       
   102     sys.stdout.flush()
       
   103           
    86 def _runtime_cleanup():
   104 def _runtime_cleanup():
    87     print "_runtime_cleanup() Called"
       
    88     runing = 0
       
    89     if wm is not None:
   105     if wm is not None:
    90         wm.close()
   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()