import spidev as SPI
import ST7789
import time
from time import sleep
from picamera import PiCamera
from PIL import Image,ImageDraw,ImageFont
import numpy as np

# Raspberry Pi pin configuration:
RST = 27
DC = 25
BL = 24
bus = 0
device = 0

# 240x240 display with hardware SPI:
disp = ST7789.ST7789(SPI.SpiDev(bus, device),RST, DC, BL)

# Initialize library.
disp.Init()

# Clear display.
disp.clear()

# Setup camera
camera = PiCamera()
camera.resolution = (240,240)
camera.rotation = 180
# Initialize camera
#with picamera.PiCamera() as camera:
camera.resolution = (240, 240)

for _ in camera.capture_continuous('frame.jpg', format='jpeg'):
	# Load and display the captured frame
	frame = Image.open('frame.jpg')
	disp.ShowImage(frame,0,0)


#while True:
#	camera.capture("temp.bmp")
#	image = Image.open("temp.bmp")
#	disp.ShowImage(image,0,0)

#	image = np.empty((camera.resolution[1] * camera.resolution[0] * 3,), dtype=np.uint8)
#	camera.capture(image, 'rgb')
#	img = image.reshape((camera.resolution[1], camera.resolution[0], 3))
#	screen_data = np.array(img)
#	screen_data = screen_data.flatten()
#	disp.ShowImage(img,0,0)
#	img = np.empty((camera.resolution[1] * camera.resolution[0] * 3, ), dtype=np.uint8)
#	camera.capture(img, 'bgr')
#	img = img.reshape((camera.resolution[1], camera.resolution[0], 3))
#	disp.ShowImage(img,0,0)
        #Convert PIL Image to raw pixel data
        #screen_data = np.array(pil_image)
        #Flatten the pixel data
        #screen_data = screen_data.flatten()
#	sleep(0.1)
