8.41. PikaStdDevice 模块 API 文档
8.41.1. API
8.41.2. PikaStdDevice
PikaStdDevice is a standard and abstract device module for PikaScript.
PikaStdDevice supplies the standard device API for users.
Document: https://pikadoc-en.readthedocs.io/en/latest/PikaStdDevice%20%E6%A0%87%E5%87%86%E8%AE%BE%E5%A4%87.html
8.41.2.1. class GPIO(BaseDev):
def __init__(self):...
def setPin(self,pinName:str):...
Use the name of the pin to select the GPIO pin.
example: "PA0"
, "PA1"
…
def setId(self,id:int):...
Use the id of the pin to select the GPIO pin.
example: 0, 1 …
def getId(self)->int:...
Get the id of the pin.
def getPin(self)->str:...
Get the name of the pin.
def setMode(self,mode:str):...
Set the mode of the pin.
example: “in”, “out” …
def getMode(self)->str:...
Get the mode of the pin.
def setPull(self,pull:str):...
Set the pull of the pin.
example: "up"
, "down"
, "none"
…
def enable(self):...
Enable the pin.
def disable(self):...
Disable the pin.
def high(self):...
Set the pin to high.
def low(self):...
Set the pin to low.
def read(self)->int:...
Read the pin value.
def setCallback(self,eventCallBack:any,filter:int):...
Add a callback function to the pin.
Example:
def cb1(signal):
print("cb1", signal)
io.setCallBack(cb1, io.SIGNAL_RISING)
The signal
parameter is the signal type.
The callback function will be called when the signal is triggered.
def setCallBack(self,eventCallBack:any,filter:int):...
deprecated, you can use setCallback
instead.
def close(self):...
def Time()->time:...
deprecated use time module instead
8.41.2.2. class Timer:
def __init__(self):...
def setPeriod(self,period_ms:int):...
Set the period of the timer.
def setMode(self,mode:str):...
Set the mode of the timer.
Example: "continuous"
, "oneshot"
…
def setCallback(self,callback:any,filter:int):...
Add a callback function to the timer.
The callback function will be called when the timer is triggered.
def enable(self):...
Enable the timer.
def disable(self):...
Disable the timer.
def setName(self,name:str):...
Set the name of the timer.
def setId(self,id:int):...
Set the id of the timer.
def getName(self)->str:...
Get the name of the timer.
def getId(self)->int:...
Get the id of the timer.
def close(self):...
Close the timer.
8.41.2.3. class ADC(BaseDev):
def __init__(self):...
def setPin(self,pin:str):...
Use the name of the pin to select the ADC pin.
example: "PA0"
, "PA1"
…
def enable(self):...
Enable the ADC.
def disable(self):...
Disable the ADC.
def read(self)->float:...
Read the ADC value.
def close(self):...
8.41.2.4. class DAC(BaseDev):
def __init__(self):...
def setPin(self,pin:str):...
Use the name of the pin to select the DAC pin.
example: "PA0"
, "PA1"
…
def enable(self):...
Enable the DAC.
def disable(self):...
Disable the DAC.
def write(self,val:float):...
write the DAC value.
def close(self):...
8.41.2.5. class UART:
def __init__(self):...
def setBaudRate(self,baudRate:int):...
Set the baud rate.
def setId(self,id:int):...
Set the id of the UART.
def setStopBits(self,stopBits:int):...
Set the stop bits of the UART.
def setParity(self,parity:int):...
Set the parity of the UART.
def setFlowControl(self,flowControl:int):...
Set the flow control of the UART.
def setDataBits(self,dataBits:int):...
Set the data bits of the UART.
def enable(self):...
Enable the UART.
def disable(self):...
Disable the UART.
def write(self,data:str):...
Write string to the UART.
def writeBytes(self,data:bytes,length:int):...
Write bytes to the UART.
def read(self,length:int)->str:...
Read string from the UART.
def readBytes(self,length:int)->bytes:...
Read bytes from the UART.
def setPinTX(self,pin:str):...
Remap the TX pin.
def setPinRX(self,pin:str):...
Remap the RX pin.
def setPinCTS(self,pin:str):...
Remap the CTS pin.
def setPinRTS(self,pin:str):...
Remap the RTS pin.
def close(self):...
def setCallback(self,eventCallBack:any,filter:int):...
Add a callback function to the pin.
Example:
def cb1(signal):
print(uart.read(-1))
io.setCallBack(cb1, uart.SIGNAL_RX)
def setCallBack(self,eventCallBack:any,filter:int):...
deprecated, you can use setCallback
instead.
8.41.2.6. class IIC(BaseDev):
def __init__(self):...
def setPinSCL(self,pin:str):...
Set the SCL pin.
def setPinSDA(self,pin:str):...
Set the SDA pin.
def setDeviceAddr(self,addr:int):...
Set the device address.
def enable(self):...
Enable the IIC.
def disable(self):...
Disable the IIC.
def write(self,addr:int,data:str):...
Write string to the IIC.
def writeBytes(self,addr:int,data:bytes,length:int):...
Write bytes to the IIC.
def read(self,addr:int,length:int)->str:...
Read string from the IIC.
def readBytes(self,addr:int,length:int)->bytes:...
Read bytes from the IIC.
8.41.2.7. class PWM(BaseDev):
def __init__(self):...
def setName(self,name:str):...
Use the device name to select the PWM pin.
exmpale: "PWM0"
, "PWM1"
…
def getName(self)->str:...
Get the device name.
def setChannel(self,ch:int):...
Set the channel.
def getChannel(self)->int:...
Get the channel.
def setPin(self,pin:str):...
Use the name of the pin to select the PWM pin.
example: "PA0"
, "PA1"
…
def setFrequency(self,freq:int):...
Set the frequency.
def setFreq(self,freq:int):...
Set the frequency.
def setDuty(self,duty:float):...
Set the duty.
def enable(self):...
Enable the PWM.
def disable(self):...
Disable the PWM.
def getFrequency(self)->int:...
Get the frequency.
def getDuty(self)->float:...
Get the duty.
def close(self):...
8.41.2.8. class SPI(BaseDev):
def __init__(self):...
def setPinSCK(self,pin:str):...
Set the SCK pin.
def setPinMOSI(self,pin:str):...
Set the MOSI pin.
def setPinMISO(self,pin:str):...
Set the MISO pin.
def setPinCS(self,pin:str):...
Set the CS pin.
def setName(self,name:str):...
Use the device name to select the SPI pin.
exmpale: "SPI0"
, "SPI1"
…
def setId(self,id:int):...
Set the id of the SPI.
example: 0
, 1
…
def setPolarity(self,polarity:int):...
Set the polarity.
def setPhase(self,phase:int):...
Set the phase.
def setBaudRate(self,baudRate:int):...
Set the baud rate.
def enable(self):...
Enable the SPI.
def disable(self):...
Disable the SPI.
def write(self,data:str):...
Write string to the SPI.
def writeBytes(self,data:bytes,length:int):...
Write bytes to the SPI.
def read(self,length:int)->str:...
Read string from the SPI.
def readBytes(self,length:int)->bytes:...
Read bytes from the SPI.
8.41.2.9. class CAN(BaseDev):
def __init__(self):...
def setName(self,name:str):...
Use the device name to select the CAN pin.
exmpale: "CAN0"
, "CAN1"
…
def setId(self,id:int):...
Use the id to select the CAN pin.
example: 0
, 1
…
def setBaudRate(self,baudRate:int):...
Set the baud rate.
def setMode(self,mode:str):...
Set the mode.
example: "normal"
, "loopback"
, "silent"
, "silent_loopback"
def enable(self):...
Enable the CAN.
def disable(self):...
Disable the CAN.
def write(self,data:str):...
Write string to the CAN.
def writeBytes(self,data:bytes,length:int):...
Write bytes to the CAN.
def read(self,length:int)->str:...
Read string from the CAN.
def readBytes(self,length:int)->bytes:...
Read bytes from the CAN.
def addFilter(self,id:int,ide:int,rtr:int,mode:int,mask:int,hdr:int):...
Add a filter.
8.41.2.10. class BaseDev:
def addEventCallback(self,eventCallback:any):...
Add an event callback.
def addEventCallBack(self,eventCallback:any):...
deprecated, use addEventCallback instead.
def platformGetEventId(self):...
8.41.3. Examples
8.41.3.1. GPIO.py
import PikaStdLib
import PikaStdDevice
mem = PikaStdLib.MemChecker()
io1 = PikaStdDevice.GPIO()
time = PikaStdDevice.Time()
io1.setPin('PA8')
io1.setMode('out')
io1.enable()
io1.low()
print('hello pikascript')
print('mem.max :')
mem.max()
print('mem.now :')
mem.now()
while True:
mem.now()
io1.low()
time.sleep_ms(500)
io1.high()
time.sleep_ms(500)
8.41.3.2. LCD.py
import PikaStdLib
import PikaStdDevice
lcd = PikaStdDevice.LCD()
lcd.init()
lcd.clear('white')
mem = PikaStdLib.MemChecker()
key = PikaStdDevice.KEY()
key.init()
time = PikaStdDevice.Time()
h = 10
w = 10
x = 10
y = 10
x_last = x
y_last = y
is_update = 0
print('mem used max:')
mem.max()
lcd.fill(x, y, w, h, 'blue')
while True:
key_val = key.get()
if key_val != -1:
x_last = x
y_last = y
is_update = 1
if key_val == 0:
x = x + 5
if key_val == 1:
y = y - 5
if key_val == 2:
y = y + 5
if key_val == 3:
x = x - 5
if is_update:
is_update = 0
lcd.fill(x_last, y_last, w, h, 'white')
lcd.fill(x, y, w, h, 'blue')
8.41.3.3. PWM.py
import PikaStdLib
import PikaStdDevice
time = PikaStdDevice.Time()
pwm = PikaStdDevice.PWM()
pwm.setPin('PA8')
pwm.setFrequency(2000)
pwm.setDuty(0.5)
pwm.enable()
mem = PikaStdLib.MemChecker()
while True:
mem.now()
time.sleep_ms(500)
pwm.setDuty(0.5)
time.sleep_ms(500)
pwm.setDuty(0.001)
8.41.3.4. Time.py
import PikaStdLib
import PikaStdDevice
time = PikaStdDevice.Time()
while True:
time.sleep_ms(500)
print('0.5s')
time.sleep_s(1)
print('1s')
8.41.3.5. UART_CALLBACK.py
import PikaStdDevice as std
print('hello pikascript')
uart = std.UART()
uart.setId(0)
uart.setBaudRate(115200)
uart.enable()
def cb1(signal):
print('recv:', uart.read(32))
uart.setCallBack(cb1, uart.SIGNAL_RX)
while True:
pass
8.41.3.6. GPIO_CALLBCK.py
import PikaStdDevice as std
print('hello pikascript')
io = std.GPIO()
io.setPin('P4')
io.setMode('in')
io.enable()
def cb1(signal):
print('cb1', signal)
io.setCallBack(cb1, io.SIGNAL_FALLING)
while True:
pass
8.41.3.7. UART.py
import PikaStdLib
import PikaStdDevice
time = PikaStdDevice.Time()
uart = PikaStdDevice.UART()
uart.setId(1)
uart.setBaudRate(115200)
uart.enable()
while True:
time.sleep_ms(500)
readBuff = uart.read(2)
print('read 2 char:')
print(readBuff)
8.41.3.8. RGB.py
import machine
import PikaStdLib
time = machine.Time()
adc = machine.ADC()
pin = machine.GPIO()
pwm = machine.PWM()
uart = machine.UART()
rgb = machine.RGB()
mem = PikaStdLib.MemChecker()
rgb.init()
rgb.enable()
print('hello 2')
print('mem used max:')
mem.max()
while True:
print('flowing')
rgb.flow()
time.sleep_ms(100)
8.41.3.9. ADC.py
import PikaStdLib
import PikaStdDevice
time = PikaStdDevice.Time()
adc1 = PikaStdDevice.ADC()
adc1.setPin('PA1')
adc1.enable()
while True:
val = adc1.read()
print('adc1 value:')
print(val)
time.sleep_ms(500)