Ejemplo n.º 1
0
class LEDmatrixConnect(object):
    """
    init() wird automatisch nach der Erzeugung der Instanz aufgerufen. (-> magische Methode)
    Erstellt ein device Objekt und baut die Verbindung zu brickd auf. (Verbindung zu Master Brick????)
    """
    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect[POST])
    def __init__(self):

        # Attribute importiert
        self.__UIDmaster = matrixProperties.UIDmaster  # "6et15y"
        self.__UIDbricklet = matrixProperties.UIDbricklet  # "wVj"
        self.__HOST = matrixProperties.HOST  # "localhost"
        self.__PORT = matrixProperties.PORT  # 4223
        self.__matrixIndexRed = matrixProperties.IndexRed  # 0
        self.__matrixIndexGreen = matrixProperties.IndexGreen  # 1
        self.__matrixIndexBlue = matrixProperties.IndexBlue  # 2
        self.__rows = matrixProperties.ROWS  # 10
        self.__columns = matrixProperties.COLUMNS  # 20
        self.__LEDnr = matrixProperties.LEDnr  # Liste LED-Nummern 0 bis 199
        self.__numLEDS = matrixProperties.NUM_LEDS  # 16
        self.__rgb = [[0 for i in range(self.__numLEDS)] for i in range(3)]  # RGB-Array

        self.__image = matrixProperties.defaultImage  # Default-Bild setzen
        self.__imageIndexRed = 0
        self.__imageIndexGreen = 1
        self.__imageIndexBlue = 2

        # Instanzvariablen
        self.__light = True  # Boolean: True / False

        # Objekte 
        print("Instanz LEDmatrix wurde erstellt.")
        self.__ipcon = IPConnection()  # Create IP connection
        self.__masterBrick = BrickMaster(self.__UIDmaster, self.__ipcon)
        self.__brickletLEDstrip = BrickletLEDStrip(self.__UIDbricklet, self.__ipcon)  # Create device object
        # Connect to brickd --> Verbindung Masterbrick? richtiger RGB-Kanal einstellen?
        self.__ipcon.connect(self.__HOST,
                             self.__PORT)
        print("Verbindung zu brickd hergestellt.")
        self.__brickletLEDstrip.set_frame_duration(20)  # Anzeigedauer Bild: 20ms ergibt 20 Bilder pro Sekunde
        self.__brickletLEDstrip.set_channel_mapping(self.__brickletLEDstrip.CHANNEL_MAPPING_RGB)  # CHANNEL_MAPPING_RGB = 6 -> Farbkanal definieren (RGB statt BGR)

        # Callback starten
        self.__brickletLEDstrip.register_callback(self.__brickletLEDstrip.CALLBACK_FRAME_RENDERED,
                                                  lambda x: self.__loadPicture__())

        print("Callback aktiviert.")

    # ======= private Methoden ==========================================================================
    # fillRGBs(), fillAsc(), fillDesc(), loadPicture()



    def __fillRGBs__(self, yMatrix, xImage, yImage):
        """ Private Methode: Füllt die Farbinformationen für einen Pixel in die einzelnen RGB-Arrays ab. """
        self.__rgb[self.__matrixIndexRed][yMatrix] = self.__image[xImage][yImage][self.__imageIndexRed]
        self.__rgb[self.__matrixIndexGreen][yMatrix] = self.__image[xImage][yImage][self.__imageIndexGreen]
        self.__rgb[self.__matrixIndexBlue][yMatrix] = self.__image[xImage][yImage][self.__imageIndexBlue]

    def __fillAsc__(self, xImage, yMatrix):
        """ Private Methode: Füllt eine Spalte für die LED-Matrix aus (von unten nach oben) """  # 0 bis 9
        yImage = 0
        i = 0
        while (i < self.__rows):  # i < 10
            self.__fillRGBs__(yMatrix, xImage, yImage)
            i += 1
            yImage += 1
            yMatrix += 1
            if (yMatrix >= self.__rows):
                yMatrix = 0

    def __fillDesc__(self, xImage, yMatrix):
        """ Private Methode: Füllt eine Spalte für die LED-Matrix aus (von oben nach unten) """  # 9 bis 0
        yImage = 0
        i = 0
        while (i < self.__rows):  # i < 10
            self.__fillRGBs__(yMatrix, xImage, yImage)
            i += 1
            yImage += 1
            yMatrix -= 1
            if (yMatrix < 0):
                yMatrix = self.__rows - 1

    def __loadPicture__(self):
        """ Methode lädt ein ganzes Bild, sofern self.__light auf True gesetzt ist => Licht an. """

        if (self.__light is True):

            xMatrix = 0
            yMatrix = 0
            xImage = 0
            columns = self.__columns  # 20
            while (columns > 0):
                if (xMatrix % 2 == 0):  # Farbwerte von image-array nach rgb-array bei gerader Spalte
                    y = 0
                    self.__fillAsc__(xImage, yMatrix)
                else:  # Farbwerte von image-array nach rgb-array bei ungerader Spalte
                    y = self.__rows - 1
                    self.__fillDesc__(xImage, self.__rows - (yMatrix + 1))

                # Aufrufen der Methode set_rgb_values(index, length, r, g, b). (r, g, b, verlangen 16er-Array's)
                self.__brickletLEDstrip.set_rgb_values(self.__LEDnr[xMatrix][y], self.__rows,  # self.__rows = 10
                                                       self.__rgb[self.__matrixIndexRed],
                                                       self.__rgb[self.__matrixIndexGreen],
                                                       self.__rgb[self.__matrixIndexBlue])

                columns -= 1
                xImage += 1
                xMatrix += 1
                if xMatrix >= self.__columns:
                    xMatrix = 0
                    xImage = 0

    # ======= public Methoden ==================================================
    # connect(), finish(), setVelocity(), lightOff(), setImage(), getImage()

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/connect[PUT])
    def connect(self):
        """ Verbindungsaufbau """
        self.__ipcon.connect(self.__HOST, self.__PORT)
        print("Verbindung zu brickd hergestellt.")

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/finish[PUT])
    def finish(self):
        """ Beendet die Verbindung. """
        # input("Press key to exit\n")
        self.__ipcon.disconnect()
        print("Verbindung beendet.")

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setVelocity/<int:frequenz>[PUT])
    def setFrameDuration(self, millisec: int = 20):
        """ Set frame duration to 50ms (20 frames per second).
        Verändert die Anzeigedauer eines Frames.
        :param millisec: int
        """
        self.__brickletLEDstrip.set_frame_duration(millisec)

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/getFrameDuration[GET])
    def getFrameDuration(self) -> int:
        """ Gibt einen int-Wert zurück. Anzeigedauer der Frames auf dem LED-Display.
        (20ms entspricht 50 Bilder/Sekunde)
        :return frame duration: int
        """
        return self.__brickletLEDstrip.get_frame_duration()

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/light/<boolean:status>[PUT])
    def light(self, status: bool = True):  # status = boolean True / False
        """ Licht-Regelung auf 'unterster' Stufe, wirkt sich auf den gesamten LED-Display.
        True = 'Licht an', False = 'Licht aus'
        :param status: boolean
        """
        self.__light = status

        time.sleep(1)

        if (status is False):
            i = 0
            while i < self.__columns:  # hier 20
                self.__brickletLEDstrip.set_rgb_values(i * 10, 10, [0] * self.__numLEDS, [0] * self.__numLEDS,
                                                       [0] * self.__numLEDS)
                i += 1

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setImage/<list:image>[PUT])
    def setImage(self, image: list):
        """ Liste mit den RGB-Werten der
        Form [[[red, green, blue] for i in range(x columns)] for i in range(y rows)]
        :param image: list
        """
        self.__image = image

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setImageRGBindex/<int:red>/<int:green>/<int:blue>[PUT])
    def setImageRGBindex(self, red: int, green: int, blue: int):
        """ Werte: 0 bis 2, Default: red = 0, green = 1, blue = 2
        :param red: int
        :param green: int
        :param blue: int
        """
        self.__imageIndexRed = red
        self.__imageIndexGreen = green
        self.__imageIndexBlue = blue
