From 0a1db4c87852bd3e5f133a7dc05c7130656c6d6c Mon Sep 17 00:00:00 2001 From: Nils Date: Sun, 19 Dec 2021 20:58:22 +0100 Subject: [PATCH] Added Button Peripheral --- main/Audiolib.h | 3 +- main/Peripherals.cpp | 44 ++++++++++++++++++++- main/Peripherals.h | 22 ++++++++--- main/main.cpp | 92 +++++++++++++++++++++++++++++--------------- 4 files changed, 121 insertions(+), 40 deletions(-) diff --git a/main/Audiolib.h b/main/Audiolib.h index 916546c..4fadf04 100644 --- a/main/Audiolib.h +++ b/main/Audiolib.h @@ -113,7 +113,7 @@ public: fout = fin * _b0 + _z1; _z1 = fin * _b1 + _z2 - _a1 * fout; _z2 = fin * _b2 - _a2 * fout; - return (in_out_type) fout; + return (in_out_type) fout * volume; } void update(float peakgain); @@ -123,6 +123,7 @@ private: float _b0, _b1, _b2, _a1, _a2; float _z1 = 0, _z2 = 0; float _k, _q; + float volume = 1.0; }; diff --git a/main/Peripherals.cpp b/main/Peripherals.cpp index 3bb459b..fa25bec 100644 --- a/main/Peripherals.cpp +++ b/main/Peripherals.cpp @@ -1,9 +1,27 @@ #include "Peripherals.h" -Potentiometer::Potentiometer(const int min, const int max, update_callback_t update_ntfy) +gpio_config_t btn_config = { + .pin_bit_mask = (uint64_t) 0, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_ENABLE, + .intr_type = GPIO_INTR_ANYEDGE +}; + +static void btn_isr_handler(void*); + +void btn_isr_handler(void *args) { + Button* obj = (Button*) args; + if (obj->on_change != NULL) { + bool state = obj->getPressed(); + (*obj->on_change)(state); + } +} + +Potentiometer::Potentiometer(const int min, const int max, pot_update_callback_t update_ntfy) : Potentiometer(ADC_UNIT_1, ADC_CHANNEL_1, min, max, update_ntfy) {} -Potentiometer::Potentiometer(const adc_unit_t unit, const adc_channel_t channel, const int min, const int max, update_callback_t update_ntfy) +Potentiometer::Potentiometer(const adc_unit_t unit, const adc_channel_t channel, const int min, const int max, pot_update_callback_t update_ntfy) : adc_unit(unit), adc_channel(channel), min_raw(min), @@ -88,3 +106,25 @@ float Potentiometer::get_percent() { int raw = get_raw(); return ((float)raw / (4096)) * 100; } + +Button::Button(gpio_num_t gpio_pin, btn_update_callback_t update_ntfy) + : on_change(update_ntfy), + gpio_pin(gpio_pin) { + gpio_config_t config = btn_config; + config.pin_bit_mask = (1 << gpio_pin); + gpio_config(&config); + + // static esp_err_t isr_service_init = gpio_install_isr_service(0); + // gpio_isr_handler_add(gpio_pin, btn_isr_handler, this); +} + +bool Button::getPressed() { + return (bool) gpio_get_level(gpio_pin); +} + +bool Button::getPressedSingle() { + bool state = (bool) gpio_get_level(gpio_pin); + bool ret = state && !lastState; + lastState = state; + return ret; +} diff --git a/main/Peripherals.h b/main/Peripherals.h index 45e213c..83bbbcc 100644 --- a/main/Peripherals.h +++ b/main/Peripherals.h @@ -11,12 +11,15 @@ #define atten ADC_ATTEN_DB_2_5 #define width ADC_WIDTH_BIT_12 -typedef void(*update_callback_t)(float); +typedef void(*pot_update_callback_t)(float); +typedef void(*btn_update_callback_t)(bool); + + class Potentiometer { public: - Potentiometer(const int, const int, update_callback_t = NULL); - Potentiometer(const adc_unit_t, const adc_channel_t, const int, const int, update_callback_t = NULL); + Potentiometer(const int, const int, pot_update_callback_t = NULL); + Potentiometer(const adc_unit_t, const adc_channel_t, const int, const int, pot_update_callback_t = NULL); int get_raw(); int update(); float get_percent(); @@ -26,7 +29,7 @@ private: adc_channel_t adc_channel; int min_raw; int max_raw; - update_callback_t on_change; + pot_update_callback_t on_change; int prev_raw; static bool init_adc(const adc_unit_t, const adc_channel_t); @@ -35,7 +38,16 @@ private: }; -class Button {}; +class Button { +public: + Button(gpio_num_t, btn_update_callback_t = NULL); + bool getPressed(); + bool getPressedSingle(); + btn_update_callback_t on_change; +private: + bool lastState = false; + gpio_num_t gpio_pin; +}; #endif \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index 0ecb2b9..0180dcd 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -14,70 +14,84 @@ #define INPUT_TIMER TIMER_0 #define DISPLAY_TIMER TIMER_1 -#define SCREENWIDTH 128 -#define SCREENHEIGHT 32 +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 32 +#define INPUT_INTERVAL_S 0.01 +#define DISPLAY_INTERVAL_S 0.09 +#define POT1_CHANNEL ADC_CHANNEL_5 + +#define BTN1_PIN GPIO_NUM_4 +#define BTN2_PIN GPIO_NUM_2 +#define BTN3_PIN GPIO_NUM_15 + +// -------------- Flags -------------- enum StatusBM { BT = 1, PLAY = 2, STOP = 4, PAUSE = 8 }; - enum eventBM { DISPLAY = 1, INPUT = 2, }; +// ----------------------------------- - +// -------------- Configs -------------- timer_config_t timer_config = { .alarm_en = TIMER_ALARM_EN, .counter_en = TIMER_PAUSE, + .intr_type = TIMER_INTR_LEVEL, .counter_dir = TIMER_COUNT_UP, .auto_reload = TIMER_AUTORELOAD_EN, .divider = TIMER_DIVIDER, }; +// ------------------------------------- u8g2_t u8g2; u8g2_esp32_hal_t u8g2_esp32_hal; +xQueueHandle event_queue; +// -------------- Global tracking vars -------------- char* titleText = (char*) ""; char* artistText = (char*) ""; -xQueueHandle event_queue; - int8_t displayStatus = 0; int8_t event = 0; - -double input_timer_interval = 0.01; // seconds -double display_timer_interval = 0.1; // seconds int x = 0; +// -------------------------------------------------- +// -------------- Function definitions -------------- void update_display(al_event_cb_t, al_event_cb_param_t*); -void draw(); -void thisfunc(); +void IRAM_ATTR draw(); bool IRAM_ATTR input_timer_cb(void* args); bool IRAM_ATTR display_timer_cb(void* args); +// -------------------------------------------------- - +// -------------- Object definitions -------------- Audiolib Audiosource = Audiolib("HESA FREDRIK", &update_display); -//void updateFilter(const uint8_t, float*, float*, CombinedChannelFilter*); -Potentiometer* pot1 = new Potentiometer(ADC_UNIT_2, ADC_CHANNEL_5, 70, 1010); //65/66 -> 572/573 -> 1019 +Potentiometer* pot1 = new Potentiometer(ADC_UNIT_2, POT1_CHANNEL, 70, 1010); //65/66 -> 572/573 -> 1019 //Potentiometer* pot2 = new Potentiometer(ADC1_CHANNEL_4, 41, 988); //Potentiometer* pot3 = new Potentiometer(ADC1_CHANNEL_6, 38, 988); //Potentiometer* pot4 = new Potentiometer(ADC1_CHANNEL_7, 29, 990); +Button* btn1 = new Button(BTN1_PIN); +Button* btn2 = new Button(BTN2_PIN); +Button* btn3 = new Button(BTN3_PIN); + CombinedChannelFilter* highshelf_filter = new CombinedChannelFilter(new Filter(HIGHSHELF, 2000, 44100, 0.8, 0), new Filter(HIGHSHELF, 2000, 44100, 0.8, 0)); CombinedChannelFilter* lowshelf_filter = new CombinedChannelFilter(new Filter(LOWSHELF, 250, 44100, 0.8, 0), new Filter(LOWSHELF, 250, 44100, 0.8, 0)); CombinedChannelFilter* highpass_filter = new CombinedChannelFilter(new Filter(LOWPASS, 8000, 44100, 0.75, 0), new Filter(LOWPASS, 8000, 44100, 0.75, 0)); CombinedChannelFilter* lowpass_filter = new CombinedChannelFilter(new Filter(HIGHPASS, 60, 44100, 0.75, 0), new Filter(HIGHPASS, 60, 44100, 0.75, 0)); CombinedChannelFilter* peak_filter = new CombinedChannelFilter(new Filter(PEAK, 700, 44100, 0.8, 0), new Filter(PEAK, 700, 44100, 0.8, 0)); +// ------------------------------------------------ extern "C" { void app_main(void){ - // -------------- U8G2 Setup -------------- + // -------------- U8G2 setup -------------- u8g2_esp32_hal.sda = GPIO_NUM_32; u8g2_esp32_hal.scl = GPIO_NUM_33; u8g2_esp32_hal_init(u8g2_esp32_hal); @@ -94,7 +108,7 @@ extern "C" { event_queue = xQueueCreate(10, 1); - // -------------- Audiosource Setup -------------- + // -------------- Audiosource setup -------------- Audiosource.set_I2S(26, 27, 25); Audiosource.add_combined_filter(highpass_filter); Audiosource.add_combined_filter(lowshelf_filter); @@ -104,16 +118,16 @@ extern "C" { Audiosource.start(); // ----------------------------------------------- - // -------------- Timer Setup -------------- + // -------------- Timer setup -------------- timer_init(TIMER_GROUP_0, INPUT_TIMER, &timer_config); timer_set_counter_value(TIMER_GROUP_0, INPUT_TIMER, 0); - timer_set_alarm_value(TIMER_GROUP_0, INPUT_TIMER, input_timer_interval * TIMER_SCALE); + timer_set_alarm_value(TIMER_GROUP_0, INPUT_TIMER, INPUT_INTERVAL_S * TIMER_SCALE); timer_enable_intr(TIMER_GROUP_0, INPUT_TIMER); timer_isr_callback_add(TIMER_GROUP_0, INPUT_TIMER, &input_timer_cb, nullptr, 0); timer_init(TIMER_GROUP_0, DISPLAY_TIMER, &timer_config); timer_set_counter_value(TIMER_GROUP_0, DISPLAY_TIMER, 0); - timer_set_alarm_value(TIMER_GROUP_0, DISPLAY_TIMER, display_timer_interval * TIMER_SCALE); + timer_set_alarm_value(TIMER_GROUP_0, DISPLAY_TIMER, DISPLAY_INTERVAL_S * TIMER_SCALE); timer_enable_intr(TIMER_GROUP_0, DISPLAY_TIMER); timer_isr_callback_add(TIMER_GROUP_0, DISPLAY_TIMER, &display_timer_cb, nullptr, 0); @@ -127,35 +141,48 @@ extern "C" { if (event & DISPLAY) { draw(); } - - - + if (event & INPUT) { + if (btn1->getPressedSingle()){ + Audiosource.previous(); + } + if (btn2->getPressedSingle()) { + if (displayStatus & PLAY) { + Audiosource.pause(); + } + else if (displayStatus & (PAUSE | STOP)){ + Audiosource.play(); + } + } + if (btn3->getPressedSingle()) { + Audiosource.next(); + } + } } } } -void IRAM_ATTR draw() { +void draw() { if (displayStatus & BT) { - u8g2_DrawGlyph(&u8g2, 0, SCREENHEIGHT/2, 127); + u8g2_DrawGlyph(&u8g2, 0, SCREEN_HEIGHT/2, 127); } if (displayStatus & STOP) { - u8g2_DrawGlyph(&u8g2, 12, SCREENHEIGHT/2, 129); + u8g2_DrawGlyph(&u8g2, 12, SCREEN_HEIGHT/2, 129); } else if (displayStatus & PAUSE) { - u8g2_DrawGlyph(&u8g2, 12, SCREENHEIGHT/2, 130); + u8g2_DrawGlyph(&u8g2, 12, SCREEN_HEIGHT/2, 130); } else if (displayStatus & PLAY) { - u8g2_DrawGlyph(&u8g2, 12, SCREENHEIGHT/2, 128); + u8g2_DrawGlyph(&u8g2, 12, SCREEN_HEIGHT/2, 128); } if ((displayStatus & PLAY) || (displayStatus & STOP) || (displayStatus & PAUSE)) { - u8g2_DrawStr(&u8g2, 23, SCREENHEIGHT/2, artistText); - u8g2_DrawStr(&u8g2, x, SCREENHEIGHT, titleText); + u8g2_DrawStr(&u8g2, 23, SCREEN_HEIGHT/2, artistText); + u8g2_DrawStr(&u8g2, x, SCREEN_HEIGHT, titleText); x += 1; - if (x == SCREENWIDTH) x = 0; + if (x == SCREEN_WIDTH) x = 0; } else { - u8g2_DrawStr(&u8g2, 0, SCREENHEIGHT/2, "Disconnected!"); - u8g2_DrawStr(&u8g2, 0, SCREENHEIGHT, "Connect Device!"); + u8g2_DrawStr(&u8g2, 0, SCREEN_HEIGHT/2, "Disconnected!"); + u8g2_DrawStr(&u8g2, 0, SCREEN_HEIGHT, "Connect Device!"); } u8g2_SendBuffer(&u8g2); u8g2_ClearBuffer(&u8g2); @@ -194,6 +221,7 @@ void update_display(al_event_cb_t event, al_event_cb_param_t* param) { if ((*artistText == '\0') || (*titleText == '\0')) { displayStatus = BT | STOP; } + x = 0; printf("AL, Meta_Update\n"); break; } -- 2.30.2