-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
rotary_encoder_example_main.c
37 lines (29 loc) · 1.15 KB
/
rotary_encoder_example_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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));
}
}