Example #1
0
    def set_colors(self, mode, speed, left_colors, right_colors=None):
        """
            Change the colors and the mode of the keyboard.

            + The available modes are: 'fixed', 'morph', 'blink'
                'fixed' and 'blink' only takes left_colors

            + Speed must be an integer. 1 =< speed =< 256

            + left_colors and right_colors can be a single hex color or a list of hex_colors.
              If both arguments are used, they must have the same number of items.
        """

        if mode not in ('fixed', 'morph', 'blink'):
            print_warning("Wrong mode" + str(mode))
            return
        elif not isinstance(speed, int):
            print_warning("The speed argument must be an integer.")
            return
        elif speed > 255:
            print_warning("The speed argument exeeds the limit > 255.")
            return
        elif speed < 1:
            print_warning("The speed argument must be >= 1.")
            return

        if not isinstance(left_colors, list) and not isinstance(
                left_colors, tuple):
            left_colors = [left_colors]

        if right_colors is None:
            right_colors = left_colors

        elif not isinstance(right_colors, list) and not isinstance(
                right_colors, tuple):
            right_colors = [right_colors]

        if len(left_colors) != len(right_colors):
            print_warning("The colors lists must have the same lenght.")
            return

        for color_list in (left_colors, right_colors):
            for color in color_list:
                if not string_is_hex_color(color):
                    print_warning(
                        "The colors argument must only contain hex colors. The color={} is not valid."
                        .format(color))
                    return

        self._controller.erase_config()

        for save, block in self._COMPUTER_BLOCKS_TO_SAVE:

            self._controller.add_block_line(save, block)
            self._controller.add_reset_line(self._computer.RESET_ALL_LIGHTS_ON)
            self._controller.add_speed_line(speed)

            for region in self._computer.get_regions():
                for i, (left_color, right_color) in enumerate(
                        zip(left_colors, right_colors)):

                    if i + 1 > region.max_commands:
                        print_warning(
                            "The number of maximum commands for the region={} have been exeed. The loop was stopped at {}."
                            .format(region.name, i + 1))
                        break

                    if mode == 'blink':
                        if region.can_blink:
                            self._controller.add_color_line(
                                region.hex_id, 'blink', left_color,
                                right_color)
                        else:
                            self._controller.add_color_line(
                                region.hex_id, 'fixed', left_color)
                            print_warning(
                                "The mode=blink is not supported for the region={}, the mode=fixed will be used instead."
                                .format(region.name))

                    elif mode == 'morph':
                        if region.can_morph:
                            self._controller.add_color_line(
                                region.hex_id, 'morph', left_color,
                                right_color)
                        else:
                            self._controller.add_color_line(
                                region.hex_id, 'fixed', left_color)
                            print_warning(
                                "The mode=morph is not supported for the region={}, the mode=fixed will be used instead."
                                .format(region.name))

                    else:
                        self._controller.add_color_line(
                            region.hex_id, 'fixed', left_color)

                self._controller.end_colors_line()

            self._controller.end_block_line()

        self._controller.apply_config()

        self._lights_state = True
Example #2
0
File: Theme.py Project: d0x90/akbl
    def load(self, path):

        print_debug('path=`{}`'.format(path))

        with open(path, encoding='utf-8', mode='rt') as f:
            lines = f.readlines()

        area_found, left_color, right_color, mode, = False, False, False, False

        imported_areas = []
        supported_region_names = self._computer.get_supported_regions_name()
        #print_debug('supported_region_names=`{}`'.format(supported_region_names))

        # Parse the configuration file
        #
        for line in lines:
            if line.strip():
                variables = line.strip().split('=')

                if len(variables) == 2:

                    var_name = variables[0]
                    var_arg = variables[1]

                    if var_name == 'name':
                        if var_arg == '':
                            name = os.path.basename(path)
                        else:
                            name = var_arg

                        if name.endswith('.cfg'):
                            name = name[:-4]

                        self.name = name

                    elif var_name == 'speed':
                        self.set_speed(var_arg)

                    elif var_name == 'area':
                        area_name = var_arg

                        if area_name in supported_region_names:
                            area_found = True
                            imported_areas.append(area_name)
                            region = self._computer.get_region_by_name(
                                area_name)
                            area = Area()
                            area.init_from_region(region)
                            self.add_area(area)
                        else:
                            area_found = False
                            print_warning(
                                "area.name `{}` not listed on computer regions names"
                                .format(area_name))

                    elif var_name in (
                            'type',
                            'mode'):  # `type`is to give support to old themes
                        mode = var_arg
                        if mode not in ('fixed', 'morph', 'blink'):
                            default_mode = self._computer.DEFAULT_MODE
                            print_warning(
                                'wrong mode=`{}` when importing theme. Using default mode=`{}`'
                                .format(mode, default_mode))
                            mode = default_mode

                    elif var_name in (
                            'color', 'color1', 'left_color'
                    ):  # `color` & `color1`are to give support to old themes

                        if string_is_hex_color(var_arg):
                            left_color = var_arg
                        else:

                            if area_found:
                                area_name = area.name
                            else:
                                area_name = "?"

                            print_warning(
                                "Wrong left hex_color={} for area={}, the default hex_color= will be used instead."
                                .format(left_color, area_name,
                                        self._computer.DEFAULT_COLOR))
                            left_color = self._computer.DEFAULT_COLOR

                    elif var_name in (
                            'color2', 'right_color'
                    ):  # `color2` is to give support to old themes

                        if string_is_hex_color(var_arg):
                            right_color = var_arg
                        else:

                            if area_found:
                                area_name = area.name
                            else:
                                area_name = "?"

                            print_warning(
                                "Wrong left hex_color={} for area={}, the default hex_color={} will be used instead."
                                .format(right_color, area_name,
                                        self._computer.DEFAULT_COLOR))
                            right_color = self._computer.DEFAULT_COLOR

                    if area_found and left_color and right_color and mode:

                        #print_debug('adding Zone to Area, mode=`{}`, left_color=`{}`, right_color=`{}`'.format(mode, left_color, right_color))

                        zone = Zone(mode=mode,
                                    left_color=left_color,
                                    right_color=right_color)
                        area.add_zone(zone)

                        left_color, right_color, mode = False, False, False

        # Add areas in case they be missing
        #
        for area_name in supported_region_names:
            if area_name not in imported_areas:

                region = self._computer.get_region_by_name(area_name)
                area = Area()
                area.init_from_region(region)

                zone = Zone(mode=self._computer.DEFAULT_MODE,
                            left_color=self._computer.DEFAULT_COLOR,
                            right_color=self._computer.DEFAULT_COLOR)
                area.add_zone(zone)
                self.add_area(area)
                print_warning("missing area.name=`{}` on theme=`{}`".format(
                    area_name, self.name))
                print_debug(
                    'adding Zone to the missing Area, mode=`{}`, left_color=`{}`, right_color=`{}`'
                    .format(zone.get_mode(), zone.get_left_color(),
                            zone.get_right_color()))

        print_debug(self)

        #
        # Add the configuration
        #
        AVAILABLE_THEMES[self.name] = self