diff --git a/tools/pikaCompiler/PikaObj.pyi b/tools/pikaCompiler/PikaObj.pyi index 7074cf700..9a85df17b 100644 --- a/tools/pikaCompiler/PikaObj.pyi +++ b/tools/pikaCompiler/PikaObj.pyi @@ -1,21 +1,24 @@ -#api class TinyObj: - pass + ... class BaseObj(TinyObj): - pass + ... -def print(val: any): - pass +class pointer: + ... -def set(argPath: str, val: any): - pass - class any: - pass + ... -def PIKA_C_MACRO_IF(fun: any): - pass \ No newline at end of file + +class int64: + ... + + +def printNoEnd(val: any): ... +def abstractmethod(fn): ... +def PIKA_C_MACRO_IF(fn): ... +def PIKA_C_MACRO_IFDEF(fn): ... diff --git a/tools/pikaCompiler/PikaStdDevice.pyi b/tools/pikaCompiler/PikaStdDevice.pyi index 24271c9b5..a2917703a 100644 --- a/tools/pikaCompiler/PikaStdDevice.pyi +++ b/tools/pikaCompiler/PikaStdDevice.pyi @@ -1,210 +1,459 @@ -#api +""" +PikaStdDevice is a standard and abstract device module for PikaScript. + +PikaStdDevice supplies the standard device API for users. + +Users need to inherit from PikaStdDevice and override the abstract methods, which is with the `@abstractmethod` decorator. + +For example, the STM32F1 device module: https://gitee.com/Lyon1998/pikascript/blob/master/package/STM32F1/STM32F1.pyi + +And for convenience, make a machine.pyi to inherit from STM32F1 device module for alias purpose. + +For example: + +- The machine.pyi for STM32F1:https://gitee.com/Lyon1998/pikascript/blob/master/bsp/stm32f103c8/pikascript/machine.pyi + +- The machine.pyi for STM32G0: https://gitee.com/Lyon1998/pikascript/blob/master/bsp/stm32g070cb/pikascript/machine.pyi +""" from PikaObj import * -class GPIO(TinyObj): - def __init__(): - pass +class GPIO(BaseDev): + def __init__(self): ... - def init(): - pass + def setPin(self, pinName: str): + """ + Use the name of the pin to select the GPIO pin. - def setPin(pinName: str): - pass + example: `"PA0"`, `"PA1"` ... + """ - def getPin() -> str: - pass + def setId(self, id: int): + """ + Use the id of the pin to select the GPIO pin. - def setMode(mode: str): - pass + example: 0, 1 ... + """ - def getMode() -> str: - pass + def getId(self) -> int: + """ + Get the id of the pin. + """ - def setPull(pull: str): - pass + def getPin(self) -> str: + """ + Get the name of the pin. + """ - def enable(): - pass + def setMode(self, mode: str): + """ + Set the mode of the pin. + example: "in", "out" ... + """ - def disable(): - pass + def getMode(self) -> str: + """ + Get the mode of the pin. + """ - def high(): - pass + def setPull(self, pull: str): + """ + Set the pull of the pin. + example: `"up"`, `"down"`, `"none"` ... + """ - def low(): - pass + def enable(self): + """Enable the pin.""" - def read() -> int: - pass + def disable(self): + """Disable the pin.""" - # need be overrid - def platformHigh(): - pass + def high(self): + """Set the pin to high.""" - # need override - def platformLow(): - pass + def low(self): + """Set the pin to low.""" - # need override - def platformEnable(): - pass + def read(self) -> int: + """Read the pin value.""" - # need override - def platformDisable(): - pass + @abstractmethod + def platformHigh(self): ... - # need override - def platformSetMode(): - pass + @abstractmethod + def platformLow(self): ... - # need override - def platformRead(): - pass + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformDisable(self): ... + + @abstractmethod + def platformSetMode(self): ... + + @abstractmethod + def platformRead(self): ... -class Time(TinyObj): - # need override - def sleep_s(s: int): - pass +class Time(BaseDev): + def __init__(self): ... - # need override - def sleep_ms(ms: int): - pass + def sleep(self, s: float): + """Sleep for s seconds.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def time(self) -> float: + """Get the current time.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def time_ns(self) -> int: + """Get the current time in nanoseconds.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def gmtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def localtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def mktime(self) -> int: + """Convert struct_time to unix time.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def asctime(self): + """Convert struct_time to string.""" + + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def ctime(self, unix_time: float): + """Convert unix time to string.""" + + @abstractmethod + def sleep_s(self, s: int): ... + + @abstractmethod + def sleep_ms(self, ms: int): ... + + @abstractmethod + @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") + def platformGetTick(): ... -class ADC(TinyObj): - def __init__(): - pass +class ADC(BaseDev): + def __init__(self): ... - def init(): - pass + def setPin(self, pin: str): + """ + Use the name of the pin to select the ADC pin. + example: `"PA0"`, `"PA1"` ... + """ - def setPin(pin: str): - pass + def enable(self): + """Enable the ADC.""" - def enable(): - pass + def disable(self): + """Disable the ADC.""" - def read() -> float: - pass + def read(self) -> float: + """Read the ADC value.""" - # need override - def platformEnable(): - pass + @abstractmethod + def platformEnable(self): ... - # need override - def platformRead(): - pass + @abstractmethod + def platformRead(self): ... + + @abstractmethod + def platformDisable(self): ... -class UART(TinyObj): - def __init__(): - pass +class UART(BaseDev): + def __init__(self): ... - def init(): - pass + def setBaudRate(self, baudRate: int): + """Set the baud rate.""" - def setBaudRate(baudRate: int): - pass + def setId(self, id: int): + """Set the id of the UART.""" - def setId(id: int): - pass + def enable(self): + """Enable the UART.""" - def enable(): - pass + def disable(self): + """Disable the UART.""" - def write(data: str): - pass + def write(self, data: str): + """Write string to the UART.""" - def read(length: int) -> str: - pass + def writeBytes(self, data: bytes, length: int): + """Write bytes to the UART.""" - # need override - def platformEnable(): - pass + def read(self, length: int) -> str: + """Read string from the UART.""" - # need override - def platformWrite(): - pass + def readBytes(self, length: int) -> bytes: + """Read bytes from the UART.""" - # need override - def platformRead(): - pass + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformWrite(self): ... + + @abstractmethod + def platformWriteBytes(self): ... + + @abstractmethod + def platformRead(self): ... + + @abstractmethod + def platformReadBytes(self): ... + + @abstractmethod + def platformDisable(self): ... -class IIC(TinyObj): - def __init__(): - pass +class IIC(BaseDev): + def __init__(self): ... - def init(): - pass + def setPinSCL(self, pin: str): + """Set the SCL pin.""" - def setPinSCL(pin: str): - pass + def setPinSDA(self, pin: str): + """Set the SDA pin.""" - def setPinSDA(pin: str): - pass + def setDeviceAddr(self, addr: int): + """Set the device address.""" - def setDeviceAddr(addr: int): - pass + def enable(self): + """Enable the IIC.""" - def enable(): - pass + def disable(self): + """Disable the IIC.""" - def write(addr: int, data: str): - pass + def write(self, addr: int, data: str): + """Write string to the IIC.""" - def read(addr: int, length: int) -> str: - pass + def writeBytes(self, addr: int, data: bytes, length: int): + """Write bytes to the IIC.""" - # need override - def platformEnable(): - pass + def read(self, addr: int, length: int) -> str: + """Read string from the IIC.""" - # need override - def platformWrite(): - pass + def readBytes(self, addr: int, length: int) -> bytes: + """Read bytes from the IIC.""" - # need override - def platformRead(): - pass + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformWrite(self): ... + + @abstractmethod + def platformWriteBytes(self): ... + + @abstractmethod + def platformRead(self): ... + + @abstractmethod + def platformReadBytes(self): ... + + @abstractmethod + def platformDisable(self): ... -class PWM(TinyObj): - def __init__(): - pass +class PWM(BaseDev): + def __init__(self): ... - def init(): - pass + def setName(self, name: str): + """Use the device name to select the PWM pin. + exmpale: `"PWM0"`, `"PWM1"` ... + """ - def setPin(pin: str): - pass + def getName(self) -> str: + """Get the device name.""" - def setFrequency(freq: int): - pass + def setChannel(self, ch: int): + """Set the channel.""" - def setDuty(duty: float): - pass + def getChannel(self) -> int: + """Get the channel.""" - def enable(): - pass + def setPin(self, pin: str): + """Use the name of the pin to select the PWM pin. + example: `"PA0"`, `"PA1"` ... + """ - def getFrequency() -> int: - pass + def setFrequency(self, freq: int): + """Set the frequency.""" - def getDuty() -> float: - pass + def setFreq(self, freq: int): + """Set the frequency.""" - # need override - def platformEnable(): - pass + def setDuty(self, duty: float): + """Set the duty.""" - # need override - def platformSetFrequency(): - pass + def enable(self): + """Enable the PWM.""" - # need override - def platformSetDuty(): - pass + def disable(self): + """Disable the PWM.""" + + def getFrequency(self) -> int: + """Get the frequency.""" + + def getDuty(self) -> float: + """Get the duty.""" + + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformSetFrequency(self): ... + + @abstractmethod + def platformSetDuty(self): ... + + @abstractmethod + def platformDisable(self): ... + + +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 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.""" + + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformWrite(self): ... + + @abstractmethod + def platformWriteBytes(self): ... + + @abstractmethod + def platformRead(self): ... + + @abstractmethod + def platformReadBytes(self): ... + + @abstractmethod + def platformDisable(self): ... + + +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.""" + + @abstractmethod + def platformEnable(self): ... + + @abstractmethod + def platformWrite(self): ... + + @abstractmethod + def platformWriteBytes(self): ... + + @abstractmethod + def platformRead(self): ... + + @abstractmethod + def platformReadBytes(self): ... + + @abstractmethod + def platformDisable(self): ... + + +class BaseDev: + @PIKA_C_MACRO_IF("PIKA_EVENT_ENABLE") + def addEventCallBack(self, eventCallback: any): + """ Add an event callback. """ + + @abstractmethod + @PIKA_C_MACRO_IF("PIKA_EVENT_ENABLE") + def platformGetEventId(self): ... diff --git a/tools/pikaCompiler/main.py b/tools/pikaCompiler/main.py index 82535834b..905d1cec2 100644 --- a/tools/pikaCompiler/main.py +++ b/tools/pikaCompiler/main.py @@ -1,5 +1,4 @@ -import PikaStdLib -import test, module +import PikaStdLib, test import TemplateDevice import test_pyo diff --git a/tools/pikaCompiler/requestment.txt b/tools/pikaCompiler/requestment.txt index 8a1621e5b..38dae5708 100644 --- a/tools/pikaCompiler/requestment.txt +++ b/tools/pikaCompiler/requestment.txt @@ -1,3 +1,4 @@ pikascript-core==latest PyInfo==latest -PikaStdDevice==latest \ No newline at end of file +PikaStdDevice==latest +modbus==latest \ No newline at end of file diff --git a/tools/pikaCompiler/rust-msc-latest-win10.exe b/tools/pikaCompiler/rust-msc-latest-win10.exe index 361d694fc..b14b187f0 100644 Binary files a/tools/pikaCompiler/rust-msc-latest-win10.exe and b/tools/pikaCompiler/rust-msc-latest-win10.exe differ diff --git a/tools/pikaCompiler/src/compiler.rs b/tools/pikaCompiler/src/compiler.rs index 46e90d0c9..5533933a4 100644 --- a/tools/pikaCompiler/src/compiler.rs +++ b/tools/pikaCompiler/src/compiler.rs @@ -161,10 +161,7 @@ impl Compiler { match file { /* py import py.o => do nothing */ Ok(_) => { - println!( - " found {}{}.{}...", - self.source_path, file_name, suffix - ); + println!(" found {}{}.{}...", self.source_path, file_name, suffix); return self; } /* continue */ @@ -225,6 +222,10 @@ impl Compiler { let mut file = match file { Ok(file) => file, Err(_) => { + if suffix == "pyi" { + /* if .pyi no exist, check .py exist */ + return self.analyse_py_package_inner(file_name.clone()); + } /* .py no exist, error */ println!( " [warning]: file: '{}{}.pyi', '{}{}.py' or '{}{}.py.o' no found",