from machine import Pin
import utime
import sys
import tm1637
if(sys.platform=="esp32"):
disp_clk = 22
disp_dio = 23
# Define the GPIO pins for rows and columns
row_pins = [27, 14, 12, 13] # Cambia questi valori con i tuoi pin ESP32
col_pins = [32, 33, 25, 26]
elif(sys.platform=="rp2"):
disp_clk = 16
disp_dio = 17
# Connect Keypad pins as below
row_pins=[9,10,11,12]
col_pins=[5,6,7,8]
# Create a map between keypad buttons and chars
key_map=[["F","E","D","C"],\
["B","A","9","8"],\
["7","6","5","4"],\
["3","2","1","0"]]
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)
# Start Function
if __name__ == '__main__':
#Set rows pin as output
for x in range(0,4):
row_pins[x]=Pin(row_pins[x], Pin.OUT)
row_pins[x].value(1)
#Set column as input
for x in range(0,4):
col_pins[x] = Pin(col_pins[x], Pin.IN, Pin.PULL_UP)
tm = tm1637.TM1637(clk = Pin((disp_clk)), dio = Pin((disp_dio)))
num =0
# Start the main loop
print("--- Ready to get user inputs ---")
while True:
key=Keypad4x4Read(col_pins, row_pins)
if key != None:
print("Pressed button: "+key)
tm.write([0, 0, 0, 0])
tm.show(str(key))
utime.sleep(0.3) # gives user enough time to release without having double inputs known as debounce