from myTimer import myTimer

from esp32 import NVS
import time

def example_function():
    """示例函数：模拟一些耗时操作"""
    time.sleep(1)  # 模拟耗时1秒的操作

def main():
    #key的长度要不大于15个字节
    nvs = NVS('TEST')
    #nvs.set_i32('test1', 1)
    ret = nvs.get_i32('test1')
    print('test1:',ret)

    #nvs.set_blob('test2', 'hello')
    buf = bytearray(10)
    nvs.get_blob('test2', buf)
    print('test2:',buf)

    # 方式1：使用start/stop方法
    _timer =  myTimer()
    _timer.start()
    example_function()
    _timer.stop()
    costTime = _timer.elapsed_ms()
    print(f"方式1 - 函数运行时间: {costTime:.3f} 毫秒\r\n")
    
    # 方式2：使用with语句（更优雅的方式）
    with myTimer() as _timer2:
        example_function()
    
    costTime = _timer2.elapsed()
    print(f"方式2 - 函数运行时间: {costTime:.3f} 秒\r\n")

if __name__ == '__main__':    
    main()