Пример #1
0
def main():
    wi = Wiimote()

    def cb_A():
        print("RELEASED A !")

    wi.on(BTN_A, RELEASED, cb_A)

    import time
    while True:
        # connect the wiimote
        if not wi.connected():
            try:
                print("connecting...")
                wi.connect()
                time.sleep(0.4)
                print("connected")
            except WiimoteError:
                print("connection fail")
                time.sleep(0.2)
        else:
            time.sleep(0.4)

    exit = False
    while not exit:
        c = sys.stdin.read(1)
        if c == "s":
            print wi._wm.state
        elif c == "a":
            print wi.is_pressed(BTN_A)
        exit = c == "x"
    wi.close()
Пример #2
0
    def activate_wiimote(self):
        """ make wiimote available
        """
        from wiimote import Wiimote, WiimoteError
        self.wiimote = Wiimote()

        def check_reconnect():
            connected = False
            while True:
                # connect the wiimote
                if not self.wiimote.connected():
                    try:
                        self.log.info("Connecting to WiiMote...")
                        self.wiimote.connect()
                        self.wait(0.4)
                        self.log.info("Connected to WiiMote !")
                    except WiimoteError:
                        self.log.info("Connection to WiiMote failed !")
                        self.wait(0.2)
                else:
                    self.wait(0.4)

        self._to_spawn.append(check_reconnect)
Пример #3
0
def main_loop():
    # Get colors based on controller roll
    colors = get_colors()
    # Get dim modifier based on controller pitch
    dim_modifier = get_dim()

    set_color([(color * dim_modifier) for color in colors])

    # Snd data to dmx
    for light in lights:
        change_data(light.start_address, light.data)
    send_dmx()


def init_lights(amount):
    for i in xrange(amount):
        lights.append(LED(i * 4, i * 4 + 3))


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "Usage: main.py <amount of lights>"
        sys.exit(1)

    wiimote = Wiimote()
    init_lights(int(sys.argv[1]))

    while True:
        main_loop()
Пример #4
0
    def run(self):
        """ Main Running loop controling bot mode and menu state """
        # Show state on OLED display
        self.show_message('Booting...')

        # Read config file FIRST
        self.read_config()

        self.show_message('Initialising Bluetooth...')

        # Never stop looking for wiimote.
        while not self.killed:
            # Show state on OLED display
            self.oled.cls()  # Clear screen
            self.oled.canvas.text((10, 10), 'Waiting for WiiMote...', fill=1)
            self.oled.canvas.text((10, 30), '***Press 1+2 now ***', fill=1)
            self.oled.display()

            self.wiimote = None
            try:
                self.wiimote = Wiimote()

            except WiimoteException:
                logging.error("Could not connect to wiimote. please try again")

            # Reset LED to NO MODE
            self.mode = self.MODE_NONE
            if self.wiimote and self.wiimote.wm:
                self.wiimote.wm.led = self.mode

            # Show state on OLED display
            self.show_mode()

            # Constantly check wiimote for button presses
            while self.wiimote:
                buttons_state = self.wiimote.get_buttons()
                classic_buttons_state = self.wiimote.get_classic_buttons()

                if buttons_state is not None:
                    if (buttons_state & cwiid.BTN_A):
                        self.start_rc_mode()

                    if (buttons_state & cwiid.BTN_HOME):
                        self.start_calibration_mode()

                    if (buttons_state & cwiid.BTN_B):
                        # Kill any previous Challenge / RC mode
                        self.stop_threads()

                    if (buttons_state & cwiid.BTN_HOME):
                        self.start_calibration_mode()

                    if (buttons_state & cwiid.BTN_UP):
                        logging.info("BUTTON_UP")
                    if (buttons_state & cwiid.BTN_DOWN):
                        logging.info("BUTTON_DOWN")
                    if (buttons_state & cwiid.BTN_LEFT):
                        logging.info("BUTTON_LEFT")
                    if (buttons_state & cwiid.BTN_RIGHT):
                        logging.info("BUTTON_RIGHT")

                if classic_buttons_state is not None:
                    if (classic_buttons_state & cwiid.CLASSIC_BTN_ZL
                            or classic_buttons_state & cwiid.CLASSIC_BTN_ZR):
                        # One of the Z buttons pressed, disable
                        # motors and set neutral.
                        self.core.enable_motors(False)
                    else:
                        # Neither Z buttons pressed,
                        # allow motors to move freely.
                        self.core.enable_motors(True)

                time.sleep(0.05)

                # Verify Wiimote is connected each loop. If not, set wiimote
                # to None and it "should" attempt to reconnect.
                if not self.wiimote.wm:
                    self.stop_threads()
                    self.wiimote = None
