Beispiel #1
0
class PdsPlugin:
    def __init__(self, matrix=True, delay=0.2):
        self.matrix = matrix
        self.author = 'Infected'
        self.name = 'PDS Plugin'
        self.version = '0.1a'

        self.image = Image(64, 16)
        self.screen = Screen(matrix=matrix, show=True, fps=int(delay))
        self.screen.add(self.image, mode="invert")
        # self.monitor = Stream(matrix=self.matrix)
        # self.delay = float(delay)

    def get_info(self):
        """Get the current state and information of the plugin"""
        print(self.name, self.author, self.version, sep='\n')

    def get_delay(self):
        return self.delay

    def get_image(self):
        return self.image

    def set_pixmap(self, pixmap):
        self.image.set_pixmap(pixmap)

    def get_pixmap(self):
        return self.image.get_pixmap()

    def stream(self):

        self.screen.refresh()

    def blank(self):
        self.image.blank()
Beispiel #2
0
class Hbd_plugin:
    def __init__(self):
        msg("input()", 2, "Hbd_plugin.__init__()")
        self.name_label = input(color("Enter label: ", "red")).lower()
        self.name_text = input(color("Enter name: ", "yellow")).lower()

        self.mask = Image(64,16)
        self.mask.fill()

        self.bg = Image(64, 16)
        self.drawer = Drawer(self.bg)
        self.drawer.dot(0, 0)
        self.drawer.dot(0, 15)
        self.drawer.dot(63, 0)
        self.drawer.dot(63, 15)

        self.label = Text(text="§" + self.name_label + "§")
        self.name = Text(text=self.name_text)

        self.screen = Screen(matrix=True, show=True, fps=1.7, tty='/dev/ttyACM1')

        self.screen.add(self.bg, refresh=False)

        xloc_label = (64 - abs(self.label.width)) // 2
        self.screen.add(self.label, x=xloc_label, y=1, refresh=False)

        xloc_text = (64 - abs(self.name.width)) // 2
        self.screen.add(self.name, x=xloc_text, y=8, refresh=False)

        self.screen.add(self.mask, refresh=False, mode="invert")

    def stream(self):
        try:
            blink = False
            while True:
                blink = not blink
                if blink:
                    self.mask.blank()
                else:
                    self.mask.fill()

                self.screen.refresh()
        except KeyboardInterrupt:
            print()
            msg(self.name_label, 0, "label")
            msg(self.name_text, 0, "name")
            exit()
