2021-10-01 20:00:15 +08:00
|
|
|
#include "PikaStdDevice_ADC.h"
|
2022-12-23 20:23:40 +08:00
|
|
|
#include "PikaStdDevice_common.h"
|
2021-10-01 20:00:15 +08:00
|
|
|
|
|
|
|
void PikaStdDevice_ADC_enable(PikaObj* self) {
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformEnable", NULL);
|
2022-03-19 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 09:53:48 +08:00
|
|
|
void PikaStdDevice_ADC_disable(PikaObj* self) {
|
|
|
|
obj_runNativeMethod(self, "platformDisable", NULL);
|
2022-03-07 14:58:21 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 20:00:15 +08:00
|
|
|
void PikaStdDevice_ADC_init(PikaObj* self) {
|
|
|
|
obj_setStr(self, "pin", "PA0");
|
|
|
|
}
|
|
|
|
|
2021-12-10 23:02:40 +08:00
|
|
|
void PikaStdDevice_ADC___init__(PikaObj* self) {
|
|
|
|
PikaStdDevice_ADC_init(self);
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:23:39 +08:00
|
|
|
pika_float PikaStdDevice_ADC_read(PikaObj* self) {
|
2022-03-19 09:53:48 +08:00
|
|
|
obj_runNativeMethod(self, "platformRead", NULL);
|
2021-10-01 20:00:15 +08:00
|
|
|
return obj_getFloat(self, "val");
|
|
|
|
}
|
|
|
|
|
2021-12-10 23:02:40 +08:00
|
|
|
void PikaStdDevice_ADC_setPin(PikaObj* self, char* pin) {
|
2021-10-01 20:00:15 +08:00
|
|
|
obj_setStr(self, "pin", pin);
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:25:28 +08:00
|
|
|
static pika_dev* _get_dev(PikaObj* self) {
|
|
|
|
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
|
|
|
if (NULL != dev) {
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
dev = pika_hal_open(PIKA_HAL_ADC, obj_getStr(self, "pin"));
|
|
|
|
if (NULL == dev) {
|
|
|
|
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
|
|
|
obj_getStr(self, "pin"));
|
|
|
|
}
|
|
|
|
obj_setPtr(self, "pika_dev", dev);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:40:06 +08:00
|
|
|
void PikaStdDevice_ADC_platformEnable(PikaObj* self) {
|
2022-12-07 19:25:28 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
if (NULL == dev) {
|
|
|
|
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
|
|
|
obj_getStr(self, "pin"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pika_hal_ADC_config cfg = {0};
|
|
|
|
cfg.continue_or_single = PIKA_HAL_ADC_SINGLE;
|
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
2021-10-01 20:00:15 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 09:53:48 +08:00
|
|
|
void PikaStdDevice_ADC_platformDisable(PikaObj* self) {
|
2022-12-07 19:25:28 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
2022-03-07 14:58:21 +08:00
|
|
|
}
|
|
|
|
|
2021-11-14 19:40:06 +08:00
|
|
|
void PikaStdDevice_ADC_platformRead(PikaObj* self) {
|
2022-12-07 19:25:28 +08:00
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
pika_hal_ADC_config* cfg = (pika_hal_ADC_config*)dev->ioctl_config;
|
|
|
|
uint32_t val_i = 0;
|
|
|
|
pika_hal_read(dev, &val_i, sizeof(val_i));
|
|
|
|
pika_float val = (pika_float)val_i / (pika_float)cfg->max * cfg->vref;
|
|
|
|
val = val_i * obj_setFloat(self, "val", val);
|
2021-11-14 19:44:15 +08:00
|
|
|
}
|
2022-12-28 06:41:36 +08:00
|
|
|
|
|
|
|
void PikaStdDevice_ADC_close(PikaObj* self) {
|
|
|
|
pika_dev* dev = _get_dev(self);
|
|
|
|
pika_hal_close(dev);
|
|
|
|
}
|