from machine import Pin
import utime
import math
pines = [2, 3, 4, 5, 6, 7, 8, 9, 10]
def GPIO_SALP(valor, listagpio):
pines_gpio = [Pin(pin, Pin.OUT) for pin in listagpio]
ii = 1
for i in range(8):
pines_gpio[i].value(1 if (valor & ii) else 0)
ii = ii << 1
def SerieFourier_Ej3(nmax):
"""
f(x) = -1 para -pi < x < 0
f(x) = 2 para 0 <= x < pi
a0 = 1, an = 0, bn = 3(1-(-1)^n)/(n*pi)
"""
xmin = 0.0
xmax = 59.0
delta = 0.5
T = 60
x = xmin
while x <= xmax:
w0 = 2 * math.pi / T
a0 = 1.0
sum_val = 0.0
for n in range(1, nmax + 1):
an = 0.0
bn = 3 * (1 - math.pow(-1, n)) / (n * math.pi)
sum_val += an * math.cos(n * w0 * x) + bn * math.sin(n * w0 * x)
fx = a0 / 2 + sum_val
print(fx)
# Mapeo a 8 bits: rango aprox [-1, 2+] -> [0, 255]
val_dac = int((fx + 2) * 50)
val_dac = max(0, min(255, val_dac))
GPIO_SALP(val_dac, pines)
x += delta
while True:
for narmonicos in range(1, 50, 1):
SerieFourier_Ej3(narmonicos)