Пример #5
0
    def connectWii(self):
        if self.connected:
            self.disconnectDevice()
            return

        self.wii = Wiimote()
        pBar = PBarDlg(self)
        pBar.setModal(True)
        pBar.show()
        conf = Configuration()
        selectedMac = conf.getValueStr("selectedmac")
        pBar.reInit(selectedMac)
        pool = []
        while 1:
            thread = self.wii.createConnectThread(selectedMac, pool)
            thread.start()

            while not thread.wait(30):
                QtWidgets.QApplication.processEvents()

            if pBar.cancelled == True:
                if self.wii.isConnected():
                    self.wii.close()

                self.wii = None
                pBar.close()
                return

            if selectedMac == '*' and len(pool) >= 1:
                if Configuration().getValueStr('nowaitdevices') == 'Yes':
                    selectedMac = pool[0]
                else:
                    pBar.inform(
                        self.tr('Found ') + str(len(pool)) +
                        self.tr(' Devices. Press to Choose'))

            if self.wii.isConnected():
                self.connected = True
                self.calibrated = False
                self.active = False
                self.updateButtons()
                self.batteryLevel.setValue(self.wii.battery() * 100)
                self.pushButtonConnect.setText(self.tr("Disconnect"))

                pBar.close()

                self.confDialog.wii = self.wii
                self.confDialog.checkButtons()

                self.wii.disable()
                self.wii.putCallbackBTN(self.makeBTNCallback())
                self.wii.putCallbackIR(None)
                self.wii.enable()

                # Start calibration if configuration says so
                conf = Configuration()
                if conf.getValueStr("autocalibration") == "Yes":
                    if conf.getValueStr("automatrix") == "Yes":
                        self.calibrateWiiFromSettings()
                    else:
                        self.calibrateWiiScreen()
                return

            if self.wii.error:
                self.wii = None
                msgbox = QtWidgets.QMessageBox(self)
                msgbox.setWindowTitle(self.tr('Error'))
                msgbox.setText(self.tr("Error. Check your bluetooth driver"))
                msgbox.setModal(True)
                ret = msgbox.exec_()
                pBar.close()
                return

            if pBar.choice:
                if len(pool) == 1:
                    selectedMac = str(pool[0])
                    pBar.reInit(selectedMac)
                else:
                    item, ok = QtWidgets.QInputDialog.getItem(
                        self, self.tr("Warning"), self.tr("Choose device"),
                        pool, 0, False)
                    if ok:
                        selectedMac = str(item)
                        pBar.reInit(selectedMac)
                    else:
                        pBar.close()
                        return
Пример #6
0
    def run(self):
        """ Main Running loop controling bot mode and menu state """
        # Show state on OLED display
        self.show_message('Booting...')

        # Read config file FIRST
        self.read_config()

        self.show_message('Initialising Bluetooth...')

        # Never stop looking for wiimote.
        while not self.killed:
            if self.oled is not None:
                # Show state on OLED display
                self.oled.cls()  # Clear screen
                self.oled.canvas.text(
                    (10, 10),
                    'Waiting for WiiMote...',
                    fill=1)
                self.oled.canvas.text(
                    (10, 30),
                    '***Press 1+2 now ***',
                    fill=1)
                self.oled.display()

            self.wiimote = None
            try:
                self.wiimote = Wiimote()

            except WiimoteException:
                logging.error("Could not connect to wiimote. please try again")

            # Show state on OLED display
            self.show_menu()

            # Constantly check wiimote for button presses
            while self.wiimote:
                buttons_state = self.wiimote.get_buttons()
                classic_buttons_state = self.wiimote.get_classic_buttons()

                if buttons_state is not None:
                    if (buttons_state & cwiid.BTN_A and
                       self.challenge is None):
                        # Only works when NOT in a challenge
                        self.menu_item_pressed()
                        self.show_menu()

                    if (buttons_state & cwiid.BTN_B):
                        # Kill any previous Challenge / RC mode
                        # NOTE: will ALWAYS work
                        self.stop_threads()

                    if (buttons_state & cwiid.BTN_UP and
                       self.challenge is None):
                        # Only works when NOT in a challenge
                        self.menu_up()

                    if (buttons_state & cwiid.BTN_DOWN and
                       self.challenge is None):
                        # Only works when NOT in a challenge
                        self.menu_down()

                if classic_buttons_state is not None:
                    if (classic_buttons_state & cwiid.CLASSIC_BTN_ZL or
                            classic_buttons_state & cwiid.CLASSIC_BTN_ZR):
                        # One of the Z buttons pressed, disable
                        # motors and set neutral.
                        # NOTE: will ALWAYS work
                        self.core.enable_motors(False)
                    else:
                        # Neither Z buttons pressed,
                        # allow motors to move freely.
                        # NOTE: will ALWAYS work
                        self.core.enable_motors(True)

                time.sleep(0.05)

                # Verify Wiimote is connected each loop. If not, set wiimote
                # to None and it "should" attempt to reconnect.
                if not self.wiimote.wm:
                    self.stop_threads()
                    self.wiimote = None
