import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico W!")
# Using a Matrix Keypad
# Waveshare 0.96inch OLED Module via SPI (SSD1315)
# Matrix Keypad 4x4 or 4x3
# Tony Goodhew 21th Aug 2023 - for thepihut.com
from machine import Pin, SPI
import time
# ============ Set up the display ==========
# Connect Red to 3.3V and Black to GND
import ssd1306
# Uses SPI port 0
spi_port = 0
MOSI = 19 # blue
CLK = 18 # yellow
CS = 17 # orange
RST = 16 # white #NB MISO not used
DC = 20 # green
WIDTH = 128
HEIGHT = 64
spi = SPI(
spi_port,
baudrate=40000000,
mosi=Pin(MOSI),
sck=Pin(CLK))
oled = ssd1306.SSD1306_SPI(WIDTH,HEIGHT,
spi,
dc=Pin(DC),
res=Pin(RST),
cs=Pin(CS),
external_vcc=False
)
oled.fill(0) # Clear display
oled.show()
def block(x,y,w,h,c): # Filled rectangle
for yy in range(h):
oled.hline(x,y+yy,w,c)
# ========= Display set up =========
# === MAIN PROGRAM ===
# Map characters to keys
# These are lists of lists!
# A 2 dimensional array
# C0 C1 C2 C3
keys = [['1', '2', '3', 'A'], # R0
['4', '5', '6', 'B'], # R1
['7', '8', '9', 'C'], # R2
['*', '0', '#', 'D']] # R3
# Pin connections left to right
row_pins = [13, 12, 11, 10]
col_pins = [9, 8, 7, 6]
cols = [] # Creat empty lists
rows = []
# Set up rows and columns
# Rows are OUTPUTS / Cols are INPUTS
for x in range(0,4):
# Rows are OUTPUTs which we set HIGH or LOW
rows.append(Pin(row_pins[x], Pin.OUT))
rows[x].value(0) # Set LOW = 0V
# Columns are INPUTS with internal PULL_DOWNS
cols.append(Pin(col_pins[x], Pin.IN, Pin.PULL_DOWN))
# Setup LED to indicate key pressed
led = Pin(25, Pin.OUT) # Pi Pico Comment out one of these
#led = Pin("LED", Pin.OUT) # Pi Pico W
# Short flash of LED to indicate program running
led.value(1)
time.sleep(0.5)
led.value(0)
if cols[col].value() == 1: # KEY PRESSED!
# Function to scan for a key press - Returns the character pressed
def scan():
scanning = True
while scanning: # Until key pressed - blocking!
for row in range(4):
for col in range(4):
rows[row].high() # Equivalent to .value(1)
if cols[col].value() == 1:
scanning = False # Terminate looping
k = keys[row][col] # Read character from grid
time.sleep(0.3) # Debounce
rows[row].low() # Equivalent to .value(0)
led.value(1) # Flash LED to indicate key pressed
time.sleep(0.1)
led.value(0)
return k # Supply values to calling code
def get_value(xx,yy): # INPUT a float - includes e-format
ns ="" # Needs 4 column keypad for full functionality
while True:
key = scan() # Get a chacter from the keypad
if key == '#': # ENTER/RETURN
break # Break out of loop
elif (key == "*") :
if len(ns) == 0:
ns = ns + "-" # Minus character (for 3 column displays)
else:
ns = ns[0:-1] # Backspace
elif key == "C":
pass # Not valid
elif key == "A":
ns = ns + "-" # 'Minus' character
elif key == "D": # Decimal point
ns = ns + "."
elif key == "B":
ns = ns +"e" # Allow scientific notation values
else:
ns = ns + key # Add character to string
print(ns) # Current string to Shell window
block(xx,yy,128,10,0) # Remove outdated number from display
oled.text(ns,xx,yy,1) # Display updated number
oled.show()
if len(ns) == 0:
return 0
if (ns.find(".") > 0) or (ns.find("e") > 0): # This is a FLOAT:
# It contains an "." OR an "e"
return float(ns)
else:
# Integer
return int(ns)
# ======= Main Loop =========
while True:
oled.text("N:",0,10) # Prompt
oled.show()
n = get_value(16,10)
oled.text(str(n),5,25,1)
oled.text(str(type(n)),5,35,1)
oled.show()
print("\n", n)
tp = str(type(n))
print(tp)
time.sleep(2)
oled.fill(0)
if n == 0: # HALT if zero
break
# Tidy up
oled.fill(0)
oled.show()