def message_receive_loop(self, q, options): # led_framework = LedFramework() with LedFramework(rs_host=options.rs, hwctrl_host=options.ctrl) as led_framework: try: while True: try: self.on_pre_exec_runloop() while True: line = self.read_data() if self.aborted: return if not line: print('') logger.w("line is empty") break if line.startswith('abort'): logger.i('abort canvas') led_framework.abort() elif line.startswith('show:'): logger.i('show by orders') orders = line[len('show:'):].strip() dic_orders = None try: dic_orders = json.loads(orders) except ValueError: logger.w('invalid order:' + str(orders)) continue led_framework.abort() q.put([led_framework.show, dic_orders]) self.on_post_exec_runloop() except Exception as exception: ret = self.on_exception_at_runloop(exception) if (ret == LedRunLoop.CONTINUE): logger.i("continue runloop") continue else: logger.e("Unexpected error:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc()) logger.e("exit runloop by exception") return except: logger.e("Unexpected error:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc()) raise finally: self.run_loop_finished = True SoundInterface.close() logger.i('finish led order loop')
def callback(in_data, frame_count, time_info, status): data = wf.readframes(frame_count) if self.event_stop.is_set(): logger.i('stop sound.') return (data, pyaudio.paComplete) if self.event_pause.is_set(): logger.i('pause sound.') while self.event_pause.is_set(): time.sleep(0.1) if self.event_stop.is_set(): # finish break else: # logger.d('non-blocking play ({}, {}, {}, {})'.format(in_data, # frame_count, time_info, status)) pass return (data, pyaudio.paContinue)
def run(self): self.opt_parser.add_option( "-d", "--dest", action="store", type="string", dest="dest", help= "(optional) ip address of destination device which connect to real 3d cube." ) self.opt_parser.add_option("-m", "--hide-simulator", action="store_true", dest="hide_simulator", default=False, help="(optional) hide simulator window") options, _ = self.opt_parser.parse_args() if options.dest != None: logger.i("External Connect To: " + (options.dest)) led.SetUrl(options.dest) led.EnableSimulator(not options.hide_simulator) th = threading.Thread(name="message_receive_loop", target=self.message_receive_loop, args=(self.message, )) th.setDaemon(True) th.start() try: while True: if self.aborted: break if self.message.empty(): time.sleep(0.1) else: msg = self.message.get() msg[0](msg[1]) except KeyboardInterrupt: logger.i('keybord Ctrl+C') self.on_keyboard_interrupt() except: logger.e("Unexpected error:" + str(sys.exc_info()[0])) logger.e(traceback.format_exc()) raise finally: self.aborted = True self.on_finish() th.join() logger.i('finish main loop')
def playsound(self, wavfile): if (wavfile == ""): logger.i('empty sound file.') return # open file logger.i('open file = {}'.format(wavfile)) wf = wave.open(wavfile, 'rb') wfinfo = wf.getparams() logger.i(wfinfo) logger.i('wave file info') logger.i('channels = {}'.format(wfinfo[0])) logger.i('sampling width = {} byte'.format(wfinfo[1])) logger.i('frame rate = {} Hz'.format(wfinfo[2])) logger.i('frame count = {}'.format(wfinfo[3])) logger.i('sound time = {} s'.format((int)(wfinfo[3] / wfinfo[2]))) # def non-blocking callback def callback(in_data, frame_count, time_info, status): data = wf.readframes(frame_count) if self.event_stop.is_set(): logger.i('stop sound.') return (data, pyaudio.paComplete) if self.event_pause.is_set(): logger.i('pause sound.') while self.event_pause.is_set(): time.sleep(0.1) if self.event_stop.is_set(): # finish break else: # logger.d('non-blocking play ({}, {}, {}, {})'.format(in_data, # frame_count, time_info, status)) pass return (data, pyaudio.paContinue) # open stream p = pyaudio.PyAudio() s = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), frames_per_buffer=CHUNK, output=True, stream_callback=callback) # play stream s.start_stream() logger.i('started non-blocking sound play.') # wait finish while s.is_active(): time.sleep(0.1) # closing stream s.stop_stream() s.close() wf.close() p.terminate() logger.i('finished sound play.')
dirname = os.path.dirname(__file__) if sys.platform == 'darwin': ledlib = dirname + '/libledLib.dylib' elif sys.platform == 'win32': arch = platform.architecture()[0] if arch == '64bit': ledlib = dirname + '/ledLib64.dll' else: ledlib = dirname + '/ledLib32.dll' elif 'linux' in sys.platform and platform.machine() == 'armv7l': ledlib = dirname + '/ledLibarmv7l.so' # for Raspberry PI3 else: raise NotImplementedError('Unsupported OS.' + sys.platform) logger.i('LoadLibrary: ' + ledlib) class LedCube(object): def __init__(self, led): self.led = led def Show(self): #print("led.Show()") self.led.Show() def SetUrl(self, dest): #print("led.SetUrl()") self.led.SetUrl(dest) def Clear(self):
if sys.platform == 'darwin': ledlib = dirname + '/libledLib.dylib' elif sys.platform == 'win32': arch = platform.architecture()[0] if arch == '64bit': ledlib = dirname + '/ledLib64.dll' else: ledlib = dirname + '/ledLib32.dll' elif 'linux' in sys.platform and platform.machine() == 'armv7l': ledlib = dirname + '/ledLibarmv7l.so' # for Raspberry PI3 elif 'linux2' == sys.platform: # for travis.ci ledlib = None else: raise NotImplementedError('Unsupported OS.' + sys.platform) logger.i('LoadLibrary: ' + str(ledlib)) def local_split_url_and_port(url): result = url.split(":") if len(result) > 2: raise ArgumentError('invalid url format.') else: try: if len(result) == 2: return result[0], int(result[1]) else: return url, None except ValueError: raise ArgumentError('invalid port number')