[tuxdroid-svn] r154 - api/python/trunk
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2007-03-14 12:23:22
|
Author: remi Date: 2007-03-14 13:23:03 +0100 (Wed, 14 Mar 2007) New Revision: 154 Modified: api/python/trunk/tuxapi_class.py Log: UPD : - The licence is added in the header. - New function to clarify the status is added - Some events are added - Some minor changes Modified: api/python/trunk/tuxapi_class.py =================================================================== --- api/python/trunk/tuxapi_class.py 2007-03-14 12:03:36 UTC (rev 153) +++ api/python/trunk/tuxapi_class.py 2007-03-14 12:23:03 UTC (rev 154) @@ -1,5 +1,27 @@ #!/usr/bin/python +# ----------------------------------------------------------------------------- +# Tux Droid - API Class +# Copyright (C) 2007 C2ME Sa <rem...@c2...> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +# 02111-1307, USA. +# ----------------------------------------------------------------------------- +# $Id$ +# ----------------------------------------------------------------------------- + import sys import os import time @@ -133,6 +155,109 @@ #------------------------------------------------------------------------------- #-------------------------------------------------------------------------- + ## Converting raw status to explicit string status + ## Return a string + #-------------------------------------------------------------------------- + def explicit_status(self): + value_onoff=0 + value_10=1 + value_8b=2 + value_16b=3 + value_np=4 + + value_type=value_onoff + line="" + status=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + for i, data in enumerate(self.last_datas_for_event): + status[i]=ord(data) + # -- Tux status + if status[4]==DATAS_STATUS_WINGS_MOTOR_BACKWARD: + line=line+"Wings motor backward->" + if status[4]==DATAS_STATUS_SPIN_MOTOR_BACKWARD: + line=line+"Spin motor backward->" + if status[4]==DATAS_STATUS_SPIN_MOTOR_FORWARD: + line=line+"Spin motor forward->" + if status[4]==DATAS_STATUS_MOUTH_OPEN_POSITION: + line=line+"Mouth open position->" + if status[4]==DATAS_STATUS_MOUTH_CLOSED_POSITION: + line=line+"Mouth closed position->" + if status[4]==DATAS_STATUS_HEAD_PUSH_POSITION: + line=line+"Head push position->" + if status[4]==DATAS_STATUS_CHARGER_INHIBIT_SIGNAL: + line=line+"Charger inhibit signal->" + if status[4]==DATAS_STATUS_WINGS_POSITION_SWITCH: + line=line+"Wings position switch->" + if status[4]==DATAS_STATUS_MOTOR_FOR_WINGS: + line=line+"Motor for wings->" + if status[4]==DATAS_STATUS_LEFT_BLUE_LED: + line=line+"Left blue led->" + if status[4]==DATAS_STATUS_HEAD_MOTOR_FOR_MOUTH: + line=line+"Head motor for beack->" + if status[4]==DATAS_STATUS_HEAD_MOTOR_FOR_EYES: + line=line+"Head motor for eyes->" + if status[4]==DATAS_STATUS_IR_RECEIVER_SIGNAL: + line=line+"Ir receiver signal->" + if status[4]==DATAS_STATUS_SPIN_POSITION_SWITCH: + line=line+"Spin position switch->" + if status[4]==DATAS_STATUS_WINGS_MOTOR_FORWARD: + line=line+"Wings motor forward->" + if status[4]==DATAS_STATUS_IR_LED: + line=line+"IR led->" + if status[4]==DATAS_STATUS_EYES_OPEN_POSITION_SWITCH: + line=line+"Eyes open position switch->" + if status[4]==DATAS_STATUS_EYES_CLOSED_POSITION_SWITCH: + line=line+"Eyes closed position switch->" + if status[4]==DATAS_STATUS_LEFT_WING_PUSH: + line=line+"Left wing push->" + if status[4]==DATAS_STATUS_RIGHT_WING_PUSH: + line=line+"Right wing push->" + if status[4]==DATAS_STATUS_POWER_PLUG_SWITCH: + line=line+"Power plug switch->" + if status[4]==DATAS_STATUS_HEAD_PUSH_SWITCH: + line=line+"Head push switch->" + if status[4]==DATAS_STATUS_CHARGER_LED_STATUS: + line=line+"Charger led status->" + if status[4]==DATAS_STATUS_MUTE_STATUS: + line=line+"Audio mute->" + if status[4]==DATAS_STATUS_LIGHT_LEVEL: + line=line+"Light level->" + value_type=value_16b + if status[4]==DATAS_STATUS_EYES_POSITION_COUNTER: + line=line+"Eyes position counter->" + value_type=value_8b + if status[4]==DATAS_STATUS_MOUTH_POSITION_COUNTER: + line=line+"Mouth position counter->" + value_type=value_8b + if status[4]==DATAS_STATUS_WINGS_POSITION_COUNTER: + line=line+"Wings position counter->" + value_type=value_8b + if status[4]==DATAS_STATUS_SPIN_POSITION_COUNTER: + line=line+"Spin position counter->" + value_type=value_8b + if status[4]==DATAS_STATUS_RIGHT_BLUE_LED: + line=line+"Right blue led->" + if status[4]==DATAS_STATUS_RF_CONNECTED: + line=line+"RF connected->" + if status[4]==DATAS_STATUS_IR_CODE: + line=line+"IR code->%s"%remote_bt_name[status[5]] + value_type=value_np + if status[4]==DATAS_STATUS_SOUND_COUNT: + line=line+"Flash sounds count->" + if status[4]==DATAS_STATUS_PONG: + line=line+"Pong-> Received : %d Remaining : %d Average : %.0f %s"\ + %(status[6],status[5],((status[6]*100)/(200-status[5])),'%') + value_type=value_np + #-- Write value + if value_type==value_onoff: + if status[5]==0: line=line+"off" + else: line=line+"on" + if value_type==value_8b: + line=line+"%d"%status[5] + if value_type==value_16b: + line=line+"%d"%((status[5]*256)+status[6]) + return line + + #-------------------------------------------------------------------------- ## Pooling on data arrival from tcp server ## SYSTEM function #-------------------------------------------------------------------------- @@ -154,19 +279,12 @@ ## SYSTEM function #-------------------------------------------------------------------------- def dispatch_data_from_tcp_server(self): - if self.event.on_status!=None: - self.event.on_status() # from sdaemon if ord(self.last_datas_for_event[0])==SOURCE_SUB_DAEMON: self.dispatch_data_from_sdaemon() # from tux droid if ord(self.last_datas_for_event[0])==SOURCE_TUX: self.dispatch_data_from_tux() - #if ord(self.last_datas[1])==DATAS_STATUS_RF: - # if ord(self.last_datas[2])==0: - # self.RF_connected=False - # else: - # self.RF_connected=True #-------------------------------------------------------------------------- ## data from sub daemon @@ -189,7 +307,9 @@ # data type response if ord(self.last_datas_for_event[2])==DATA_TP_RSP: if ord(self.last_datas_for_event[3])==SUBDATA_TP_STATUS: - #head button + if self.event.on_status!=None: + self.event.on_status() + #Head button if ord(self.last_datas_for_event[4])==DATAS_STATUS_HEAD_PUSH_SWITCH: if ord(self.last_datas_for_event[5])==1: if self.event.on_head_bt_pushed!=None: @@ -201,7 +321,7 @@ self.event.on_head_bt_released() if self.event.on_bt_released!=None: self.event.on_bt_released() - #left wing button + #Left wing button if ord(self.last_datas_for_event[4])==DATAS_STATUS_LEFT_WING_PUSH: if ord(self.last_datas_for_event[5])==1: if self.event.on_lwing_bt_pushed!=None: @@ -213,7 +333,7 @@ self.event.on_lwing_bt_released() if self.event.on_bt_released!=None: self.event.on_bt_released() - #right wing button + #Right wing button if ord(self.last_datas_for_event[4])==DATAS_STATUS_RIGHT_WING_PUSH: if ord(self.last_datas_for_event[5])==1: if self.event.on_rwing_bt_pushed!=None: @@ -225,12 +345,90 @@ self.event.on_rwing_bt_released() if self.event.on_bt_released!=None: self.event.on_bt_released() - #remote button + #Remote button if ord(self.last_datas_for_event[4])==DATAS_STATUS_IR_CODE: if self.event.on_remote_bt[ord(self.last_datas_for_event[5])]!=None: self.event.on_remote_bt[ord(self.last_datas_for_event[5])]() if self.event.on_remote!=None: self.event.on_remote(ord(self.last_datas_for_event[5])) + #Mouth open + if ord(self.last_datas_for_event[4])==DATAS_STATUS_MOUTH_OPEN_POSITION: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_mouth_open!=None: + self.event.on_mouth_open() + #Mouth close + if ord(self.last_datas_for_event[4])==DATAS_STATUS_MOUTH_CLOSED_POSITION: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_mouth_close!=None: + self.event.on_mouth_close() + #Ledl + if ord(self.last_datas_for_event[4])==DATAS_STATUS_LEFT_BLUE_LED: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_left_blue_led_on!=None: + self.event.on_left_blue_led_on() + else: + if self.event.on_left_blue_led_off!=None: + self.event.on_left_blue_led_off() + #Ledr + if ord(self.last_datas_for_event[4])==DATAS_STATUS_RIGHT_BLUE_LED: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_right_blue_led_on!=None: + self.event.on_right_blue_led_on() + else: + if self.event.on_right_blue_led_off!=None: + self.event.on_right_blue_led_off() + #Eyes open/close + if ord(self.last_datas_for_event[4])==DATAS_STATUS_EYES_CLOSED_POSITION_SWITCH: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_eyes_close!=None: + self.event.on_eyes_close() + else: + if self.event.on_eyes_open!=None: + self.event.on_eyes_open() + #Power plug switch + if ord(self.last_datas_for_event[4])==DATAS_STATUS_POWER_PLUG_SWITCH: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_power_plugged!=None: + self.event.on_power_plugged() + else: + if self.event.on_power_unplugged!=None: + self.event.on_power_unplugged() + #RF status + if ord(self.last_datas_for_event[4])==DATAS_STATUS_RF_CONNECTED: + if ord(self.last_datas_for_event[5])==1: + if self.event.on_rf_connected!=None: + self.event.on_rf_connected() + else: + if self.event.on_rf_disconnected!=None: + self.event.on_rf_disconnected() + #PONG + if ord(self.last_datas_for_event[4])==DATAS_STATUS_PONG: + if self.event.on_pong_received!=None: + rcv=ord(self.last_datas_for_event[6]) + rem=ord(self.last_datas_for_event[5]) + avg=int((100*rcv)/(200-rem)) + self.event.on_pong_received(rcv,rem,avg) + #mouth stop + if ord(self.last_datas_for_event[4])==DATAS_STATUS_MOUTH_POSITION_COUNTER: + if ord(self.last_datas_for_event[5])==0: + if self.event.on_mouth_stop!=None: + self.event.on_mouth_stop() + #eyes stop + if ord(self.last_datas_for_event[4])==DATAS_STATUS_EYES_POSITION_COUNTER: + if ord(self.last_datas_for_event[5])==0: + if self.event.on_eyes_stop!=None: + self.event.on_eyes_stop() + + #wings stop + if ord(self.last_datas_for_event[4])==DATAS_STATUS_WINGS_POSITION_COUNTER: + if ord(self.last_datas_for_event[5])==0: + if self.event.on_wings_stop!=None: + self.event.on_wings_stop() + #spin stop + if ord(self.last_datas_for_event[4])==DATAS_STATUS_SPIN_POSITION_COUNTER: + if ord(self.last_datas_for_event[5])==0: + if self.event.on_spin_stop!=None: + self.event.on_spin_stop() #============================================================================== # TUXTCPCommunicator - sys - class @@ -351,6 +549,23 @@ self.on_light_level=None self.on_connected=None self.on_disconnected=None + self.on_mouth_open=None + self.on_mouth_close=None + self.on_power_plugged=None + self.on_power_unplugged=None + self.on_left_blue_led_on=None + self.on_left_blue_led_off=None + self.on_right_blue_led_on=None + self.on_right_blue_led_off=None + self.on_eyes_open=None + self.on_eyes_close=None + self.on_rf_connected=None + self.on_rf_disconnected=None + self.on_pong_received=None + self.on_mouth_stop=None + self.on_eyes_stop=None + self.on_wings_stop=None + self.on_spin_stop=None #-------------------------------------------------------------------------- ## store all events @@ -370,6 +585,23 @@ self.s_on_light_level=self.on_light_level self.s_on_connected=self.on_connected self.s_on_disconnected=self.on_disconnected + self.s_on_mouth_open=self.on_mouth_open + self.s_on_mouth_close=self.on_mouth_close + self.s_on_power_plugged=self.on_power_plugged + self.s_on_power_unplugged=self.on_power_unplugged + self.s_on_left_blue_led_on=self.on_left_blue_led_on + self.s_on_left_blue_led_off=self.on_left_blue_led_off + self.s_on_right_blue_led_on=self.on_right_blue_led_on + self.s_on_right_blue_led_off=self.on_right_blue_led_off + self.s_on_eyes_open=self.on_eyes_open + self.s_on_eyes_close=self.on_eyes_close + self.s_on_rf_connected=self.on_rf_connected + self.s_on_rf_disconnected=self.on_rf_disconnected + self.s_on_pong_received=self.on_pong_received + self.s_on_mouth_stop=on_mouth_stop + self.s_on_eyes_stop=on_eyes_stop + self.s_on_wings_stop=on_wings_stop + self.s_on_spin_stop=on_spin_stop #-------------------------------------------------------------------------- ## restore all events @@ -389,6 +621,23 @@ self.on_light_level=self.s_on_light_level self.on_connected=self.s_on_connected self.on_disconnected=self.s_on_disconnected + self.on_mouth_open=self.s_on_mouth_open + self.on_mouth_close=self.s_on_mouth_close + self.on_power_plugged=self.s_on_power_plugged + self.on_power_unplugged=self.s_on_power_unplugged + self.on_left_blue_led_on=self.s_on_left_blue_led_on + self.on_left_blue_led_off=self.s_on_left_blue_led_off + self.on_right_blue_led_on=self.s_on_right_blue_led_on + self.on_right_blue_led_off=self.s_on_right_blue_led_off + self.on_eyes_open=self.s_on_eyes_open + self.on_eyes_close=self.s_on_eyes_close + self.on_rf_connected=self.s_on_rf_connected + self.on_rf_disconnected=self.s_on_rf_disconnected + self.on_pong_received=self.s_on_pong_received + self.on_mouth_stop=s_on_mouth_stop + self.on_eyes_stop=s_on_eyes_stop + self.on_wings_stop=s_on_wings_stop + self.on_spin_stop=s_on_spin_stop #-------------------------------------------------------------------------- ## wait until status event arrival @@ -412,7 +661,6 @@ if ord(self.parent.last_datas[3])==SUBDATA_TP_STATUS: if ord(self.parent.last_datas[4])==DATA_STATUS: if ord(self.parent.last_datas[5])==DATA_VALUE: - #print self.parent.sys.time()-time_beginin return True if (self.parent.sys.time()-time_beginin)>=time_out and time_out!=9999: return False @@ -588,7 +836,7 @@ time.sleep(0.001) if ord(self.parent.last_datas[0])==SOURCE_TUX: if ord(self.parent.last_datas[1])==SS_DEFAULT: - if ord(self.parent.last_datas[2])==DATA_TP_ACK: + if ord(self.parent.last_datas[2])==DATA_TP_ACK_CMD: return ord(self.parent.last_datas[4]) if (self.parent.sys.time()-time_beginin)>=2: return ACK_CMD_TIMEOUT @@ -1073,7 +1321,7 @@ time.sleep(0.001) if ord(self.parent.last_datas[0])==SOURCE_SUB_DAEMON: if ord(self.parent.last_datas[1])==SS_DEFAULT: - if ord(self.parent.last_datas[2])==DATA_TP_ACK: + if ord(self.parent.last_datas[2])==DATA_TP_ACK_CMD: return ord(self.parent.last_datas[4]) if (self.parent.sys.time()-time_beginin)>=2: return ACK_CMD_TIMEOUT |