88 lines
4.1 KiB
Markdown
88 lines
4.1 KiB
Markdown
# esp_component_template
|
|
|
|
esp_component_template
|
|
|
|
#include "zh_encoder.h"
|
|
|
|
zh_encoder_handle_t encoder_handle = {0};
|
|
|
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
|
|
|
// #define ROT_ENC_A_GPIO (CONFIG_ROT_ENC_A_GPIO)
|
|
// #define ROT_ENC_B_GPIO (CONFIG_ROT_ENC_B_GPIO)
|
|
|
|
// #define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
|
|
// #define RESET_AT 0 // Set to a positive non-zero number to reset the position if this value is exceeded
|
|
// #define FLIP_DIRECTION false // Set to true to reverse the clockwise/counterclockwise sense
|
|
|
|
void app_main(void)
|
|
{
|
|
esp_log_level_set("zh_encoder", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
|
|
esp_event_loop_create_default();
|
|
#ifdef CONFIG_IDF_TARGET_ESP8266
|
|
esp_event_handler_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL);
|
|
#else
|
|
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL, NULL);
|
|
#endif
|
|
zh_encoder_init_config_t encoder_init_config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
|
|
encoder_init_config.a_gpio_number = GPIO_NUM_26;
|
|
encoder_init_config.b_gpio_number = GPIO_NUM_27;
|
|
encoder_init_config.encoder_min_value = -10; // Just for example.
|
|
encoder_init_config.encoder_max_value = 20; // Just for example.
|
|
encoder_init_config.encoder_step = 0.1; // Just for example.
|
|
encoder_init_config.encoder_number = 1;
|
|
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
|
zh_encoder_reset(&encoder_handle); // Just for example.
|
|
zh_encoder_set(&encoder_handle, 5); // Just for example.
|
|
|
|
// esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
|
|
// ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
|
|
|
// // Initialise the rotary encoder device with the GPIOs for A and B signals
|
|
// rotary_encoder_info_t info = { 0 };
|
|
// ESP_ERROR_CHECK(rotary_encoder_init(&info, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO));
|
|
// ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(&info, ENABLE_HALF_STEPS));
|
|
// #ifdef FLIP_DIRECTION
|
|
// // ESP_ERROR_CHECK(rotary_encoder_flip_direction(&info));
|
|
// #endif
|
|
|
|
// // Create a queue for events from the rotary encoder driver.
|
|
// // Tasks can read from this queue to receive up to date position information.
|
|
// QueueHandle_t event_queue = rotary_encoder_create_queue();
|
|
// ESP_ERROR_CHECK(rotary_encoder_set_queue(&info, event_queue));
|
|
|
|
// while (1)
|
|
// {
|
|
// // Wait for incoming events on the event queue.
|
|
// rotary_encoder_event_t event = { 0 };
|
|
// if (xQueueReceive(event_queue, &event, portMAX_DELAY) == pdTRUE)
|
|
// {
|
|
// ESP_LOGI(TAG, "Event: position %ld, direction %s", event.state.position,
|
|
// event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
|
// }
|
|
// // else
|
|
// {
|
|
// // Poll current position and direction
|
|
// rotary_encoder_state_t state = { 0 };
|
|
// ESP_ERROR_CHECK(rotary_encoder_get_state(&info, &state));
|
|
// ESP_LOGI(TAG, "Poll: position %ld, direction %s", state.position,
|
|
// state.direction ? (state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
|
|
|
// // Reset the device
|
|
// if (RESET_AT && (state.position >= RESET_AT || state.position <= -RESET_AT))
|
|
// {
|
|
// ESP_LOGI(TAG, "Reset");
|
|
// ESP_ERROR_CHECK(rotary_encoder_reset(&info));
|
|
// }
|
|
// }
|
|
// }
|
|
// ESP_LOGE(TAG, "queue receive failed");
|
|
|
|
// ESP_ERROR_CHECK(rotary_encoder_uninit(&info));
|
|
}
|
|
|
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
|
{
|
|
zh_encoder_event_on_isr_t *event = event_data;
|
|
printf("Uncoder number %d position %f.\n", event->encoder_number, event->encoder_position);
|
|
} |