add api for stddevice.PWM and support rtDevice_PWM

This commit is contained in:
lyon 2022-02-28 19:44:09 +08:00
parent a74a592eeb
commit 589b6d938e
4 changed files with 84 additions and 1 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -0,0 +1,32 @@
#include "pikaRTDevice_PWM.h"
#include <rtdevice.h>
#include <rtthread.h>
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);
}