Esempio n. 1
0
    def __init__(self, parent, device_number):
        super(RippleEffectThread, self).__init__()

        self._logger = logging.getLogger(
            'razer.device{0}.ripplethread'.format(device_number))
        self._parent = parent

        self._colour = (255, 0, 255)
        self._refresh_rate = 0.100

        self._shutdown = False
        self._active = False

        self._kerboard_grid = KeyboardColour()
Esempio n. 2
0
    def __init__(self, parent, device_number):
        super(RippleEffectThread, self).__init__()

        self._logger = logging.getLogger("razer.device{0}.ripplethread".format(device_number))
        self._parent = parent

        self._colour = (0, 255, 0)
        self._refresh_rate = 0.100

        self._shutdown = False
        self._active = False

        self._kerboard_grid = KeyboardColour()
Esempio n. 3
0
class RippleEffectThread(threading.Thread):
    """
    Ripple thread.

    This thread contains the run loop which performs all the circle calculations and generating of the binary payload
    """
    def __init__(self, parent, device_number):
        super(RippleEffectThread, self).__init__()

        self._logger = logging.getLogger(
            'razer.device{0}.ripplethread'.format(device_number))
        self._parent = parent

        self._colour = (255, 0, 255)
        self._refresh_rate = 0.100

        self._shutdown = False
        self._active = False

        self._kerboard_grid = KeyboardColour()

    @property
    def shutdown(self):
        """
        Get the shutdown flag
        """
        return self._shutdown

    @shutdown.setter
    def shutdown(self, value):
        """
        Set the shutdown flag

        :param value: Shutdown
        :type value: bool
        """
        self._shutdown = value

    @property
    def active(self):
        """
        Get if the thread is active

        :return: Active
        :rtype: bool
        """
        return self._active

    @property
    def key_list(self):
        """
        Get key list

        :return: Key list
        :rtype: list
        """
        return self._parent.key_list

    def enable(self, colour, refresh_rate):
        """
        Enable the ripple effect

        If the colour tuple contains None then it will set the ripple to random colours
        :param colour: Colour tuple like (0, 255, 255)
        :type colour: tuple

        :param refresh_rate: Refresh rate in seconds
        :type refresh_rate: float
        """
        if colour[0] is None:
            self._colour = None
        else:
            self._colour = colour
        self._refresh_rate = refresh_rate
        self._active = True

    def disable(self):
        """
        Disable the ripple effect
        """
        self._active = False

    def run(self):
        """
        Event loop
        """
        # pylint: disable=too-many-nested-blocks,too-many-branches
        expire_diff = datetime.timedelta(seconds=2)

        # TODO time execution and then sleep for _refresh_rate - time_taken
        while not self._shutdown:
            if self._active:
                # Clear keyboard
                self._kerboard_grid.reset_rows()

                now = datetime.datetime.now()

                radiuses = []

                for expire_time, (key_row, key_col), colour in self.key_list:
                    event_time = expire_time - expire_diff

                    now_diff = now - event_time

                    # Current radius is based off a time metric
                    if self._colour is not None:
                        colour = self._colour
                    radiuses.append((key_row, key_col,
                                     now_diff.total_seconds() * 12, colour))

                for row in range(0, 7):
                    for col in range(0, 22):
                        if row == 0 and col == 20:
                            continue
                        if row == 6:
                            if col != 11:
                                continue
                            else:
                                # To account for logo placement
                                for cirlce_centre_row, circle_centre_col, rad, colour in radiuses:
                                    radius = math.sqrt(
                                        math.pow(cirlce_centre_row - row, 2) +
                                        math.pow(circle_centre_col - col, 2))
                                    if rad >= radius >= rad - 1:
                                        self._kerboard_grid.set_key_colour(
                                            0, 20, colour)
                                        break
                        else:
                            for cirlce_centre_row, circle_centre_col, rad, colour in radiuses:
                                radius = math.sqrt(
                                    math.pow(cirlce_centre_row - row, 2) +
                                    math.pow(circle_centre_col - col, 2))
                                if rad >= radius >= rad - 1:
                                    self._kerboard_grid.set_key_colour(
                                        row, col, colour)
                                    break

                payload = self._kerboard_grid.get_total_binary()

                self._parent.set_rgb_matrix(payload)
                self._parent.refresh_keyboard()

            time.sleep(self._refresh_rate)
