Skip to content

Commit

Permalink
Merge pull request #200 from jbanana/TinyTunes_1_2
Browse files Browse the repository at this point in the history
TinyTunes 1.2
  • Loading branch information
jmtinycircuits authored Dec 4, 2023
2 parents 0e85e6b + f919acb commit bf6b57d
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 49 deletions.
116 changes: 86 additions & 30 deletions TinyTunes/TinyTunes.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,24 @@ def splash():
thumby.display.drawText( "Tunes", thumby.display.width - 48, 10, 1 )
thumby.display.update()
thumby.display.setFont( "/lib/font5x7.bin", 5, 10, 1 )
thumby.display.drawText( "Music editor", 1, 22, 1 )
thumby.display.update()
thumby.display.update()
thumby.display.drawText( "A/B: start", 7, 33, 1 )
while( not thumby.actionJustPressed() ):
thumby.display.drawText( "Music editor", 1, 22, 1 )
buttonMsg = 'Help: A Start: B'
offset = 0
strLen = len( buttonMsg ) * 6 + thumby.display.width
global baseFPS
thumby.display.setFPS( 45 )
while True:
thumby.display.update()
if thumby.buttonA.justPressed():
thumby.display.setFPS( baseFPS )
return True
if thumby.buttonB.justPressed():
thumby.display.setFPS( baseFPS )
return False
thumby.display.drawFilledRectangle(0, 33, thumby.display.width, 8, 0)
thumby.display.drawText( buttonMsg, thumby.display.width - offset, 33, 1 )
offset = ( offset + 1 ) % strLen

def instructions():
thumby.display.fill( 0 )
Expand All @@ -41,15 +53,15 @@ def instructions():
thumby.display.drawText( " L: prev", 0, 16, 1 )
thumby.display.drawText( " R: nxt/add", 0, 24, 1 )
thumby.display.drawText( " -- more --", 0, 32, 1 )
while( not thumby.actionJustPressed() ):
while not thumby.actionJustPressed():
thumby.display.update()
thumby.display.fill( 0 )
thumby.display.drawText( "A: menu,", 0, 0, 1 )
thumby.display.drawText( " choose", 0, 8, 1 )
thumby.display.drawText( "B: play tune", 0, 16, 1 )
thumby.display.drawText( " close menu", 0, 24, 1 )
thumby.display.drawText( " -- start --", 0, 32, 1 )
while( not thumby.actionJustPressed() ):
while not thumby.actionJustPressed():
thumby.display.update()

