このドキュメンテーションは、MicroPython の最新開発ブランチのためのものです。 リリースバージョンでは利用できない機能に言及することがあります。

特定のリリースのドキュメントをお探しの場合は、左側のドロップダウンメニューを使って、 望みのバージョンを選択します。

Zephyr ポートのクイックリファレンス

以下は Zephyr ポートのクイックリファレンスです。このポートを初めて使う場合は、まず次の章を参照してください:

MicroPython の実行

次のチュートリアルの該当セクションを参照してください: Zephyr ポートでの MicroPython の始め方

遅延とタイミング

time モジュールを使います:

import time

time.sleep(1)           # 1秒間、一時停止する
time.sleep_ms(500)      # 500ミリ秒間、一時停止する
time.sleep_us(10)       # 10マイクロ秒間、一時停止する
start = time.ticks_ms() # ミリ秒カウンター値を取得
delta = time.ticks_diff(time.ticks_ms(), start) # 時差を計算

ピンと GPIO

machine.Pin クラスを使います:

from machine import Pin

pin = Pin(("gpiob", 21), Pin.IN)  # GPIOポートBの入力ピンを作成
print(pin)                          # ピンのポートと番号を表示

pin.init(Pin.OUT, Pin.PULL_UP, value=1)     # ピンを再初期化

pin.value(1)                        # ピンを high に設定
pin.value(0)                        # ピンを low low に設定

pin.on()                            # ピンを high に設定
pin.off()                           # ピンを low に設定

pin = Pin(("gpiob", 21), Pin.IN)              # GPIOポートBの入力ピンを作成

pin = Pin(("gpiob", 21), Pin.OUT, value=1)    # 作成時にピンを high に設定

pin = Pin(("gpiob", 21), Pin.IN, Pin.PULL_UP) # 内部プルアップ抵抗を有効化

switch = Pin(("gpioc", 6), Pin.IN)            # スイッチ用の入力ピンを作成
switch.irq(lambda t: print("SW2 changed"))    # スイッチの状態の変更時に割り込みを有効化

PWM

machine.PWM クラスを使います:

from machine import PWM

pwm = PWM(("pwm0", 0), freq=3921568, duty_ns=200, invert=True)    # PWM0 について pwm を作成
print(pwm)                                                        # pwm を表示

print(pwm.duty_ns())                                              # pwm のデューティ比をナノ秒単位で表示
pwm.duty_ns(255)                                                  # pwm の新しいデューティ比をナノ秒単位で設定

pwm.deinit()

ハードウェア I2C バス

ハードウェア I2C には machine.I2C クラスを使ってアクセスします。

from machine import I2C

i2c = I2C("i2c0")          # i2c バスを構築
print(i2c)                  # デバイス名を表示

i2c.scan()                  # 利用可能な I2C スレーブのデバイスをスキャン

i2c.readfrom(0x1D, 4)                # スレーブ 0x1D から 4 バイト読み込み
i2c.readfrom_mem(0x1D, 0x0D, 1)      # スレーブ 0x1D のスレーブメモリー 0x0D から 1 バイト読み込み

i2c.writeto(0x1D, b'abcd')           # アドレス 0x1D のスレーブに書き込み
i2c.writeto_mem(0x1D, 0x0D, b'ab')   # スレーブ 0x1D のスレーブメモリー 0x0D に書き込み

buf = bytearray(8)                  # サイズ 8 のバッファを作成
i2c.writeto(0x1D, b'abcd')          # スレーブ 0x1D に buf を書き込み

ハードウェア SPI バス

machine.SPI クラスを介してアクセスします:

from machine import SPI

spi = SPI("spi0")           # construct a SPI bus with default configuration
spi.init(baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB) # set configuration

# equivalently, construct the SPI bus and set configuration at the same time
spi = SPI("spi0", baudrate=100000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB)
print(spi)                  # print device name and bus configuration

spi.read(4)                 # read 4 bytes on MISO
spi.read(4, write=0xF)      # read 4 bytes while writing 0xF on MOSI

buf = bytearray(8)          # create a buffer of size 8
spi.readinto(buf)           # read into the buffer (reads number of bytes equal to the buffer size)
spi.readinto(buf, 0xF)      # read into the buffer while writing 0xF on MOSI

spi.write(b'abcd')          # write 4 bytes on MOSI

buf = bytearray(4)                  # create buffer of size 8
spi.write_readinto(b'abcd', buf)    # write to MOSI and read from MISO into the buffer
spi.write_readinto(buf, buf)        # write buf to MOSI and read back into the buf

アナログ-デジタル変換器(ADC)

machine.ADC クラスを使います。

ADC を使ってピンのアナログ値を読み取る例(zephyr,user ノードにはすべての ADC チャネルを含む io-channels プロパティ が含まれている必要があります):

