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(0) # Start silent
# Dictionnaire amélioré des émotions
emotion_words = {
"happy": {"joy", "love", "happy", "great", "excellent", "amazing", "fantastic", "wonderful", "awesome", "super"},
"sad": {"sad", "bad", "angry", "terrible", "awful", "miserable", "depressed", "unhappy", "cry", "pain"},
"neutral": {"okay", "fine", "normal", "meh", "alright", "average", "so-so", "usual", "calm", "balanced"}
}
def play_tone(freq, duration):
if freq > 0:
buzzer.freq(freq)
buzzer.duty_u16(20000)
time.sleep_ms(duration)
buzzer.duty_u16(0) # Ensure silence
def play_sad_sound():
for freq in [622, 587, 554]:
play_tone(freq, 150) # Faster sequence
for pitch in range(-10, 11, 2): # Larger step for faster effect
play_tone(523 + pitch, 3)
def play_happy_sound():
for freq in [330, 392, 659, 523, 587, 784]:
play_tone(freq, 100) # Reduce duration for speed
def draw_glowing_crescent(display, x, y, radius, glow_radius, color):
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)
display.fill_arc(x, y, radius, radius-5, 200, 340, color)
def draw_eyes(display, mood="neutral"):
display.fill_screen(0x0000)
eye_color = color565(98, 35, 204)
if mood == "neutral":
display.fill_rect(60, 120, 50, 80, eye_color)
display.fill_rect(130, 120, 50, 80, eye_color)
elif mood == "sad":
display.fill_rect(60, 120, 50, 50, eye_color)
display.fill_rect(130, 120, 50, 50, eye_color)
display.fill_triangle(60, 170, 110, 170, 85, 200, 0x0000)
display.fill_triangle(130, 170, 180, 170, 155, 200, 0x0000)
play_sad_sound()
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 analyze_word(word):
for mood, words in emotion_words.items():
if word in words:
return mood
return "neutral"
def main():
while True:
user_input = input("Entrez un mot: ").strip().lower()
mood = analyze_word(user_input)
print(f"Émotion détectée: {mood}")
draw_eyes(display, mood)
time.sleep(1) # Reduce delay for faster response
main()