Rose-Hulman Robotics Team

Changeset 660 for trunk/software

Show
Ignore:
Timestamp:
02/07/10 19:48:05 (2 years ago)
Author:
mosttw
Message:

Added support for some messages in Wiimote daemon

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/software/wiimoted.py

    r637 r660  
    3838logger = logging.getLogger('wiimoted') 
    3939 
     40 
    4041class ClientChannel(asynchat.async_chat): 
    4142        def __init__(self, server, sock, addr): 
     
    6566                self.producer_fifo.append(frame) 
    6667 
     68 
    6769class Server(asyncore.dispatcher): 
    6870        ''' 
     
    113115                cmd = frame[0] 
    114116                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' 
    117137                else: 
    118138                        logger.error('Unknown command %r', frame) 
     
    125145                while True: 
    126146                        if self.connect(): 
     147                                for client in self.clients: 
     148                                        client.send_message('c 1') 
    127149                                while self.wiimote: # Just idle until a disconnection 
    128150                                        time.sleep(.5) 
     151                                for client in self.cients: 
     152                                        client.send_message('c 0') 
    129153         
    130154        def translate_loop(self): 
     
    140164                                time.sleep(0.001) 
    141165                        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) 
    145173 
    146174        def connect(self): 
     
    184212 
    185213def 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        ''' 
    186218        if type == cwiid.MESG_BTN: 
    187                 return '{0} {1}\r\n'.format(type, data) 
     219                return 'm {0} {1}\r\n'.format(type, data) 
    188220        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) 
    191223        else: 
    192                 logger.error('Unknown message type %r', type) 
     224                raise ValueError('Can\'t convert message of type %r %r', type, data) 
    193225 
    194226