Ejemplo n.º 2
0
class LEDmatrixConnect(object):
    """
    init() wird automatisch nach der Erzeugung der Instanz aufgerufen. (-> magische Methode)
    Erstellt ein device Objekt und baut die Verbindung zu brickd auf. (Verbindung zu Master Brick????)
    """

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect[POST])
    def __init__(self):

        # Attribute importiert
        self.__UIDmaster = matrixProperties.UIDmaster  # "6et15y"
        self.__UIDbricklet = matrixProperties.UIDbricklet  # "wVj"
        self.__HOST = matrixProperties.HOST  # "localhost"
        self.__PORT = matrixProperties.PORT  # 4223
        self.__matrixIndexRed = matrixProperties.IndexRed  # 0
        self.__matrixIndexGreen = matrixProperties.IndexGreen  # 1
        self.__matrixIndexBlue = matrixProperties.IndexBlue  # 2
        self.__rows = matrixProperties.ROWS  # 10
        self.__columns = matrixProperties.COLUMNS  # 20
        self.__LEDnr = matrixProperties.LEDnr  # Liste LED-Nummern 0 bis 199
        self.__numLEDS = matrixProperties.NUM_LEDS  # 16
        self.__rgb = [[0 for i in range(self.__numLEDS)]
                      for i in range(3)]  # RGB-Array

        self.__image = matrixProperties.defaultImage  # Default-Bild setzen
        self.__imageIndexRed = 0
        self.__imageIndexGreen = 1
        self.__imageIndexBlue = 2

        # Instanzvariablen
        self.__light = True  # Boolean: True / False

        # Objekte
        print("Instanz LEDmatrix wurde erstellt.")
        self.__ipcon = IPConnection()  # Create IP connection
        self.__masterBrick = BrickMaster(self.__UIDmaster, self.__ipcon)
        self.__brickletLEDstrip = BrickletLEDStrip(
            self.__UIDbricklet, self.__ipcon)  # Create device object
        # Connect to brickd --> Verbindung Masterbrick? richtiger RGB-Kanal einstellen?
        self.__ipcon.connect(self.__HOST, self.__PORT)
        print("Verbindung zu brickd hergestellt.")
        self.__brickletLEDstrip.set_frame_duration(
            20)  # Anzeigedauer Bild: 20ms ergibt 20 Bilder pro Sekunde
        self.__brickletLEDstrip.set_channel_mapping(
            self.__brickletLEDstrip.CHANNEL_MAPPING_RGB
        )  # CHANNEL_MAPPING_RGB = 6 -> Farbkanal definieren (RGB statt BGR)

        # Callback starten
        self.__brickletLEDstrip.register_callback(
            self.__brickletLEDstrip.CALLBACK_FRAME_RENDERED,
            lambda x: self.__loadPicture__())

        print("Callback aktiviert.")

    # ======= private Methoden ==========================================================================
    # fillRGBs(), fillAsc(), fillDesc(), loadPicture()

    def __fillRGBs__(self, yMatrix, xImage, yImage):
        """ Private Methode: Füllt die Farbinformationen für einen Pixel in die einzelnen RGB-Arrays ab. """
        self.__rgb[self.__matrixIndexRed][yMatrix] = self.__image[xImage][
            yImage][self.__imageIndexRed]
        self.__rgb[self.__matrixIndexGreen][yMatrix] = self.__image[xImage][
            yImage][self.__imageIndexGreen]
        self.__rgb[self.__matrixIndexBlue][yMatrix] = self.__image[xImage][
            yImage][self.__imageIndexBlue]

    def __fillAsc__(self, xImage, yMatrix):
        """ Private Methode: Füllt eine Spalte für die LED-Matrix aus (von unten nach oben) """  # 0 bis 9
        yImage = 0
        i = 0
        while (i < self.__rows):  # i < 10
            self.__fillRGBs__(yMatrix, xImage, yImage)
            i += 1
            yImage += 1
            yMatrix += 1
            if (yMatrix >= self.__rows):
                yMatrix = 0

    def __fillDesc__(self, xImage, yMatrix):
        """ Private Methode: Füllt eine Spalte für die LED-Matrix aus (von oben nach unten) """  # 9 bis 0
        yImage = 0
        i = 0
        while (i < self.__rows):  # i < 10
            self.__fillRGBs__(yMatrix, xImage, yImage)
            i += 1
            yImage += 1
            yMatrix -= 1
            if (yMatrix < 0):
                yMatrix = self.__rows - 1

    def __loadPicture__(self):
        """ Methode lädt ein ganzes Bild, sofern self.__light auf True gesetzt ist => Licht an. """

        if (self.__light is True):

            xMatrix = 0
            yMatrix = 0
            xImage = 0
            columns = self.__columns  # 20
            while (columns > 0):
                if (
                        xMatrix % 2 == 0
                ):  # Farbwerte von image-array nach rgb-array bei gerader Spalte
                    y = 0
                    self.__fillAsc__(xImage, yMatrix)
                else:  # Farbwerte von image-array nach rgb-array bei ungerader Spalte
                    y = self.__rows - 1
                    self.__fillDesc__(xImage, self.__rows - (yMatrix + 1))

                # Aufrufen der Methode set_rgb_values(index, length, r, g, b). (r, g, b, verlangen 16er-Array's)
                self.__brickletLEDstrip.set_rgb_values(
                    self.__LEDnr[xMatrix][y],
                    self.__rows,  # self.__rows = 10
                    self.__rgb[self.__matrixIndexRed],
                    self.__rgb[self.__matrixIndexGreen],
                    self.__rgb[self.__matrixIndexBlue])

                columns -= 1
                xImage += 1
                xMatrix += 1
                if xMatrix >= self.__columns:
                    xMatrix = 0
                    xImage = 0

    # ======= public Methoden ==================================================
    # connect(), finish(), setVelocity(), lightOff(), setImage(), getImage()

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/connect[PUT])
    def connect(self):
        """ Verbindungsaufbau """
        self.__ipcon.connect(self.__HOST, self.__PORT)
        print("Verbindung zu brickd hergestellt.")

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/finish[PUT])
    def finish(self):
        """ Beendet die Verbindung. """
        # input("Press key to exit\n")
        self.__ipcon.disconnect()
        print("Verbindung beendet.")

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setVelocity/<int:frequenz>[PUT])
    def setFrameDuration(self, millisec: int = 20):
        """ Set frame duration to 50ms (20 frames per second).
        Verändert die Anzeigedauer eines Frames.
        :param millisec: int
        """
        self.__brickletLEDstrip.set_frame_duration(millisec)

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/getFrameDuration[GET])
    def getFrameDuration(self) -> int:
        """ Gibt einen int-Wert zurück. Anzeigedauer der Frames auf dem LED-Display.
        (20ms entspricht 50 Bilder/Sekunde)
        :return frame duration: int
        """
        return self.__brickletLEDstrip.get_frame_duration()

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/light/<boolean:status>[PUT])
    def light(self, status: bool = True):  # status = boolean True / False
        """ Licht-Regelung auf 'unterster' Stufe, wirkt sich auf den gesamten LED-Display.
        True = 'Licht an', False = 'Licht aus'
        :param status: boolean
        """
        self.__light = status

        time.sleep(1)

        if (status is False):
            i = 0
            while i < self.__columns:  # hier 20
                self.__brickletLEDstrip.set_rgb_values(i * 10, 10,
                                                       [0] * self.__numLEDS,
                                                       [0] * self.__numLEDS,
                                                       [0] * self.__numLEDS)
                i += 1

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setImage/<list:image>[PUT])
    def setImage(self, image: list):
        """ Liste mit den RGB-Werten der
        Form [[[red, green, blue] for i in range(x columns)] for i in range(y rows)]
        :param image: list
        """
        self.__image = image

    # @LEDmatrix.route('ledmatrix.bfh.ch/api/v1.0/LEDmatrixConnect/setImageRGBindex/<int:red>/<int:green>/<int:blue>[PUT])
    def setImageRGBindex(self, red: int, green: int, blue: int):
        """ Werte: 0 bis 2, Default: red = 0, green = 1, blue = 2
        :param red: int
        :param green: int
        :param blue: int
        """
        self.__imageIndexRed = red
        self.__imageIndexGreen = green
        self.__imageIndexBlue = blue
