Skip to content

Instantly share code, notes, and snippets.

@root42
Last active September 10, 2022 23:41
Show Gist options
  • Save root42/bd22b0b1a29b256b716b979848ad06e6 to your computer and use it in GitHub Desktop.
Save root42/bd22b0b1a29b256b716b979848ad06e6 to your computer and use it in GitHub Desktop.
Plays "silent night" on a piezo buzzer attached to pin 18 of the Onion Omega2
#!/usr/bin/env python
# Plays "silent night" on a piezo buzzer attached to pin 18 of the Onion Omega2
# Things required: python lite, pwm needs to be enabled via:
# omega2-ctrl gpiomux set pwm0 pwm
from time import sleep
exportStr="/sys/class/pwm/pwmchip0/export"
periodStr="/sys/class/pwm/pwmchip0/pwm%s/period"
dutyStr="/sys/class/pwm/pwmchip0/pwm%s/duty_cycle"
enableStr="/sys/class/pwm/pwmchip0/pwm%s/enable"
unexportStr="/sys/class/pwm/pwmchip0/unexport"
# set a PWM channel to a duty cycle and frequency
def setPwmChannel( channel, duty, freq ):
period = 1.0 / freq * 1000000000
pulseWidth = period * duty / 100.0
period = int(period)
pulseWidth = int(pulseWidth)
# set the PWM
with open(exportStr,"w") as exportFile:
exportFile.write( str(channel) + "\n" )
with open(periodStr%str(channel),"w") as periodFile:
periodFile.write( str(period) + "\n" )
with open(dutyStr%str(channel),"w") as dutyFile:
dutyFile.write( str(pulseWidth) + "\n" )
with open(enableStr%str(channel),"w") as enableFile:
enableFile.write( "1\n" )
with open(unexportStr,"w") as unexportFile:
unexportFile.write( str(channel) + "\n" )
def disablePwmChannel( channel ):
# disable the PWM chanel
with open(exportStr,"w") as exportFile:
exportFile.write( str(channel) + "\n" )
with open(enableStr%str(channel),"w") as enableFile:
enableFile.write( "0\n" )
with open(unexportStr,"w") as unexportFile:
unexportFile.write( str(channel) + "\n" )
notes = {
"cL": 129,
"cLS": 139,
"dL": 146,
"dLS": 156,
"eL": 163,
"fL": 173,
"fLS": 185,
"gL": 194,
"gLS": 207,
"aL": 219,
"aLS": 228,
"bL": 232,
"c": 261,
"cS": 277,
"d": 294,
"dS": 311,
"e": 329,
"f": 349,
"fS": 370,
"g": 391,
"gS": 415,
"a": 440,
"aS": 455,
"b": 466,
"cH": 523,
"cHS": 554,
"dH": 587,
"dHS": 622,
"eH": 659,
"fH": 698,
"fHS": 740,
"gH": 784,
"gHS": 830,
"aH": 880,
"aHS": 910,
"bH": 933
}
def beep( note, duration ):
if note == "pause":
sleep( duration / 1000.0 )
return
setPwmChannel( 0, 50, notes[note] )
sleep( duration / 1000.0 )
disablePwmChannel( 0 )
sleep( 0.02 )
song = [
( "f",750 ), # si-
( "g",250 ),
( "f",500 ), # lent
( "d",1500 ), # night
( "f",750 ), # ho-
( "g",250 ),
( "f",500 ), # ly
( "d",1500 ), # night
( "cH",1000 ), # all
( "cH",500 ), # is
( "a",1500 ), # calm
( "aS",1000 ), # all
( "aS",500 ), # is
( "f",1000 ), # bright
( "pause", 500 ),
( "g",1000 ), # round
( "g",500 ), # you
( "aS",750 ), # vir-
( "a",250 ),
( "g",500 ), # gin
( "f",750 ), # moth-
( "g",250 ), # er
( "f",500 ), # and
( "d",1000 ), # child
( "pause", 500 ),
( "g",1000 ), # ho-
( "g",500 ), # ly
( "aS",750 ), # in-
( "a",250 ), # fant
( "g",500 ), # so
( "f",750 ), # ten-
( "g",250 ), # der
( "f",500 ), # and
( "d",1500 ), # mild
( "cH",1000 ), # sleep
( "cH",500 ), # in
( "dHS",750 ), # heav-
( "cH",250 ), # en-
( "a",500 ), # ly
( "aS",1500 ), # peace
( "dH",1500 ),
( "aS",500 ), # Sleep
( "f",500 ),
( "d",500 ), # in
( "f",750 ), # heav-
( "dS",250 ), # en-
( "c",500 ), # ly
( "bL",1500 ) # peace
]
map( lambda x: beep(x[0],x[1]),song )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment