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")) # スイッチの状態の変更時に割り込みを有効化
ハードウェア I2C バス¶
ハードウェア I2C には machine.I2C クラスを使ってアクセスします。
from machine import I2C
i2c = I2C("i2c0") # construct an i2c bus
print(i2c) # print device name
i2c.scan() # scan the device for available I2C slaves
i2c.readfrom(0x1D, 4) # read 4 bytes from slave 0x1D
i2c.readfrom_mem(0x1D, 0x0D, 1) # read 1 byte from slave 0x1D at slave memory 0x0D
i2c.writeto(0x1D, b'abcd') # write to slave with address 0x1D
i2c.writeto_mem(0x1D, 0x0D, b'ab') # write to slave 0x1D at slave memory 0x0D
buf = bytearray(8) # create buffer of size 8
i2c.writeto(0x1D, b'abcd') # write buf to slave 0x1D
ハードウェア 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 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
ディスクアクセス¶
ディスクのファイルシステムをサポートするには zephyr.DiskAccess クラスを使います:
import vfs
from zephyr import DiskAccess
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()) # ファイルの内容を表示
フラッシュ領域¶
フラッシュ領域のファイルシステムをサポートするには zephyr.FlashArea クラスを使います。
import vfs
from zephyr import FlashArea
block_dev = FlashArea(4, 4096) # frdm-k64f フラッシュスクラッチパーティションにブロックデバイスオブジェクトを作成
vfs.VfsLfs2.mkfs(block_dev) # フラッシュブロックデバイスを使って、lfs2フォーマットでファイルシステムを作成
vfs.mount(block_dev, '/flash') # フラッシュ用のサブディレクトリにファイルシステムをマウント
# マウントしたファイルシステムでは、普通にファイル操作が可能
with open('/flash/hello.txt','w') as f: # ディレクトリ中に新しいファイルをオープン
f.write('Hello world') # ファイルに書き出し
print(open('/flash/hello.txt').read()) # ファイルの内容を表示
センサー¶
センサーデータにアクセスするには zsensor.Sensor クラスを使います:
import zsensor
from zsensor import Sensor
accel = Sensor("fxos8700") # create sensor object for the accelerometer
accel.measure() # obtain a measurement reading from the accelerometer
# each of these prints the value taken by measure()
accel.get_float(zsensor.ACCEL_X) # print measurement value for accelerometer X-axis sensor channel as float
accel.get_millis(zsensor.ACCEL_Y) # print measurement value for accelerometer Y-axis sensor channel in millionths
accel.get_micro(zsensor.ACCEL_Z) # print measurement value for accelerometer Z-axis sensor channel in thousandths
accel.get_int(zsensor.ACCEL_X) # print measurement integer value only for accelerometer X-axis sensor channel