Skip to content

Commit 1e24a14

Browse files
committed
first commit Larsio Paint Music
1 parent 389027c commit 1e24a14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2831
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# SPDX-FileCopyrightText: 2025 John Park for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
'''
5+
Larsio Paint Music
6+
Fruit Jam w mouse, HDMI, audio out
7+
'''
8+
9+
# Main application file for the Music Staff application
10+
11+
from sound_manager import SoundManager
12+
from note_manager import NoteManager
13+
from ui_manager import UIManager
14+
import time
15+
import gc
16+
17+
# Configuration
18+
AUDIO_OUTPUT = "i2s" # Options: "pwm" or "i2s"
19+
20+
class MusicStaffApp:
21+
"""Main application class that ties everything together"""
22+
23+
def __init__(self, audio_output="pwm"):
24+
# Initialize the sound manager with selected audio output
25+
# Calculate tempo parameters
26+
BPM = 120 # Beats per minute
27+
SECONDS_PER_BEAT = 60 / BPM
28+
SECONDS_PER_EIGHTH = SECONDS_PER_BEAT / 2
29+
30+
# Initialize components in a specific order
31+
# First, force garbage collection to free memory
32+
gc.collect()
33+
34+
# Initialize the sound manager
35+
print("Initializing sound manager...")
36+
self.sound_manager = SoundManager(
37+
audio_output=audio_output,
38+
seconds_per_eighth=SECONDS_PER_EIGHTH
39+
)
40+
41+
# Give hardware time to stabilize
42+
time.sleep(0.5)
43+
gc.collect()
44+
45+
# Initialize the note manager
46+
print("Initializing note manager...")
47+
self.note_manager = NoteManager(
48+
start_margin=25, # START_MARGIN
49+
staff_y_start=int(240 * 0.1), # STAFF_Y_START
50+
line_spacing=int((240 - int(240 * 0.1) - int(240 * 0.2)) * 0.95) // 8 # LINE_SPACING
51+
)
52+
53+
gc.collect()
54+
55+
# Initialize the UI manager
56+
print("Initializing UI manager...")
57+
self.ui_manager = UIManager(self.sound_manager, self.note_manager)
58+
59+
def run(self):
60+
"""Set up and run the application"""
61+
# Setup the display and UI
62+
print("Setting up display...")
63+
self.ui_manager.setup_display()
64+
65+
# Give hardware time to stabilize
66+
time.sleep(0.5)
67+
gc.collect()
68+
69+
# Try to find the mouse with multiple attempts
70+
MAX_ATTEMPTS = 5
71+
RETRY_DELAY = 1 # seconds
72+
73+
mouse_found = False
74+
for attempt in range(MAX_ATTEMPTS):
75+
print(f"Mouse detection attempt {attempt+1}/{MAX_ATTEMPTS}")
76+
if self.ui_manager.find_mouse():
77+
mouse_found = True
78+
print("Mouse found successfully!")
79+
break
80+
81+
print(f"Mouse detection attempt {attempt+1} failed, retrying...")
82+
time.sleep(RETRY_DELAY)
83+
84+
if not mouse_found:
85+
print("WARNING: Mouse not found after multiple attempts.")
86+
print("The application will run, but mouse control may be limited.")
87+
88+
# Enter the main loop
89+
self.ui_manager.main_loop()
90+
91+
92+
# Create and run the application
93+
if __name__ == "__main__":
94+
# Start with garbage collection
95+
gc.collect()
96+
print("Starting Music Staff Application...")
97+
98+
#
99+
try:
100+
app = MusicStaffApp(audio_output=AUDIO_OUTPUT)
101+
app.run()
102+
except Exception as e:
103+
print(f"Error with I2S audio: {e}")
104+
print("Retrying with PWM audio...")
105+
106+
# Force garbage collection
107+
gc.collect()
108+
time.sleep(1)
109+
110+
# Fallback to PWM
111+
try:
112+
app = MusicStaffApp(audio_output="pwm")
113+
app.run()
114+
except Exception as e:
115+
print(f"Fatal error: {e}")

0 commit comments

Comments
 (0)