Controller Input
Source: src/controller_input.c · 112 lines · 4 functions.
Screen / module overview
- Initialise joystick ADC
Reject repeat initialisation. Create ADC unit 1 and configure X and Y channels for 12-bit readings.
- Handle initialisation failure
If channel setup fails, delete the ADC unit and clear its handle.
- Read joystick
Reject null output or missing ADC unit. Read X; if successful, read Y and return the driver result.
- Optional battery setup
Configure the battery channel and create curve-fitting calibration.
- Read battery voltage
Validate pointers and handles; read raw ADC and convert to millivolts.
Module entry points
These functions are exposed to the rest of the project. Open one to follow its decisions and the helpers it calls.
How to read these diagrams
Each function has its own expandable diagram using the actual expressions and statements from this file. Decisions split into True and False; loop bodies are indented. Returns end the function immediately; break and continue follow their C meanings. Arrows between statements show their order when execution reaches them.
These are structural source diagrams before preprocessing, not runtime traces. Conditional compilation and macros remain source expressions. Ternary choices remain inside their statement. Calls are linked separately; callback execution and task scheduling are not inferred.
Functions in this file
controller_input_initialize() · source line 14
esp_err_t controller_input_initialize(void)
- Decision
joystick_adc != NULLTrue- Exit this function
return ESP_ERR_INVALID_STATE;
FalseContinue after this decision.
- Execute
const adc_oneshot_unit_init_cfg_t unit_config = { .unit_id = JOYSTICK_ADC_UNIT, .ulp_mode = ADC_ULP_MODE_DISABLE, }; - Execute
esp_err_t result = adc_oneshot_new_unit(&unit_config, &joystick_adc); - Decision
result != ESP_OKTrue- Execute
joystick_adc = NULL; - Exit this function
return result;
FalseContinue after this decision.
- Execute
const adc_oneshot_chan_cfg_t channel_config = { .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_12, }; - Execute
result = adc_oneshot_config_channel( joystick_adc, JOYSTICK_X_CHANNEL, &channel_config); - Decision
result != ESP_OKTrue- Execute
adc_oneshot_del_unit(joystick_adc); - Execute
joystick_adc = NULL; - Exit this function
return result;
FalseContinue after this decision.
- Execute
result = adc_oneshot_config_channel( joystick_adc, JOYSTICK_Y_CHANNEL, &channel_config); - Decision
result != ESP_OKTrue- Execute
adc_oneshot_del_unit(joystick_adc); - Execute
joystick_adc = NULL; - Exit this function
return result;
FalseContinue after this decision.
- Exit this function
return ESP_OK;
Read original function source
esp_err_t controller_input_initialize(void)
{
if (joystick_adc != NULL) {
return ESP_ERR_INVALID_STATE;
}
const adc_oneshot_unit_init_cfg_t unit_config = {
.unit_id = JOYSTICK_ADC_UNIT,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
esp_err_t result =
adc_oneshot_new_unit(&unit_config, &joystick_adc);
if (result != ESP_OK) {
joystick_adc = NULL;
return result;
}
const adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
result = adc_oneshot_config_channel(
joystick_adc,
JOYSTICK_X_CHANNEL,
&channel_config);
if (result != ESP_OK) {
adc_oneshot_del_unit(joystick_adc);
joystick_adc = NULL;
return result;
}
result = adc_oneshot_config_channel(
joystick_adc,
JOYSTICK_Y_CHANNEL,
&channel_config);
if (result != ESP_OK) {
adc_oneshot_del_unit(joystick_adc);
joystick_adc = NULL;
return result;
}
return ESP_OK;
}controller_input_read() · source line 60
esp_err_t controller_input_read(joystick_state_t *state)
- Decision
state == NULLTrue- Exit this function
return ESP_ERR_INVALID_ARG;
FalseContinue after this decision.
- Decision
joystick_adc == NULLTrue- Exit this function
return ESP_ERR_INVALID_STATE;
FalseContinue after this decision.
- Execute
esp_err_t result = adc_oneshot_read( joystick_adc, JOYSTICK_X_CHANNEL, &state->x_raw); - Decision
result != ESP_OKTrue- Exit this function
return result;
FalseContinue after this decision.
- Exit this function
return adc_oneshot_read( joystick_adc, JOYSTICK_Y_CHANNEL, &state->y_raw);
Read original function source
esp_err_t controller_input_read(joystick_state_t *state)
{
if (state == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (joystick_adc == NULL) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t result = adc_oneshot_read(
joystick_adc,
JOYSTICK_X_CHANNEL,
&state->x_raw);
if (result != ESP_OK) {
return result;
}
return adc_oneshot_read(
joystick_adc,
JOYSTICK_Y_CHANNEL,
&state->y_raw);
}controller_input_configure_battery() · source line 84
esp_err_t controller_input_configure_battery(void)
- Decision
joystick_adc == NULLTrue- Exit this function
return ESP_ERR_INVALID_STATE;
FalseContinue after this decision.
- Decision
battery_cali != NULLTrue- Exit this function
return ESP_OK;
FalseContinue after this decision.
- Execute
const adc_oneshot_chan_cfg_t channel_config = { .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_12, }; - Execute
esp_err_t result = adc_oneshot_config_channel(joystick_adc, BATTERY_CHANNEL, &channel_config); - Decision
result != ESP_OKTrue- Exit this function
return result;
FalseContinue after this decision.
- Execute
const adc_cali_curve_fitting_config_t calibration_config = { .unit_id = JOYSTICK_ADC_UNIT, .chan = BATTERY_CHANNEL, .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_12, }; - Exit this function
return adc_cali_create_scheme_curve_fitting(&calibration_config, &battery_cali);
Read original function source
esp_err_t controller_input_configure_battery(void)
{
if (joystick_adc == NULL) return ESP_ERR_INVALID_STATE;
if (battery_cali != NULL) return ESP_OK;
const adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
esp_err_t result = adc_oneshot_config_channel(joystick_adc, BATTERY_CHANNEL,
&channel_config);
if (result != ESP_OK) return result;
const adc_cali_curve_fitting_config_t calibration_config = {
.unit_id = JOYSTICK_ADC_UNIT,
.chan = BATTERY_CHANNEL,
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
return adc_cali_create_scheme_curve_fitting(&calibration_config, &battery_cali);
}controller_input_read_battery_millivolts() · source line 104
esp_err_t controller_input_read_battery_millivolts(int *millivolts)
- Decision
millivolts == NULLTrue- Exit this function
return ESP_ERR_INVALID_ARG;
FalseContinue after this decision.
- Decision
joystick_adc == NULL || battery_cali == NULLTrue- Exit this function
return ESP_ERR_INVALID_STATE;
FalseContinue after this decision.
- Execute
int raw = 0; - Execute
esp_err_t result = adc_oneshot_read(joystick_adc, BATTERY_CHANNEL, &raw); - Decision
result != ESP_OKTrue- Exit this function
return result;
FalseContinue after this decision.
- Exit this function
return adc_cali_raw_to_voltage(battery_cali, raw, millivolts);
Read original function source
esp_err_t controller_input_read_battery_millivolts(int *millivolts)
{
if (millivolts == NULL) return ESP_ERR_INVALID_ARG;
if (joystick_adc == NULL || battery_cali == NULL) return ESP_ERR_INVALID_STATE;
int raw = 0;
esp_err_t result = adc_oneshot_read(joystick_adc, BATTERY_CHANNEL, &raw);
if (result != ESP_OK) return result;
return adc_cali_raw_to_voltage(battery_cali, raw, millivolts);
}Complete original file, including constants and data
/* Coded By JD(2026) */
#error "Reference listing only — not provided as build-ready source."
#include "controller_input.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali_scheme.h"
#define JOYSTICK_ADC_UNIT ADC_UNIT_1
#define JOYSTICK_X_CHANNEL ADC_CHANNEL_0
#define JOYSTICK_Y_CHANNEL ADC_CHANNEL_1
#define BATTERY_CHANNEL ADC_CHANNEL_3
static adc_oneshot_unit_handle_t joystick_adc = NULL;
static adc_cali_handle_t battery_cali = NULL;
esp_err_t controller_input_initialize(void)
{
if (joystick_adc != NULL) {
return ESP_ERR_INVALID_STATE;
}
const adc_oneshot_unit_init_cfg_t unit_config = {
.unit_id = JOYSTICK_ADC_UNIT,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
esp_err_t result =
adc_oneshot_new_unit(&unit_config, &joystick_adc);
if (result != ESP_OK) {
joystick_adc = NULL;
return result;
}
const adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
result = adc_oneshot_config_channel(
joystick_adc,
JOYSTICK_X_CHANNEL,
&channel_config);
if (result != ESP_OK) {
adc_oneshot_del_unit(joystick_adc);
joystick_adc = NULL;
return result;
}
result = adc_oneshot_config_channel(
joystick_adc,
JOYSTICK_Y_CHANNEL,
&channel_config);
if (result != ESP_OK) {
adc_oneshot_del_unit(joystick_adc);
joystick_adc = NULL;
return result;
}
return ESP_OK;
}
esp_err_t controller_input_read(joystick_state_t *state)
{
if (state == NULL) {
return ESP_ERR_INVALID_ARG;
}
if (joystick_adc == NULL) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t result = adc_oneshot_read(
joystick_adc,
JOYSTICK_X_CHANNEL,
&state->x_raw);
if (result != ESP_OK) {
return result;
}
return adc_oneshot_read(
joystick_adc,
JOYSTICK_Y_CHANNEL,
&state->y_raw);
}
esp_err_t controller_input_configure_battery(void)
{
if (joystick_adc == NULL) return ESP_ERR_INVALID_STATE;
if (battery_cali != NULL) return ESP_OK;
const adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
esp_err_t result = adc_oneshot_config_channel(joystick_adc, BATTERY_CHANNEL,
&channel_config);
if (result != ESP_OK) return result;
const adc_cali_curve_fitting_config_t calibration_config = {
.unit_id = JOYSTICK_ADC_UNIT,
.chan = BATTERY_CHANNEL,
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_12,
};
return adc_cali_create_scheme_curve_fitting(&calibration_config, &battery_cali);
}
esp_err_t controller_input_read_battery_millivolts(int *millivolts)
{
if (millivolts == NULL) return ESP_ERR_INVALID_ARG;
if (joystick_adc == NULL || battery_cali == NULL) return ESP_ERR_INVALID_STATE;
int raw = 0;
esp_err_t result = adc_oneshot_read(joystick_adc, BATTERY_CHANNEL, &raw);
if (result != ESP_OK) return result;
return adc_cali_raw_to_voltage(battery_cali, raw, millivolts);
}