import Adafruit_CharLCD as LCD
import RPi.GPIO as GPIO #import Adafruit_GPIO as GPIO
import time



lcd_rs        = 25  # Note this might need to be changed to 21 for older revision Pi's.
lcd_en        = 9 #24
lcd_d4        = 3 #23
lcd_d5        = 4 #17
lcd_d6        = 17 #21
lcd_d7        = 22
lcd_backlight = 4

red=23 #red led gpio
blue=24 
recvr=5 
fire=15
flag=0
passflag=1
speaker=18
door=14 
rfid=18 
ROW1=6 
ROW2=13
ROW3=19
ROW4=26
COL1=16
COL2=20
COL3=21
value=0
c1=[1,0,0]#scanning list
c2=[0,1,0]
c3=[0,0,1]
passcode=[2,2,2,2]
x=0 #the cursor counter
pb=0 #push button counter

lcd_columns = 16
lcd_rows    = 2
GPIO.setmode(GPIO.BCM) 
GPIO.setup(rfid,GPIO.IN)#same with enter
GPIO.setup(fire,GPIO.IN)#same with enter
GPIO.setup(recvr,GPIO.IN)#same with enter
GPIO.setup(door,GPIO.IN)#same with enter
GPIO.setup(ROW1,GPIO.IN)#same with enter
GPIO.setup(ROW2,GPIO.IN)#same with enter
GPIO.setup(ROW3,GPIO.IN)#same with enter
GPIO.setup(ROW4,GPIO.IN)#same with enter
GPIO.setup(COL1,GPIO.OUT)#same with enter
GPIO.setup(COL2,GPIO.OUT)
GPIO.setup(COL3,GPIO.OUT)



lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                           lcd_columns, lcd_rows, lcd_backlight)
GPIO.add_event_detect(rfid,GPIO.RISING) #rfid interrupt
GPIO.add_event_detect(fire,GPIO.FALLING) #fire interrupt
GPIO.add_event_detect(door,GPIO.FALLING) #door interrupt
GPIO.add_event_detect(recvr,GPIO.RISING) #rfid interrupt

def disarmed():
    x=0
    i=0 #delay counter 
    time.sleep(0.25)
    lcd.clear()
    lcd.set_cursor(1,0)
    lcd.message('Arm')
    lcd.set_cursor(8,0)
    lcd.message('DISARMED')
    lcd.set_cursor(1,1)
    lcd.message('Settings')
    lcd.set_cursor(0,0)
    lcd.blink(1)
    while (1):
        #print('in while loop')
        flag=0 #disarmed
        value=push()
        time.sleep(.25)
        if (value==2) and (x==1):
            x=0
            lcd.set_cursor(0,0)
            lcd.blink(1)
        elif (value==5) and (x==0):#down is pressed but cursor is at the bottom
            x=1
            lcd.set_cursor(0,1)
            lcd.blink(1)
        elif (value==5) and (x==1):
            pass
        elif (value==12) and (x==0): #if entered was pressed then enter password
            passcodescreen(flag)
        elif (value==12) and (x==1): 
            settings()
#armed screen
def armed():
    x=0
    flag=1
    lcd.clear()
    lcd.set_cursor(1,0)
    lcd.message('Disarm')
    lcd.set_cursor(11,0)
    lcd.message('ARMED')
    lcd.set_cursor(0,0)
    lcd.blink(1)
    while (1):
        value=push()
        if (value==12) and (x==0): #if entered was pressed then enter password
            passcodescreen(flag)
            
