def ListenForSubscriptionUpdates(self, event): global projector_calibration, LAST_SERVER_HEALTH_CHECK, HEALTH_CHECK_DELAY_S start = time.time() wishes = [] deaths = [] old_calibration = copy.deepcopy(projector_calibration) received_msg = True received_msg_count = 0 max_received_msg_in_loop = 1000 while received_msg and received_msg_count < max_received_msg_in_loop: received_msg = listen(blocking=False) received_msg_count += 1 if received_msg_count > 1: logging.info("received more than 1 message! {}".format( received_msg_count)) if old_calibration != projector_calibration: if projector_calibration and len(projector_calibration) is 4: logging.error("RECAL PROJECTION MATRIX") pts1 = np.float32(projector_calibration) pts2 = np.float32([[0, 0], [CAM_WIDTH, 0], [CAM_WIDTH, CAM_HEIGHT], [0, CAM_HEIGHT]]) self.projection_matrix = cv2.getPerspectiveTransform( pts1, pts2) end = time.time() # print(1000*(end - start), "ms", 1.0/(end - start), "fps") if time.time() - LAST_SERVER_HEALTH_CHECK > HEALTH_CHECK_DELAY_S: LAST_SERVER_HEALTH_CHECK = time.time() check_server_connection()
def ListenForSubscriptionUpdates(self, event): global projector_calibration, LAST_SERVER_HEALTH_CHECK, HEALTH_CHECK_DELAY_S start = time.time() wishes = [] deaths = [] received_msg = True received_msg_count = 0 max_received_msg_in_loop = 1000 while received_msg and received_msg_count < max_received_msg_in_loop: received_msg = listen(blocking=False) received_msg_count += 1 if received_msg_count > 1: logging.info("received more than 1 message! {}".format( received_msg_count)) end = time.time() # print(1000*(end - start), "ms", 1.0/(end - start), "fps") if time.time() - LAST_SERVER_HEALTH_CHECK > HEALTH_CHECK_DELAY_S: LAST_SERVER_HEALTH_CHECK = time.time() check_server_connection()
def listen(): def callback(ch, method, properties, body): print(" [x] Received %r" % body) helper.listen(q='hello',call_back=callback)
[0, CONTOUR_PERSPECTIVE_IMAGE_HEIGHT - 1]], dtype = "float32") contour_perspective_matrix = cv2.getPerspectiveTransform(contour_perspective, dst) contour_perspective_image = cv2.warpPerspective(image, contour_perspective_matrix, (CONTOUR_PERSPECTIVE_IMAGE_WIDTH, CONTOUR_PERSPECTIVE_IMAGE_HEIGHT)) with lock: blob_images.append(contour_perspective_image) # claim for i in range(len(blob_images)): claims.append({"type": "claim", "fact": [ ["id", get_my_id_str()], ["id", "0"], ["text", "camera"], ["text", "sees"], ["text", "subframe"], ["text", str(i)], ["text", "http://{}:{}/src/files/cv-frame-{}.jpg".format(get_host_ip(), PORT, i)], ["text", "@"], ["integer", str(int(time.time()*1000))], ]}) except Exception as e: logging.error(traceback.format_exc()) batch(claims) threading.Thread(target=create_server).start() init(__file__, skipListening=True) while True: more_messages_to_receive = True while more_messages_to_receive: more_messages_to_receive = listen(blocking=False) run_cv() time.sleep(1)
def playNextNode(midi_out, instrument, pitch, duration_s): midi_out.set_instrument(instrument) # 74 is middle C, 127 is "how loud" - max is 127 midi_out.note_on(pitch, 127) time.sleep(duration_s) midi_out.note_off(pitch, 127) init(__file__, skipListening=True) pygame.init() pygame.midi.init() port = pygame.midi.get_default_output_id() logging.info("using output_id :%s:" % port) midi_out = pygame.midi.Output(port, 0) try: lastUpdateTime = time.time() while True: listen(blocking=False) if time.time() - lastUpdateTime >= 60.0 / BPM and len(MELODY) > 0: playNextNode(midi_out, INSTRUMENT, MELODY[MELODY_INDEX], 60.0 / BPM) MELODY_INDEX = (MELODY_INDEX + 1) % len(MELODY) else: time.sleep(0.01) finally: midi_out.note_off(MELODY[MELODY_INDEX], 127) midi_out.close() del midi_out pygame.midi.quit()
def check_for_new_frames(): more_messages_to_receive = True while more_messages_to_receive: more_messages_to_receive = listen(blocking=False)
global write_buffer if results: for result in results: write_buffer.append(b'S000000') init(__file__, skipListening=True) with serial.Serial('/dev/ttyUSB0', 9600, timeout=0.1) as ser: ser.reset_input_buffer() ser.reset_output_buffer() while True: received_msg = True while received_msg: logging.info("checking for messages from room") received_msg = listen(blocking=False) # Send new messages if there are any if len(write_buffer) > 0: logging.info("writing to serial:") logging.info(write_buffer) for line in write_buffer: ser.write(line) write_buffer = [] # Receive serial messages claims = [{ "type": "retract", "fact": [["id", get_my_id_str()], ["id", "0"], ["postfix", ""]] }] logging.info("reading serial lines") lines = ser.readlines() # used the serial timeout specified above
def room_thread(): global room_ui_requests @prehook def room_prehook_callback(): batch([{"type": "death", "fact": [["id", get_my_id_str()]]}]) @subscription([ "$ $ paper $targetId has RFID $rfid", "$ $ $targetName has paper ID $targetId", "$ $ $targetName has source code $sourceCode" ]) def rfid_source_code_callback(results): rfid_to_source_code = {} if results: for result in results: source_code = result["sourceCode"].replace(chr(9787), '"') rfid_to_source_code[result["rfid"]] = (result["targetName"], source_code) room_rfid_code_updates.put(rfid_to_source_code) print("updated RFID to source code map") init(__file__, skipListening=True) while True: listen(blocking=False) try: message = room_ui_requests.get(block=False) if message is None: print("received none message in room thread, exiting") return req_type = message[0] active_rfid = message[1] active_program_name = message[2] source_code = message[3] clean_source_code = source_code.replace('"', chr(9787)) if req_type == ROOM_REQUEST_SAVE: batch([{ "type": "claim", "fact": [ ["id", get_my_id_str()], ["id", "0"], ["text", "wish"], ["text", active_program_name], ["text", "has"], ["text", "source"], ["text", "code"], ["text", clean_source_code], ] }]) elif req_type == ROOM_REQUEST_PRINT: generate_and_upload_code_image("Code for {}\n\n{}".format( active_program_name, clean_source_code)) batch([{ "type": "claim", "fact": [ ["id", get_my_id_str()], ["id", "0"], ["text", "wish"], ["text", "1854-code.png"], ["text", "would"], ["text", "be"], ["text", "thermal"], ["text", "printed"], ["text", "on"], ["text", "epson"], ] }]) elif req_type == ROOM_REQUEST_PRINT_FRONT: generate_and_upload_code_front_image(active_program_name) batch([{ "type": "claim", "fact": [ ["id", get_my_id_str()], ["id", "0"], ["text", "wish"], ["text", "1854-front.png"], ["text", "would"], ["text", "be"], ["text", "thermal"], ["text", "printed"], ["text", "on"], ["text", "epson"], ] }]) except queue.Empty: pass time.sleep(0.1)
def onLatencyCheckTimer(self, event): print("listening...") more_messages_to_receive = True while more_messages_to_receive: more_messages_to_receive = listen(blocking=False)