Beispiel #3
0
class MatrixDrawer:
    def __init__(self, x=64, y=16):
        self.image = Image(width=x, height=y)
        self.drawer = Drawer(self.image)
        self.updater = Updater(self)

        self.x = x
        self.y = y

        self.live = False
        self.drawmode = True
        self.updatethread = None

        self.create_window()
        self.create_canvas()
        self.create_buttons()
        self.root.mainloop()    # Launch tkinter eventloop
        # This is run only after tkinter is closed and its event loop ends
        # Cleaning up any potential loose ends at close of program
        if self.updatethread is not None:
            self.updater.live = False
            self.updatethread.join()

    def create_window(self):
        self.root = tkinter.Tk()
        self.root.configure(bg="light blue")
        self.root.title("Matrix Drawer")

        self.root.bind("<Button-1>", self.mouse_interact_left)
        self.root.bind("<B1-Motion>", self.mouse_interact_left)
        self.root.bind("<Button-3>", self.mouse_interact_right)
        self.root.bind("<B3-Motion>", self.mouse_interact_right)

    def create_canvas(self):
        self.canvasframe = tkinter.Frame(self.root)
        self.canvasframe.pack(side="right")
        pixel_size = 20
        self.canvas = tkinter.Canvas(self.canvasframe, bg="black",
                                     height=self.y*pixel_size,
                                     width=self.x*pixel_size)

        self.canvas.grid(row=0, column=0)

        for y in range(self.y):
            for x in range(self.x):
                self.canvas.create_oval(x*pixel_size, y*pixel_size,
                                        x*pixel_size + pixel_size,
                                        y*pixel_size + pixel_size,
                                        fill="grey")

    def create_buttons(self):
        self.buttonframe = tkinter.Frame(self.root, bg="light blue")
        self.buttonframe.pack(side="left")
        self.updatebutton = tkinter.Button(self.buttonframe,
                                             text="Send To Matrix",
                                             bg="Yellow",
                                             command=self.updater.one_refresh
                                             )
        self.clearbutton = tkinter.Button(self.buttonframe,
                                          text="Clear All",
                                          bg="Yellow",
                                          command=self.clearall
                                          )

        self.fillbutton = tkinter.Button(self.buttonframe,
                                         text="Fill All",
                                         bg="Yellow",
                                         command=self.fillall
                                         )
        self.livebutton = tkinter.Button(self.buttonframe,
                                         text="Status: Manual",
                                         bg="Yellow",
                                         command=self.togglelive
                                         )
        self.drawbutton = tkinter.Button(self.buttonframe,
                                         command=self.toggledraw,
                                         text="Draw Mode",
                                         bg="Yellow",
                                         relief="sunken",
                                         activebackground="Yellow"
                                         )
        self.erasebutton = tkinter.Button(self.buttonframe,
                                          command=self.toggleerase,
                                          text="Erase Mode",
                                          bg="Grey",
                                          activebackground="Grey"
                                          )

        self.updatebutton.grid(row=0, column=0, columnspan=2)
        self.clearbutton.grid(row=1, column=0, columnspan=2)
        self.fillbutton.grid(row=2, column=0, columnspan=2)
        self.livebutton.grid(row=3, column=0, columnspan=2)
        self.drawbutton.grid(row=4, column=0)
        self.erasebutton.grid(row=4, column=1)

    def toggledraw(self):
        """
        Sets left mouse button to drawing
        """
        self.drawmode = True
        self.drawbutton.configure(relief="sunken",
                                  bg="Yellow",
                                  activebackground="Yellow")
        self.erasebutton.configure(relief="raised",
                                   bg="Grey",
                                   activebackground="Grey")

    def toggleerase(self):
        """
        Sets left mouse button to erasing
        """
        self.drawmode = False
        self.drawbutton.configure(relief="raised",
                                  bg="Grey",
                                  activebackground="Grey")
        self.erasebutton.configure(relief="sunken",
                                   bg="Yellow",
                                   activebackground="Yellow")

    def togglelive(self):
        """
        Creates new thread and launches permanent loop to keep matrix updated
        in real time
        """
        if self.updater.live:   # we were already live, going manual now
            self.livebutton.configure(text="Status: Manual")
            # Stop the infinite loop in the other thread and terminate it
            self.updater.live = False
            self.updatethread.join()
            self.updatethread = None

        else:           # we were manual, now going live
            self.livebutton.configure(text="Status: Live")
            self.updater.live = True
            # Open a new thread and launch the infinite update loop
            self.updatethread = threading.Thread(target=self.updater.toggle_live)
            self.updatethread.start()

    def clearall(self):
        """
        Clears all pixels, resetting them back to grey
        """
        for i in range(self.x * self.y):
            self.canvas.itemconfig(i+1, fill="grey")

        self.image.blank()

    def fillall(self):
        """
        Fills all pixels, setting them to red
        """
        for i in range(self.x * self.y):
            self.canvas.itemconfig(i+1, fill="red")

        self.image.fill()

    def mouse_interact_left(self, event):
        """
        Left mouse's function depends on the 2 draw/erase buttons available to
        the user
        """
        if event.widget.winfo_id() == self.canvas.winfo_id():
            x = self.canvas.canvasx(event.x)
            y = self.canvas.canvasy(event.y)

            pixel = self.canvas.find_closest(x, y)

            if self.drawmode:
                self.canvas.itemconfig(pixel, fill="red")
                self.drawer.dot((pixel[0]-1) % self.x,
                                (pixel[0] - 1) // self.x)

            else:
                self.canvas.itemconfig(pixel, fill="grey")
                self.drawer.erase((pixel[0]-1) % self.x,
                                (pixel[0] - 1) // self.x)

    def mouse_interact_right(self, event):
        """
        Right mouse button always used for erasing
        """
        if event.widget.winfo_id() == self.canvas.winfo_id():
            x = self.canvas.canvasx(event.x)
            y = self.canvas.canvasy(event.y)

            pixel = self.canvas.find_closest(x, y)
            self.canvas.itemconfig(pixel, fill="grey")
            self.drawer.erase((pixel[0]-1) % self.x,
                              (pixel[0] - 1) // self.x)
Beispiel #4
0
class Screen:
    """
    Screen is the Image manager for the plugins.
    Each child of it is an Image and can be added with the 'add' method.
    On each 'refresh' all the images are flattened to one Image and sent to
    the streamer

    Screen must have the same size of the matrix

    Keyword arguments:
    x -- x height (default MAT_HEIGHT)
    y -- y width (default MAT_WIDTH)
    matrix -- output to serial (default True)
    show -- verbose output (default False)
    fps -- not implemented (default 0)
    """
    def __init__(
            self,
            width=MAT_WIDTH,
            height=MAT_HEIGHT,
            matrix=True,
            show=False,
            guishow=False,
            fps=0,
            tty='/dev/ttyACM0'):

        if fps > 0:
            self.fps = 1 / fps
        else:
            self.fps = 0
        self.image = Image(width=width, height=height)
        self.streamer = Stream(matrix=matrix, tty=tty)
        self.show = show
        self.childs = []
        if guishow:
            self.show_gui()

    def add(self, element, x=0, y=0, refresh=True, mode="fill", name="Child"):
        """
        Add a new Image to the childs.

        Keyword arguments:
        image -- Image
        x -- x paste location (default 0)
        y -- y paste location (default 0)
        refresh -- blank Image after refresh (default True)
        mode -- paste mode [Image.paste()] (default "fill")
        name -- name (default "Child")
        """
        if str(type(element)) == "<class 'libs.slide.Slide'>":
            self.childs.append((element.view, x, y, refresh, mode, name))
        elif str(type(element)) == "<class 'libs.text.Text'>":
            self.childs.append((element, x, y, refresh, mode, name))
        elif str(type(element)) == "<class 'libs.image.Image'>":
            self.childs.append((element, x, y, refresh, mode, name))
        else:
            msg("not a valid element", 2, "Screen.add()", type(element))

    def remove(self, id_):
        """Delete a child by his id"""
        if id_ <= len(self.childs) - 1:
            msg(self.childs.pop(id_)[5], 0, "Removed")
        else:
            msg("no such child", 2, "Screen.remove()", len(self.childs), id_)


    def refresh(self):
        """
        Flatten all childs into one Image and send it to the streamer
        and/or print it in the terminal.
        """
        self.image.blank()
        for child in self.childs:
            self.image.paste(child[0], x=child[1], y=child[2], mode=child[4])

            # Refresh
            if child[3]:
                child[0].blank()

        self.streamer.set_data(self.image)
        self.streamer.send_to_serial()
        if self.show:
            system('clear')
            print(self.streamer)
        sleep(self.fps)

    def __str__(self):
        count = len(self.childs) - 1
        string = color("Screen", "green") + "\n"
        for n, child in enumerate(self.childs):
            if n < count:
                string += color('├─', 'blue')
            else:
                string += color('└─', 'blue')

            string += color(str(n), 'red')
            string += color("..", 'yellow')
            string += color(child[5], 'green', False, None, "Underline")
            if child[3]:
                string += "[" + color("1", "magenta", False) + "]"
            else:
                string += "[" + color("O", "magenta", False) + "]"
            string += "\n"
        return string

    def show_gui(self):
        """
        Instantiates the tkinter gui and gets it running. The gui is updated
        from within itself by a function that is run at the end of each
        turn of the tkinter mainloop.
        """
        gui_thread = Thread(target=lambda: GuiViewer(self.image))
        gui_thread.daemon = True
        gui_thread.start()