def passcodescreen(flag):
    while(1):
        lcd.clear()
        temp=[]
        lcd.set_cursor(1,0)
        lcd.message('Enter Passcode')
        lcd.set_cursor(6,1)
        for i in range (0,4):
            time.sleep(0.25)
            value=push()
            print flag
            if (value==10)and (flag==0):
                disarmed()
            elif(value==10)and (flag==1):
                armed()
                
            lcd.message('*')
            temp.append(value)
        i=0
        while (i<3):
            if not(temp[i]==passcode[i]):
                passflag=1
                break
            else:
                passflag=0
            i+=1
        if (passflag==1): #password is invalid code
            lcd.clear()
            lcd.set_cursor(2,0)
            lcd.message('Invalid Code')
            time.sleep(3)
        elif (flag==0):
            armed()
        elif (flag==1):
            disarmed()
        else:
            return True

def settings():
    x=0
    i=0 #delay counter 
    lcd.clear()
    lcd.set_cursor(1,0)
    lcd.message('Set Code')
    lcd.set_cursor(1,1)
    lcd.message('Set Time')
    lcd.set_cursor(0,0)
    lcd.blink(1)
    while (1):
        value=push()
        if (value==2) and (x==0):#up is pressed but cursor is at the top
            pass
        elif (value==2) and (x==1):
            x=0
            lcd.set_cursor(0,0)
            lcd.blink(1)
        elif (value==5) and (x==0):#down is pressed but cursor is at the bottom
            x=1
            lcd.set_cursor(0,1)
            lcd.blink(1)
        elif (value==5) and (x==1):
            pass
        elif (value==12) and (x==0): #if entered was pressed then enter password
            editPasscode(passcode)
        elif (value==12) and (x==1): #if entered was pressed then enter password
            #editTime()
            pass
        elif (value==10):
            disarmed()
def editPasscode(passcode):
    flag=2
    value=passcodescreen(flag)#authorizes passcode change
    if (value==1):
        lcd.clear()
        lcd.set_cursor(4,0)
        lcd.message('Enter New')
        lcd.set_cursor(4,1)
        lcd.message('Passcode')
        time.sleep(2)
        lcd.clear()
        lcd.set_cursor(6,0)
        i=0
        passcode=[]
        while (i<4):
            value=push()
            time.sleep(0.25)
            lcd.message('%d'%value)
            passcode.append(value)
            i+=1
        print passcode
        settings()
    else:
        lcd.clear()
        lcd.message('Access Denied')
        settings()
    

def push():
    while (1):
        pb=1 #button counter starts at 1
#row 1 scan
        if GPIO.event_detected(rfid):
            if (flag==0):
                armed()
            elif(flag==1):
                disarmed()
        if GPIO.event_detected(fire):
            firealarm()
        if GPIO.event_detected(door):
            if (flag==0):
                doorchimes()
            elif(flag==1):
                alarm()
        if GPIO.event_detected(recvr):
            if(flag==1):
                alarm()
        for i in range (0,3):
            test=c1[i]
            GPIO.output(COL1,c1[i])
            GPIO.output(COL2,c2[i])
            GPIO.output(COL3,c3[i])
            #time.sleep(0.005)
            value=GPIO.input(ROW1)

            if (value==1):
                return pb
            pb+=1
#row 2 scan
        for i in range (0,3):
            GPIO.output(COL1,c1[i])
            GPIO.output(COL2,c2[i])
            GPIO.output(COL3,c3[i])
            #time.sleep(0.005)
            value=GPIO.input(ROW2)

            if (value==1):
                return pb
            pb+=1

#row 3 scan
        for i in range (0,3):
            GPIO.output(COL1,c1[i])
            GPIO.output(COL2,c2[i])
            GPIO.output(COL3,c3[i])
            #time.sleep(0.005)
            value=GPIO.input(ROW3)

            if (value==1):
                return pb
            pb+=1
#row 4 scan
        for i in range (0,3):
            GPIO.output(COL1,c1[i])
            GPIO.output(COL2,c2[i])
            GPIO.output(COL3,c3[i])
            #time.sleep(0.005)
            value=GPIO.input(ROW4)

            if (value==1):
                return pb
            pb+=1


#main code begins here
print 'made it through the code'
#while (1):
    #value=GPIO.input(enter)
    #print ('%d'%value)#testing pushbutton value
disarmed()