Ejemplo n.º 3
0
class LightingModule(StateModule):
    menu_title = "Lighting"

    outputs = {}
    edit = False
    device = None
    current = 0
    intens = 100
    color = 0
    colors = [
        {
            "name": "Warm",
            "color": (255, 160, 25)
        },
        {
            "name": "Cool",
            "color": (255, 255, 50)
        },
        {
            "name": "Red",
            "color": (255, 0, 0)
        },
        {
            "name": "Blue",
            "color": (0, 0, 255)
        },
        {
            "name": "Magenta",
            "color": (255, 0, 255)
        },
        {
            "name": "Green",
            "color": (0, 255, 0)
        },
    ]

    def __init__(self, controller):
        super(LightingModule, self).__init__(controller)
        self.set_light()

    def draw(self, clear=True):
        if clear:
            self.controller.screen.device.clear_display()
        if not len(self.outputs):
            self.controller.screen.draw("values", {})
            return
        outputs = self.outputs[self.outputs.keys()[self.current]]
        if self.edit:
            name = outputs["name"] + "*"
        else:
            name = outputs["name"]
        if outputs["type"] == "dimmer":
            self.controller.screen.draw("values", {
                "title": name,
                "value": str(self.intens) + " %",
            })
        if outputs["type"] == "select":
            self.controller.screen.draw(
                "values", {
                    "title": name,
                    "value": str(self.colors[self.color]["name"]),
                })

    def try_bricklet(self, uid, device_identifier, position):
        if device_identifier == 231 and "taskint" not in self.outputs:
            self.device = BrickletLEDStrip(uid, self.controller.ipcon)
            self.outputs["taskint"] = {
                "name": "Task Intensity",
                "type": "dimmer",
            }
            self.outputs["taskcol"] = {
                "name": "Task Colour",
                "type": "select",
            }
            self.device.set_chip_type(self.device.CHIP_TYPE_WS2811)
            self.device.set_channel_mapping(self.device.CHANNEL_MAPPING_BRG)
            print("Created LEDBrick Output")

    def change_light(self, direction):
        if self.outputs[self.outputs.keys()[self.current]]["type"] == "dimmer":
            if direction == "up" and self.intens < 100:
                self.intens += 10
            elif direction == "down" and self.intens > 0:
                self.intens -= 10
        else:
            if direction == "up":
                if self.color >= len(self.colors) - 1:
                    self.color = 0
                else:
                    self.color += 1
            elif direction == "down":
                if self.color <= 0:
                    self.color = len(self.colors) - 1
                else:
                    self.color -= 1
        # print "change " + direction + str(self.color)

    def set_light(self):
        if self.device:
            color = self.colors[self.color]["color"]
            color = (int(color[0] * self.intens / 100),
                     int(color[1] * self.intens / 100),
                     int(color[2] * self.intens / 100))
            r = [color[0] for i in range(16)]
            b = [color[1] for i in range(16)]
            g = [color[2] for i in range(16)]
            self.device.set_rgb_values(0, 16, r, b, g)

            r = [255 for i in range(16)]
            b = [0 for i in range(16)]
            g = [0 for i in range(16)]
            self.device.set_rgb_values(17, 1, r, b, g)

        self.controller.scheduler.enter(
            1,
            1,
            self.set_light,
            (),
        )

    def navigate(self, direction):
        if not self.edit:
            if direction == "forward":
                self.edit = True
                self.draw()
            if direction == "back":
                self.controller.prev_module()
            if direction in ["down", "up"]:
                if direction == "down":
                    self.current = self.current + 1
                else:
                    self.current = self.current - 1
                if self.current >= len(self.outputs):
                    self.current = 0
                elif self.current < 0:
                    self.current = len(self.outputs) - 1
                # print "Output: " + str(list(self.outputs)[self.current])
                self.draw()
        else:
            if direction == "back":
                self.edit = False
                self.draw()
            if direction in ["down", "up"]:
                self.change_light(direction)
                self.draw()

    def tick(self):
        self.draw(clear=False)