1 /* PCNT example -- Rotary Encoder
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include "freertos/FreeRTOS.h"
10 #include "freertos/task.h"
11 #include "esp_log.h"
12 #include "rotary_encoder.h"
13
14 static const char *TAG = "example";
15
app_main(void)16 void app_main(void)
17 {
18 // Rotary encoder underlying device is represented by a PCNT unit in this example
19 uint32_t pcnt_unit = 0;
20
21 // Create rotary encoder instance
22 rotary_encoder_config_t config = ROTARY_ENCODER_DEFAULT_CONFIG((rotary_encoder_dev_t)pcnt_unit, 14, 15);
23 rotary_encoder_t *encoder = NULL;
24 ESP_ERROR_CHECK(rotary_encoder_new_ec11(&config, &encoder));
25
26 // Filter out glitch (1us)
27 ESP_ERROR_CHECK(encoder->set_glitch_filter(encoder, 1));
28
29 // Start encoder
30 ESP_ERROR_CHECK(encoder->start(encoder));
31
32 // Report counter value
33 while (1) {
34 ESP_LOGI(TAG, "Encoder value: %d", encoder->get_counter_value(encoder));
35 vTaskDelay(pdMS_TO_TICKS(1000));
36 }
37 }
38