# check callback feature of the timer class

import sys
import time
from pyb import Timer

tim_first = 1
tim_second = 2
tim_third = 4

if "STM32L1" in sys.implementation._machine:
    tim_first = 5
elif "STM32WB" in sys.implementation._machine:
    tim_third = 16


# callback function that disables the callback when called
def cb1(t):
    print("cb1")
    t.callback(None)


# callback function that disables the timer when called
def cb2(t):
    print("cb2")
    t.deinit()


# callback where cb4 closes over cb3.y
def cb3(x):
    y = x

    def cb4(t):
        print("cb4", y)
        t.callback(None)

    return cb4


# create a timer with a callback, using callback(None) to stop
tim = Timer(tim_first, freq=100, callback=cb1)
time.sleep_ms(5)
print("before cb1")
time.sleep_ms(15)

# create a timer with a callback, using deinit to stop
tim = Timer(tim_second, freq=100, callback=cb2)
time.sleep_ms(5)
print("before cb2")
time.sleep_ms(15)

# create a timer, then set the freq, then set the callback
tim = Timer(tim_third)
tim.init(freq=100)
tim.callback(cb1)
time.sleep_ms(5)
print("before cb1")
time.sleep_ms(15)

# test callback with a closure
tim.init(freq=100)
tim.callback(cb3(3))
time.sleep_ms(5)
print("before cb4")
time.sleep_ms(15)
