Example #1
0
 def notify(self, event):  # ** IMPORTANT **
     if event.signal == "WIIMOTE_BUTTON_PRESS":
         if self.option["button"]: print event.data["button"], "pressed"
         if event.data["button"] == "Home":
             wiiremote.quit()
             return
         if event.data["button"] == "A":  # in init_accel()
             if self.param["acc_zero"] == [0, 0, 0]:
                 self.param["acc_zero"] = [1, 1, 1]
                 return
             if self.option["sound"]:
                 if self.param["saber"]:  # saber off
                     self.play_sound("off")
                     self.param["saber"] = False
                     wiiremote.do(wiiremote.whileRumble500msec)
                 else:  # saber on
                     self.play_sound("on")
                     self.param["saber"] = True
                     wiiremote.do(wiiremote.whileRumble500msec)
                 return
     elif event.signal == "WIIMOTE_BUTTON_RELEASE":
         if self.option["button"]: print event.data["button"], "released"
         return
     elif event.signal == "WIIMOTE_ACCEL":
         self.accel(event.data)
         return
     elif event.signal == "WIIMOTE_GYRO":
         self.gyro(event.data)
         return
     elif event.signal == "WIIMOTE_STATUS":
         print "[--- status ---]"
         print "flag:\n ", wiiremote.getFlag_format(event.data["flag"])
         print "LED:", wiiremote.getLED_format(event.data["LEDs"])
         print "battery: %f %%" % (event.data["battery"] * 100)
         print ""
         wiiremote.end_status(self.param["org_mode"])  # restore mode
         self.param["org_mode"] = None
         return
     elif event.signal == "WIIMOTE_DISCONNECT":
         self.param["running"] = False
         sys.exit(1)
         return
Example #2
0
    def init_accel(self):
        print "[--- Initalize accel ---]"
        if not self.param["acc_zero"]:  #or not param["acc_gain"]:
            acc = [[], [], []]
            self.param["acc_zero"] = [0, 0, 0]
            self.param["acc_gain"] = [0, 0, 0]
            for i in range(3):
                print "%d. Set WiiRemote %s >" % (i + 1, usage[i])
                while wiiremote.running() and not acc[i]:
                    pygame.time.wait(10)
                    if self.accel:
                        if self.param["acc_zero"] == [1, 1, 1]:
                            acc[i] = self.accel

            if not wiiremote.running():
                wiiremote.quit()
                return
            #calc
            self.param["acc_zero"][0] = (acc[0][0] + acc[1][0]) / 2
            self.param["acc_zero"][1] = (acc[0][1] + acc[2][1]) / 2
            self.param["acc_zero"][2] = (acc[1][2] + acc[2][2]) / 2
            self.param["acc_gain"][0] = acc[2][0] - self.param["acc_zero"][0]
            self.param["acc_gain"][1] = acc[1][1] - self.param["acc_zero"][1]
            self.param["acc_gain"][2] = acc[0][2] - self.param["acc_zero"][2]
            for c in range(3):
                self.conf.set("ACC_ZERO", ["x", "y", "z"][c],
                              str(self.param["acc_zero"][c]))
                self.conf.set("ACC_GAIN", ["x", "y", "z"][c],
                              str(self.param["acc_gain"][c]))

            with open(CONF_FILENAME, "wb") as fd:
                self.conf.write(fd)

        print "acc_zero =", self.param["acc_zero"]
        print "acc_gain =", self.param["acc_gain"]
        print ""
        return
Example #3
0
    def main(self):
        #init
        self.load_conf()
        pygame.init()
        wiiremote.init(self)
        if not wiiremote.running(): return
        wiiremote.pause(True)  #pause to get acc and gyro data

        #visual
        visual.scene.width = self.param["width"]
        visual.scene.height = self.param["height"]
        visual.scene.title = "WiiRemote"
        visual.scene.forward = visual.vector(1, 0, 0)  # not to error ?
        visual.scene.up = visual.vector(0, 0, 1)
        visual.scene.forward = visual.vector(-1, 1, -1)
        visual.scene.autoscale = 0
        self.visual["axis"][0] = visual.arrow(color=visual.color.red,
                                              axis=(3, 0, 0),
                                              headwidth=1,
                                              shaftwidth=0.5,
                                              fixedwidth=1)
        self.visual["axis"][1] = visual.arrow(color=visual.color.green,
                                              axis=(0, 3, 0),
                                              headwidth=1,
                                              shaftwidth=0.5,
                                              fixedwidth=1)
        self.visual["axis"][2] = visual.arrow(color=visual.color.blue,
                                              axis=(0, 0, 3),
                                              headwidth=1,
                                              shaftwidth=0.5,
                                              fixedwidth=1)
        self.visual["wiiobj"] = visual.box(
            pos=(0, 0, 0),
            length=1,
            height=4,
            width=1.5,
            color=visual.color.white,
        )

        #sound
        if self.option["sound"]:
            self.init_sound()

        #status
        self.get_status()

        #accel
        self.init_accel()
        acc = [0, 0, 0]

        #pygame
        old = [self.param["height"] / 2] * 3
        view = [True, True, True]
        screen = pygame.display.set_mode(
            (self.param["width"], self.param["height"]))
        font = pygame.font.SysFont(None,
                                   int(self.param["sidebar_width"] * 1.5))
        self.param[
            "width_"] = self.param["width"] - self.param["sidebar_width"]

        print "[--- start ---]"
        if self.option["sound"]:
            print "press A to on/off saber"

        while self.param["running"]:
            #self.re.refresh()
            pygame.time.wait(100)
            pygame.display.update()

            screen.fill((0, 0, 0),
                        (self.param["width_"], 0, self.param["sidebar_width"],
                         self.param["height"]))
            screen.blit(screen, (-1, 0))

            for i in range(int(maxA * 2 + 1)):
                screen.blit(
                    font.render("%-d" % (maxA - i), True, (144, 144, 144)),
                    (self.param["width_"], float(self.param["height"]) /
                     (maxA * 2) * i - self.param["sidebar_width"] * 1.5 / 4))
                pygame.draw.line(
                    screen, (144, 144, 144),
                    (0, float(self.param["height"]) / (maxA * 2) * i),
                    (self.param["width_"], float(self.param["height"]) /
                     (maxA * 2) * i))

            #pygame
            for c in range(3):  # 3axis
                if view[c]:
                    s = int((acc[c] * self.param["height"] / maxA +
                             self.param["height"]) / 2)
                    s = max(0, min(self.param["height"] - 1, s))
                    pygame.draw.line(screen, colors[c],
                                     (self.param["width_"] - 3, old[c]),
                                     (self.param["width_"] - 2, s))
                    old[c] = s

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    wiiremote.quit()
                    sys.exit(1)
                    return
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        wiiremote.quit()
                        sys.exit(1)
                        return
                    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