pcnt: add rotary encoder example

This commit is contained in:
morris
2020-09-16 13:50:56 +08:00
parent 74d78148bc
commit 153e819e8a
21 changed files with 480 additions and 11 deletions
@@ -0,0 +1,2 @@
idf_component_register(SRCS "rotary_encoder_example_main.c"
INCLUDE_DIRS ".")
@@ -0,0 +1,3 @@
#
# Main Makefile. This is basically the same as a component makefile.
#
@@ -0,0 +1,37 @@
/* PCNT example -- Rotary Encoder
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "rotary_encoder.h"
static const char *TAG = "example";
void app_main(void)
{
// Rotary encoder underlying device is represented by a PCNT unit in this example
uint32_t pcnt_unit = 0;
// Create rotary encoder instance
rotary_encoder_config_t config = ROTARY_ENCODER_DEFAULT_CONFIG((rotary_encoder_dev_t)pcnt_unit, 14, 15);
rotary_encoder_t *encoder = NULL;
ESP_ERROR_CHECK(rotary_encoder_new_ec11(&config, &encoder));
// Filter out glitch (1us)
ESP_ERROR_CHECK(encoder->set_glitch_filter(encoder, 1));
// Start encoder
ESP_ERROR_CHECK(encoder->start(encoder));
// Report counter value
while (1) {
ESP_LOGI(TAG, "Encoder value: %d", encoder->get_counter_value(encoder));
vTaskDelay(pdMS_TO_TICKS(1000));
}
}