# cPy - sound huiswerk opgage, laat horen: do, re mi, fa .. do
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-01-08  initial release

import	time
from	machine	import	Pin, PWM

# ============================ main ===============================

# convert tone like 'A1', 'C3', 'C#3' into a integer frequency
def toneToFreq(tone='A1'):
    try:
        octave  = int(tone[-1])
        pitch_s = tone[:-1]
        pitch   = ('A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#').index(pitch_s)
        return round(27.5 * pow(2, octave + pitch / 12))
    except:
        print('toneToFreq: exception with ', tone)
        

def doReMi():
    pwm = PWM(Pin(25), duty_u16=0x8000)
    try:
#        for tone in ('C2', 'D2', 'E2', 'F2', 'G2', 'A3', 'B3', 'C3'):
        for tone in ('C2', 'C#2', 'D2', 'D#2', 'E2', 'F2', 'F#2', 'G2', 'G#2', 'A3', 'A#3', 'B3', 'C3'):
            pwm.freq(toneToFreq(tone))
            time.sleep(1)
    finally: pwm.deinit()


print(toneToFreq('A4'))
doReMi()

# ============================== end ==================================
