From 2156cb1e1a84de16c5950af35316f0e79482d464 Mon Sep 17 00:00:00 2001 From: Austin Morlan Date: Thu, 14 May 2020 18:44:56 -0700 Subject: [PATCH] Part 2: Input --- src/main.c | 23 +++++++++-- src/odroid/input.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++ src/odroid/input.h | 22 +++++++++++ 3 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 src/odroid/input.c create mode 100644 src/odroid/input.h diff --git a/src/main.c b/src/main.c index d90c908..4f9bfd7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,33 @@ +#include "odroid/input.h" +#include #include #include -#include + + +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(); } + diff --git a/src/odroid/input.c b/src/odroid/input.c new file mode 100644 index 0000000..a0a6648 --- /dev/null +++ b/src/odroid/input.c @@ -0,0 +1,99 @@ +#include "input.h" +#include +#include +#include +#include + + +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; +} + diff --git a/src/odroid/input.h b/src/odroid/input.h new file mode 100644 index 0000000..656fd3f --- /dev/null +++ b/src/odroid/input.h @@ -0,0 +1,22 @@ +#pragma once + +#include + + +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); +