Changeset 660 for trunk/software
- Timestamp:
- 02/07/10 19:48:05 (2 years ago)
- Files:
-
- 1 modified
-
trunk/software/wiimoted.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/wiimoted.py
r637 r660 38 38 logger = logging.getLogger('wiimoted') 39 39 40 40 41 class ClientChannel(asynchat.async_chat): 41 42 def __init__(self, server, sock, addr): … … 65 66 self.producer_fifo.append(frame) 66 67 68 67 69 class Server(asyncore.dispatcher): 68 70 ''' … … 113 115 cmd = frame[0] 114 116 arg = frame[1:] 115 if cmd == 'c': 116 return '1' if self.wiimote else '0' 117 if cmd == 'c': # Get if connected 118 return 'c 1' if self.wiimote else 'c 0' 119 elif cmd == 'r': # Disable rumble 120 if self.wiimote: 121 self.wiimote.rumble = Fale 122 return 'r 1' 123 else: 124 return 'r 0' 125 elif cmd == 'R': # Enable rumble 126 if self.wiimote: 127 self.wiimote.rumble = True 128 return 'R 1' 129 else: 130 return 'R 0' 131 elif cmd == 'L': # Set LEDs 132 try: 133 self.wiimote.led = int(arg) 134 return 'L 1' 135 except (AttributeError, ValueError): 136 return 'L 0' 117 137 else: 118 138 logger.error('Unknown command %r', frame) … … 125 145 while True: 126 146 if self.connect(): 147 for client in self.clients: 148 client.send_message('c 1') 127 149 while self.wiimote: # Just idle until a disconnection 128 150 time.sleep(.5) 151 for client in self.cients: 152 client.send_message('c 0') 129 153 130 154 def translate_loop(self): … … 140 164 time.sleep(0.001) 141 165 else: 142 string = wiimote_update_to_string(type, data) 143 for client in self.clients: 144 client.send_message(string) 166 try: 167 string = wiimote_update_to_string(type, data) 168 assert string.startswith('m '), "Is a message" 169 for client in self.clients: 170 client.send_message(string) 171 except ValueError: 172 logger.exception('Unknown message type %r', type) 145 173 146 174 def connect(self): … … 184 212 185 213 def wiimote_update_to_string(type, data): 214 ''' 215 Convert a cwiid message into a string message (starting with the 216 message type indicator 'm'). 217 ''' 186 218 if type == cwiid.MESG_BTN: 187 return ' {0} {1}\r\n'.format(type, data)219 return 'm {0} {1}\r\n'.format(type, data) 188 220 elif type == cwiid.MESG_NUNCHUK: 189 return (' {type} {acc[0]} {acc[1]} {acc[2]} '190 '{buttons} {stick[0]} {stick[1]}').format(type=type, **data)221 return ('m {type} {stick[0]} {stick[1]} {acc[0]} {acc[1]} {acc[2]} ' 222 '{buttons}').format(type=type, **data) 191 223 else: 192 logger.error('Unknown message type %r', type)224 raise ValueError('Can\'t convert message of type %r %r', type, data) 193 225 194 226

