from machine import Pin, SPI, PWM
import time
from ili9341 import ILI9341, color565
import _thread
# Initialize SPI and display
spi = SPI(1, baudrate=10000000, sck=Pin(14), mosi=Pin(15))
display = ILI9341(spi, dc=Pin(6), cs=Pin(17), rst=Pin(7))
# Initialize Buzzer on GP26
buzzer = PWM(Pin(26))
buzzer.duty_u16(20000) # Set volume (moderate level)
def play_tone(freq, duration):
"""Plays a tone at the given frequency for the given duration (ms)."""
if freq > 0:
buzzer.freq(freq)
buzzer.duty_u16(20000) # Moderate volume
else:
buzzer.duty_u16(0) # Silence
time.sleep_ms(duration)
def play_sad_sound():
"""Plays the sad sound sequence."""
play_tone(622, 300)
play_tone(587, 300)
play_tone(554, 300)
# Wobbling effect
for _ in range(10):
for pitch in range(-10, 11):
play_tone(523 + pitch, 5)
# Stop sound
buzzer.duty_u16(0)
time.sleep_ms(500)
def play_happy_sound():
"""Plays a level-up sound instead of the current happy sound."""
play_tone(330, 150)
play_tone(392, 150)
play_tone(659, 150)
play_tone(523, 150)
play_tone(587, 150)
play_tone(784, 150)
buzzer.duty_u16(0)
time.sleep_ms(500)
def draw_glowing_crescent(display, x, y, radius, glow_radius, color):
"""
Draws a crescent shape with a glow effect at position (x, y) faster.
"""
# Speed up by using a larger step in the loop
for i in range(glow_radius, radius, -3): # Faster drawing
glow_color = color565(int(98 * (i / glow_radius)), int(35 * (i / glow_radius)), int(204 * (i / glow_radius)))
display.fill_arc(x, y, i, i-3, 200, 340, glow_color)
# Main crescent
display.fill_arc(x, y, radius, radius-5, 200, 340, color)
def draw_eyes(display, mood="neutral"):
"""Draws eyes based on the given mood."""
display.fill_screen(0x0000) # Black background
eye_color = color565(98, 35, 204)
if mood == "neutral":
# Neutral eyes (two full rectangles)
display.fill_rect(60, 120, 50, 80, eye_color) # Left eye
display.fill_rect(130, 120, 50, 80, eye_color) # Right eye
elif mood == "sad":
# Sad eyes with downward triangles
display.fill_rect(60, 120, 50, 50, eye_color) # Left eye (shorter)
display.fill_rect(130, 120, 50, 50, eye_color) # Right eye
display.fill_triangle(60, 170, 110, 170, 85, 200, 0x0000) # Cut bottom left
display.fill_triangle(130, 170, 180, 170, 155, 200, 0x0000) # Cut bottom right
play_sad_sound() # Play sad sound when sad face is drawn
elif mood == "happy":
draw_glowing_crescent(display, 85, 140, 25, 30, eye_color) # Left crescent
draw_glowing_crescent(display, 155, 140, 25, 30, eye_color) # Right crescent
play_happy_sound()
def animate_eyes(display):
while True:
draw_eyes(display, mood="neutral") # Neutral expression
time.sleep(1)
draw_eyes(display, mood="sad") # Sad expression
time.sleep(1)
draw_eyes(display, mood="happy") # Happy expression
time.sleep(1)
_thread.start_new_thread(animate_eyes, (display,))