sprites = {
Expand Down Expand Up @@ -324,7 +336,7 @@ def play( self ):
totalMillis = totalMillis * 1.5
if not 'r' in self.flags:
if 'z' in self.flags:
soundMillis = totalMillis
soundMillis = totalMillis * 1.1
elif 's' in self.flags:
soundMillis = totalMillis * 0.75
else:
Expand Down Expand Up @@ -432,11 +444,13 @@ def prev( self ):
else:
buzz()

def next( self ):
def next( self, allowDuplicate ):
if self.note < len( self.notes ) - 1:
self.note = self.note + 1
else:
elif allowDuplicate:
self.duplicate()
else:
buzz()

def shorten( self ):
self.notes[ self.note ].shorten()
Expand Down Expand Up @@ -524,11 +538,13 @@ def __init__( self, items ):
self.item = 0

def handleButton( self, button ):
if button == 'U':
if not button:
return
if button in 'Uu':
if self.item > 0:
self.item = self.item - 1
return False
if button == 'D':
if button in 'Dd':
if self.item < len( self.items ) - 1:
self.item = self.item + 1
return False
Expand Down Expand Up @@ -577,7 +593,9 @@ def __init__( self ):
( 'Play here', lambda: self.tune.playHere() ),
( 'Save...', lambda: self.menuSave() ),
( 'Load...', lambda: self.load() ),
( 'Del save...', lambda: self.delete() )
( 'Del save...', lambda: self.delete() ),
( 'New tune...', lambda: self.confirmNew() ),
( 'Exit', lambda: thumby.reset() )
]
)
self.noteMenu = Menu(
Expand Down Expand Up @@ -612,35 +630,54 @@ def menuNote( self ):
self.noteMenu.display()

def menuSave( self ):
menuItems = []
if self.fileName:
menuItems.append( ( self.fileName, lambda: self.saveReplace() ) )
menuItems.append( ( 'Save as...', lambda: self.saveAs() ) )
saveMenu = Menu( menuItems )
saveMenu = Menu(
[
( self.fileName, lambda: self.saveReplace() ),
( 'Save as...', lambda: self.saveAs() )
]
)
saveMenu.display()
while not saveMenu.handleButton( buttons.whichButton() ):
saveMenu.display()
else:
self.saveAs()

def saveAs( self ):
self.fileName = keyboard.Keyboard().getOutput()
if self.fileName in self.directory:
tempFileName = keyboard.Keyboard().getOutput()
print( 'Name is ' + tempFileName )
if tempFileName in self.directory:
print( 'Name ' + tempFileName + ' found in directory' )
confirmMenu = Menu(
[
( 'Replace', lambda : self.saveReplace() ),
( 'Cancel', lambda : None )
( 'Cancel', lambda: None ),
( 'Replace', lambda: self.saveWithName( tempFileName ) )
]
)
while not confirmMenu.handleButton( buttons.whichButton() ):
confirmMenu.display()
else:
self.fileName = tempFileName
print( 'Saving as ' + self.fileName )
if not self.fileName in self.directory:
print( 'Writing directory entry for ' + self.fileName )
self.directory.append( self.fileName )
self.writeDir()
print( 'Writing tune for ' + self.fileName )
thumby.saveData.setItem( self.fileName, str( self.tune ) )
thumby.saveData.save()

def saveWithName( self, fileName ):
self.fileName = fileName
self.saveReplace()

thumby.saveData.setItem( self.fileName, str( self.tune ) )
thumby.saveData.save()

def saveReplace( self ):
print( 'Replacing ' + self.fileName )
if not self.fileName in self.directory:
print( 'Writing directory entry for ' + self.fileName )
self.directory.append( self.fileName )
self.writeDir()
print( 'Writing tune for ' + self.fileName )
thumby.saveData.setItem( self.fileName, str( self.tune ) )
thumby.saveData.save()

Expand All @@ -665,7 +702,7 @@ def noTunes( self ):
thumby.display.fill( 0 )
thumby.display.drawText( 'No tunes', 10, 9, 1 )
thumby.display.drawText( 'A/B continue', 0, 25, 1 )
while( not thumby.actionJustPressed() ):
while not thumby.actionJustPressed():
thumby.display.update()

def loadName( self, name ):
Expand Down Expand Up @@ -699,19 +736,38 @@ def deleteName( self, name ):
self.writeDir()
thumby.saveData.save()

def confirmNew( self ):
confirmMenu = Menu(
[
( 'No, keep', lambda: None ),
( 'Yes, new', lambda: self.newTune() )
]
)
confirmMenu.display()
while not confirmMenu.handleButton( buttons.whichButton() ):
confirmMenu.display()

def newTune( self ):
self.tune = Tune()
self.fileName = None

def handleInput( self ):
button = buttons.whichButton()
if not button:
return
if self.menuMode:
if self.menu.handleButton( button ):
self.menuMode = False
elif button == 'U':
elif button in 'Uu':
self.tune.raisePitch()
elif button == 'D':
elif button in 'Dd':
self.tune.lowerPitch()
elif button == 'L':
elif button in 'Ll':
self.tune.prev()
elif button == 'R':
self.tune.next()
self.tune.next( True )
elif button == 'r':
self.tune.next( False )
elif button == 'B':
self.tune.play()
elif button == 'A':
Expand All @@ -724,8 +780,8 @@ def display( self ):
self.tune.display()

thumby.display.setFPS( baseFPS )
splash()
instructions()
if splash():
instructions()
editor = Editor()
while True:
editor.handleInput()
Expand Down
10 changes: 9 additions & 1 deletion TinyTunes/arcade_description.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ A music editor: write tunes on your Thumby.

If you can't read music, don't worry, just experiment. You can write a tune!

v1.0
v1.2
Better scrolling through tunes and menu
Wrap around keyboard
Added "Exit" menu item
v1.1
Instructions optional
"New tune" menu option
Better slurring
"Save As" fix
54 changes: 42 additions & 12 deletions TinyTunes/buttons.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
import thumby

repeat = 0

def whichButton():
if thumby.buttonU.justPressed():
return 'U'
if thumby.buttonD.justPressed():
return 'D'
if thumby.buttonL.justPressed():
return 'L'
if thumby.buttonR.justPressed():
return 'R'
if thumby.buttonA.justPressed():
return 'A'
if thumby.buttonB.justPressed():
return 'B'
global repeat
if repeat >= 4:
if thumby.buttonU.pressed():
return 'u'
elif thumby.buttonD.pressed():
return 'd'
elif thumby.buttonL.pressed():
return 'l'
elif thumby.buttonR.pressed():
return 'r'
elif thumby.buttonA.pressed():
return 'a'
elif thumby.buttonB.pressed():
return 'b'
else:
repeat = 0
elif repeat > 0:
if thumby.inputPressed():
repeat = repeat + 1
else:
repeat = 0
else:
if thumby.buttonU.justPressed():
repeat = 1
return 'U'
if thumby.buttonD.justPressed():
repeat = 1
return 'D'
if thumby.buttonL.justPressed():
repeat = 1
return 'L'
if thumby.buttonR.justPressed():
repeat = 1
return 'R'
if thumby.buttonA.justPressed():
repeat = 1
return 'A'
if thumby.buttonB.justPressed():
repeat = 1
return 'B'
return None
20 changes: 14 additions & 6 deletions TinyTunes/keyboard.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ def __init__( self, rowNum, letters, selected = 0 ):
self.select( selected )

def select( self, index ):
rowLen = len( self.keys )
self.keys[ self.selected ].select( False )
self.selected = max( 0, min( len( self.keys ) - 1, index ) )
if index < 0:
self.selected = rowLen - 1
elif index >= rowLen:
self.selected = 0
else:
self.selected = index
self.keys[ self.selected ].select( True )

def deselect( self ):
Expand Down Expand Up @@ -104,23 +110,25 @@ def display( self ):

def handleInput( self ):
button = buttons.whichButton()
if button == 'U':
if not button:
return None
if button in 'Uu':
if self.row > 0:
index = self.rows[ self.row ].selected
self.rows[ self.row ].deselect()
self.row = self.row - 1
self.rows[ self.row ].select( index )
elif button == 'D':
elif button in 'Dd':
if self.row < len( self.rows ) - 1:
index = self.rows[ self.row ].selected
self.rows[ self.row ].deselect()
self.row = self.row + 1
self.rows[ self.row ].select( index )
elif button == 'R':
elif button in 'Rr':
self.rows[ self.row ].right()
elif button == 'L':
elif button in 'Ll':
self.rows[ self.row ].left()
elif button == 'A':
elif button in 'Aa':
keyOut = self.rows[ self.row ].getLetter()
self.output = self.output + keyOut
elif button == 'B':
Expand Down

0 comments on commit bf6b57d

Please sign in to comment.