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.
182 lines
3.3 KiB
182 lines
3.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";
|
|
|
|
const gpio_num_t BUTTON_PIN_A = GPIO_NUM_32;
|
|
const gpio_num_t BUTTON_PIN_B = GPIO_NUM_33;
|
|
const gpio_num_t BUTTON_PIN_START = GPIO_NUM_39;
|
|
const gpio_num_t BUTTON_PIN_SELECT = GPIO_NUM_27;
|
|
const gpio_num_t BUTTON_PIN_VOLUME = GPIO_NUM_0;
|
|
const gpio_num_t BUTTON_PIN_MENU = GPIO_NUM_13;
|
|
const adc1_channel_t DPAD_PIN_X_AXIS = ADC1_GPIO34_CHANNEL;
|
|
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;
|
|
}
|
|
|
|
void Odroid_PrintInputState(Odroid_Input* input)
|
|
{
|
|
bool inputSet = false;
|
|
if (input->a)
|
|
{
|
|
inputSet = true;
|
|
printf("a ");
|
|
}
|
|
if (input->b)
|
|
{
|
|
inputSet = true;
|
|
printf("b ");
|
|
}
|
|
if (input->volume)
|
|
{
|
|
inputSet = true;
|
|
printf("volume ");
|
|
}
|
|
if (input->menu)
|
|
{
|
|
inputSet = true;
|
|
printf("menu ");
|
|
}
|
|
if (input->select)
|
|
{
|
|
inputSet = true;
|
|
printf("select ");
|
|
}
|
|
if (input->start)
|
|
{
|
|
inputSet = true;
|
|
printf("start ");
|
|
}
|
|
if (input->left)
|
|
{
|
|
inputSet = true;
|
|
printf("left ");
|
|
}
|
|
if (input->right)
|
|
{
|
|
inputSet = true;
|
|
printf("right ");
|
|
}
|
|
if (input->up)
|
|
{
|
|
inputSet = true;
|
|
printf("up ");
|
|
}
|
|
if (input->down)
|
|
{
|
|
inputSet = true;
|
|
printf("down ");
|
|
}
|
|
|
|
if (inputSet)
|
|
printf("\n");
|
|
}
|
|
|
|
int Odroid_HasAnyInput(Odroid_Input* input)
|
|
{
|
|
if (input->a)
|
|
return true;
|
|
if (input->b)
|
|
return true;
|
|
if (input->volume)
|
|
return true;
|
|
if (input->menu)
|
|
return true;
|
|
if (input->select)
|
|
return true;
|
|
if (input->start)
|
|
return true;
|
|
if (input->left)
|
|
return true;
|
|
if (input->right)
|
|
return true;
|
|
if (input->up)
|
|
return true;
|
|
if (input->down)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|