[tuxdroid-svn] r368 - api/python/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2007-06-11 16:27:18
|
Author: jaguarondi Date: 2007-06-11 18:27:12 +0200 (Mon, 11 Jun 2007) New Revision: 368 Modified: api/python/trunk/tuxapi_class.py api/python/trunk/tuxapi_const.py Log: * Added all connection commands in the python api. There's still some firmware * issues that should be fixed before we can test them thoroughly. Modified: api/python/trunk/tuxapi_class.py =================================================================== --- api/python/trunk/tuxapi_class.py 2007-06-11 16:07:01 UTC (rev 367) +++ api/python/trunk/tuxapi_class.py 2007-06-11 16:27:12 UTC (rev 368) @@ -37,6 +37,7 @@ #============================================================================== api_version ="0.2.2" +TCPIP_FRAME_LENGTH = 16 #============================================================================== # TUXTCPCommunicator class @@ -54,6 +55,7 @@ functions "status" as class : Class which manages the request of a status + "connect" as class : Class which manages connection functions "sys" as class : Class which manages the system functions "tts" as class : Class which manages the text to speech @@ -93,6 +95,7 @@ self.tcp_data_fifo_event_mutex = threading.Lock() self.event=TUXevent(self) self.cmd=TUXcmd(self) + self.connect=TUXconnect(self) self.sys=TUXsys(self) self.daemon=TUXdaemon(self) self.sdaemon=self.daemon @@ -458,7 +461,180 @@ """ self.daemon.disconnect() +#============================================================================== +# TUXTCPCommunicator - connect - class +#============================================================================== +class TUXconnect(object): + """Class which manages connection functions + """ + #-------------------------------------------------------------------------- + # Constructor of the class + #-------------------------------------------------------------------------- + def __init__(self,parent): + """ + Constructor of the class + """ + self.parent=parent + + #-------------------------------------------------------------------------- + # Send a connection command and wait for the result + #-------------------------------------------------------------------------- + def tux_connection(self, connection_command, connection_args=[]): + """ + """ + if not self.parent.daemon.connected: + return 0 + data=[DEST_TUX, SD_DEFAULT, USB_CONNECTION_CMD, 0, connection_command] + data.extend(connection_args) + if len(data) > TCPIP_FRAME_LENGTH: + return False + # Fill up the end of the frame with 0 + data.extend([0] * (TCPIP_FRAME_LENGTH - len(data))) + self.parent.sock.send("".join( [chr(x) for x in data] )) + return self.parent.status.rsp_status(connection_command, timeout=5) + + #-------------------------------------------------------------------------- + # Disconnect from tux + #-------------------------------------------------------------------------- + def disconnect_from_tux(self): + """Disconnect from tux + """ + ack_frame = self.tux_connection(TUX_CONNECTION_DISCONNECT) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Connect to a tux by its ID + #-------------------------------------------------------------------------- + def connect_to_tux(self, id): + """Connect to a tux by its ID + """ + # 0 is an invalid id and 0xFFFF is reserved + assert 0 < id < 0xFFFF + id_msb = id >> 8 + id_lsb = id & 0xFF + ack_frame = self.tux_connection(TUX_CONNECTION_CONNECT, + [id_msb, id_lsb]) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Connect to the first tux discovered + #-------------------------------------------------------------------------- + def random_connect_to_tux(self): + """Connect to the first tux discovered + + Catch any disconnected tux, request it's ID and connect to it. + + """ + ack_frame = self.tux_connection(TUX_CONNECTION_RANDOM) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Get the ID of the first disconnected tux which is discoverred + #-------------------------------------------------------------------------- + def id_request(self): + """Get the ID of tux currently connected + """ + ack_frame = self.tux_connection(TUX_CONNECTION_ID_REQUEST) + if ack_frame and ord(ack_frame[5]) == TUX_CONNECTION_ACK: + return (ord(ack_frame[6])*256) + ord(ack_frame[7]) + + return False + + #-------------------------------------------------------------------------- + # Get the ID of the first disconnected tux which is discoverred + #-------------------------------------------------------------------------- + def id_lookup(self): + """Get the ID of the first disconnected tux which is discoverred + + The first disconnected tux that will detect this command will reply + with it's ID and disconnect immediately. You can then connect to + that tux with the ID you just got. + + In order to get the ID's of more than one disconnected tux, you have + to issue this command multiple times until you don't get any new ID. + """ + ack_frame = self.tux_connection(TUX_CONNECTION_ID_LOOKUP) + if ack_frame and ord(ack_frame[5]) == TUX_CONNECTION_ACK: + return (ord(ack_frame[6])*256) + ord(ack_frame[7]) + + return False + + #-------------------------------------------------------------------------- + # Changes the ID of a disconnected tux + #-------------------------------------------------------------------------- + def change_id(self, id): + """Changes the ID of a disconnected tux + + You have to push on the head button of tux for XXX seconds while + sending this command in order to validate the ID change request, this + in order to avoid stealing a tux too easily. + """ + assert 0 < id < 0xFFFF + id_msb = id >> 8 + id_lsb = id % 256 + ack_frame = self.tux_connection(TUX_CONNECTION_CHANGE_ID, + [id_msb, id_lsb]) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Set tux in sleep mode + #-------------------------------------------------------------------------- + def sleep(self): + """Set tux in sleep mode + """ + ack_frame = self.tux_connection(TUX_CONNECTION_SLEEP) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Wake-up a tux if it's in sleep mode. + #-------------------------------------------------------------------------- + def wakeup(self, id): + """Wake-up a tux if it's in sleep mode. + """ + assert 0 < id < 0xFFFF + id_msb = id >> 8 + id_lsb = id % 256 + ack_frame = self.tux_connection(TUX_CONNECTION_WAKEUP, + [id_msb, id_lsb]) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #-------------------------------------------------------------------------- + # Configure the RF module to avoid a given wifi channel + #-------------------------------------------------------------------------- + def avoid_wifi(self, channel): + """Configure the RF module to avoid a given wifi channel + + Channels from 1 to 14 are valid wifi channels. Set the wifi channel to + 0 to disable channel avoidance and use the complete range of + frequencies. + """ + ack_frame = self.tux_connection(TUX_CONNECTION_WIRELESS_CHANNEL, + [channel]) + if not ack_frame or ord(ack_frame[5]) != TUX_CONNECTION_ACK: + return False + + return True + + #============================================================================== # TUXTCPCommunicator - sys - class #============================================================================== @@ -2273,7 +2449,7 @@ #-------------------------------------------------------------------------- # wait a specified response status #-------------------------------------------------------------------------- - def rsp_status(self,DATA_STATUS): + def rsp_status(self, DATA_STATUS, timeout=2): """ Not a user function """ @@ -2292,7 +2468,7 @@ self.parent._insert_lock_in_list(data_to_match_list,cond_lock_mutex,returned_data) # wait the lock has unblocked. With a time-out cond_lock_mutex.acquire() - cond_lock_mutex.wait(2) + cond_lock_mutex.wait(timeout) cond_lock_mutex.release() # returns if len(returned_data) == 0: Modified: api/python/trunk/tuxapi_const.py =================================================================== --- api/python/trunk/tuxapi_const.py 2007-06-11 16:07:01 UTC (rev 367) +++ api/python/trunk/tuxapi_const.py 2007-06-11 16:27:12 UTC (rev 368) @@ -157,6 +157,22 @@ DATAS_STATUS_SOUND_COUNT = 0x23 DATAS_STATUS_PONG = 0x24 +# Tux connection commands +TUX_CONNECTION_DISCONNECT = 1 +TUX_CONNECTION_CONNECT = 2 +TUX_CONNECTION_RANDOM = 3 +TUX_CONNECTION_ID_REQUEST = 4 +TUX_CONNECTION_ID_LOOKUP = 5 +TUX_CONNECTION_CHANGE_ID = 6 +TUX_CONNECTION_SLEEP = 7 +TUX_CONNECTION_WAKEUP = 8 +TUX_CONNECTION_WIRELESS_CHANNEL = 9 + +# Tux connection command ack +TUX_CONNECTION_NACK = 0 +TUX_CONNECTION_ACK = 1 +TUX_CONNECTION_NOTFOUND = 2 + # Datas TuxDroid DATAS_STATUS_DONGLE = 0x00 DATAS_STATUS_RF = 0x01 @@ -189,6 +205,7 @@ DATA_TP_RSP = 0x03 DATA_TP_ACK_CMD = 0x04 DATA_TP_ACK_DP = 0x05 +USB_CONNECTION_CMD = 0x06 # Sub data types SUBDATA_TP_RAW = 0x01 SUBDATA_TP_STRUCT = 0x02 |