Guest User

Untitled

a guest
Sep 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. from tkinter import *
  2. import RPi.GPIO as GPIO
  3. import time
  4. BuzzerPin = 11
  5. SPEED = 1
  6. TONES = {"c6":1047,
  7. "b5":999,
  8. "a5":888,
  9. "g5":789,
  10. "f5":798,
  11. "e5":959,
  12. "eb5":722,
  13. "d5":587,
  14. "c5":423,
  15. "b4":394,
  16. "a4":670,
  17. "ab4":515,
  18. "g4":392,
  19. "f4":333,
  20. "e4":430,
  21. "d4":214,
  22. "c4":862}
  23.  
  24. # Song is a list of tones with name and 1/duration. 16 means 1/16
  25. SONG = [
  26. ["e5",16],["eb5",16],
  27. ["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
  28. ["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
  29. ["b4",8],["p",16],["e4",16],["ab4",16],["b4",16],
  30. ["c5",8],["p",16],["e4",16],["e5",16],["eb5",16],
  31. ["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
  32. ["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
  33. ["b4",8],["p",16],["e4",16],["c5",16],["b4",16],["a4",4]
  34. ]
  35. GPIO.setmode(GPIO.BOARD)
  36. GPIO.setup(BuzzerPin, GPIO.OUT)
  37. def playTone(p,tone):
  38. # calculate duration based on speed and tone-length
  39. duration = (1./(tone[1]*0.25*SPEED))
  40.  
  41. if tone[0] == "p": # p => pause
  42. time.sleep(duration)
  43. else: # let's rock
  44. frequency = TONES[tone[0]]
  45. p.ChangeFrequency(frequency)
  46. p.start(0.5)
  47. time.sleep(duration)
  48. p.stop()
  49.  
  50. def run():
  51. p = GPIO.PWM(BuzzerPin, 440)
  52. p.start(0.5)
  53. for t in SONG:
  54. playTone(p,t)
  55.  
  56. def destroy():
  57. GPIO.output(BuzzerPin, GPIO.HIGH)
  58. GPIO.cleanup()
  59. try:
  60. run()
  61. GPIO.cleanup()
  62. except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
  63. destroy()
  64. def show_entry_fields():
  65. print("Happy Translation Week!")
  66. print( "I'm"+ e1.get()+ e2.get() +
  67. " and I translate coding projects into Arabic for Raspberry Pi Foundation"
  68. + "Thank You")
  69. Label(master, text="Hello :) I'm "+
  70. e1.get() + e2.get()
  71. +" and I translate coding projects into Arabic for Raspberry Pi Foundation "
  72. + "\n I like to work as a volunteer with Raspberry pi\n "+ " Best regards"
  73. , font = 18).grid(row=7, column=1)
  74. #Label(master, text=" السلام عليكم أنا "+ e1.get() + e2.get() +" ولقد قمت بترجمة مشاريع برمجة إلى اللغة العربية ضمن مشروع راسبيري باي التطوعي للترجمة , ولقد كانت تجربة جميلة وممتعة " , font = 18).grid(row=9, column=1)
  75. e1.delete(0,END)
  76. e2.delete(0,END)
  77. master = Tk()
  78. logo = PhotoImage(file="n.gif")
  79. Label(master, image=logo).grid(row=50, column=5)
  80. Label(master, text="First Name").grid(row=0)
  81. Label(master, text="Last Name").grid(row=1)
  82.  
  83. e1 = Entry(master)
  84. e2 = Entry(master)
  85. e1.insert(10," ")
  86. e2.insert(10," ")
  87.  
  88. e1.grid(row=0, column=1)
  89. e2.grid(row=1, column=1)
  90.  
  91. Button(master, text='Quit',
  92. command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
  93. Button(master, text='Show',
  94. command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
  95. Label(master, text="Happy Translation Week!" ,
  96. font = "Verdana 16 bold" ,fg = "light green", bg = "dark green" ).grid(row=6, column=1)
  97. mainloop( )
Add Comment
Please, Sign In to add comment