# Code made by: Lucas Carvalho Heredia

# Header

import adafruit_dht
import board
import guizero as gui
from PIL import Image
import sqlite3 as sq
import time
from sqlite3 import Error
import RPi.GPIO as GPIO
from widgetlords.pi_spi import *

# import Adafruit_DHT
# # Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D4)

CHAR_COLOR = "#4B483F"
BG_COLOR = "#D5CFB7"


#temp sensor init
init()
inputs = Mod8AI()

#flags
pump_f = False
uv_f = False
laser_f = False


# Helper Functions

#####################################################

def pump_output(): 
  global pump_f

  if pump_f:
    pump_f = False
    pump_button.text = "Pump: OFF"
    GPIO.output(pump_pin, GPIO.LOW)
    print(pump_f)
  else:
    pump_f = True
    pump_button.text = "Pump: ON"
    GPIO.output(pump_pin, GPIO.HIGH)
    print(pump_f)  
  return

#####################################################

def uv_output():
  global uv_f
  if uv_f:
    uv_f = False
    uv_button.text = "UV: OFF"
    GPIO.output(uv_pin, GPIO.LOW)
    print(uv_f)
  else:
    uv_f = True
    uv_button.text = "UV: ON"
    GPIO.output(uv_pin, GPIO.HIGH)
    print(uv_f)  
  return

def laser_output():
  
  global laser_f
  if laser_f:
    laser_f = False
    laser_button.text = "Laser: OFF"
    GPIO.output(laser_pin, GPIO.LOW)
    print(laser_f)
  else:
    laser_f = True
    laser_button.text = "Laser: ON"
    GPIO.output(laser_pin, GPIO.HIGH)
    print(laser_f)  
  return

def temp_output():
  global inputs   
  tmp = inputs.read_single(0)
  temp = float(tmp)
  if tmp < 10:   
    print ("ERROR")
  else:
    res=int((temp-366)/5.6) 
    print (res)
    temp_text.value = " Temp: "+str(res)+"°C \n"
#   global dhtDevice
#   while True:
#     try:
#       temp_c =  dhtDevice.temperature 
#       time.sleep(1)
#       if temp_c == None:
#         continue
#       else:
#         temp_text.value = " Temp: "+str(temp_c)+"°C \n"       
#         print(int(temp_c))
#         return
        
#     except RuntimeError:
#       print("RuntimeError, trying again...")
#       continue

# Pin Definitons:
pump_pin = 21 # Pin 40
temp_pin = 20 # Pin 38
laser_pin = 19 # Pin 35
uv_pin = 26 # Pin 37
  


#body
app = gui.App("Efigma!",bg = BG_COLOR,width=600,height=320)
#app.bg = "white"  work!!




title = gui.Text(app,
  align = "top",
  text = "\nUV Water analysis system!\n",
  size = 20,
  font = "Helvetica", 
  color = CHAR_COLOR ) 


box_1 = gui.Box(app,
align = "right",
layout = "grid",
width = 220,
height = 250,)

# box_1.bg = "#000000"


header_text = gui.Text(box_1,
  align = "top" ,
  text="Controls:", 
  size=15, 
  font="Times New Roman", 
  color=CHAR_COLOR,
  grid = [0,0]
  )

box_space_1 = gui.Box(box_1,
layout = "grid",
height = 20,
width = 1,
grid = [0,1]
)


# temp_text = gui.Text(box_1,
#   align = "top" ,
#   text=temp_output(), 
#   size=15, 
#   font="Times New Roman", 
#   color="white",
#   grid = [0,0]
#   )
##############################################################################

pump_button = gui.PushButton(box_1,
  align = "top",
  pady=5,
  padx=10,
  text="Pump: OFF",
  width = 20,
  command = pump_output, ####
  grid = [0,2]) 
#pump_button.update_command(pump_output(pump_f)) 
pump_button.text_color =  CHAR_COLOR

######################################################################

#formatting
box_space_2 = gui.Box(box_1,
layout = "grid",
height = 20,
width = 20,
grid = [0,3]
)


uv_button = gui.PushButton(box_1,
  align = "top" ,
  pady=5,
  padx=10,
  width = 20,
  text="UV: OFF",
  command = uv_output,  
  grid = [0,4])

uv_button.text_color =  CHAR_COLOR



#formatting
box_space_3 = gui.Box(box_1,
layout = "grid",
height = 20,
width = 1,
grid = [0,5]
)


# laser Button
laser_button = gui.PushButton(box_1,
  align = "top" ,
  pady = 5,
  width = 20,
  command = laser_output, 
  text="Laser: OFF",
  grid = [0,6])
laser_button.text_color =  CHAR_COLOR


#formatting
foo_text = gui.Text(box_1,
  align = "top" ,
  text="           ", 
  size=15, 
  font="Times New Roman", 
  color="white",
  grid = [1,0])  


#formatting
box_0 = gui.Box(app,
align = "left",
layout = "grid",
height = 100,
width = 250,
)

#formatting
result_text1 = gui.Text(box_0,
  
  text= "                ", 
  size=15, 
  font="Times New Roman", 
  color=CHAR_COLOR,
  grid =[0,0]
  )


# run_button = gui.PushButton(box_0,
#   align = "top",
#   command = pumpButton,
#   pady=80,
#   padx=40,
#   image = "play1.png",
#   grid = [1,0]
#   )




# result_text = gui.Text(box_0,
  
#   text= "        Loading.....\n", 
#   size=20, 
#   font="Times New Roman", 
#   color="white",
#   align="bottom",
#   grid =[2,0]
#   )


# temperature text
temp_text = gui.Text(box_0, 
  text= " Temp:  \n", 
  size=20, 
  font="Times New Roman", 
  color=CHAR_COLOR,
  align="bottom",
  grid =[1,0]
  )

temp_button = gui.PushButton(box_0,
  align = "top" ,
  pady = 5,
  width = 20,
  command = temp_output, 
  text="Check",
  grid = [1,2])
laser_button.text_color =  CHAR_COLOR


if __name__ == "__main__":

# Pin Definitons:
  pump_pin = 21 # Pin 40
  temp_pin = 20 # Pin 38
  laser_pin = 19 # Pin 35
  uv_pin = 26 # Pin 37  

# Pin Setup:
  GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
  GPIO.setup(laser_pin, GPIO.OUT) # laser pin Output
  GPIO.setup(pump_pin, GPIO.OUT) # pump pin Output
  GPIO.setup(uv_pin, GPIO.OUT) # uv pin Output
  GPIO.setup(temp_pin, GPIO.OUT) # temp pin Input
  
  #temperature
  
  
  #tmeperature backup
  #dhtSensor = adafruit_dht.DHT22(board.D4)

 #Initial states of pins
  GPIO.output(laser_pin, GPIO.HIGH)
  GPIO.output(pump_pin, GPIO.HIGH)
  GPIO.output(uv_pin, GPIO.HIGH)
  GPIO.output(temp_pin, GPIO.HIGH)
  time.sleep(2)  

  app.display()
