mirror of
https://github.com/esphome/esphome.git
synced 2025-04-08 20:00:27 +01:00
fix adc
This commit is contained in:
parent
05b97be419
commit
b1f9c57fc9
@ -424,7 +424,7 @@ std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
|
|||||||
|
|
||||||
#ifdef USE_ZEPHYR
|
#ifdef USE_ZEPHYR
|
||||||
float ADCSensor::sample() {
|
float ADCSensor::sample() {
|
||||||
uint16_t buf;
|
int16_t buf = 0;
|
||||||
struct adc_sequence sequence = {
|
struct adc_sequence sequence = {
|
||||||
.buffer = &buf,
|
.buffer = &buf,
|
||||||
/* buffer size in bytes, not number of samples */
|
/* buffer size in bytes, not number of samples */
|
||||||
@ -432,9 +432,13 @@ float ADCSensor::sample() {
|
|||||||
};
|
};
|
||||||
int32_t val_mv;
|
int32_t val_mv;
|
||||||
|
|
||||||
adc_sequence_init_dt(adc_channel_, &sequence);
|
auto err = adc_sequence_init_dt(adc_channel_, &sequence);
|
||||||
|
if (err < 0) {
|
||||||
|
ESP_LOGE(TAG, "Could sequence init %s (%d)", adc_channel_->dev->name, err);
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
auto err = adc_read(adc_channel_->dev, &sequence);
|
err = adc_read(adc_channel_->dev, &sequence);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
ESP_LOGE(TAG, "Could not read %s (%d)", adc_channel_->dev->name, err);
|
ESP_LOGE(TAG, "Could not read %s (%d)", adc_channel_->dev->name, err);
|
||||||
return 0.0;
|
return 0.0;
|
||||||
@ -449,6 +453,10 @@ float ADCSensor::sample() {
|
|||||||
val_mv = (int32_t) ((int16_t) buf);
|
val_mv = (int32_t) ((int16_t) buf);
|
||||||
} else {
|
} else {
|
||||||
val_mv = (int32_t) buf;
|
val_mv = (int32_t) buf;
|
||||||
|
// https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/0ed4d9ffc674ae407be7cacf5696a02f5e789861/cores/nRF5/wiring_analog_nRF52.c#L222
|
||||||
|
if (val_mv < 0) {
|
||||||
|
val_mv = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (output_raw_) {
|
if (output_raw_) {
|
||||||
|
@ -104,6 +104,8 @@ async def to_code(config):
|
|||||||
adc = cg.new_Pvariable(nrf_saadc, rhs)
|
adc = cg.new_Pvariable(nrf_saadc, rhs)
|
||||||
cg.add(var.set_adc_channel(adc))
|
cg.add(var.set_adc_channel(adc))
|
||||||
gain = "ADC_GAIN_1_6"
|
gain = "ADC_GAIN_1_6"
|
||||||
|
if config[CONF_PIN][CONF_NUMBER] == "VDDHDIV5":
|
||||||
|
gain = "ADC_GAIN_1_2"
|
||||||
zephyr_add_user("io-channels", f"<&adc {channel_id}>")
|
zephyr_add_user("io-channels", f"<&adc {channel_id}>")
|
||||||
zephyr_add_overlay(
|
zephyr_add_overlay(
|
||||||
f"""
|
f"""
|
||||||
|
@ -138,6 +138,8 @@ def zephyr_add_cdc_acm(config):
|
|||||||
# prevent device to go to susspend, without this communication stop working in python
|
# prevent device to go to susspend, without this communication stop working in python
|
||||||
# there should be a way to solve it
|
# there should be a way to solve it
|
||||||
zephyr_add_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False)
|
zephyr_add_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False)
|
||||||
|
# prevent logging when buffer is full
|
||||||
|
zephyr_add_prj_conf("USB_CDC_ACM_LOG_LEVEL_WRN", True)
|
||||||
zephyr_add_overlay(
|
zephyr_add_overlay(
|
||||||
"""
|
"""
|
||||||
&zephyr_udc0 {
|
&zephyr_udc0 {
|
||||||
@ -163,14 +165,15 @@ def copy_files():
|
|||||||
|
|
||||||
write_file_if_changed(CORE.relative_build_path("zephyr/prj.conf"), prj_conf)
|
write_file_if_changed(CORE.relative_build_path("zephyr/prj.conf"), prj_conf)
|
||||||
|
|
||||||
zephyr_add_overlay(
|
if CORE.data[KEY_ZEPHYR][KEY_USER]:
|
||||||
f"""
|
zephyr_add_overlay(
|
||||||
|
f"""
|
||||||
/ {{
|
/ {{
|
||||||
zephyr,user {{
|
zephyr,user {{
|
||||||
{[f"{key} = {', '.join(value)};" for key, value in CORE.data[KEY_ZEPHYR][KEY_USER].items()][0]}
|
{[f"{key} = {', '.join(value)};" for key, value in CORE.data[KEY_ZEPHYR][KEY_USER].items()][0]}
|
||||||
}};
|
}};
|
||||||
}};"""
|
}};"""
|
||||||
)
|
)
|
||||||
|
|
||||||
write_file_if_changed(
|
write_file_if_changed(
|
||||||
CORE.relative_build_path("zephyr/app.overlay"),
|
CORE.relative_build_path("zephyr/app.overlay"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user