from machine import Pin
import utime
import lcd_4bit_mode
from machine import I2C

#from cython import PyMem_Malloc, PyMem_Free




#LCD Pins
RS = machine.Pin(19,machine.Pin.OUT)
ENABLE = machine.Pin(20,machine.Pin.OUT)
BACK_LIGHT = machine.Pin(8,machine.Pin.OUT)
D4 = machine.Pin(21,machine.Pin.OUT)
D5 = machine.Pin(22,machine.Pin.OUT)
D6 = machine.Pin(26,machine.Pin.OUT)
D7 = machine.Pin(27,machine.Pin.OUT)

col_list=[5,4,3,2]
row_list=[9,8,7,6]

display = lcd_4bit_mode.LCD16x2(RS,ENABLE,BACK_LIGHT,D4,D5,D6,D7)

#Password
password = ""

accepted = False



#Keypad Setup
for x in range(0,4):
    row_list[x]=Pin(row_list[x], Pin.OUT)
    row_list[x].value(1)


for x in range(0,4):
    col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)

key_map=[["1","2","3","A"],\
        ["4","5","6","B"],\
        ["7","8","9","C"],\
        ["","0","#","D"]]

def Keypad4x4Read(cols,rows):
  for r in rows:
    r.value(0)
    result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]

    if min(result)==0:
      key=key_map[int(rows.index(r))][int(result.index(0))]
      r.value(1) # manages key keept pressed
      return(key)
    r.value(1)

print("Welcome to Pi pico!")
#---------------------------------------------------------------------------------
def getlargest():
    largest = 0
    for i, num in enumerate(array):
        if largest < num:
            largest=num
    return largest
    

display.WriteLine("Enter Array Size", 1)


while True:
    size = Keypad4x4Read()
    if size != None: 
      break

size = int(size)
for i in range(size):
  display.ClearScreenCursorHome()
  display.WriteLine("Enter num[" + str(i) + "]", 1)
  while True:
    num = Keypad4x4Read()
    if num != None:
        array.append(int(num))
        break

display.ClearScreenCursorHome()
display.move_to(0,0)
display.WriteLine("Largest:",1)
display.move_to(0,1)
display.WriteLine(str(getlargest()))
print("Array Size: ", size)
print("Array: ", array)
print("Largest Number: ", str(getlargest()))
utime.sleep(1)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT