# Esp32 - kit FKS0001 generating sound by DAC
# provided by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-01-14  initial release

from 	machine 	import 	Pin, ADC, DAC, reset, freq;	freq(240_000_000)
import	time
import	math

# ========================== dvm =============================

# a DVM by using the ADC and the potentiometer as input
# connect IO26 with POTM
def testDvm():
    potm = ADC(Pin(26), atten=ADC.ATTN_11DB)
    while True:
        print(round(potm.read_uv()/1_000_000, 2))	# -->  0.13 .. 3.13 Volt
        time.sleep(1)
    
    
testDvm()

# =========================== adc ============================

# measure usable DAC range. connect IO25 (DAC) to IO26 (ADC) as generator
def testDac():
    dac = DAC(Pin(25))
    adc = ADC(Pin(26), atten=ADC.ATTN_11DB)
    for i in range(26):
        dac.write(i * 10)
        time.sleep_ms(10)
        print(f'{i*10:3}   {adc.read_uv()/1_000_000:.2f}')
        #print(adc.read_uv()/1_000_000)		# View | Plotter and manual reset (Btn EN)
    reset()									# release DAC 
    
    
# testDac()

# ======================= sineWave ===========================

# create an in memory sine wave
def testSineWave():
    step = 2 * math.pi / 12						# 12 * 30 degrees = 360 degrees
    wave = [128 + int(100*math.sin(i*step)) for i in range(12)]
    print(wave)
    #for n in range(3):							# show with plotter
    #    for y in wave: print(y)
    
    
# testSineWave()

# ======================= tone ===========================

# create a 1000 Hz tone. Connect IO25 with SPEAKER
def testTone():
    dac = DAC(Pin(25))
    try:
        step = 2 * math.pi / 12						# 12 * 30 degrees = 360 degrees
        wave = [128 + int(100*math.sin(i*step)) for i in range(12)]
        while True:
            for y in wave:
                dac.write(y)
                time.sleep_us(78)
    finally: reset()
    
    
# testTone()

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