machine vs. pyb libraries

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
jamonda
Posts: 36
Joined: Thu May 21, 2020 3:48 pm

machine vs. pyb libraries

Post by jamonda » Mon Aug 17, 2020 6:25 pm

Hi.
What is the difference between pyb and machine libraries?
Can I use methods (Pin and RTC, for example) interchangeably?
Last edited by jamonda on Sun Aug 23, 2020 2:13 am, edited 1 time in total.

jamonda
Posts: 36
Joined: Thu May 21, 2020 3:48 pm

Re: machine vs. pub libraries

Post by jamonda » Sat Aug 22, 2020 2:26 am

I think I will have to try elsewhere...

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: machine vs. pub libraries

Post by Roberthh » Sat Aug 22, 2020 5:33 am

the pyb module exists only on pyboards. The machine module is present on almost all ports. You can use either on one the pyboard. But for code consistency it may be better to use the machine module whenever possible. But that is moe a choice of personal flavor than a must.

jamonda
Posts: 36
Joined: Thu May 21, 2020 3:48 pm

Re: machine vs. pub libraries

Post by jamonda » Sat Aug 22, 2020 11:11 am

Hi, RobertHH.
It's an honor to talk to you (I see your name everywhere when I look for micropython on the Internet).
I understand what you say about code consistency. I didn't know we had the machine library on Pyboards until I started to learn how to deal with my new Pyboard D. I always had scripts for my Pyboard v1.1 and slightly different ones for my Esp32 board.
Thank you for your help and for being patient with a newbie like me!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: machine vs. pub libraries

Post by pythoncoder » Sat Aug 22, 2020 6:09 pm

Because machine is portable it lacks some hardware-specific features. My approach is to use machine where possible, but be aware that there are some very nice features of the STM boards accessible only via pyb.
Peter Hinch
Index to my micropython libraries.

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: machine vs. pub libraries

Post by rcolistete » Sat Aug 22, 2020 8:47 pm

(Sorry, I haven't seen pythoncoder's message)

Yes, the recommendation is to use 'machine' module when it has the equivalent functionality of 'pyb' module, so the code is more portable between MicroPython boards.

But no all functionality of 'pyb' module is available on 'machine' module :

Code: Select all

MicroPython v1.12 on 2019-12-20; PYBD-SF2W with STM32F722IEK
Type "help()" for more information.
>>> import pyb
>>> pyb.
__class__       __name__        main            stop
ADC             DAC             RTC             ADCAll
CAN             ExtInt          Flash           I2C
LED             MMCard          Pin             SD
SDCard          SPI             Servo           Switch
Timer           UART            USB_HID         USB_VCP
bootloader      country         delay           dht_readinto
disable_irq     elapsed_micros  elapsed_millis  enable_irq
fault_debug     freq            hard_reset      have_cdc
hid             hid_keyboard    hid_mouse       info
micros          millis          mount           pwm
repl_info       repl_uart       rng             servo
standby         sync            udelay          unique_id
usb_mode        wfi
>>> import machine
>>> machine.
__class__       __name__        ADC             RTC
DEEPSLEEP_RESET                 HARD_RESET      I2C
PWRON_RESET     Pin             SOFT_RESET      SPI
Signal          Timer           UART            WDT
WDT_RESET       bootloader      deepsleep       disable_irq
enable_irq      freq            idle            info
lightsleep      mem16           mem32           mem8
reset           reset_cause     rng             sleep
soft_reset      time_pulse_us   unique_id
For example, Pyboard D especific and new features, like 'pyb.MMCard()' supporting the WBUS-EMMC adapter board.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

jamonda
Posts: 36
Joined: Thu May 21, 2020 3:48 pm

Re: machine vs. pub libraries

Post by jamonda » Sun Aug 23, 2020 2:12 am

pythoncoder wrote:
Sat Aug 22, 2020 6:09 pm
Because machine is portable it lacks some hardware-specific features. My approach is to use machine where possible, but be aware that there are some very nice features of the STM boards accessible only via pyb.
Taking lessons from another legend: Peter Hinch. Thank you so much!

Post Reply