mirror of
https://github.com/nopnop2002/esp-idf-ssd1306.git
synced 2024-10-03 18:18:47 -04:00
Changed old adc driver to new driver when ESP-IDF V5
This commit is contained in:
parent
b375737adb
commit
db33a7559b
@ -4,10 +4,14 @@
|
||||
#include <math.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#else
|
||||
#include "driver/adc.h"
|
||||
#endif
|
||||
|
||||
#include "ssd1306.h"
|
||||
|
||||
/*
|
||||
@ -98,52 +102,27 @@ static uint8_t VUMeter1[1024] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
|
||||
//ADC Attenuation
|
||||
#define ADC_EXAMPLE_ATTEN ADC_ATTEN_DB_11
|
||||
|
||||
//ADC Calibration
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_VREF
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
|
||||
#elif CONFIG_IDF_TARGET_ESP32S3
|
||||
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP_FIT
|
||||
#endif
|
||||
|
||||
static esp_adc_cal_characteristics_t adc1_chars;
|
||||
static esp_adc_cal_characteristics_t adc2_chars;
|
||||
|
||||
static bool adc_calibration_init(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
bool cali_enable = false;
|
||||
|
||||
ret = esp_adc_cal_check_efuse(ADC_EXAMPLE_CALI_SCHEME);
|
||||
if (ret == ESP_ERR_NOT_SUPPORTED) {
|
||||
ESP_LOGW(TAG, "Calibration scheme not supported, skip software calibration");
|
||||
} else if (ret == ESP_ERR_INVALID_VERSION) {
|
||||
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
|
||||
} else if (ret == ESP_OK) {
|
||||
cali_enable = true;
|
||||
esp_adc_cal_characterize(ADC_UNIT_1, ADC_EXAMPLE_ATTEN, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
|
||||
esp_adc_cal_characterize(ADC_UNIT_2, ADC_EXAMPLE_ATTEN, ADC_WIDTH_BIT_DEFAULT, 0, &adc2_chars);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Invalid arg");
|
||||
}
|
||||
|
||||
return cali_enable;
|
||||
}
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
adc_calibration_init();
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
// ADC1 Init
|
||||
adc_oneshot_unit_handle_t adc1_handle;
|
||||
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
||||
|
||||
// ADC1 config
|
||||
adc_oneshot_chan_cfg_t config = {
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
.atten = ADC_ATTEN_DB_11,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_0, &config));
|
||||
#else
|
||||
// ADC1 config
|
||||
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));
|
||||
ESP_ERROR_CHECK(adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_EXAMPLE_ATTEN));
|
||||
ESP_ERROR_CHECK(adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11));
|
||||
#endif
|
||||
|
||||
SSD1306_t dev;
|
||||
|
||||
@ -199,22 +178,29 @@ void app_main(void)
|
||||
// Get from internal buffer to local buffer
|
||||
ssd1306_get_buffer(&dev, buffer);
|
||||
|
||||
int hMeter = 65; // horizontal center for needle animation
|
||||
//int vMeter = 85; // vertical center for needle animation (outside of dislay limits)
|
||||
int vMeter = 63; // vertical center for needle animation (outside of dislay limits)
|
||||
int rMeter = 50; // length of needle animation or arch of needle travel
|
||||
int hMeter = 65; // horizontal center for needle animation
|
||||
//int vMeter = 85; // vertical center for needle animation (outside of dislay limits)
|
||||
int vMeter = 63; // vertical center for needle animation (outside of dislay limits)
|
||||
int rMeter = 50; // length of needle animation or arch of needle travel
|
||||
|
||||
while(1) {
|
||||
//ssd1306_clear_screen(&dev, false);
|
||||
int sample = adc1_get_raw(ADC1_CHANNEL_0);
|
||||
int adc_raw;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_0, &adc_raw));
|
||||
#else
|
||||
adc_raw = adc1_get_raw(ADC1_CHANNEL_0);
|
||||
#endif
|
||||
|
||||
// Convert to 10 bits(from 0 to 1023). Because the original code is for ATMEGA328.
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32S2
|
||||
sample = sample / 4; // 12 bits -> 10 bits. Because the original code is for ATMEGA328.
|
||||
adc_raw = adc_raw / 4; // 12 bits -> 10 bits.
|
||||
#else
|
||||
// ESP32S2 only support 13 bit width
|
||||
sample = sample / 8; // 13 bits -> 10 bits. Because the original code is for ATMEGA328.
|
||||
adc_raw = adc_raw / 8; // 13 bits -> 10 bits.
|
||||
#endif
|
||||
ESP_LOGD(TAG, "sample=%d", sample);
|
||||
float MeterValue = sample * 120.079 / 1023;
|
||||
ESP_LOGD(TAG, "adc_raw=%d", adc_raw);
|
||||
float MeterValue = adc_raw * 120.079 / 1023;
|
||||
MeterValue = MeterValue - 60.039;
|
||||
int a1 = (hMeter + (sin(MeterValue / 502.64 * 6.283) * rMeter));
|
||||
int a2 = (vMeter - (cos(MeterValue / 502.64 * 6.283) * rMeter));
|
||||
|
Loading…
Reference in New Issue
Block a user