Skip to content

Commit

Permalink
feat: external pull up detector
Browse files Browse the repository at this point in the history
  • Loading branch information
Otrebor671 committed Dec 27, 2024
1 parent 378fa64 commit 63ebe32
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "leds.h"
#include "menus_module.h"
#include "preferences.h"
#include "resistor_detector.h"
#include "sd_card.h"
#include "uart_bridge.h"

Expand All @@ -19,6 +20,7 @@

static const char* TAG = "main";
void app_main() {
resistor_detector(CONFIG_GPIO_RIGHT_BUTTON);
#if !defined(CONFIG_MAIN_DEBUG)
esp_log_level_set(TAG, ESP_LOG_NONE);
#endif
Expand Down
58 changes: 58 additions & 0 deletions firmware/main/modules/resistor_detector/resistor_detector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "resistor_detector.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "keyboard_module.h"

#define SAMPLE_COUNT 50
#define SAMPLE_DELAY_MS 5
#define C_DISCHARGE_MS 5

static const char* TAG = "Resistor_check";

esp_err_t resistor_detector(gpio_num_t gpio_num) {
gpio_config_t temp_config = {.pin_bit_mask = (1ULL << gpio_num),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = 0,
.pull_down_en = 0,
.intr_type = 0};
esp_err_t ret = gpio_config(&temp_config);
gpio_set_level(gpio_num, 0);
vTaskDelay(pdMS_TO_TICKS(C_DISCHARGE_MS));

gpio_config_t new_config = {.pin_bit_mask = (1ULL << gpio_num),
.mode = GPIO_MODE_INPUT,
.pull_up_en = 0,
.pull_down_en = 0,
.intr_type = 0};
ret = gpio_config(&new_config);

if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error configurando GPIO %d", gpio_num);
return ret;
}

int high_count = 0;
int low_count = 0;

for (int i = 0; i < SAMPLE_COUNT; i++) {
int pin_value = gpio_get_level(gpio_num);

if (pin_value == 1) {
high_count++;
} else {
low_count++;
}

vTaskDelay(pdMS_TO_TICKS(5));
}

printf("UP: %d | DOWN: %d\n", high_count, low_count);

if (ret != ESP_OK) {
ESP_LOGE(TAG, "Error restaurando configuración GPIO %d", gpio_num);
return ret;
}

return ESP_OK;
}
9 changes: 9 additions & 0 deletions firmware/main/modules/resistor_detector/resistor_detector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <stdio.h>

#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_system.h"

esp_err_t resistor_detector(gpio_num_t gpio_num);

0 comments on commit 63ebe32

Please sign in to comment.