from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from picozero import Speaker
import framebuf, utime
pix_res_x = 128
pix_res_y = 64
i2c = I2C(1, scl=Pin(27), sda=Pin(26), freq=200000)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c)
speaker = Speaker(10)
rows = [Pin(i, Pin.IN, Pin.PULL_DOWN) for i in (2, 3, 4, 5)]
cols = [Pin(i, Pin.OUT) for i in (6, 7, 8, 9)]
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
password = "040606"
input_pw = ""
# Gambar hati
def draw_heart(oled, x, y):
heart = bytearray([
0b00000000, 0b00000000, 0b00000000,
0b00001110, 0b00001110, 0b00000000,
0b00011111, 0b10011111, 0b00000000,
0b00111111, 0b11111111, 0b10000000,
0b01111111, 0b11111111, 0b11000000,
0b01111111, 0b11111111, 0b11000000,
0b01111111, 0b11111111, 0b11000000,
0b00111111, 0b11111111, 0b10000000,
0b00011111, 0b11111111, 0b00000000,
0b00001111, 0b11111110, 0b00000000,
0b00000111, 0b11111100, 0b00000000,
0b00000011, 0b11111000, 0b00000000,
0b00000001, 0b11110000, 0b00000000,
0b00000000, 0b11100000, 0b00000000,
0b00000000, 0b01000000, 0b00000000
])
fb = framebuf.FrameBuffer(heart, 24, 15, framebuf.MONO_HLSB)
oled.blit(fb, x, y)
# Lagu Happy Birthday (nada + durasi)
melody = [
(262, 0.1), (262, 0.1), (294, 0.2), (262, 0.2), (349, 0.2), (330, 0.5),
(262, 0.1), (262, 0.1), (294, 0.2), (262, 0.2), (392, 0.2), (349, 0.5),
(262, 0.1), (262, 0.1), (523, 0.2), (440, 0.2), (349, 0.2), (330, 0.2), (294, 0.5),
(466, 0.1), (466, 0.1), (440, 0.2), (349, 0.2), (392, 0.2), (349, 0.5)
]
def play_happy_birthday():
for note, duration in melody:
speaker.play(note)
utime.sleep(duration)
speaker.off()
utime.sleep(0.02) # jeda antar nada
# Fungsi scan keypad
def read_key():
for col_num, col in enumerate(cols):
col.high()
for row_num, row in enumerate(rows):
if row.value():
utime.sleep(0.3) # debounce
col.low()
return keys[row_num][col_num]
col.low()
return None
# Tampilan awal
oled.fill(0)
oled.text("Password:", 5, 10)
oled.show()
# MadeByAdhit
# Proses input password
while True:
key = read_key()
if key:
if key == '#': # Submit password
if input_pw == password:
oled.fill(0)
draw_heart(oled, 100, 50)
draw_heart(oled, 100, 25)
draw_heart(oled, 100, 0)
oled.text("Happy", 5, 10)
oled.text("Birthday", 5, 25)
oled.text("Sayang <3", 5, 40)
oled.show()
play_happy_birthday() # mainkan lagu
else:
oled.fill_rect(0, 50, 128, 14, 0)
oled.text("Salah! Coba lagi", 5, 52)
oled.show()
input_pw = ""
utime.sleep(1.5)
oled.fill_rect(0, 50, 128, 14, 0)
elif key == '*': # Reset
input_pw = ""
oled.fill_rect(0, 30, 128, 20, 0)
oled.show()
else:
if len(input_pw) < 6:
input_pw += key
oled.fill_rect(0, 30, 128, 20, 0)
oled.text("*" * len(input_pw), 5, 35)
oled.show()