def set_angle(self, angle): msg = "Set Angle {}".format(angle) logger.debug(msg) if angle == self.last_angle or angle is None: return self.last_angle = angle logger.info(msg) pz.setOutput(self.servo_id, angle)
def set_angle(self, angle): msg = "DefaultServoProvider({}).set_angle({})".format( self._servo_id, angle) logger.debug(msg) if angle == self.last_angle: return logger.info(msg) self.last_angle = angle
def handleMessage(self): logger.debug('handleMessage, from {}'.format(self.address)) try: obj = json.loads(self.data) DefaultWebSocketHandler.callee.on_websocket_message(obj) except Exception as e: logger.exception("Error in handleMessage")
def set_speed(self, speed): msg = "set_speed({}, {})".format(self.motor_id, speed) logger.debug(msg) if speed == self._last_speed or speed is None: return self._last_speed = speed logger.info(msg) # TODO: scale -100..100 to -128..127 speed = constrain(speed, -128, 127) pz.setMotor(self.motor_id, speed)
def set_speed(self, speed): msg = "set_speed({}, {})".format(self.motor_id, speed) logger.debug(msg) speed = constrain(speed, -100, 100) try: PimoroniExplorerHatMotor.motors[self.motor_id].speed(speed) pass except AttributeError as ae: logger.error("Pimoroni Explorer HAT error setting motor speed, perhaps the HAT is not attached?")
def set_angle(self, angle): msg = "Set Angle {}".format(angle) logger.debug(msg) if angle == self.last_angle or angle is None: return logger.info(msg) self.last_angle = angle angle -= 90 try: self.set_servo_angle(angle) except IOError as e: logger.exception("cant set angle")
def __init__(self, camera): logger.debug('Spawning background conversion process') self.converter = Popen([ 'avconv', '-f', 'rawvideo', '-pix_fmt', 'yuv420p', '-s', '%dx%d' % camera.resolution, '-r', str(float(camera.framerate)), '-i', '-', '-f', 'mpeg1video', '-b', '800k', '-r', str(float(camera.framerate)), '-' ], stdin=PIPE, stdout=PIPE, stderr=io.open(os.devnull, 'wb'), shell=False, close_fds=True)
def _start(self): for event in self.input_device.read_loop(): cat_event = categorize(event) if isinstance(cat_event, AbsEvent): #logger.debug("Event= {} Cat={}".format(event, cat_event)) axis_key = ABS[cat_event.event.code] axis_val = event.value # TODO: move to init logger.debug("Joypad event: {} = {}".format( axis_key, axis_val)) if axis_key not in self.controller_state: self.controller_state[axis_key] = 0 if self.controller_state[axis_key] != axis_val: self.controller_state[axis_key] = axis_val if self._listener: self._listener(self) elif isinstance(cat_event, KeyEvent): logger.debug( "KeyEvent, keycode={}, scancode={}, keystate={}". format(cat_event.keycode, cat_event.scancode, cat_event.keystate)) button_id = self._get_button_id(cat_event.keycode) if button_id is not None: Button(button_id).value = 1 if bool( cat_event.keystate) else 0 else: logger.debug("{}, {}".format(event, cat_event)) if not self.keep_running: break
def _process_events(self, events): # TODO: to play nicely with other toys in the future this really should be fed from a global event loop for e in events: logger.debug("Joystick event: {}".format(e)) if e.type == JOYAXISMOTION: for axis_num in range(0, self.joystick.get_numaxes()): axis_key = 'AXIS' + str(axis_num) axis_val = self.joystick.get_axis(axis_num) if axis_key not in self.controller_state or self.controller_state[ axis_key] != axis_val: self.controller_state[axis_key] = axis_val if self._listener: self._listener(self) elif e.type == JOYBUTTONDOWN: logger.debug(e.button) Button(e.button).value = 1 elif e.type == JOYBUTTONUP: logger.debug(e.button) Button(e.button).value = 0 else: logger.debug(e)
def handleClose(self): logger.debug('handleClose, from {}'.format(self.address)) DefaultWebSocketHandler.clients.remove(self)
def handleConnected(self): logger.debug('handleConnected, from {}'.format(self.address)) DefaultWebSocketHandler.clients.append(self)
def send_all_clients_message(message): logger.debug("Sending all WS Clients: {}".format(message)) for client in DefaultWebSocketHandler.clients: client.sendMessage(message)
def main(): logger.info('Initializing camera') with picamera.PiCamera() as camera: config = get_config() w = config.getint('camera', 'width', fallback=640) h = config.getint('camera', 'height', fallback=480) camera.resolution = (w, h) camera.framerate = config.getint('camera', 'framerate', fallback=24) camera.rotation = config.getint('camera', 'rotation', fallback=0) camera.hflip = config.getboolean('camera', 'hflip', fallback=False) camera.vflip = config.getboolean('camera', 'vflip', fallback=False) camera.led = config.getboolean('camera', 'led', fallback=False) sleep(1) # camera warm-up tim logger.info('Initializing websockets server on port %d' % WS_PORT) websocket_server = make_server( '', WS_PORT, server_class=WSGIServer, handler_class=HTTP11WebSocketWSGIRequestHandler, app=WebSocketWSGIApplication(handler_cls=StreamingWebSocket)) websocket_server.initialize_websockets_manager() websocket_thread = Thread(target=websocket_server.serve_forever) #logger.info('Initializing HTTP server on port %d' % HTTP_PORT) #http_server = StreamingHttpServer() #http_thread = Thread(target=http_server.serve_forever) logger.debug('Initializing broadcast thread') output = BroadcastOutput(camera) broadcast_thread = BroadcastThread(output.converter, websocket_server) logger.info('Starting recording') camera.start_recording(output, 'yuv') try: logger.debug('Starting websockets thread') websocket_thread.start() #logger.info('Starting HTTP server thread') #http_thread.start() logger.debug('Starting broadcast thread') broadcast_thread.start() while True: camera.wait_recording(1) except KeyboardInterrupt: pass finally: logger.info('Stopping recording') camera.stop_recording() logger.debug('Waiting for broadcast thread to finish') broadcast_thread.join() logger.debug('Shutting down HTTP server') #http_server.shutdown() logger.debug('Shutting down websockets server') websocket_server.shutdown() logger.debug('Waiting for HTTP server thread to finish') #http_thread.join() logger.debug('Waiting for websockets thread to finish') websocket_thread.join()
def flush(self): logger.debug('Waiting for background conversion process to exit') self.converter.stdin.close() self.converter.wait()