A timing app designed to run on my Odroid-GO
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.3 KiB

#include "input.h"
#include <driver/adc.h>
#include <driver/gpio.h>
#include <esp_log.h>
#include <soc/adc_channel.h>
static const char* LOG_TAG = "OdroidInput";
static const gpio_num_t BUTTON_PIN_A = GPIO_NUM_32;
static const gpio_num_t BUTTON_PIN_B = GPIO_NUM_33;
static const gpio_num_t BUTTON_PIN_START = GPIO_NUM_39;
static const gpio_num_t BUTTON_PIN_SELECT = GPIO_NUM_27;
static const gpio_num_t BUTTON_PIN_VOLUME = GPIO_NUM_0;
static const gpio_num_t BUTTON_PIN_MENU = GPIO_NUM_13;
static const adc1_channel_t DPAD_PIN_X_AXIS = ADC1_GPIO34_CHANNEL;
static const adc1_channel_t DPAD_PIN_Y_AXIS = ADC1_GPIO35_CHANNEL;
void Odroid_InitializeInput()
{
// Configure digital buttons
{
gpio_config_t gpioConfig = {};
gpioConfig.mode = GPIO_MODE_INPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_ENABLE;
gpioConfig.pin_bit_mask =
(1ULL << BUTTON_PIN_A)
| (1ULL << BUTTON_PIN_B)
| (1ULL << BUTTON_PIN_START)
| (1ULL << BUTTON_PIN_SELECT)
| (1ULL << BUTTON_PIN_VOLUME)
| (1ULL << BUTTON_PIN_MENU);
ESP_ERROR_CHECK(gpio_config(&gpioConfig));
ESP_LOGI(LOG_TAG, "Buttons initialized");
}
// Configure analog d-pad
{
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
ESP_ERROR_CHECK(adc1_config_channel_atten(DPAD_PIN_X_AXIS,ADC_ATTEN_DB_11));
ESP_ERROR_CHECK(adc1_config_channel_atten(DPAD_PIN_Y_AXIS,ADC_ATTEN_DB_11));
ESP_LOGI(LOG_TAG, "D-pad initialized");
}
}
Odroid_Input Odroid_PollInput(void)
{
Odroid_Input input = {};
const uint32_t ADC_HIGH_LEVEL = 3072; // 75% of 12-bit (4096) range
const uint32_t ADC_LOW_LEVEL = 1024; // 25% of 12-bit (4096) range
{
uint32_t dpadX = adc1_get_raw(DPAD_PIN_X_AXIS);
if (dpadX > ADC_HIGH_LEVEL)
{
input.left = 1;
}
else if (dpadX > ADC_LOW_LEVEL)
{
input.right = 1;
}
}
{
uint32_t dpadY = adc1_get_raw(DPAD_PIN_Y_AXIS);
if (dpadY > ADC_HIGH_LEVEL)
{
input.up = 1;
}
else if (dpadY > ADC_LOW_LEVEL)
{
input.down = 1;
}
}
// Buttons are pulled up so must invert
input.a = !gpio_get_level(BUTTON_PIN_A);
input.b = !gpio_get_level(BUTTON_PIN_B);
input.select = !gpio_get_level(BUTTON_PIN_SELECT);
input.start = !gpio_get_level(BUTTON_PIN_START);
input.menu = !gpio_get_level(BUTTON_PIN_MENU);
input.volume = !gpio_get_level(BUTTON_PIN_VOLUME);
return input;
}