from pynq.drivers.video import HDMI
from pynq import Bitstream_Part
from pynq.board import Register
from pynq import Overlay

Overlay("demo.bit").download()

"""
Explanation: Don't forget to delete the hdmi_out and hdmi_in when finished
Invert Filter Example
In this notebook, we will explore how to invert colors in an image. Also known as a negative image, it is a total inversion, in which light areas appear dark and dark appears light. Additionally, these images are color-reversed, with red appearing as cyan, greens appearing magenta, and blues appearing yellow, and vice versa.
To obtain the inverted color value, you take the maximum value that the color can be. For example, if you have a 256 color scheme, 255 is the maximum color value possible (from 0 - 255). You subtract the actual color from the maximum color to get its inverse. 
So if you have a yellow color that is R = 255, G = 255, and B = 0, and you subtract the values to get the inverse
you would result with R = 0, G = 0, and B = 255 which leaves you with just the color blue.
You can see this color inversion in the table below.
<img src="data/ColorNegatives.png"/>
This diagram shows color-reserved colors (from en.wikipedia.org/wiki/Negative_(photography) ).
This notebook will use a video filter that inverts the input image.
1. Download base overlay to the board
Ensure that the camera is not connected to the board. Run the following script to provide the PYNQ with its base overlay.
End of explanation
"""


hdmi_in = HDMI('in')
hdmi_out = HDMI('out', frame_list=hdmi_in.frame_list)
hdmi_out.mode(2) #2 is for camera
hdmi_out.start()
hdmi_in.start()

#Valid resolutions are:
#
# 0 : '640x480@60Hz' 
# 1 : '800x600@60Hz' 
# 2 : '1280x720@60Hz' 
# 3 : '1280x1024@60Hz' 
# 4 : '1920x1080@60Hz'

"""
Explanation: 2. Connect camera
Physically connect the camera to the HDMI-in port of the PYNQ. Run the following code to instruct the PYNQ to capture the video from the camera and to begin streaming video to your monitor (connected to the HDMI-out port).
End of explanation
"""


Bitstream_Part("invert_p.bit").download()

"""
Explanation: 3. Program board with Invert Filter
Run the following script to download the Invert Filter to the PYNQ. This will allow us to modify the colors of the video stream.
End of explanation
"""


import ipywidgets as widgets
from ipywidgets import Button, HBox, VBox, Label

words = ['HDMI Reset']
items = [Button(description=w) for w in words]


def on_hdmi_clicked(b):
    hdmi_out.stop()
    hdmi_in.stop()
    hdmi_out.start()
    hdmi_in.start()

items[0].on_click(on_hdmi_clicked)

widgets.VBox([items[0]])

"""
Explanation: 4. Create HDMI Reset Button
Run the following script to create an HDMI Reset button that can be used to reset the HDMI if there are issues.
End of explanation
"""


hdmi_out.stop()
hdmi_in.stop()
del hdmi_out
del hdmi_in

"""
Explanation: 5. Clean up
When you are done playing with the Invert filter, run the following code to stop the video stream
End of explanation
"""


