PSRAM auf Oct geändert, POTI Funktionen geändert auf EDIF 4.4, sdkconfig angepasst für nfc manager und potis
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* @file potentiometer_manager.c
|
||||
* @brief Potentiometer Manager Implementation - COMPLETE!
|
||||
* @brief Potentiometer Manager Implementation - ESP-IDF v4.4 Compatible
|
||||
*
|
||||
* Features:
|
||||
* - Dual ADC reading (Volume + Brightness)
|
||||
@@ -8,12 +8,13 @@
|
||||
* - Automatic monitoring task
|
||||
* - Callback on changes
|
||||
* - Direct ST7789 brightness control
|
||||
*
|
||||
* NOTE: Simplified for ESP-IDF v4.4 - removed esp_adc_cal (not available in v4.4)
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "potentiometer_manager.h"
|
||||
@@ -36,10 +37,6 @@ static TaskHandle_t poti_task_handle = NULL;
|
||||
static bool poti_task_running = false;
|
||||
static poti_change_callback_t change_callback = NULL;
|
||||
|
||||
// ADC calibration
|
||||
static esp_adc_cal_characteristics_t adc1_chars;
|
||||
static esp_adc_cal_characteristics_t adc2_chars;
|
||||
|
||||
/**
|
||||
* @brief Initialize ADC for potentiometers
|
||||
*/
|
||||
@@ -52,27 +49,23 @@ static esp_err_t poti_adc_init(void)
|
||||
// Configure ADC2 (Brightness - GPIO 46)
|
||||
adc2_config_channel_atten(ADC2_CHANNEL_5, POTI_ADC_ATTEN); // GPIO 46 = ADC2_CH5
|
||||
|
||||
// Characterize ADC
|
||||
esp_adc_cal_characterize(ADC_UNIT_1, POTI_ADC_ATTEN, POTI_ADC_WIDTH,
|
||||
1100, &adc1_chars);
|
||||
esp_adc_cal_characterize(ADC_UNIT_2, POTI_ADC_ATTEN, POTI_ADC_WIDTH,
|
||||
1100, &adc2_chars);
|
||||
ESP_LOGI(TAG, "ADC configured for potentiometers");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read ADC with averaging
|
||||
* @brief Read ADC with averaging (simplified - no calibration)
|
||||
*/
|
||||
static uint32_t poti_read_adc1_averaged(void)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
|
||||
for (int i = 0; i < ADC_SAMPLES; i++) {
|
||||
uint32_t voltage;
|
||||
int raw = adc1_get_raw(ADC1_CHANNEL_2);
|
||||
voltage = esp_adc_cal_raw_to_voltage(raw, &adc1_chars);
|
||||
sum += voltage;
|
||||
if (raw >= 0) {
|
||||
sum += raw;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
}
|
||||
|
||||
@@ -82,34 +75,38 @@ static uint32_t poti_read_adc1_averaged(void)
|
||||
static uint32_t poti_read_adc2_averaged(void)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
int valid_samples = 0;
|
||||
|
||||
for (int i = 0; i < ADC_SAMPLES; i++) {
|
||||
int raw;
|
||||
uint32_t voltage;
|
||||
|
||||
// ADC2 read (can fail if WiFi is active!)
|
||||
esp_err_t ret = adc2_get_raw(ADC2_CHANNEL_5, POTI_ADC_WIDTH, &raw);
|
||||
if (ret == ESP_OK) {
|
||||
voltage = esp_adc_cal_raw_to_voltage(raw, &adc2_chars);
|
||||
sum += voltage;
|
||||
if (ret == ESP_OK && raw >= 0) {
|
||||
sum += raw;
|
||||
valid_samples++;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "ADC2 read failed (WiFi active?)");
|
||||
return sum / (i > 0 ? i : 1); // Return partial average
|
||||
ESP_LOGD(TAG, "ADC2 read failed (WiFi active?)");
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
}
|
||||
|
||||
return sum / ADC_SAMPLES;
|
||||
if (valid_samples == 0) {
|
||||
return 0; // No valid readings
|
||||
}
|
||||
|
||||
return sum / valid_samples;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert voltage to percentage (0-100%)
|
||||
* @brief Convert raw ADC value to percentage (0-100%)
|
||||
* For 12-bit ADC: 0-4095 range
|
||||
*/
|
||||
static uint8_t voltage_to_percent(uint32_t voltage_mv, uint32_t max_mv)
|
||||
static uint8_t raw_to_percent(uint32_t raw_value, uint32_t max_value)
|
||||
{
|
||||
if (voltage_mv >= max_mv) return 100;
|
||||
return (voltage_mv * 100) / max_mv;
|
||||
if (raw_value >= max_value) return 100;
|
||||
return (raw_value * 100) / max_value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,13 +117,13 @@ static void poti_monitor_task(void *arg)
|
||||
ESP_LOGI(TAG, "Potentiometer monitoring task started");
|
||||
|
||||
while (poti_task_running) {
|
||||
// Read potentiometers
|
||||
uint32_t volume_mv = poti_read_adc1_averaged();
|
||||
uint32_t brightness_mv = poti_read_adc2_averaged();
|
||||
// Read potentiometers (raw ADC values)
|
||||
uint32_t volume_raw = poti_read_adc1_averaged();
|
||||
uint32_t brightness_raw = poti_read_adc2_averaged();
|
||||
|
||||
// Convert to percentage
|
||||
uint8_t new_volume = voltage_to_percent(volume_mv, 3300);
|
||||
uint8_t new_brightness = voltage_to_percent(brightness_mv, 3300);
|
||||
// Convert to percentage (12-bit ADC: 0-4095)
|
||||
uint8_t new_volume = raw_to_percent(volume_raw, 4095);
|
||||
uint8_t new_brightness = raw_to_percent(brightness_raw, 4095);
|
||||
|
||||
// Clamp brightness to minimum (display shouldn't be completely off)
|
||||
if (new_brightness < LCD_BCKL_MIN) {
|
||||
@@ -169,12 +166,16 @@ esp_err_t poti_manager_init(void)
|
||||
ESP_LOGI(TAG, "Initializing Potentiometer Manager...");
|
||||
|
||||
// Initialize ADC
|
||||
ESP_ERROR_CHECK(poti_adc_init());
|
||||
esp_err_t ret = poti_adc_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize ADC");
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Read initial values
|
||||
poti_update();
|
||||
|
||||
ESP_LOGI(TAG, "✓ Potentiometer Manager initialized");
|
||||
ESP_LOGI(TAG, "Potentiometer Manager initialized");
|
||||
ESP_LOGI(TAG, " Volume: %d%%, Brightness: %d%%",
|
||||
current_volume, current_brightness);
|
||||
|
||||
@@ -206,7 +207,7 @@ esp_err_t poti_manager_start(poti_change_callback_t callback)
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "✓ Potentiometer monitoring started");
|
||||
ESP_LOGI(TAG, "Potentiometer monitoring started");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -236,11 +237,11 @@ uint8_t poti_get_brightness(void)
|
||||
esp_err_t poti_update(void)
|
||||
{
|
||||
// Manual single update (without task)
|
||||
uint32_t volume_mv = poti_read_adc1_averaged();
|
||||
uint32_t brightness_mv = poti_read_adc2_averaged();
|
||||
uint32_t volume_raw = poti_read_adc1_averaged();
|
||||
uint32_t brightness_raw = poti_read_adc2_averaged();
|
||||
|
||||
current_volume = voltage_to_percent(volume_mv, 3300);
|
||||
current_brightness = voltage_to_percent(brightness_mv, 3300);
|
||||
current_volume = raw_to_percent(volume_raw, 4095);
|
||||
current_brightness = raw_to_percent(brightness_raw, 4095);
|
||||
|
||||
if (current_brightness < LCD_BCKL_MIN) {
|
||||
current_brightness = LCD_BCKL_MIN;
|
||||
|
||||
Reference in New Issue
Block a user