def main(self): # initalize self.load_conf() pygame.init() # sound if self.option["sound"]: self.init_sound() # pygame old = [self.param["height"] / 2] * 9 font_title = pygame.font.Font("font/ipag.ttf", int(self.param["sidebar_width"] * 1.5)) font_small = pygame.font.Font("font/ipag.ttf", int(self.param["sidebar_width"])) screen = pygame.display.set_mode( (self.param["width"] * 3, self.param["height"] + self.param["sidebar_height"] * 3), ) screen.fill((255, 255, 255)) pygame.display.set_caption(u"WiiRemote Orientation Graph") notes = pygame.Surface( (self.param["width_"] / 2.0 - 10, self.param["sidebar_height"] * 3 - 10)).convert() acc_graph = pygame.Surface( (self.param["width_"], self.param["height_"])).convert() acc_base = pygame.Surface( (self.param["width"], self.param["height"])).convert() gyro_graph = pygame.Surface( (self.param["width_"], self.param["height_"])).convert() gyro_base = pygame.Surface( (self.param["width"], self.param["height"])).convert() average_graph = pygame.Surface( (self.param["width_"], self.param["height_"])).convert() average_base = pygame.Surface( (self.param["width"], self.param["height"])).convert() acc_title = font_title.render(u"加速度", True, (0, 0, 0)) gyro_title = font_title.render(u"ジャイロ", True, (0, 0, 0)) average_title = font_title.render(u"加速度+ジャイロ", True, (0, 0, 0)) pitch_font = font_small.render(u"pitch角", True, (255, 255, 255)) #(0,0,0)) roll_font = font_small.render(u"roll角", True, (255, 255, 255)) #(0,0,0)) yaw_font = font_small.render(u"yaw角", True, (255, 255, 255)) #(0,0,0)) for i in xrange(3): pygame.draw.line(notes, self.param["colors"][i], (3, self.param["sidebar_height"] * i + self.param["sidebar_height"] / 2.0 - 5), (self.param["width_"] / 4.0 - 3, self.param["sidebar_height"] * i + self.param["sidebar_height"] / 2.0 - 5)) notes.blit(pitch_font, (self.param["width_"] / 4.0, 0)) notes.blit(roll_font, (self.param["width_"] / 4.0, self.param["sidebar_height"])) notes.blit( yaw_font, (self.param["width_"] / 4.0, self.param["sidebar_height"] * 2)) #visual self.init_visual() # connect WiiRemote wiiremote.init() if not wiiremote.running(): return # sensors self.init_sensors() # get status if not self.option["limited"]: self.get_status() print "[--- start ---]\n" if self.option["sound"]: print "press [A] to on/off saber" while not self.event_handling(): pygame.display.update() pygame.time.wait(10) bgcolor = (255, 255, 255) if self.param["fall"]: self.param["fall"] = False bgcolor = (255, 0, 0) screen.fill(bgcolor) screen.blit(notes, (self.param["width"] * 2.5, self.param["height"])) acc_graph.blit(acc_graph, (-1, 0)) gyro_graph.blit(gyro_graph, (-1, 0)) average_graph.blit(average_graph, (-1, 0)) for c in xrange(3): if self.param["view-axis"][c]: pygame.draw.line(acc_graph, self.param["colors"][c], (self.param["width_"] - 3, old[c]), (self.param["width_"] - 2, self.data["graph_acc_orient"][c])) old[c] = self.data["graph_acc_orient"][c] if self.param["view-axis"][c + 3]: pygame.draw.line(gyro_graph, self.param["colors"][c], (self.param["width_"] - 3, old[c + 3]), (self.param["width_"] - 2, self.data["graph_gyro_orient"][c])) old[3 + c] = self.data["graph_gyro_orient"][c] if self.param["view-axis"][c + 6]: pygame.draw.line(average_graph, self.param["colors"][c], (self.param["width_"] - 3, old[c + 6]), (self.param["width_"] - 2, self.data["graph_average_orient"][c])) old[6 + c] = self.data["graph_average_orient"][c] acc_base.fill(bgcolor) gyro_base.fill(bgcolor) average_base.fill(bgcolor) acc_base.blit( acc_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) gyro_base.blit( gyro_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) average_base.blit( average_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) acc_base.blit(acc_title, (self.param["sidebar_width"] + 10, 0)) gyro_base.blit(gyro_title, (self.param["sidebar_width"], 0)) average_base.blit(average_title, (self.param["sidebar_width"], 0)) pygame.draw.line( acc_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"] / 2), (self.param["sidebar_width"] + self.param["width_"], self.param["sidebar_height"] + self.param["height_"] / 2)) pygame.draw.line( gyro_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"] / 2), (self.param["sidebar_width"] + self.param["width_"], self.param["sidebar_height"] + self.param["height_"] / 2)) pygame.draw.line( average_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"] / 2), (self.param["sidebar_width"] + self.param["width_"], self.param["sidebar_height"] + self.param["height_"] / 2)) screen.blit(acc_base, (0, 0)) screen.blit(gyro_base, (self.param["width"], 0)) screen.blit(average_base, (self.param["width"] * 2, 0)) for p in xrange(len(self.param["patterns"])): if self.param["cur_pattern"] == p: screen.fill( (255, 0, 255), (self.param["sidebar_width"], self.param["height"] + self.param["sidebar_width"] * p, self.param["width"] * 2 - self.param["sidebar_width"], self.param["sidebar_width"] )) # from 2 to 2.25 ?############# screen.blit( font_small.render(self.param["patterns"][p], True, (0, 0, 0)), (self.param["sidebar_width"], self.param["height"] + self.param["sidebar_width"] * p)) else: wiiremote.quit(self.option["limited"]) self.quit_visual() sys.exit(1)
def event_handling(self): if not wiiremote.running(): return 1 for event in wiiremote.get_queue(): if event.type == wiiremote.WIIMOTE_ACCEL_GYRO: self.accel_gyro(event.accel, event.gyro, event.fast_mode, event.time) continue for event in pygame.event.get(): #print event if event.type == pygame.QUIT: return 1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return 1 elif event.key == pygame.K_a: self.param["view-axis"][0] = not self.param["view-axis"][0] continue elif event.key == pygame.K_b: self.param["view-axis"][1] = not self.param["view-axis"][1] continue elif event.key == pygame.K_c: self.param["view-axis"][2] = not self.param["view-axis"][2] continue elif event.key == pygame.K_d: self.param["view-axis"][3] = not self.param["view-axis"][3] continue elif event.key == pygame.K_e: self.param["view-axis"][4] = not self.param["view-axis"][4] continue elif event.key == pygame.K_f: self.param["view-axis"][5] = not self.param["view-axis"][5] continue elif event.key == pygame.K_x: self.param["view-axis"][6] = not self.param["view-axis"][6] continue elif event.key == pygame.K_y: self.param["view-axis"][7] = not self.param["view-axis"][7] continue elif event.key == pygame.K_z: self.param["view-axis"][8] = not self.param["view-axis"][8] continue elif event.type == wiiremote.WIIMOTE_BUTTON_PRESS: if self.option["button"]: print event.button, "pressed" if event.button == "Home": return 1 elif event.button == "A": if self.param["gyro_zero"] == [ 0, 0, 0 ] or self.param["acc_zero"] == [0, 0, 0]: return 1 if self.option["sound"]: if self.param["saber"]: self.play_sound("off") self.param["saber"] = False wiiremote.do(wiiremote.whileRumble500msec) else: self.play_sound("on") self.param["saber"] = True wiiremote.do(wiiremote.whileRumble500msec) return 1 self.calibration(reset_gyro_zero=False) #elif event.button == "2": # self.calibration(reset_gyro_zero=True) continue elif event.type == wiiremote.WIIMOTE_BUTTON_RELEASE: if self.option["button"]: print event.button, "released" continue elif event.type == wiiremote.WIIMOTE_STATUS: print "[--- status ---]" print "flag:\n ", wiiremote.getFlag_format(event.flag) print "LED:", wiiremote.getLED_format(event.LEDs) print "battery: %f %%" % (event.battery * 100) print "" if self.param["org_mode"]: wiiremote.end_status( self.param["org_mode"]) # restore mode self.param["org_mode"] = None return 1 continue else: return None
def event_handling(self): if not wiiremote.running(): return 1 for event in wiiremote.get_queue(): if event.type == wiiremote.WIIMOTE_ACCEL_GYRO: self.accel_gyro(event.accel, event.gyro, event.fast_mode, event.time) continue for event in pygame.event.get(): #print event if event.type == pygame.QUIT: return 1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return 1 elif event.key == pygame.K_a: self.param["view-axis"][0] = not self.param["view-axis"][0] continue elif event.key == pygame.K_b: self.param["view-axis"][1] = not self.param["view-axis"][1] continue elif event.key == pygame.K_c: self.param["view-axis"][2] = not self.param["view-axis"][2] continue elif event.key == pygame.K_d: self.param["view-axis"][3] = not self.param["view-axis"][3] continue elif event.key == pygame.K_e: self.param["view-axis"][4] = not self.param["view-axis"][4] continue elif event.key == pygame.K_f: self.param["view-axis"][5] = not self.param["view-axis"][5] continue elif event.key == pygame.K_x: self.param["view-axis"][6] = not self.param["view-axis"][6] continue elif event.key == pygame.K_y: self.param["view-axis"][7] = not self.param["view-axis"][7] continue elif event.key == pygame.K_z: self.param["view-axis"][8] = not self.param["view-axis"][8] continue elif event.type == wiiremote.WIIMOTE_BUTTON_PRESS: if self.option["button"]: print event.button, "pressed" if event.button == "Home": return 1 elif event.button == "A": if self.param["gyro_zero"] == [0,0,0] or self.param["acc_zero"] == [0,0,0]: return 1 if self.option["sound"]: if self.param["saber"]: self.play_sound("off") self.param["saber"] = False wiiremote.do(wiiremote.whileRumble500msec) else: self.play_sound("on") self.param["saber"] = True wiiremote.do(wiiremote.whileRumble500msec) return 1 self.calibration(reset_gyro_zero=False) elif event.button == "2": self.calibration(reset_gyro_zero=True) continue elif event.type == wiiremote.WIIMOTE_BUTTON_RELEASE: if self.option["button"]: print event.button, "released" continue elif event.type == wiiremote.WIIMOTE_STATUS: print "[--- status ---]" print "flag:\n ", wiiremote.getFlag_format(event.flag) print "LED:", wiiremote.getLED_format(event.LEDs) print "battery: %f %%" % (event.battery * 100) print "" if self.param["org_mode"]: wiiremote.end_status(self.param["org_mode"]) # restore mode self.param["org_mode"] = None return 1 continue else: return None
def main(self): # initalize self.load_conf() pygame.init() # sound if self.option["sound"]: self.init_sound() # pygame old = [self.param["height"] / 2] * 9 font_title = pygame.font.Font("font/ipag.ttf", int(self.param["sidebar_width"]*1.5)) font_small = pygame.font.Font("font/ipag.ttf", int(self.param["sidebar_width"])) screen = pygame.display.set_mode((self.param["width"] *3 , self.param["height"]+self.param["sidebar_height"]*3), ) screen.fill((255, 255, 255)) pygame.display.set_caption(u"WiiRemote Orientation Graph") notes = pygame.Surface((self.param["width_"]/2.0-10, self.param["sidebar_height"]*3-10)).convert() acc_graph = pygame.Surface((self.param["width_"], self.param["height_"])).convert() acc_base = pygame.Surface((self.param["width"], self.param["height"])).convert() gyro_graph = pygame.Surface((self.param["width_"], self.param["height_"])).convert() gyro_base = pygame.Surface((self.param["width"], self.param["height"])).convert() average_graph = pygame.Surface((self.param["width_"], self.param["height_"])).convert() average_base = pygame.Surface((self.param["width"], self.param["height"])).convert() acc_title = font_title.render(u"加速度", True, (0, 0, 0)) gyro_title = font_title.render(u"ジャイロ", True, (0,0,0)) average_title = font_title.render(u"加速度+ジャイロ", True, (0,0,0)) pitch_font = font_small.render(u"pitch角", True, (255,255,255))#(0,0,0)) roll_font = font_small.render(u"roll角", True, (255,255,255))#(0,0,0)) yaw_font = font_small.render(u"yaw角", True, (255,255,255))#(0,0,0)) for i in xrange(3): pygame.draw.line(notes, self.param["colors"][i], (3, self.param["sidebar_height"]*i+self.param["sidebar_height"]/2.0-5), (self.param["width_"]/4.0-3, self.param["sidebar_height"]*i+self.param["sidebar_height"]/2.0-5)) notes.blit(pitch_font, (self.param["width_"]/4.0, 0)) notes.blit(roll_font, (self.param["width_"]/4.0, self.param["sidebar_height"])) notes.blit(yaw_font, (self.param["width_"]/4.0, self.param["sidebar_height"]*2)) #visual self.init_visual() # connect WiiRemote wiiremote.init() if not wiiremote.running(): return # sensors self.init_sensors() # get status if not self.option["limited"]: self.get_status() print "[--- start ---]\n" if self.option["sound"]: print "press [A] to on/off saber" while not self.event_handling(): pygame.display.update() pygame.time.wait(10) bgcolor = (255, 255, 255) if self.param["fall"]: self.param["fall"] = False bgcolor = (255, 0, 0) screen.fill(bgcolor) screen.blit(notes, (self.param["width"]*2.5, self.param["height"])) acc_graph.blit(acc_graph, (-1, 0)) gyro_graph.blit(gyro_graph, (-1, 0)) average_graph.blit(average_graph, (-1, 0)) for c in xrange(3): if self.param["view-axis"][c]: pygame.draw.line(acc_graph, self.param["colors"][c], (self.param["width_"]-3, old[c]), (self.param["width_"]-2, self.data["graph_accel"][c])) old[c] = self.data["graph_accel"][c] if self.param["view-axis"][c+3]: pygame.draw.line(gyro_graph, self.param["colors"][c], (self.param["width_"]-3, old[c+3]), (self.param["width_"]-2, self.data["graph_gyro"][c])) old[3+c] = self.data["graph_gyro"][c] if self.param["view-axis"][c+6]: pygame.draw.line(average_graph, self.param["colors"][c], (self.param["width_"]-3, old[c+6]), (self.param["width_"]-2, self.data["graph_average_orient"][c])) old[6+c] = self.data["graph_average_orient"][c] acc_base.fill(bgcolor) gyro_base.fill(bgcolor) average_base.fill(bgcolor) acc_base.blit(acc_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) gyro_base.blit(gyro_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) average_base.blit(average_graph, (self.param["sidebar_width"], self.param["sidebar_height"])) acc_base.blit(acc_title, (self.param["sidebar_width"]+10, 0)) gyro_base.blit(gyro_title, (self.param["sidebar_width"], 0)) average_base.blit(average_title, (self.param["sidebar_width"], 0)) pygame.draw.line(acc_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"]/2), (self.param["sidebar_width"]+self.param["width_"], self.param["sidebar_height"]+self.param["height_"]/2)) pygame.draw.line(gyro_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"]/2), (self.param["sidebar_width"]+self.param["width_"], self.param["sidebar_height"]+self.param["height_"]/2)) pygame.draw.line(average_base, (144, 144, 144), (self.param["sidebar_width"], self.param["sidebar_height"] + self.param["height_"]/2), (self.param["sidebar_width"]+self.param["width_"], self.param["sidebar_height"]+self.param["height_"]/2)) screen.blit(acc_base, (0,0)) screen.blit(gyro_base, (self.param["width"], 0)) screen.blit(average_base, (self.param["width"]*2, 0)) for p in xrange(len(self.param["patterns"])): if self.param["cur_pattern"] == p: screen.fill((255, 0, 255), (self.param["sidebar_width"], self.param["height"]+self.param["sidebar_width"]*p, self.param["width"]*2-self.param["sidebar_width"], self.param["sidebar_width"])) # from 2 to 2.25 ?############# screen.blit(font_small.render(self.param["patterns"][p], True, (0,0,0)), (self.param["sidebar_width"], self.param["height"]+self.param["sidebar_width"]*p)) else: wiiremote.quit(self.option["limited"]) self.quit_visual() sys.exit(1)
def main(): #exec options debug = {"button":False, "accel":False, "orient":False, "divide":False, "sound":True, "fall":False, "gyro":False, "save":False} if "--button" in sys.argv[1:]: debug["button"] = True if "--acc" in sys.argv[1:]: debug["accel"] = True if "--acc_orient" in sys.argv[1:]: debug["orient"] = True if "--gyro" in sys.argv[1:]: debug["gyro"] = True if "--divide-view" in sys.argv[1:]: debug["divide"] = True if "--sound-off" in sys.argv[1:]: debug["sound"] = False if "--fall-detect" in sys.argv[1:]: debug["fall"] = True debug["sound"] = True if "--config" in sys.argv[1:]: pass if "--help" in sys.argv[1:] or "-h" in sys.argv[1:]: print ( "Usage: %s" % sys.argv[0] + "--button Show press or release button.\n" + "--acc Show 3axis accel data.\n" + "--acc_orient Show 3axis orient data calclated from acc.\n" + "--gyro Show 3axis gyro data.\n" + "--divide-view Show divided every axis graph view.\n" + "--sound-off Disable light-saber sound.\n" + "--fall-detect Enable fall detection.\n" + ) #init global sounds, param load_conf(divide=debug["divide"]) pygame.init() wiiremote.init() if not wiiremote.running(): return #sound wiiremote.pause(True) #pause if debupg["sound"]: init_sound() #status wiiremote.status() print "--- status ---" print "flag:\n ", wiiremote.getFlag_p() print "LED:", wiiremote.getLED_p() print "battery: %f %%" % (wiiremote.getBattery() * 100) print "" wiiremote.pause(False) #pause release #accel init_accel() acc = [0, 0, 0] #gyro angle_yaw = 0 angle_pitch = 0 angle_roll = 0 #pygame old = [param["height"] / 2] * 3 view = [True, True, True] if debug["divide"]: screen = pygame.display.set_mode((param["width"], param["height"]*3)) else: screen = pygame.display.set_mode((param["width"], param["height"])) font = pygame.font.SysFont(None, side_width*1.5) param["width_"] = param["width"] - side_width print "--- start ---" if debug["sound"]: print "press A to on/off saber" prev_time = time.time() # init prev_time while wiiremote.wiimote.go: if debug["divide"]: for c in [1, 2]: pygame.draw.line(screen, (255, 255, 255), (0, param["height"]*c), (param["width_"], param["height"]*c)) pygame.time.wait(10) pygame.display.update() screen.fill((0, 0, 0), (param["width_"], 0, side_width, param["height"])) screen.blit(screen, (-1, 0)) if not debug["divide"]: for i in range(int(maxA*2+1)): screen.blit(font.render("%-d" % (maxA - i), True, (144, 144, 144)), (param["width"]-side_width, float(param["height"]) / (maxA*2) * i - side_width*1.5/4)) pygame.draw.line(screen, (144, 144, 144), (0, float(param["height"]) / (maxA*2) * i), (param["width_"], float(param["height"]) / (maxA*2) * i)) #pygame for c in range(3): # 3axis if view[c]: s = int((acc[c] * param["height"] / maxA + param["height"]) / 2) s = max(0, min(param["height"] - 1, s)) if debug["divide"]: pygame.draw.line(screen, colors[c], (param["width_"]-3, param["height"]*c+old[c]), (param["width_"]-2, param["height"]*c+s)) else: pygame.draw.line(screen, colors[c], (param["width_"]-3, old[c]), (param["width_"]-2, s)) old[c] = s for event in pygame.event.get(): if event.type == pygame.QUIT: wiiremote.quit() break if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: wiiremote.quit() break if event.key == pygame.K_x: if view[0]: view[0] = False else: view[0] = True elif event.key == pygame.K_y: if view[1]: view[1] = False else: view[1] = True elif event.key == pygame.K_z: if view[2]: view[2] = False else: view[2] = True elif event.type == wiiremote.WIIMOTE_BUTTON_PRESS: if debug["button"]: print event.button, "pressed" if event.button == "A": if debug["sound"]: if param["saber"]: wiiremote.do(saber_off) else: wiiremote.do(saber_on) #if event.button == "B": # wiiremote.setRumble(1) elif event.type == wiiremote.WIIMOTE_BUTTON_RELEASE: if debug["button"]: print event.button, "released" #if event.button == "B": # wiiremote.setRumble(0) elif event.type == wiiremote.WIIMOTE_ACCEL: acc[0] = (event.accel[0] - param["acc_zero"][0]) / param["acc_gain"][0] acc[1] = (event.accel[1] - param["acc_zero"][1]) / param["acc_gain"][1] acc[2] = (event.accel[2] - param["acc_zero"][2]) / param["acc_gain"][2] if debug["accel"]: print "acc =", acc if debug["save"]: pass if debug["sound"] and param["saber"]: if (4 > abs(acc[0]) >= 1.5) or (4 > abs(acc[1]) >= 2.5) or (4 > abs(acc[2]) >= 2.5): play_sound("swing1", True) elif (abs(acc[0]) >= 4) or (abs(acc[1]) >= 3) or (abs(acc[2]) >= 3): play_sound("strike1", False, stop=True) if acc[0] > 1: gx = 1 elif acc[0] < -1: gx = -1 else: gx = acc[0] if acc[1] > 1: gy = 1 elif acc[1] < -1: gy = -1 else: gy = acc[1] if acc[2] > 1: gz = 1 elif acc[2] < -1: gz = -1 else: gz = acc[2] #print "gx, gy, gz =", gx, gy, gz jx = 90 - (math.asin(gx) * 180 / math.pi) jy = 90 - (math.asin(gy) * 180 / math.pi) jz = 90 - (math.asin(gz) * 180 / math.pi) if debug["orient"]: print "acc orient : %f %f %f" % (jx, jy, jz) if debug["fall"]: if abs(gx) * 10 < 1 and abs(gy) * 10 < 1 and abs(gz) * 10 < 1 and not pygame.mixer.get_busy(): print "Fall!" wiiremote.do(sounds["fall"].play) elif event.type == wiiremote.WIIMOTE_ORIENT: yaw = (event.orient[0] - param["gyro_zero"][0]) / param["gyro_gain"] if event.fast_mode[0]: yaw *= param["gyro_fast"] pitch = (event.orient[1] - param["gyro_zero"][1]) / param["gyro_gain"] if event.fast_mode[1]: pitch *= param["gyro_fast"] roll = (event.orient[2] - param["gyro_zero"][2]) / param["gyro_gain"] if event.fast_mode[2]: roll *= param["gyro_fast"] frametime = time.time() - prev_time prev_time = time.time() #print frametime angle_yaw += yaw * frametime angle_pitch += pitch * frametime angle_roll += roll * frametime # -180 <= angle < 180 angle_yaw = angle_yaw % 360 angle_pitch = angle_pitch % 360 angle_roll = angle_roll % 360 if debug["gyro"]: #print "yaw, pitch, roll =", yaw, pitch, roll print "angle_yaw, angle_pitch, angle_roll =", angle_yaw, angle_pitch, angle_roll wiiremote.quit() print "--- stop ---" return