from machine import Pin, PWM, SoftI2C
import time
import machine 
from ssd1306 import SSD1306_I2C 
i2c = SoftI2C(sda = Pin(12), scl = Pin(13)) 
oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
DNUM=100
LED_R = PWM(Pin(4),freq = DNUM, duty =0 )
A4_dir = Pin(2,Pin.OUT)

pushbutton1=Pin(5,Pin.IN,Pin.PULL_UP)
pushbutton2=Pin(18,Pin.IN,Pin.PULL_UP)
pushbutton3=Pin(14,Pin.IN,Pin.PULL_UP)

starta=0
starta1=0
fmax=1000
fmin=100
fva=(fmax-fmin)*(fmax-fmin+1)/2
sva=2000
svs=sva-fva
DNUM=4000
        
def key2(pushbutton2):
    global DNUM
    global starta
    global starta1
    starta=starta+1
    starta1=starta1+1
    LED_R.freq(1000)
    if starta>=3000:
        LED_R.duty(0)
        starta=0
    '''
    if  DNUM<fmax and starta<=200:
        LED_R.duty(512)
        DNUM=DNUM+10
    elif  DNUM>=1000 and starta>800:
        DNUM=DNUM-10
    elif starta>=1000:
        LED_R.duty(0)
        starta=0
        DNUM=500
    '''   
    
def key1(timer):
    print("OK")
    key2(pushbutton2)
    LED_R.duty(512)
        
def OLEDSHOW(pushbutton3):
    oled.fill(0)
    oled.text("Freq" + '-' + str(DNUM), 0, 20)
    oled.text("Step" + '-' + str(starta1), 0, 30)
    oled.text("Duty" + '-' + str(LED_R.duty()), 0, 40)    
    oled.show()
#pushbutton1.irq(key1,Pin.IRQ_FALLING)    #外部中断
pushbutton2.irq(key2,Pin.IRQ_FALLING)  
pushbutton3.irq(OLEDSHOW,Pin.IRQ_FALLING) 
timer = machine.Timer(0)
timer.init(period= 5000, mode=machine.Timer.PERIODIC, callback=key1)
A4988