Skip to content

Instantly share code, notes, and snippets.

@epilys
Created January 3, 2024 22:01
Show Gist options
  • Save epilys/7a911d3ba6f40da1ba668f2ca9be7336 to your computer and use it in GitHub Desktop.
Save epilys/7a911d3ba6f40da1ba668f2ca9be7336 to your computer and use it in GitHub Desktop.
#!python3
# Copyright (c) 2024 Manos Pitsidianakis <[email protected]>
# Licensed under the EUPL-1.2-or-later.
#
# You may obtain a copy of the Licence at:
# https://joinup.ec.europa.eu/software/page/eupl
#
# SPDX-License-Identifier: EUPL-1.2
import RPi.GPIO as GPIO
from time import sleep
"""
Connect PC SPKR:
/-< cathode
| /-< anode
______|________|__________________
| v v |
| ⚬ ⚬ ⚬ ⚬ ⚬ ⚬ ⚬ ⚬ ⚬ ⚬ |
| G G 3 P P G G G G G |
| N N V 2 2 0 0 0 0 1 |
| D D 3 7 2 9 0 1 5 2 |
| O |
. .
___________________________________
Beeper GPIO diagram on
top of the screen
"""
"""
The tunes follow the GRUB_INIT_TUNE format: tempo [freq duration]*
"""
ZELDA_SECRET = "1000 784 2 740 2 622 2 440 2 415 2 659 2 831 2 1046 5"
EXORCIST = "300 328 1 440 1 328 1 494 1 328 1 390 1 440 1"
ZELDA_GREAT_FAIRY = "1999 1760 5 0 1 1175 5 0 1 932 5 0 1 784 5 0 1 1568 5 0 1 1175 5 0 1 932 5 0 1 784 5 0 1 1397 5 0 1 1175 5 0 1 932 5 0 1 784 5 0 1 1568 5 0 1 1175 5 0 1 932 5 0 1 784 5 0 1 1568 5 0 1 1047 5 0 1 880 5 0 1 698 5 0 1 1397 5 0 1 1047 5 0 1 880 5 0 1 698 5 0 1 1319 5 0 1 1047 5 0 1 880 5 0 1 698 5 0 1 1397 5 0 1 1047 5 0 1 880 5 0 1 698 5 0 1 1397 5 0 1 932 5 0 1 784 5 0 1 659 5 0 1 1319 5 0 1 932 5 0 1 784 5 0 1 659 5 0 1 1245 5 0 1 932 5 0 1 784 5 0 1 659 5 0 1 1319 5 0 1 932 5 0 1 784 5 0 1 659 5 0 1 1319 5 0 1 880 5 0 1 698 5 0 1 587 5 0 1 1175 5 0 1 880 5 0 1 698 5 0 1 587 5 0 1 1109 5 0 1 880 5 0 1 698 5 0 1 587 5 0 1 1175 5 0 1 880 5 0 1 698 5 0 1 587 5 0 1"
PFSENSE_STARTUP = "240 500 1 400 1 600 1 800 1 800 1"
PFSENSE_SHUTDOWN = "240 600 1 800 1 500 1 400 1 400 1"
SUPER_MARIO = "1000 334 1 334 1 0 1 334 1 0 1 261 1 334 1 0 1 392 2 0 4 196 2"
SUPER_MARIO_COIN = "600 988 1 1319 8"
SUPER_MARIO_MUSHROOM = "1750 523 1 392 1 523 1 659 1 784 1 1047 1 784 1 415 1 523 1 622 1 831 1 622 1 831 1 1046 1 1244 1 1661 1 1244 1 466 1 587 1 698 1 932 1 1195 1 1397 1 1865 1 1397 1"
FUR_ELISE = "480 420 1 400 1 420 1 400 1 420 1 315 1 370 1 335 1 282 3 180 1 215 1 282 1 315 3 213 1 262 1 315 1 335 3 213 1 420 1 400 1 420 1 400 1 420 1 315 1 370 1 335 1 282 3 180 1 215 1 282 1 315 3 213 1 330 1 315 1 282 3"
MY_LITTLE_PONY = "2400 587 18 554 4 587 8 659 12 587 16 0 10 587 4 659 4 740 8 587 4 784 12 740 8 659 8 587 4 740 20 587 40"
MACINTOSH = "1000 271 3 331 3 394 3 443 8"
BEEP = "480 440 1"
ZELDA_OVERWORLD = (
"12000 440 100 0 1 329 150 440 50 0 1 440 25 493 25 523 25 587 25 659 200"
)
ALL = {
"ZELDA_SECRET": ZELDA_SECRET,
"EXORCIST": EXORCIST,
"ZELDA_GREAT_FAIRY": ZELDA_GREAT_FAIRY,
"PFSENSE_STARTUP": PFSENSE_STARTUP,
"PFSENSE_SHUTDOWN": PFSENSE_SHUTDOWN,
"SUPER_MARIO": SUPER_MARIO,
"SUPER_MARIO_COIN": SUPER_MARIO_COIN,
"SUPER_MARIO_MUSHROOM": SUPER_MARIO_MUSHROOM,
"FUR_ELISE": FUR_ELISE,
"MACINTOSH": MACINTOSH,
"BEEP": BEEP,
"ZELDA_OVERWORLD": ZELDA_OVERWORLD,
}
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, GPIO.HIGH)
pin22 = GPIO.PWM(22, 20)
pin22.ChangeDutyCycle(20)
def calc_dur(dur_secs=10, tempo=480):
return ((60000 * dur_secs / tempo)) / 1000.0
def play_freq(freq, dur, tempo):
if freq > 0.0:
pin22.ChangeFrequency(freq)
pin22.start(50)
# print("sleeping for dur_secs = ", dur, " calc_dur =", calc_dur(dur, tempo))
sleep(calc_dur(dur, tempo))
pin22.stop()
return
sleep(calc_dur(dur, tempo))
def play_tune(TUNE):
ws = list(TUNE.split(" "))
tempo = float(ws[0])
# print("tempo = ", tempo)
ws = ws[1:]
while len(ws) > 0:
# print("len left = ", len(ws))
freq = float(ws[0])
dur = float(ws[1])
ws = ws[2:]
play_freq(freq, dur, tempo)
for k in ALL:
print("playing ", k, "...")
play_tune(ALL[k])
# print("done.")
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment