[tuxdroid-svn] r716 - api/python/trunk
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2007-11-20 11:00:28
|
Author: remi Date: 2007-11-20 12:00:23 +0100 (Tue, 20 Nov 2007) New Revision: 716 Modified: api/python/trunk/tuxapi_class.py Log: UPD: Micro on-off Modified: api/python/trunk/tuxapi_class.py =================================================================== --- api/python/trunk/tuxapi_class.py 2007-11-20 10:22:34 UTC (rev 715) +++ api/python/trunk/tuxapi_class.py 2007-11-20 11:00:23 UTC (rev 716) @@ -1160,16 +1160,18 @@ funct(args) except: self.remove(idx) + + fff = frame[5:] + ord_frame = [] + for val in fff: + ord_frame.append(ord(val)) for i, event in enumerate(events): if event[1] != None: if event[0] == ord(frame[4]): - frame = frame[5:] - ord_frame = [] - for val in frame: - ord_frame.append(ord(val)) - args = tuple(ord_frame) - thread.start_new_thread(__load_funct_async, (i, event[1], args)) + m_args = tuple(ord_frame) + t = threading.Thread(target = __load_funct_async, args = (i, event[1], m_args)) + t.start() #============================================================================== @@ -2552,6 +2554,15 @@ """ self.last_ack=self.structured(TUX_CMD_STRUCT_SOUND, \ TUX_CMD_STRUCT_SUB_TEST,0,0,0) + + #-------------------------------------------------------------------------- + # test + #-------------------------------------------------------------------------- + def sound_on(self): + self.last_ack = self.raw(0x92, 0, 0, 0) + + def sound_off(self): + self.last_ack = self.raw(0x92, 0, 0, 0) #-------------------------------------------------------------------------- # Send a command to tux for the "ping-pong" test @@ -5235,8 +5246,11 @@ def __init__(self,parent): self.parent = parent self.__state = False + self.__state_mutex = threading.Lock() self.__process = None self.__process_mutex = threading.Lock() + self.__mic_on_mutex = threading.Lock() + self.__mic_on_flag = False self.__thread = None self.on_buffer = EventControl() self.on_capture_stop = EventControl() @@ -5303,9 +5317,10 @@ >>> tux.micro.capture_start_free('/home/remi/Desktop/test.wav', 10.0) >>> tux.micro.capture_start_free('/home/remi/Desktop/test.wav') """ + time.sleep(0.1) if self.__capturing: return False - if not self.__state: + if not self.__get_state(): return False self.__capture_path = out_path self.__capture_buffer = [] @@ -5344,6 +5359,28 @@ wavfile.close() except: pass + + def __set_state(self, value): + self.__state_mutex.acquire() + self.__state = value + self.__state_mutex.release() + + def __get_state(self): + self.__state_mutex.acquire() + ret = self.__state + self.__state_mutex.release() + return ret + + def __set_mic_on_flag(self, value): + self.__state_mutex.acquire() + self.__mic_on_flag = value + self.__state_mutex.release() + + def __get_mic_on_flag(self): + self.__state_mutex.acquire() + ret = self.__mic_on_flag + self.__state_mutex.release() + return ret def on(self): """ @@ -5352,25 +5389,43 @@ Example: >>> tux.micro.on() """ - if self.__state: - return def get_micro_data(): device = self.parent.hw.alsa_device cmd = ["arecord", "-D", device, "-t", "raw"] - self.__process = subprocess.Popen(cmd, stdout = subprocess.PIPE,\ - stderr = subprocess.PIPE) - self.__state = True - while self.__process.poll() == None: + try: + time.sleep(0.1) + self.__process = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) + except: + return + print 'MICRO STREAM: ACQUISITION ON' + self.__set_state(True) + while (self.__process.poll() == None): self.__process_mutex.acquire() buff = self.__process.stdout.read(800) self.__on_new_buffer(buff) self.__process_mutex.release() time.sleep(0.01) - self.__state = False + print 'MICRO STREAM: ACQUISITION OFF' + self.__process_mutex.acquire() + self.__process = None + self.__process_mutex.release() + self.__set_state(False) + + self.__mic_on_mutex.acquire() + if self.__get_state(): + self.__mic_on_mutex.release() + return + if self.__thread != None: + if self.__thread.isAlive(): + self.__mic_on_mutex.release() + self.__thread._Thread__stop() + return + self.__thread = threading.Thread(target=get_micro_data) self.__thread.setName('micro.on') self.__thread.start() + self.__mic_on_mutex.release() def off(self): """ @@ -5379,11 +5434,15 @@ Example: >>> tux.micro.off() """ - if not self.__state: + if not self.__get_state(): return self.__process_mutex.acquire() - os.kill(self.__process.pid, signal.SIGKILL) - os.waitpid(-1, os.WNOHANG) + try: + if self.__process != None: + os.kill(self.__process.pid, signal.SIGKILL) + os.waitpid(-1, os.WNOHANG) + except: + pass self.__process_mutex.release() self.capture_stop() @@ -5391,7 +5450,7 @@ i_buffer = [] for c in buffer: i_buffer.append(ord(c)) - if self.__state: + if self.__get_state(): self.__send_energy_monitoring(i_buffer) self.on_buffer.notify(i_buffer) |