Esempio n. 1
0
    def get_data(self, function_id: FunctionId or str or int,
                 speed: int) -> dict:
        """
        Generates a binary data packet containing the request to set a function
        
        :param function_id: ID of the function 
        :param speed: function speed [0..255] 0 is slow, 255 is fast
        :return: binary data packet
        """

        from sunix_ledstrip_controller_client import functions

        # try to accept str and int types
        if isinstance(function_id, str):
            function_id = FunctionId[function_id]
        if isinstance(function_id, int):
            function_id = FunctionId(function_id)

        if not functions.is_valid(function_id):
            raise ValueError("Invalid function id")

        if speed < 0 or speed > 255:
            raise ValueError("Invalid speed value! Expected 0-255, got: %d" %
                             speed)

        params = dict(packet_id=0x61,
                      function_id=function_id.value,
                      speed=255 - speed,
                      remote_or_local=0x0F,
                      checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 2
0
    def get_rgbww_data(self, red: int, green: int, blue: int, warm_white: int,
                       cold_white: int) -> dict:
        """
        Generates a binary data packet containing the request to change colors for all 5 channels
        
        :param red: red amount 
        :param green: green amount 
        :param blue: blue amount
        :param warm_white: warm white amount 
        :param cold_white: cold white amount
        :return: binary data packet
        """
        params = dict(packet_id=0x31,
                      red=red,
                      green=green,
                      blue=blue,
                      warm_white=warm_white,
                      cold_white=cold_white,
                      rgbww_selection=0xFF,
                      remote_or_local=0x0F,
                      checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 3
0
    def get_data(self) -> dict:
        """
        Generates a binary data packet containing the a request for the current state of the controller
        :return: binary data packet
        """
        params = dict(packet_id=0x81, payload1=0x8A, payload2=0x8B, checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 4
0
    def get_data(self, on: bool) -> dict:
        """
        Generates a binary data packet containing the request to change the power state of the controller

        :param on: True if the controller should turn on, False for turning off
        :return: binary data packet
        """

        from sunix_ledstrip_controller_client.controller import Controller
        params = dict(packet_id=0x71,
                      power_status=Controller.POWER_STATE_ON
                      if on else Controller.POWER_STATE_OFF,
                      remote_or_local=0x0F,
                      checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 5
0
    def get_data(self, dt: datetime) -> dict:
        """
        Generates a binary data packet containing the a request for the current time of the controller
        :return: binary data packet
        """
        params = dict(packet_id=0x10,
                      payload1=0x14,
                      year=dt.year - 2000,
                      month=dt.month,
                      day=dt.day,
                      hour=dt.hour,
                      minute=dt.minute,
                      second=dt.second,
                      weekday=dt.isoweekday(),
                      payload2=0x00,
                      remote_or_local=0x0F,
                      checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 6
0
    def get_ww_data(self, warm_white: int, cold_white: int) -> dict:
        """
        Generates a binary data packet containing the request to change ww colors

        :param warm_white: warm white amount 
        :param cold_white: cold white amount
        :return: binary data packet
        """

        params = dict(packet_id=0x31,
                      red=0,
                      green=0,
                      blue=0,
                      warm_white=warm_white,
                      cold_white=cold_white,
                      rgbww_selection=0x0F,
                      remote_or_local=0x0F,
                      checksum=0)

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)
Esempio n. 7
0
    def get_data(self, colors: [(int, int, int, int)], speed: int,
                 transition_type: TransitionType) -> dict:
        """
        Generates a binary data packet containing the request to set a function

        :param colors: a list of color tuples of the form (red, green, blue) or (red, green, blue, unknown).
                       I couldn't figure out what the last parameter is used for so the rgb is a shortcut.
        :param transition_type: the transition type between colors
        :param speed: function speed [0..255] 0 is slow, 255 is fast
        :return: binary data packet
        """

        # do a little input validation
        if len(colors) > 16:
            raise ValueError(
                "Only up to 16 color states are supported! You provided %d :("
                % len(colors))

        for color in colors:
            if len(color) is not 3 and len(color) is not 4:
                raise ValueError(
                    "Unexpected tuple size %d in color %s! Expected: 3 or 4" %
                    (len(color), str(color)))

        if speed < 0 or speed > 255:
            raise ValueError("Invalid speed value! Expected 0-255, got: %d" %
                             speed)

        processed_colors = []

        # set default values
        for i in range(16):
            processed_colors.append([0x01, 0x02, 0x03, 0x00])

        index_red = 0
        index_green = 1
        index_blue = 2
        index_brightness = 3

        # apply colors from arguments
        for color_idx, color in enumerate(colors):
            for channel_idx, value in enumerate(color):
                processed_colors[color_idx][channel_idx] = value

        params = dict(
            packet_id=0x51,

            # config data
            speed=255 - speed,
            transition_type=transition_type.value,
            rgbww_selection=0xFF,
            remote_or_local=0x0F,
            checksum=0)

        # append color data to dictionary
        for idx, color in enumerate(processed_colors):
            idx += 1
            params["red_%d" % idx] = color[index_red]
            params["green_%d" % idx] = color[index_green]
            params["blue_%d" % idx] = color[index_blue]
            params["unknown_%d" % idx] = color[index_brightness]

        checksum = _calculate_checksum(params)
        params["checksum"] = checksum

        return self.build(params)