
# Pulsing green effect

#!/usr/bin/env python
from samplebase import SampleBase
from rgbmatrix import graphics
import time
import random


class PulsingColors(SampleBase):
    def __init__(self, *args, **kwargs):
        super(PulsingColors, self).__init__(*args, **kwargs)
        
    def dim_down(self):
        max_brightness = self.matrix.brightness
        while (True):
            if self.matrix.brightness < 1:
                self.matrix.brightness = max_brightness
                return
            else:
                self.matrix.brightness -= 1
                time.sleep(.5)


    def run(self):
        # self.offscreen_canvas = self.matrix.CreateFrameCanvas()
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        count = 0
        direction = 1
        max_brightness = self.matrix.brightness
    
        
        temp = vars(pulsing_colors.args)
        for item in temp:
            print(item, ':', temp[item])
        
        
        print(pulsing_colors.args.led_rows)
        print(" ")
        print(" Really, there should be some text above this line.")

        NumPanelRows = pulsing_colors.args.led_rows
        NumPanelCol = pulsing_colors.args.led_cols
        NumLinestoDisplay = pulsing_colors.args.led_chain
        DisplayPanelLines = offscreen_canvas.width / NumPanelCol
        DisplayPanelRows = offscreen_canvas.height / NumPanelRows
        print("Ready player one?")
        print("")

        print(DisplayPanelLines, "width per panel")
        print(DisplayPanelRows, "height per panel")

        print("")
        print(NumPanelRows*NumPanelCol, "total number of panels")
        print("")

        print(offscreen_canvas.width, "offscreen.canvas.width")
        print(offscreen_canvas.height, "offscreen.canvas.height")

        self._font_large = graphics.Font()
        self._font_large.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/10x20.bdf")


        self._font_small = graphics.Font()
        self._font_small.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/6x10.bdf")


        self._font_tiny = graphics.Font()
        self._font_tiny.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/tom-thumb.bdf")

        # Define colors
        self._white = graphics.Color(255, 255, 255)
        self._red = graphics.Color(255, 0, 0)
        self._blue = graphics.Color(64, 64, 255)
        self._yellow = graphics.Color(255, 255, 0)
        self._green = graphics.Color(0, 255, 0)
        self._cyan = graphics.Color(0, 255, 255)
        self._magenta = graphics.Color(255, 0, 255)
        self._gray = graphics.Color(128, 128, 128)
        self._silver = graphics.Color(192, 192, 192)
        self._maroon = graphics.Color(128, 0, 0)
        self._olive = graphics.Color(128, 128, 0)
        self._lime = graphics.Color(0, 128, 0)
        self._purple = graphics.Color(128, 0, 128)
        self._teal = graphics.Color(0, 128, 128)
        self._navy = graphics.Color(0, 0, 128)
        self._ltgray = graphics.Color(211, 211, 211)
        self._dimgray = graphics.Color(105, 105, 105)
        self._gainsboro = graphics.Color(220, 220, 220)
        self._whitesmoke = graphics.Color(245, 245, 245)
        self._blank = graphics.Color(0, 0, 0)
        
        


# options.brightness = self.args.led_brightness

        pos = offscreen_canvas.width
        print(" ")
        # print("offscreen_canvas.width", pos)
        # print("color set to ", )
        print(" ")
        
        print(" ")
        print(" Really, there should be some text above this line.")

        count = 0

        while True:
            #  self.usleep(5 * 100)

            # Sometimes you just want to glow green
            red = 0
            green = 255
            blue = 0
            
            # Lets make some random flames.
            # red = random.randrange(175, 255, 1)
            # green = random.randrange(74, 100, 1)
            # blue = 0

            # self.args.led_brightness = count
            
            count = count + direction
            if (count >= 255):
                direction = -1
                print("ping")
            if (count <= 2):
                direction = 1
                print("pong")

            max_brightness = self.matrix.brightness   
            
            if (direction == 1):
                print("in the zone")
                time.sleep(.5)

                for mycount in range (0,100, 1):
                    # print(mycount, max_brightness, self.matrix.brightness)
                   
                    if self.matrix.brightness <= 1:
                        # self.matrix.brightness = max_brightness
                        direction = -1
                        print("and we are done here")
                        break
                    else:
                        self.matrix.brightness -= 1
                        # print("and for the down count", self.matrix.brightness, max_brightness)
                        offscreen_canvas.Fill(red, green, blue)
                        offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
                        time.sleep(.1)
                
                
                
            if (direction == -1):
                print("in the other zone")
                time.sleep(.5)

                for mycount in range (0,100, 1):
                    # print(mycount, max_brightness, self.matrix.brightness)
                    # offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
                    if self.matrix.brightness >= max_brightness:
                        print("and we are done here, but we should be going the other way")
                        break
                    else:
                        self.matrix.brightness += 1
                        # print("and for the down count but there should be addition going on", self.matrix.brightness, max_brightness)
                        offscreen_canvas.Fill(red, green, blue)
                        offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
                        time.sleep(.1)
                time.sleep(.5)

            offscreen_canvas.Fill(red, green, blue)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)


# Main function
if __name__ == "__main__":
    pulsing_colors = PulsingColors()
    if (not pulsing_colors.process()):
        pulsing_colors.print_help()
