Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 73 additions & 34 deletions main/espFeatures/adcFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

#include "driver/adc.h"


template<class Next>
class AdcFeature : public Next {
template <class Next> class AdcFeature : public Next {
using PinConfig = Next::PlatformInfo::PinConfig;

static std::pair<int, int> getAnalogPin(int pin) {
Expand All @@ -23,20 +21,34 @@ class AdcFeature : public Next {
}

class Adc {
public:
public:
void configure(int pin, int atten = static_cast<int>(ADC_ATTEN_DB_12)) {
int adcNum;
int channel;
std::tie(adcNum, channel) = getAnalogPin(pin);

esp_err_t err = adc1_config_width(static_cast<adc_bits_width_t>(ADC_WIDTH_BIT_DEFAULT));
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
}

err = adc1_config_channel_atten(static_cast<adc1_channel_t>(channel), static_cast<adc_atten_t>(atten));
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
if (adcNum == 1) {
esp_err_t err = adc1_config_width(
static_cast<adc_bits_width_t>(ADC_WIDTH_BIT_DEFAULT));
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
}

err = adc1_config_channel_atten(
static_cast<adc1_channel_t>(channel),
static_cast<adc_atten_t>(atten));
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
}
} else if (adcNum == 2) {
esp_err_t err = adc2_config_channel_atten(
static_cast<adc2_channel_t>(channel),
static_cast<adc_atten_t>(atten));
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
}
} else {
throw std::runtime_error("Unsupported ADC unit");
}
}

Expand All @@ -45,39 +57,66 @@ class AdcFeature : public Next {
int channel;
std::tie(adcNum, channel) = getAnalogPin(pin);

return adc1_get_raw(static_cast<adc1_channel_t>(channel)) >> (ADC_WIDTH_BIT_DEFAULT - 10);
int raw_val = 0;

if (adcNum == 1) {
raw_val = adc1_get_raw(static_cast<adc1_channel_t>(channel));
} else if (adcNum == 2) {
esp_err_t err = adc2_get_raw(
static_cast<adc2_channel_t>(channel),
static_cast<adc_bits_width_t>(ADC_WIDTH_BIT_DEFAULT),
&raw_val);
if (err != ESP_OK) {
throw std::runtime_error(esp_err_to_name(err));
}
} else {
throw std::runtime_error("Unsupported ADC unit");
}

return raw_val >> (ADC_WIDTH_BIT_DEFAULT - 10);
}
};

public:
public:
Adc adc;

void initialize() {
Next::initialize();

jac::FunctionFactory ff(this->context());
jac::Module& adcModule = this->newModule("adc");
jac::Module &adcModule = this->newModule("adc");

jac::Object attten = jac::Object::create(this->context());
attten.defineProperty("Db0", jac::Value::from(this->context(), static_cast<int>(ADC_ATTEN_DB_0)));
attten.defineProperty("Db2_5", jac::Value::from(this->context(), static_cast<int>(ADC_ATTEN_DB_2_5)));
attten.defineProperty("Db6", jac::Value::from(this->context(), static_cast<int>(ADC_ATTEN_DB_6)));
attten.defineProperty("Db12", jac::Value::from(this->context(), static_cast<int>(ADC_ATTEN_DB_12)));

adcModule.addExport("configure", ff.newFunctionVariadic([this](std::vector<jac::ValueWeak> args) {
if (args.size() < 1 || args.size() > 2) {
throw std::runtime_error("Invalid number of arguments");
}

int pin = args[0].to<int>();
int atten = static_cast<int>(ADC_ATTEN_DB_12);
if (args.size() == 2) {
atten = args[1].to<int>();
}

adc.configure(pin, atten);
}));
adcModule.addExport("read", ff.newFunction(noal::function(&Adc::read, &adc)));
attten.defineProperty(
"Db0", jac::Value::from(this->context(),
static_cast<int>(ADC_ATTEN_DB_0)));
attten.defineProperty(
"Db2_5", jac::Value::from(this->context(),
static_cast<int>(ADC_ATTEN_DB_2_5)));
attten.defineProperty(
"Db6", jac::Value::from(this->context(),
static_cast<int>(ADC_ATTEN_DB_6)));
attten.defineProperty(
"Db12", jac::Value::from(this->context(),
static_cast<int>(ADC_ATTEN_DB_12)));

adcModule.addExport(
"configure",
ff.newFunctionVariadic([this](std::vector<jac::ValueWeak> args) {
if (args.size() < 1 || args.size() > 2) {
throw std::runtime_error("Invalid number of arguments");
}

int pin = args[0].to<int>();
int atten = static_cast<int>(ADC_ATTEN_DB_12);
if (args.size() == 2) {
atten = args[1].to<int>();
}

adc.configure(pin, atten);
}));
adcModule.addExport("read",
ff.newFunction(noal::function(&Adc::read, &adc)));
adcModule.addExport("Attenuation", attten);
}
};
15 changes: 13 additions & 2 deletions main/platform/esp32s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ struct PlatformInfo {
{ 7, { 1, ADC1_GPIO7_CHANNEL } },
{ 8, { 1, ADC1_GPIO8_CHANNEL } },
{ 9, { 1, ADC1_GPIO9_CHANNEL } },
{ 10, { 1, ADC1_GPIO10_CHANNEL } }
};
{ 10, { 1, ADC1_GPIO10_CHANNEL } },

{ 11, { 2, ADC2_GPIO11_CHANNEL } },
{ 12, { 2, ADC2_GPIO12_CHANNEL } },
{ 13, { 2, ADC2_GPIO13_CHANNEL } },
{ 14, { 2, ADC2_GPIO14_CHANNEL } },
{ 15, { 2, ADC2_GPIO15_CHANNEL } },
{ 16, { 2, ADC2_GPIO16_CHANNEL } },
{ 17, { 2, ADC2_GPIO17_CHANNEL } },
{ 18, { 2, ADC2_GPIO18_CHANNEL } },
{ 19, { 2, ADC2_GPIO19_CHANNEL } },
{ 20, { 2, ADC2_GPIO20_CHANNEL } }
};
static inline const std::set<int> INTERRUPT_PINS = DIGITAL_PINS;
static inline constexpr int DEFAULT_I2C_SDA_PIN = 0;
static inline constexpr int DEFAULT_I2C_SCL_PIN = 1;
Expand Down