feat(lib): Introduce adc_sensor library

Adds a new `adc_sensor` library to abstract reading analog values from ADC channels. The output of this library is currently simulated.

This library is now used by the `modbus_server` to read the motor current and the main supply voltage, replacing the previous implementation. This change improves modularity by centralizing ADC-related code into a dedicated module.

The build system has been updated to include the new library.
This commit is contained in:
2025-07-08 15:19:44 +02:00
parent edf0fb2563
commit c9b0f38576
7 changed files with 117 additions and 2 deletions

View File

@@ -0,0 +1,61 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <lib/adc_sensor.h>
LOG_MODULE_REGISTER(adc_sensor, LOG_LEVEL_INF);
// Simulated values
#define SIMULATED_VOLTAGE_MV 12000
#define SIMULATED_CURRENT_MA 45
static bool initialized = false;
int adc_sensor_init(void)
{
if (initialized) {
return 0;
}
#ifdef CONFIG_ADC_SENSOR_SIMULATED
LOG_INF("ADC sensor initialized (simulated mode)");
LOG_INF("Simulated values: %dmV, %dmA", SIMULATED_VOLTAGE_MV, SIMULATED_CURRENT_MA);
#else
// TODO: Initialize real ADC hardware
LOG_INF("ADC sensor initialized (real ADC mode - not yet implemented)");
#endif
initialized = true;
return 0;
}
uint16_t adc_sensor_get_voltage_mv(void)
{
if (!initialized) {
LOG_WRN("ADC sensor not initialized, calling adc_sensor_init()");
adc_sensor_init();
}
#ifdef CONFIG_ADC_SENSOR_SIMULATED
return SIMULATED_VOLTAGE_MV;
#else
// TODO: Read real ADC value for voltage
// For now return simulated value
return SIMULATED_VOLTAGE_MV;
#endif
}
uint16_t adc_sensor_get_current_ma(void)
{
if (!initialized) {
LOG_WRN("ADC sensor not initialized, calling adc_sensor_init()");
adc_sensor_init();
}
#ifdef CONFIG_ADC_SENSOR_SIMULATED
return SIMULATED_CURRENT_MA;
#else
// TODO: Read real ADC value for current
// For now return simulated value
return SIMULATED_CURRENT_MA;
#endif
}