Esempio n. 4
0
def upgrade_old_pref(config_version):
    print(" ** Upgrading configuration from v{0} to v{1}...".format(
        config_version, version))
    if config_version < 3:
        # *** "chroma_editor" group now "editor" ***
        data = load_file(path.preferences, True)
        data["editor"] = data["chroma_editor"]
        data.pop("chroma_editor")
        save_file(path.preferences, data)

        # *** Profiles now indexed, not based on file names. ***
        import time
        index = {}
        for profile in os.listdir(path.profile_folder):
            uid = int(time.time() * 1000000)
            old = os.path.join(path.profile_folder, profile)
            new = os.path.join(path.profile_folder, str(uid))
            os.rename(old, new)
            index[str(uid)] = {}
            index[str(uid)]["name"] = profile
        save_file(path.profiles, index)

        # *** Clear backups ***
        shutil.rmtree(path.profile_backups)
        os.mkdir(path.profile_backups)

    if config_version < 4:
        # *** Convert old serialised profile binary to JSON ***
        # Thanks to @terrycain for providing the conversion code.

        # Backup the old serialised versions, although they're useless now.
        if not os.path.exists(path.profile_backups):
            os.mkdir(path.profile_backups)

        # Load profiles and old index (meta data will now be part of that file)
        profiles = os.listdir(path.profile_folder)
        index = load_file(path.profiles, True)

        # Import daemon class required for conversion.
        from razer_daemon.keyboard import KeyboardColour

        for filename in profiles:
            # Get paths and backup.
            backup_path = os.path.join(path.profile_backups, filename)
            old_path = os.path.join(path.profile_folder, filename)
            new_path = os.path.join(path.profile_folder, filename + '.json')
            os.rename(old_path, backup_path)

            # Open the serialised format and export to JSON instead.
            blob = open(backup_path, 'rb').read()
            rgb_profile_object = KeyboardColour()
            rgb_profile_object.get_from_total_binary(blob)

            json_structure = {'rows': {}}
            for row_id, row in enumerate(rgb_profile_object.rows):
                row_id = str(row_id)  # JSON doesn't like numbered keys
                json_structure['rows'][row_id] = [rgb.get() for rgb in row]

            # Migrate index meta data, and save.
            uuid = os.path.basename(old_path)
            json_structure["name"] = index[uuid]["name"]
            json_structure["icon"] = index[uuid]["icon"]
            save_file(new_path, json_structure)

        # Delete index file as no longer needed.
        os.remove(path.profiles)

    # Ensure that new version number is written.
    pref_data = load_file(path.preferences, True)
    pref_data["config_version"] = version
    save_file(path.preferences, pref_data)

    print(" ** Configuration successfully upgraded.")
Esempio n. 5
0
class RippleEffectThread(threading.Thread):
    """
    Ripple thread.

    This thread contains the run loop which performs all the circle calculations and generating of the binary payload
    """

    def __init__(self, parent, device_number):
        super(RippleEffectThread, self).__init__()

        self._logger = logging.getLogger("razer.device{0}.ripplethread".format(device_number))
        self._parent = parent

        self._colour = (0, 255, 0)
        self._refresh_rate = 0.100

        self._shutdown = False
        self._active = False

        self._kerboard_grid = KeyboardColour()

    @property
    def shutdown(self):
        """
        Get the shutdown flag
        """
        return self._shutdown

    @shutdown.setter
    def shutdown(self, value):
        """
        Set the shutdown flag

        :param value: Shutdown
        :type value: bool
        """
        self._shutdown = value

    @property
    def active(self):
        """
        Get if the thread is active

        :return: Active
        :rtype: bool
        """
        return self._active

    @property
    def key_list(self):
        """
        Get key list

        :return: Key list
        :rtype: list
        """
        return self._parent.key_list

    def enable(self, colour, refresh_rate):
        """
        Enable the ripple effect

        If the colour tuple contains None then it will set the ripple to random colours
        :param colour: Colour tuple like (0, 255, 255)
        :type colour: tuple

        :param refresh_rate: Refresh rate in seconds
        :type refresh_rate: float
        """
        if colour[0] is None:
            self._colour = None
        else:
            self._colour = colour
        self._refresh_rate = refresh_rate
        self._active = True

    def disable(self):
        """
        Disable the ripple effect
        """
        self._active = False

    def run(self):
        """
        Event loop
        """
        # pylint: disable=too-many-nested-blocks,too-many-branches
        expire_diff = datetime.timedelta(seconds=2)

        # TODO time execution and then sleep for _refresh_rate - time_taken
        while not self._shutdown:
            if self._active:
                # Clear keyboard
                self._kerboard_grid.reset_rows()

                now = datetime.datetime.now()

                radiuses = []

                for expire_time, (key_row, key_col), colour in self.key_list:
                    event_time = expire_time - expire_diff

                    now_diff = now - event_time

                    # Current radius is based off a time metric
                    if self._colour is not None:
                        colour = self._colour
                    radiuses.append((key_row, key_col, now_diff.total_seconds() * 12, colour))

                for row in range(0, 7):
                    for col in range(0, 22):
                        if row == 0 and col == 20:
                            continue
                        if row == 6:
                            if col != 11:
                                continue
                            else:
                                # To account for logo placement
                                for cirlce_centre_row, circle_centre_col, rad, colour in radiuses:
                                    radius = math.sqrt(
                                        math.pow(cirlce_centre_row - row, 2) + math.pow(circle_centre_col - col, 2)
                                    )
                                    if rad >= radius >= rad - 1:
                                        self._kerboard_grid.set_key_colour(0, 20, colour)
                                        break
                        else:
                            for cirlce_centre_row, circle_centre_col, rad, colour in radiuses:
                                radius = math.sqrt(
                                    math.pow(cirlce_centre_row - row, 2) + math.pow(circle_centre_col - col, 2)
                                )
                                if rad >= radius >= rad - 1:
                                    self._kerboard_grid.set_key_colour(row, col, colour)
                                    break

                payload = self._kerboard_grid.get_total_binary()

                self._parent.set_rgb_matrix(payload)
                self._parent.refresh_keyboard()

            time.sleep(self._refresh_rate)