|
1 |
| -// Board support package API: Hackerhotel 2024 implementation |
2 | 1 | // SPDX-FileCopyrightText: 2025 Nicolai Electronics
|
3 | 2 | // SPDX-License-Identifier: MIT
|
4 | 3 |
|
5 | 4 | #include <inttypes.h>
|
6 | 5 | #include <stdint.h>
|
7 | 6 | #include <string.h>
|
| 7 | +#include "bsp/i2c.h" |
8 | 8 | #include "bsp/input.h"
|
9 | 9 | #include "driver/gpio.h"
|
10 | 10 | #include "esp_check.h"
|
|
14 | 14 | #include "freertos/projdefs.h"
|
15 | 15 | #include "freertos/queue.h"
|
16 | 16 | #include "hal/gpio_types.h"
|
| 17 | +#include "kami_hardware.h" |
| 18 | +#include "mpr121.h" |
17 | 19 |
|
18 | 20 | static char const* TAG = "BSP: INPUT";
|
19 | 21 |
|
20 |
| -static QueueHandle_t event_queue = NULL; |
| 22 | +static QueueHandle_t event_queue = NULL; |
| 23 | +static mpr121_handle_t mpr121 = NULL; |
21 | 24 |
|
22 | 25 | esp_err_t bsp_input_initialize(void) {
|
23 | 26 | if (event_queue == NULL) {
|
24 | 27 | event_queue = xQueueCreate(32, sizeof(bsp_input_event_t));
|
25 | 28 | ESP_RETURN_ON_FALSE(event_queue, ESP_ERR_NO_MEM, TAG, "Failed to create input event queue");
|
26 | 29 | }
|
| 30 | + |
| 31 | + static i2c_master_bus_handle_t i2c_bus_handle_internal = NULL; |
| 32 | + static SemaphoreHandle_t i2c_concurrency_semaphore = NULL; |
| 33 | + ESP_RETURN_ON_ERROR(bsp_i2c_primary_bus_get_handle(&i2c_bus_handle_internal), TAG, "Failed to get I2C bus handle"); |
| 34 | + ESP_RETURN_ON_ERROR(bsp_i2c_primary_bus_get_semaphore(&i2c_concurrency_semaphore), TAG, |
| 35 | + "Failed to get I2C bus semaphore"); |
| 36 | + |
| 37 | + mpr121_config_t mpr121_config = { |
| 38 | + .int_io_num = BSP_MPR121_INT_PIN, |
| 39 | + .i2c_bus = i2c_bus_handle_internal, |
| 40 | + .i2c_address = BSP_MPR121_I2C_ADDRESS, |
| 41 | + .concurrency_semaphore = i2c_concurrency_semaphore, |
| 42 | + .touch_callback = NULL, // TODO |
| 43 | + .input_callback = NULL, // TODO |
| 44 | + .i2c_timeout = 1000, |
| 45 | + }; |
| 46 | + |
| 47 | + ESP_RETURN_ON_ERROR(mpr121_initialize(&mpr121_config, &mpr121), TAG, "Failed to initialize MPR121"); |
| 48 | + |
| 49 | + /*mpr121_touch_set_baseline(mpr121_handle_t handle, uint8_t pin, uint8_t baseline); |
| 50 | + mpr121_touch_set_touch_threshold(mpr121_handle_t handle, uint8_t pin, uint8_t touch_threshold); |
| 51 | + mpr121_touch_set_release_threshold(mpr121_handle_t handle, uint8_t pin, uint8_t release_threshold);*/ |
| 52 | + |
| 53 | + mpr121_gpio_set_mode(mpr121, BSP_MPR121_PIN_INPUT_CHRG, MPR121_INPUT_PULL_UP); |
| 54 | + mpr121_gpio_set_mode(mpr121, BSP_MPR121_PIN_INPUT_SD_DET, MPR121_INPUT_PULL_UP); |
| 55 | + |
| 56 | + mpr121_touch_configure(mpr121, 10, 0, true); // Use first 10 electrodes for touch |
| 57 | + |
| 58 | + /*while (1) { |
| 59 | + for (uint8_t pin = 0; pin < 10; pin++) { |
| 60 | + uint16_t value; |
| 61 | + if (mpr121_touch_get_analog(mpr121, pin, &value) == ESP_OK) { |
| 62 | + printf("%04x ", value); |
| 63 | + } else { |
| 64 | + ESP_LOGE(TAG, "Failed to read touch state from MPR121"); |
| 65 | + } |
| 66 | + } |
| 67 | + for (uint8_t pin = 10; pin < 12; pin++) { |
| 68 | + bool value; |
| 69 | + if (mpr121_gpio_get_level(mpr121, pin, &value) == ESP_OK) { |
| 70 | + printf("%s ", value ? "H" : "L"); |
| 71 | + } else { |
| 72 | + ESP_LOGE(TAG, "Failed to read input state from MPR121"); |
| 73 | + } |
| 74 | + } |
| 75 | + printf("\r\n"); |
| 76 | + vTaskDelay(100); |
| 77 | + }*/ |
| 78 | + |
27 | 79 | return ESP_OK;
|
28 | 80 | }
|
29 | 81 |
|
|
0 commit comments