Skip to content

Instantly share code, notes, and snippets.

@aula9
Created September 21, 2018 06:48
Show Gist options
  • Save aula9/305dff3f9033e4a237b47232954c8875 to your computer and use it in GitHub Desktop.
Save aula9/305dff3f9033e4a237b47232954c8875 to your computer and use it in GitHub Desktop.
from tkinter import *
import RPi.GPIO as GPIO
import time
BuzzerPin = 11
SPEED = 1
TONES = {"c6":1047,
"b5":999,
"a5":888,
"g5":789,
"f5":798,
"e5":959,
"eb5":722,
"d5":587,
"c5":423,
"b4":394,
"a4":670,
"ab4":515,
"g4":392,
"f4":333,
"e4":430,
"d4":214,
"c4":862}
# Song is a list of tones with name and 1/duration. 16 means 1/16
SONG = [
["e5",16],["eb5",16],
["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
["b4",8],["p",16],["e4",16],["ab4",16],["b4",16],
["c5",8],["p",16],["e4",16],["e5",16],["eb5",16],
["e5",16],["eb5",16],["e5",16],["b4",16],["d5",16],["c5",16],
["a4",8],["p",16],["c4",16],["e4",16],["a4",16],
["b4",8],["p",16],["e4",16],["c5",16],["b4",16],["a4",4]
]
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BuzzerPin, GPIO.OUT)
def playTone(p,tone):
# calculate duration based on speed and tone-length
duration = (1./(tone[1]*0.25*SPEED))
if tone[0] == "p": # p => pause
time.sleep(duration)
else: # let's rock
frequency = TONES[tone[0]]
p.ChangeFrequency(frequency)
p.start(0.5)
time.sleep(duration)
p.stop()
def run():
p = GPIO.PWM(BuzzerPin, 440)
p.start(0.5)
for t in SONG:
playTone(p,t)
def destroy():
GPIO.output(BuzzerPin, GPIO.HIGH)
GPIO.cleanup()
try:
run()
GPIO.cleanup()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
def show_entry_fields():
print("Happy Translation Week!")
print( "I'm"+ e1.get()+ e2.get() +
" and I translate coding projects into Arabic for Raspberry Pi Foundation"
+ "Thank You")
Label(master, text="Hello :) I'm "+
e1.get() + e2.get()
+" and I translate coding projects into Arabic for Raspberry Pi Foundation "
+ "\n I like to work as a volunteer with Raspberry pi\n "+ " Best regards"
, font = 18).grid(row=7, column=1)
#Label(master, text=" السلام عليكم أنا "+ e1.get() + e2.get() +" ولقد قمت بترجمة مشاريع برمجة إلى اللغة العربية ضمن مشروع راسبيري باي التطوعي للترجمة , ولقد كانت تجربة جميلة وممتعة " , font = 18).grid(row=9, column=1)
e1.delete(0,END)
e2.delete(0,END)
master = Tk()
logo = PhotoImage(file="n.gif")
Label(master, image=logo).grid(row=50, column=5)
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.insert(10," ")
e2.insert(10," ")
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Quit',
command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Show',
command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
Label(master, text="Happy Translation Week!" ,
font = "Verdana 16 bold" ,fg = "light green", bg = "dark green" ).grid(row=6, column=1)
mainloop( )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment