######################################################################################
#Title   :  Raspberry pi pico based temperature controller
#Author  :  Bardia Alikhan Afshar <bardia.a.afshar@gmail.com>
#Language:  Python
#Hardware:  Raspberry pi pico
#####################################################################################
import ustruct
from machine import Pin, SPI
import time
from max31855 import *
from lcd import *
from Kalman import *
# PIN Definitions
RELAY = Pin(0, Pin.OUT)
KEYDOWN = Pin(13, Pin.IN, Pin.PULL_UP)
KEYOK = Pin(14, Pin.IN, Pin.PULL_UP)
KEYUP = Pin(15, Pin.IN, Pin.PULL_UP)

tc = MAX31855(SPInum=0,CS=1,MISO=4,SCK=6)                                 #Using MA31855 Class
LCD=lcd(RS=16, RW=17, EN=18, D4=19, D5=20, D6=21,D7=22,COL=16, ROW=2)     #Using lcd Class
LCD.init()
# MAX31855 Errors and other Variables
ErrorText = {0:"",1: "Open  Connection",2: "S-Circuit to GND",4: "S-Circuit to VCC"}
SV=25
differential=1
offset=0




def setting():
    LCD.clrscr()
    global SV
    global differential
    global offset
    setflag=1
    while True:
           
       if(KEYOK.value()==0):
           time.sleep(0.1)
           LCD.clrscr()
           setflag+=1
       #------------------------------
       if setflag==1:
           LCD.pos_puts(3,0,"SET  POINT")
           LCD.pos_puts(4,1,"SV={:03d}ßC".format(int(SV)))
           if(KEYUP.value()==0):
               time.sleep(0.1)
               SV+=1
               if(SV>999):
                   SV=0
           if(KEYDOWN.value()==0): 
               time.sleep(0.1)
               SV-=1
               if(SV<-50):
                   SV=999
                   #------------------------------
       if setflag==2:
           LCD.pos_puts(2,0,"Differential")
           LCD.pos_puts(3,1,"DIFF={:03d}ßC".format(int(differential)))
           if(KEYUP.value()==0):
               time.sleep(0.1)
               differential+=1
               if(differential>50):
                   differential=1
           if(KEYDOWN.value()==0): 
               time.sleep(0.1)
               differential-=1
               if(differential<1):
                   differential=50
       if setflag==3:
            LCD.pos_puts(5,0,"OFFSET")
            LCD.pos_puts(3,1,"OFST={:03d}ßC".format(int(offset))) 
            if(KEYUP.value()==0):
               time.sleep(0.1)
               offset+=1
               if(offset>50):
                   offset=-50
            if(KEYDOWN.value()==0): 
               time.sleep(0.1)
               offset-=1
               if(offset<-50):
                   offset=50
       if setflag==4:
            main()
                   



def show_error():
    RELAY.value(0)
    while True:
         error,PV=tc.ReadTemp()
         LCD.pos_puts(0,0,ErrorText[error])
         if(KEYOK.value()==0):
            time.sleep(0.15)
            setting()
         if(error==0):
             LCD.clrscr()
             return


def main():
    LCD.pos_puts(4,1,"SV={:03d}ßC".format(int(SV)))
    
    while True:
        error,PV=tc.ReadTemp()
        if(error!=0):
            show_error()
        
        PV=Kalman(PV)
        PV=PV+offset
        LCD.pos_puts(4,0,"PV={:03d}ßC".format(int(PV)))
        LCD.pos_puts(4,1,"SV={:03d}ßC".format(int(SV)))
        if(PV>=SV):
            RELAY.value(0)
        if(PV<=(SV-differential)):
            RELAY.value(1)
        if(KEYOK.value()==0):
            time.sleep(0.15)
            
            
            setting()
  
        
main()    
    