mBlock-Python Editor can be used to program devices as well as creating Python 3 projects.

This section uses CyberPi as an example to describe how to program a device.

Connect a device

  1. Use a USB cable to connect CyberPi to a computer.

Program Devices - 图1

:::info Note: If a device to be connected to mBlock-Python Editor has a power switch, you need to power on the device. :::

  1. Click Connect in mBlock-Python Editor, and then click Connect in the dialog box that appears.

The editor automatically identifies the serial port used by CyberPi.
Connect.png :::info Note: You need to run mLink before using mBlock-Python Editor. :::

After CyberPi is connected, the Connect button turns into Connected.

Live and Upload modes

For device programming, mBlock-Python Editor provides two program execution modes: Live and Upload.
You can click to switch the programming modes.
image.png

Live

The Live mode supports Python 3 libraries and is used to control devices in real time.

In Live mode, you don’t need to upload programs to devices and can click Run to execute a program in real time.
image.png

:::info Note: In Live mode, programs compiled for a device can’t be executed if the device is disconnected from mBlock-Python Editor. :::

Upload

The Upload mode supports only MicroPython, and you can’t use third-party Python 3 libraries to program devices.

In Upload mode, you need to click Upload to upload a program to a device. After being uploaded to a device, a program can be executed on the device even when the device is disconnected from mBlock-Python Editor.
**image.png

Program CyberPi

CyberPi is the latest main control board developed independently by Makeblock. To program CyberPi, you need to install the cyberpi library. For details about how to install a Python library, see “Install or Uninstall Python Libraries.”

  1. Set the programming mode to Live.

  2. Choose File > New project, and enter your code in the project file editing area.
    image.png

Compile a simple program to implement the marquee effect of the LEDs on CyberPi.

Enter the following code:

  1. import math
  2. import random
  3. import cyberpi
  4. from time import sleep
  5. cyberpi.led.show("r g b y c")
  6. count = 0
  7. while True:
  8. cyberpi.led.set_bri(math.sin(count / 4) * 50 + 52)
  9. cyberpi.led.move(1)
  10. count += 1
  11. sleep(0.1)

image.png

  1. Click Run.

The onboard LEDs of CyberPi are lit up in marquee mode.