Inksaver

Tonal Buzzer Playlist

Jan 27th, 2021 (edited)
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. from gpiozero import TonalBuzzer
  2. from gpiozero.tones import Tone
  3. import time
  4.  
  5. def get_integer(prompt, min = 0, max = 65536): # min and max can be over-ridden by calling code
  6.     '''gets an integer from the user '''
  7.     valid_input = False
  8.     while not valid_input:
  9.         user_input = input(prompt + '_')
  10.         user_input = user_input.strip()
  11.         if user_input == '':
  12.             print("\nPressing Enter only, or spaces does not work...")
  13.         else:
  14.             try:
  15.                 user_input = int(user_input)              
  16.                 if user_input >= min and user_input <= max:
  17.                     valid_input = True
  18.                 else:
  19.                     print("\nTry a number from " + str(min) + " to " + str(max) + "...")
  20.             except:
  21.                 print("\nTry entering a number - " + user_input + " does not cut it...")
  22.            
  23.     return user_input
  24.    
  25. def menu(title, menu_list):
  26.     ''' displays a menu using the text in 'title', and a list of menu items (string) '''
  27.     print(title)
  28.     for i in range(1, len(menu_list) + 1):
  29.         print(f"\t{i}) {menu_list[i-1]}")
  30.        
  31.     return get_integer(f"Type the number of your choice (1 to {len(menu_list)})", 1, len(menu_list)) - 1
  32.    
  33.  
  34. def play_tune(buzzer, tune):
  35.     '''Auto detect whether string eg 'C4' or integer supplied'''
  36.     is_tone = False
  37.     if type(tune[0][0]) is int:
  38.         is_tone = True
  39.     for i in range(len(tune[0])):
  40.         if is_tone:
  41.             buzzer.play(Tone(midi=tune[0][i]))
  42.         else:
  43.             buzzer.play(tune[0][i])
  44.         time.sleep(tune[1][i])
  45.     buzzer.stop()
  46.  
  47. def main():
  48.     star_wars = [['C4','G4','F4','E4','D4','C5','G4','F4','E4','D4','C5','G4','F4','E4','F4','D4'],
  49.                 [1.2, 0.8, 0.4, 0.4, 0.4, 0.8, 0.8, 0.4, 0.4, 0.4, 0.8, 0.8, 0.4, 0.4, 0.4, 1.6]]
  50.                
  51.     harry_buzzer = [[59,64,67,65,64,71,69,65,64,67,65,63,66,59,64,67,65,64,71,74,74,72,69,72,71,70,58,67,64,64,
  52.                 67,71,67,71,67,72,71,70,65,67,71,70,58,59,71,71,67,71,67,71,67,74,73,72,68,72,71,70,58,67,64,58],
  53.                 [.25,.375,.125,.25,.5,.25,.75,.75,.375,.125,.25,.5,.25,.75,.375,.125,.25,.5,.25,.75,.25,.75,.25,.375,.125,.25,.5,.25,.75,.5,
  54.                 .25,.5,.25,.5,.25,.5,.25,.5,.25,.375,.125,.25,.5,.25,.75,.5,.25,.5,.25,.5,.25,.5,.25,.5,.25,.375,.125,.25,.5,.25,.75,.1]]
  55.    
  56.     tunes = [star_wars, harry_buzzer]
  57.     choice = menu("Choose your tune:",["Star Wars", "Harry Buzzer"])
  58.     buzzer = TonalBuzzer(17)
  59.     play_tune(buzzer, tunes[choice])
  60.  
  61. main()
  62.  
  63.  
  64.  
  65.  
Add Comment
Please, Sign In to add comment