from machine import Pin, SoftI2C,I2C
from i2c_lcd import I2cLcd
from servo import Servo
import ssd1306
import time
#from mode import Switch
key = None
#buzzer
bz= Pin(25, Pin.OUT)
# servo
sv = Servo(pin=26)
sv.move(0)

# # khai bao LCD
# I2C_ADDR = 0x27
# totalRows = 2
# totalColumns = 16
# i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=40000)
# lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# #setup lcd
# def setup_lcd():
#     lcd.clear()
#     lcd.putstr('A: nhap pass')
#     lcd.move_to(0,1)
#     lcd.putstr('B: doi pass')

#oled
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('Tran Thai Quyen', 2, 5 )   
oled.show()   
time.sleep(2)

def setup_lcd():
    oled.fill(0)
    oled.text('A: Nhap pass', 5,5)
    oled.text('B: Doi pass', 5,20)
    oled.show()

# keypad
matrix_keys = [['1', '2', '3', 'A'],
               ['4', '5', '6', 'B'],
               ['7', '8', '9', 'C'],
               ['*', '0', '#', 'D']]
keypad_rows = [15,2,4,5]
keypad_columns = [13,12,14,27]
col_pins = []
row_pins = []
for x in range(0,4):
    row_pins.append(Pin(keypad_rows[x], Pin.OUT))
    row_pins[x].value(1)
    col_pins.append(Pin(keypad_columns[x], Pin.IN, Pin.PULL_DOWN))
    col_pins[x].value(0)
 

#quet phim
def scankeys():  
    key_press = ''
    for row in range(4):
        for col in range(4):
            row_pins[row].value(1)
            if col_pins[col].value() == 1:
                bz.value(1)
                key_press = matrix_keys[row][col]
                time.sleep(0.03)  
        row_pins[row].value(0)
        bz.value(0)
    return key_press
setup_lcd()
password = ['2','0','0','1']
enter_pass = []
mode = ''
while True:
    key = scankeys()
    if key == 'A':
        # lcd.clear()
        # lcd.putstr('Moi nhap pass:')
        # lcd.move_to(1,1)
        oled.fill(0)
        oled.text('Moi nhap pass:',5,5)
        oled.show()
        mode = 'A' 
    elif key == 'B':
        oled.fill(0)
        oled.text('Moi nhap pass:', 5,5)
        oled.show()
        # lcd.clear()
        # lcd.putstr('Nhap pass:')
        # lcd.move_to(1,1)
        mode = 'B' 
    
    if mode == 'A':
        if key != 'A' and key!= 'B' and key != 'C' and key !='D' and key !='':
            enter_pass += key
            print(enter_pass)
            # lcd.putstr('*')
            oled.text('*', len(enter_pass)*10+ 5,20)
            # lcd.move_to(len(enter_pass)+1,1)
            oled.show()
            if len(enter_pass)>= 4:
                if enter_pass == password:
                    # lcd.clear()
                    # lcd.putstr("welcome!")
                    oled.fill(0)
                    oled.text('Welcome', 5,5)
                    oled.show()
                    sv.move(120)
                    time.sleep(3)
                    sv.move(0)
                    enter_pass = []
                    mode = ''
                    setup_lcd()
                else:
                    # lcd.clear()
                    # lcd.putstr("incorrect")
                    oled.fill(0)
                    oled.text('Nhap pass sai', 5,5)
                    oled.show()
                    enter_pass = []
                    mode= ''
                    time.sleep(2)
                    setup_lcd()
    elif mode == 'B':
        if key != 'A' and key!= 'B' and key != 'C' and key !='D' and key !='':
            enter_pass += key
            print(enter_pass)
            # lcd.putstr('*')
            # lcd.move_to(len(enter_pass)+1,1)
            oled.text('*', len(enter_pass)*10+ 5,20)
            oled.show()
            if len(enter_pass)>= 4:
                if enter_pass == password:
                    # lcd.clear()
                    # lcd.putstr("Nhap pass moi:")
                    # lcd.move_to(1,1)
                    oled.fill(0)
                    oled.text('Nhap Pass moi', 5,5)
                    oled.show()
                    enter_pass = []
                    mode = 'pass'
                else:
                    # lcd.clear()
                    # lcd.putstr("incorrect")
                    oled.fill(0)
                    oled.text('Nhap pass sai', 5,5)
                    oled.show()
                    enter_pass = []
                    mode= ''
                    time.sleep(2)
                    setup_lcd()
    elif mode == 'pass':
        if key != 'A' and key!= 'B' and key != 'C' and key !='D' and key !='':
            enter_pass += key
            print(enter_pass)
            # lcd.putstr('*')
            # lcd.move_to(len(enter_pass)+1,1)       
            oled.text('*', len(enter_pass)*10+5,20)   
            oled.show()     
            if len(enter_pass)== 4:
                password = enter_pass
                enter_pass = []
                oled.fill(0)
                oled.text('Doi thanh cong!', 5,5)
                oled.show()
                mode = ''
                setup_lcd()