Пример #7
0
    def run(self):
        """ Main Running loop controling bot mode and menu state """
        # Tell user how to connect wiimote
        self.lcd.clear()
        self.lcd.message('Press 1+2 \n')
        self.lcd.message('On Wiimote')

        # Initiate the drivetrain
        self.drive = drivetrain.DriveTrain(pwm_i2c=0x40)
        self.wiimote = None
        try:
            self.wiimote = Wiimote()

        except WiimoteException:
            logging.error("Could not connect to wiimote. please try again")

        if not self.wiimote:
            # Tell user how to connect wiimote
            self.lcd.clear()
            self.lcd.message('Wiimote \n')
            self.lcd.message('Not Found' + '\n')

        # Constantly check wiimote for button presses
        loop_count = 0
        while self.wiimote:
            buttons_state = self.wiimote.get_buttons()
            nunchuk_buttons_state = self.wiimote.get_nunchuk_buttons()
            joystick_state = self.wiimote.get_joystick_state()

            #            logging.info("joystick_state: {0}".format(joystick_state))
            #            logging.info("button state {0}".format(buttons_state))
            # Always show current menu item
            # logging.info("Menu: " + self.menu[self.menu_state])

            if loop_count >= self.lcd_loop_skip:
                # Reset loop count if over
                loop_count = 0

                self.lcd.clear()
                if self.shutting_down:
                    # How current menu item on LCD
                    self.lcd.message('Shutting Down Pi' + '\n')
                else:
                    # How current menu item on LCD
                    self.lcd.message(self.menu[self.menu_state] + '\n')

                    # If challenge is running, show it on line 2
                    if self.challenge:
                        self.lcd.message('[' + self.challenge_name + ']')

            # Increment Loop Count
            loop_count = loop_count + 1

            # Test if B button is pressed
            if joystick_state is None or (buttons_state & cwiid.BTN_B) or (
                    nunchuk_buttons_state & cwiid.NUNCHUK_BTN_Z):
                # No nunchuk joystick detected or B or Z button
                # pressed, must go into neutral for safety
                logging.info("Neutral")
                self.set_neutral(self.drive, self.wiimote)
            else:
                # Enable motors
                self.set_drive(self.drive, self.wiimote)

            if ((buttons_state & cwiid.BTN_A) or (buttons_state & cwiid.BTN_UP)
                    or (buttons_state & cwiid.BTN_DOWN)):
                # Looking for state change only
                if not self.menu_button_pressed and (buttons_state
                                                     & cwiid.BTN_A):
                    # User wants to select a menu item
                    self.menu_item_selected()
                elif not self.menu_button_pressed and (buttons_state
                                                       & cwiid.BTN_UP):
                    # Decrement menu index
                    self.menu_state = self.menu_state - 1
                    if self.menu_state < 0:
                        # Loop back to end of list
                        self.menu_state = len(self.menu) - 1
                    logging.info("Menu item: {0}".format(
                        self.menu[self.menu_state]))
                elif not self.menu_button_pressed and (buttons_state
                                                       & cwiid.BTN_DOWN):
                    # Increment menu index
                    self.menu_state = self.menu_state + 1
                    if self.menu_state >= len(self.menu):
                        # Loop back to start of list
                        self.menu_state = 0
                    logging.info("Menu item: {0}".format(
                        self.menu[self.menu_state]))

                # Only change button state AFTER we have used it
                self.menu_button_pressed = True
            else:
                # No menu buttons pressed
                self.menu_button_pressed = False

            time.sleep(0.05)
Пример #8
0
last_pressed = 0
DOUBLECLICK_THRESHOLD = 0.5


def button_pressed():
    global last_pressed
    # skip on doubleclick
    if time() - last_pressed < DOUBLECLICK_THRESHOLD:
        skipped()

    last_pressed = time()


if __name__ == '__main__':
    wiimote = Wiimote(button_callback=button_pressed)
    wiimote.pair()
    gait_analyzer = GaitAnalyzer(
        step_frequency_observer=step_frequency_changed, skip_observer=skipped)

    count = 0
    SMOOTHING_WINDOW = 2
    # array of SMOOTHING_WINDOW*2 + 1 values.
    values = []
    while True:
        if IS_SMOOTHING:
            values.append(wiimote.read_accelerometer())
            # print values
            if len(values) == SMOOTHING_WINDOW * 2 + 1:
                # smooth the values and extract x, y, z
                x = sum([x for x, y, z in values]) / len(values)