wokwi / rp2040js

A Raspberry Pi Pico Emulator in JavaScript
MIT License
384 stars 40 forks source link

Problem when initialising Timer #95

Closed tmsch13 closed 2 years ago

tmsch13 commented 2 years ago

Playing around with Timers in the microPython Simulator I found out that import machine does not import machine.Timer

so:

import machine

def mycallback(t):
    print("timer fired")

timer = Timer(period=1000, mode=Timer.PERIODIC, callback=mycallback)

gives me:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
NameError: name 'Timer' isn't defined

but

from machine import Timer

def mycallback(t):
    print("timer fired")

timer = Timer(period=1000, mode=Timer.PERIODIC, callback=mycallback)

works perfectly.

I would expect importing machine would import machine.Timer to, as it also does for machine.Pin for example.

urish commented 2 years ago

Hi @tmsch13, this is how Python works. If you just import machine, then the Timer class is available under the machine namespace, as machine.Timer.