from machine import ADC

adc = ADC(("adc", 0))
adc.read_uv()

ディスクアクセス

SDカードなどのストレージデバイスは、起動時に自動的にマウントされます(例: /sd)。手動でマウントするには zephyr.DiskAccess クラスを使います:

import vfs
from zephyr import DiskAccess

print(DiskAccess.disks)             # 利用可能なディスク名のリスト。例: ('SDHC',)

block_dev = DiskAccess('SDHC')      # SDカード用のブロックデバイスオブジェクトを作成
vfs.VfsFat.mkfs(block_dev)          # ディスクストレージブロックを用いて FAT ファイルシステムオブジェクトを作成
vfs.mount(block_dev, '/sd')         # SD カード用のサブディレクトリにファイルシステムをマウント

# マウントしたファイルシステムでは、普通にファイル操作が可能
with open('/sd/hello.txt','w') as f:     # ディレクトリ中に新しいファイルをオープン
    f.write('Hello world')                  # ファイルに書き出し
print(open('/sd/hello.txt').read())      # ファイルの内容を表示

フラッシュ領域

フラッシュストレージは起動時に /flash へ自動的にマウントされ、ファイルシステムも自動的に作成されます。手動でマウントする場合は zephyr.FlashArea クラスを使ってください。

import vfs
from zephyr import FlashArea

print(FlashArea.areas)              # list available areas, e.g., {'storage': 1, 'scratch': 4}

block_dev = FlashArea(FlashArea.areas['scratch'], 4096)  # creates a block device object using the scratch partition
vfs.VfsLfs2.mkfs(block_dev)         # create filesystem in lfs2 format using the flash block device
vfs.mount(block_dev, '/flash')      # mount the filesystem at the flash subdirectory

# with the filesystem mounted, files can be manipulated as normal
with open('/flash/hello.txt','w') as f:     # open a new file in the directory
    f.write('Hello world')                  # write to the file
print(open('/flash/hello.txt').read())      # print contents of the file

センサー

センサーデータにアクセスするには zsensor.Sensor クラスを使います:

import zsensor
from zsensor import Sensor

accel = Sensor("fxos8700")    # 加速度センサーのセンサーオブジェクトを作成

accel.measure()               # 加速度センサーから読み込んだ計測データを取得

# 以下は measure() で取得した値を表示
accel.get_float(zsensor.ACCEL_X)  # 加速度センサーのX軸センサーチャンネル計測値を float で表示
accel.get_millis(zsensor.ACCEL_Y) # 加速度センサーのY軸センサーチャンネル計測値を100万分の1単位で表示
accel.get_micro(zsensor.ACCEL_Z)  # 加速度センサーのY軸センサーチャンネル計測値を1000分の1単位で表示
accel.get_int(zsensor.ACCEL_X)    # 加速度センサーのX軸センサーチャンネル計測値を int で表示

zsensor.Sensor.get_int(), zsensor.Sensor.get_float(), zsensor.Sensor.get_millis(), zsensor.Sensor.get_micros() メソッドの引数として使われるチャンネルIDは zsensor モジュール内の定数です。

zsensor.Sensor.attr_set() メソッドを使用使って、フルスケール範囲や更新レートなどのセンサー属性を設定できます。

# XIAO BLE NRF52840 SENSE の例
from zsensor import *
accel = Sensor('lsm6ds3tr_c')  # name from Devicetree
# Set full-scale to 2g (19.613300 m/sec^2)
# units are micro-m/s^2 (given as a float)
accel.attr_set(ACCEL_XYZ, ATTR_FULL_SCALE, 19.613300)
# Set sampling frequency to 104 Hz (as a pair of integers)
accel.attr_set(ACCEL_XYZ, ATTR_SAMPLING_FREQUENCY, 104, 0)
accel.measure()
accel.get_float(ACCEL_X) # -0.508 (m/s^2)
accel.get_float(ACCEL_Y) # -3.62 (m/s^2)
accel.get_float(ACCEL_Z) # 9.504889 (m/s^2)

zsensor.Sensor.attr_get_float(), zsensor.Sensor.attr_get_int(), zsensor.Sensor.attr_get_millis(), zsensor.Sensor.attr_get_micros() メソッドもありますが、多くのセンサーではサポートされていません。

full_scale = accel.attr_get_float(ATTR_FULL_SCALE)

zsensor.Sensor.attr_set(), zsensor.Sensor.attr_get_float(), zsensor.Sensor.attr_get_int(), zsensor.Sensor.attr_get_millis(), zsensor.Sensor.attr_get_micros() メソッドの引数に使われる属性IDは zsensor モジュール内の ATTR_* 定数です。