1
0
Fork 0

Part 2: Input

This commit is contained in:
Austin Morlan 2020-05-14 18:44:56 -07:00
parent e6687c543c
commit 2156cb1e1a
Signed by: austin
GPG Key ID: FD6B27654AF5E348
3 changed files with 141 additions and 3 deletions

View File

@ -1,16 +1,33 @@
#include "odroid/input.h"
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>
static const char* LOG_TAG = "Main";
void app_main(void)
{
Odroid_InitializeInput();
ESP_LOGI(LOG_TAG, "Odroid initialization complete - entering main loop");
for (;;)
{
printf("Hello World!\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
Odroid_Input input = Odroid_PollInput();
printf(
"\ra: %d b: %d start: %d select: %d vol: %d menu: %d up: %d down: %d left: %d right: %d ",
input.a, input.b, input.start, input.select, input.volume, input.menu,
input.up, input.down, input.left, input.right);
fflush(stdout);
vTaskDelay(250 / portTICK_PERIOD_MS);
}
// Should never get here
esp_restart();
}

99
src/odroid/input.c Normal file
View File

@ -0,0 +1,99 @@
#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;
}

22
src/odroid/input.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <stdint.h>
typedef struct
{
uint16_t a : 1;
uint16_t b : 1;
uint16_t volume : 1;
uint16_t menu : 1;
uint16_t select : 1;
uint16_t start : 1;
uint16_t left : 1;
uint16_t right : 1;
uint16_t up : 1;
uint16_t down : 1;
} Odroid_Input;
void Odroid_InitializeInput(void);
Odroid_Input Odroid_PollInput(void);