diff --git a/package/PikaStdDevice/PikaStdDevice.py b/package/PikaStdDevice/PikaStdDevice.py index 3b78f5fe7..b9ec43e05 100644 --- a/package/PikaStdDevice/PikaStdDevice.py +++ b/package/PikaStdDevice/PikaStdDevice.py @@ -184,12 +184,27 @@ class PWM(TinyObj): def init(): pass + def setName(name: str): + pass + + def getName() -> str: + pass + + def setChannel(ch: int): + pass + + def getChannel() -> int: + pass + def setPin(pin: str): pass def setFrequency(freq: int): pass + def setFreq(freq: int): + pass + def setDuty(duty: float): pass diff --git a/package/PikaStdDevice/PikaStdDevice_PWM.c b/package/PikaStdDevice/PikaStdDevice_PWM.c index 4e2b15442..a1bbea58c 100644 --- a/package/PikaStdDevice/PikaStdDevice_PWM.c +++ b/package/PikaStdDevice/PikaStdDevice_PWM.c @@ -2,8 +2,10 @@ #include "BaseObj.h" void PikaStdDevice_PWM_init(PikaObj* self) { - obj_setStr(self, "pin", "PA8"); + obj_setStr(self, "pin", "none"); + obj_setStr(self, "name", "none"); obj_setInt(self, "freq", 1000); + obj_setInt(self, "ch", 0); obj_setFloat(self, "duty", 0.5f); } @@ -49,3 +51,23 @@ void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self) { obj_setErrorCode(self, 1); obj_setSysOut(self, "[error] platform method need to be override."); } + +char* PikaStdDevice_PWM_getName(PikaObj* self) { + return obj_getStr(self, "name"); +} + +void PikaStdDevice_PWM_setName(PikaObj* self, char* name) { + obj_setStr(self, "name", name); +} + +void PikaStdDevice_PWM_setChannel(PikaObj* self, int ch) { + obj_setInt(self, "ch", ch); +} + +int PikaStdDevice_PWM_getChannel(PikaObj* self) { + return obj_getInt(self, "ch"); +} + +void PikaStdDevice_PWM_setFreq(PikaObj* self, int freq) { + PikaStdDevice_PWM_setFrequency(self, freq); +} \ No newline at end of file diff --git a/package/pikaRTDevice/pikaRTDevice.py b/package/pikaRTDevice/pikaRTDevice.py index 657d036c6..177b1c4ca 100644 --- a/package/pikaRTDevice/pikaRTDevice.py +++ b/package/pikaRTDevice/pikaRTDevice.py @@ -19,3 +19,17 @@ class GPIO(PikaStdDevice.GPIO): def platformRead(): pass + + +class PWM(PikaStdDevice.PWM): + # override + def platformEnable(): + pass + + # override + def platformSetFrequency(): + pass + + # override + def platformSetDuty(): + pass diff --git a/package/pikaRTDevice/pikaRTDevice_PWM.c b/package/pikaRTDevice/pikaRTDevice_PWM.c new file mode 100644 index 000000000..760c39cea --- /dev/null +++ b/package/pikaRTDevice/pikaRTDevice_PWM.c @@ -0,0 +1,32 @@ +#include "pikaRTDevice_PWM.h" +#include +#include + +struct rt_device_pwm* PWM_getDevice(PikaObj* self) { + char* name = obj_getStr(self, "name"); + rt_device_t pwm_device = rt_device_find(name); + if(NULL == pwm_device){ + printf("[error] PWM: device \"%s\" no found.\r\n", name); + } + return (struct rt_device_pwm*)pwm_device; +} + +void pikaRTDevice_PWM_platformEnable(PikaObj* self) { + struct rt_device_pwm* pwm_dev = PWM_getDevice(self); + int ch = obj_getInt(self, "ch"); + rt_pwm_enable(pwm_dev, ch); +} + +void pikaRTDevice_PWM_platformSetDuty(PikaObj* self) { + struct rt_device_pwm* pwm_dev = PWM_getDevice(self); + int ch = obj_getInt(self, "ch"); + int freq = obj_getInt(self, "freq"); + float duty = obj_getFloat(self, "duty"); + int period = (int)((1.0 / (float)freq) * 1000 * 1000); + int pulse = (int)((float)period * duty); + rt_pwm_set(pwm_dev, ch, period, pulse); +} + +void pikaRTDevice_PWM_platformSetFrequency(PikaObj* self) { + pikaRTDevice_PWM_platformSetDuty(self); +}