Controller Input

Source: src/controller_input.c · 112 lines · 4 functions.

Screen / module overview

  1. Initialise joystick ADC

    Reject repeat initialisation. Create ADC unit 1 and configure X and Y channels for 12-bit readings.

  2. Handle initialisation failure

    If channel setup fails, delete the ADC unit and clear its handle.

  3. Read joystick

    Reject null output or missing ADC unit. Read X; if successful, read Y and return the driver result.

  4. Optional battery setup

    Configure the battery channel and create curve-fitting calibration.

  5. 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)
  1. Decisionjoystick_adc != NULL
    True
      1. Exit this functionreturn ESP_ERR_INVALID_STATE;
    False

    Continue after this decision.

  2. Executeconst adc_oneshot_unit_init_cfg_t unit_config = { .unit_id = JOYSTICK_ADC_UNIT, .ulp_mode = ADC_ULP_MODE_DISABLE, };
  3. Executeesp_err_t result = adc_oneshot_new_unit(&unit_config, &joystick_adc);
  4. Decisionresult != ESP_OK
    True
      1. Executejoystick_adc = NULL;
      2. Exit this functionreturn result;
    False

    Continue after this decision.

  5. Executeconst adc_oneshot_chan_cfg_t channel_config = { .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_12, };
  6. Executeresult = adc_oneshot_config_channel( joystick_adc, JOYSTICK_X_CHANNEL, &channel_config);
  7. Decisionresult != ESP_OK
    True
      1. Executeadc_oneshot_del_unit(joystick_adc);
      2. Executejoystick_adc = NULL;
      3. Exit this functionreturn result;
    False

    Continue after this decision.

  8. Executeresult = adc_oneshot_config_channel( joystick_adc, JOYSTICK_Y_CHANNEL, &channel_config);
  9. Decisionresult != ESP_OK
    True
      1. Executeadc_oneshot_del_unit(joystick_adc);
      2. Executejoystick_adc = NULL;
      3. Exit this functionreturn result;
    False

    Continue after this decision.

  10. Exit this functionreturn 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)
  1. Decisionstate == NULL
    True
      1. Exit this functionreturn ESP_ERR_INVALID_ARG;
    False

    Continue after this decision.

  2. Decisionjoystick_adc == NULL
    True
      1. Exit this functionreturn ESP_ERR_INVALID_STATE;
    False

    Continue after this decision.

  3. Executeesp_err_t result = adc_oneshot_read( joystick_adc, JOYSTICK_X_CHANNEL, &state->x_raw);
  4. Decisionresult != ESP_OK
    True
      1. Exit this functionreturn result;
    False

    Continue after this decision.

  5. Exit this functionreturn 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)
  1. Decisionjoystick_adc == NULL
    True
    1. Exit this functionreturn ESP_ERR_INVALID_STATE;
    False

    Continue after this decision.

  2. Decisionbattery_cali != NULL
    True
    1. Exit this functionreturn ESP_OK;
    False

    Continue after this decision.

  3. Executeconst adc_oneshot_chan_cfg_t channel_config = { .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_12, };
  4. Executeesp_err_t result = adc_oneshot_config_channel(joystick_adc, BATTERY_CHANNEL, &channel_config);
  5. Decisionresult != ESP_OK
    True
    1. Exit this functionreturn result;
    False

    Continue after this decision.

  6. Executeconst 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, };
  7. Exit this functionreturn 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)
  1. Decisionmillivolts == NULL
    True
    1. Exit this functionreturn ESP_ERR_INVALID_ARG;
    False

    Continue after this decision.

  2. Decisionjoystick_adc == NULL || battery_cali == NULL
    True
    1. Exit this functionreturn ESP_ERR_INVALID_STATE;
    False

    Continue after this decision.

  3. Executeint raw = 0;
  4. Executeesp_err_t result = adc_oneshot_read(joystick_adc, BATTERY_CHANNEL, &raw);
  5. Decisionresult != ESP_OK
    True
    1. Exit this functionreturn result;
    False

    Continue after this decision.

  6. Exit this functionreturn 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);
}