From 4945a6df404b0c2045c2e41bd445a445b1f1867a Mon Sep 17 00:00:00 2001 From: cf2g21 <cf2g21@soton.ac.uk> Date: Sat, 13 May 2023 22:44:03 +0000 Subject: [PATCH] Upload skeleton.py --- skeleton.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 skeleton.py diff --git a/skeleton.py b/skeleton.py new file mode 100644 index 0000000..a2fcd1b --- /dev/null +++ b/skeleton.py @@ -0,0 +1,137 @@ +from machine import Pin, PWM +import time + +# TODO: Find buzzer pin value + +# Note to frequency +note_freq = { + 'B1': 31, + 'C2': 33, + 'CS2': 35, + 'D2': 37, + 'DS2': 39, + 'E2': 41, + 'F2': 44, + 'FS2': 46, + 'G2': 49, + 'GS2': 52, + 'A2': 55, + 'AS2': 58, + 'B2': 62, + 'C3': 65, + 'CS3': 69, + 'D3': 73, + 'DS3': 78, + 'E3': 82, + 'F3': 87, + 'FS3': 93, + 'G3': 98, + 'GS3': 104, + 'A3': 110, + 'AS3': 117, + 'B3': 123, + 'C4': 131, + 'CS4': 139, + 'D4': 147, + 'DS4': 156, + 'E4': 165, + 'F4': 175, + 'FS4': 185, + 'G4': 196, + 'GS4': 208, + 'A4': 220, + 'AS4': 233, + 'B4': 247, + 'C5': 262, + 'CS5': 277, + 'D5': 294, + 'DS5': 311, + 'E5': 330, + 'F5': 349, + 'FS5': 370, + 'G5': 392, + 'GS5': 415, + 'A5': 440, + 'AS5': 466, + 'B5': 494, + 'C6': 523, + 'CS6': 554, + 'D6': 587, + 'DS6': 622, + 'E6': 659, + 'F6': 698, + 'FS6': 740, + 'G6': 784, + 'GS6': 831, + 'A6': 880, + 'AS6': 932, + 'B6': 988, + 'C7': 1047, + 'CS7': 1109, + 'D7': 1175, + 'DS7': 1245, + 'E7': 1319, + 'F7': 1397, + 'FS7': 1480, + 'G7': 1568, + 'GS7': 1661, + 'A7': 1760, + 'AS7': 1865, + 'B7': 1976, + 'C8': 2093, + 'CS8': 2217, + 'D8': 2349, + 'DS8': 2489, + 'E8': 2637, + 'F8': 2794, + 'FS8': 2960, + 'G8': 3136, + 'GS8': 3322, + 'A8': 3520, + 'AS8': 3729, + 'B8': 3951, + 'C9': 4186, + 'CS9': 4435, + 'D9': 4699, + 'DS9': 4978, + 'P': 0 + } + + + + + + + +def parseRTTTL(text): + try: + whole = (60000/b)*4 + # TODO: Parse the title, default values (d, o, b) and notes from the text + except: + return 'Not a valid RTTTL string' + notes = 'abcdefgp' + freqLengthList = [] + # TODO: From the text, construct a list of pairs - the frequency representing the note (using note_freq dictionary) and the length of the note as an int + # Consult the RTTTL specification to find out what to do add to the frequency key in the case of no length or octave being provided, and the use of 'p' + length = whole/length + if '.' in note: + length += length/2 + return freqLengthList + +def play(text): + song = parseRTTTL(text) + if type(song) is not list: + return song + for freq, seconds in song: + milliseconds = seconds * 0.001 + if freq > 0: + # TODO: Make the buzzer sound + time.sleep(milliseconds*0.9) + if freq > 0: + buzzer.deinit() + # Stop buzzer after playing + time.sleep(milliseconds*0.1) # 0.1 second gap between notes + +exampleSong = 'MissionImpossible:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d' + +play(exampleSong) -- GitLab