Exemple #1
0
    def init_command(self, init_list):
        """
        :param init_list: The list of pins to be initialized.
        :return: True for successful execution.
        Creates the initialization command for later execution.
        """
        if isinstance(init_list, list):
            if all(isinstance(command, tuple) for command in init_list):
                if all(len(command) == 2 for command in init_list):
                    if all(
                            isinstance(command[0], numbers.Number)
                            for command in init_list):
                        if all(
                                isinstance(command[1], bool)
                                for command in init_list):
                            if self.servos:
                                for command in init_list:
                                    pin = command[0]
                                    servo = next(servo for servo in self.servos
                                                 if servo.pin == pin)

                                    if servo:
                                        init_list = [
                                            command for command in init_list
                                            if command[0] != pin
                                        ]

                                        if command[1]:
                                            if Controller.execute_servo(
                                                    servo, servo.init_pulse):
                                                self.error(
                                                    'Execution of single command was successful'
                                                )
                                            else:
                                                self.error(
                                                    'Execution of single command failed'
                                                )
                                                return False
                                        else:
                                            if Controller.add_servo_to_stack(
                                                    servo, servo.init_pulse):
                                                self.error(
                                                    'Command was added to Execution-list.'
                                                )
                                            else:
                                                self.error(
                                                    'Command could not be added to Execution-list.'
                                                )
                                                return False
                                    else:
                                        self.error('Desired servo with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                            if self.motors:
                                for command in init_list:
                                    pin = command[0]
                                    motor = next(motor for motor in self.motors
                                                 if motor.pin == pin)

                                    if motor:
                                        if command[1]:
                                            if Controller.execute_motor(
                                                    motor,
                                                    motor.init_direction,
                                                    motor.init_percentage):
                                                self.error(
                                                    'Execution of single command was successful'
                                                )
                                            else:
                                                self.error(
                                                    'Execution of single command failed'
                                                )
                                                return False
                                        else:
                                            if Controller.add_motor_to_stack(
                                                    motor,
                                                    motor.init_direction,
                                                    motor.init_percentage):
                                                self.error(
                                                    'Command was added to Execution-list.'
                                                )
                                            else:
                                                self.error(
                                                    'Command could not be added to Execution-list.'
                                                )
                                                return False
                                    else:
                                        self.error('Desired motor with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                            if self.servos or self.motors:
                                return True
                            else:
                                self.error('No servo or motor list available.')
                                return False
                        else:
                            self.error(
                                'Unexpected tuple element. Second element must be a boolean.'
                            )
                            return False
                    else:
                        self.error(
                            'Unexpected tuple element. First element must be a number'
                        )
                        return False
                else:
                    self.error('Unexpected tuple size. Expected tuple of two')
                    return False
            else:
                self.error('Unexpected list element. Expected list of tuples')
                return False
        else:
            self.error('Unexpected input: ' + type(init_list).__name__ +
                       '. Expected: list')
            return False
Exemple #2
0
    def move_servo(self, command_list):
        """
        :param command_list: List of commands to be executed.
        :return: True for successful execution.
        Move servos according to the command_list.
        """
        if self.servos:
            if isinstance(command_list, list):
                if all(isinstance(command, tuple) for command in command_list):
                    if all(len(command) == 3 for command in command_list):
                        if all(
                                isinstance(number, numbers.Number)
                                for command in command_list
                                for number in command[:1]):
                            if all(
                                    isinstance(command[2], bool)
                                    for command in command_list):
                                for command in command_list:
                                    pin = command[0]
                                    degree = command[1]
                                    servo = next(servo for servo in self.servos
                                                 if servo.pin == pin)

                                    if servo:
                                        if servo.min_pulse <= degree <= servo.max_pulse:
                                            if command[2]:
                                                if Controller.execute_servo(
                                                        servo, degree):
                                                    self.error(
                                                        'Execution of single command was successfull'
                                                    )
                                                else:
                                                    self.error(
                                                        'Execution of single command failed'
                                                    )
                                                    return False
                                            else:
                                                if Controller.add_servo_to_stack(
                                                        servo, degree):
                                                    self.error(
                                                        'Command was added to Execution-list.'
                                                    )
                                                else:
                                                    self.error(
                                                        'Command could not be added to Execution-list.'
                                                    )
                                                    return False
                                        else:
                                            self.error(
                                                str(degree) +
                                                ' is not allowed.')
                                            return False
                                    else:
                                        self.error('Desired servo with pin ' +
                                                   str(pin) +
                                                   ' is not available.')
                                        return False

                                return True

                            else:
                                self.error(
                                    'Unexpected tuple element. Last Element must be a boolean.'
                                )
                                return False
                        else:
                            self.error(
                                'Unexpected tuple element. First two items must be numbers'
                            )
                            return False
                    else:
                        self.error(
                            'Unexpected tuple size. Expected tuple of three')
                        return False
                else:
                    self.error(
                        'Unexpected list element. Expected list of tuples')
                    return False
            else:
                self.error('Unexpected input: ' + type(command_list).__name__ +
                           '. Expected: list')
                return False
        else:
            self.error('Component does not contain any servos.')
            return False