from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
resX = 128
resY = 64
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
display = SSD1306_I2C(resX, resY, i2c)
keys = [['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']
]
alphabet = [
[['_', '_', '_'], ['A', 'B', 'C'], ['D', 'E', 'F'],],
[['G', 'H', 'I'], ['J', 'K', 'L'], ['M', 'N', 'O'],],
[['P', 'Q', 'R', 'S'], ['T', 'U', 'V'], ['W', 'X', 'Y', 'Z'], ],
]
rows = [16, 17, 18, 19]
cols = [10, 11, 12]
row_pins = []
col_pins = []
for x in range(0, 4): # 0-3
row_pins.append(Pin(rows[x], Pin.OUT))
row_pins[x].high()
if x <= 2: # 0-2
col_pins.append(Pin(cols[x], Pin.IN, Pin.PULL_DOWN))
col_pins[x].low()
def scankeys():
keyPressed = None
for row in range(4):
for col in range(3):
row_pins[row].high()
if col_pins[col].value() == 1:
keyPressed = keys[row][col]
utime.sleep(0.3)
row_pins[row].low()
return keyPressed
def updateTop(text):
display.fill_rect(0,0,128,32, 0)
display.text(text, 0, 24)
display.show()
def updateBottom(text):
display.fill_rect(0,40,128,64, 0)
display.text(text, 0, 40)
display.show()
def getRow(num):
if num in range(2, 4):
return 1
elif num in range(4, 7):
return 2
elif num in range(7, 10):
return 3
else:
return 1
def getColumn(num):
if num in [1, 4, 7]:
return 1
elif num in [2, 5, 8]:
return 2
elif num in [3, 6, 9]:
return 3
else:
return 1
def getLetter(inputted):
firstNum = inputted[0]
firstNumInt = int(firstNum)
row = getRow(firstNumInt) - 1
column = getColumn(firstNumInt) - 1
length = len(inputted) - 1
full = ""
for x in range(len(inputted)):
if inputted[x] != firstNum:
print("not equal")
full = " "
break
full = alphabet[row][column][length]
return full
topLine = "> "
bottomLine = "x"
updateTop(topLine)
updateBottom(bottomLine)
while True:
key = scankeys()
if key != None:
if key == '#':
topLine = "> "
bottomLine = "x"
updateTop(topLine)
updateBottom("")
elif key == '*':
topLine += "- "
bottomLine += " "
updateTop(topLine)
updateBottom(bottomLine)
#elif len(topLine.split()[-1]) == 3:
# topLine += "- "
# bottomLine += " "
else:
print("pressed: "+ key)
topLine += key
updateTop(topLine)
returned = getLetter(topLine.split()[-1])
list1 = list(bottomLine)
list1[-1] = returned
bottomLine = ''.join(list1)
updateBottom(bottomLine)
if topLine.split()[-1][0] == '7' or topLine.split()[-1][0] == '9':
if len(topLine.split()[-1]) == 4:
topLine += "- "
bottomLine += " "
updateTop(topLine)
updateBottom(bottomLine)
continue
elif len(topLine.split()[-1]) == 3:
topLine += "- "
bottomLine += " "
updateTop(topLine)
updateBottom(bottomLine)
continue