Add encoder lib; Custom task, init - functional
currently handles encoder connected to pins configured in encoder.hpp and receives and logs all available events in encoder task Works as expected TODO: migrate with previous implementation of commands in button.cpp
This commit is contained in:
@@ -8,6 +8,7 @@ idf_component_register(
|
||||
"http.cpp"
|
||||
"auto.cpp"
|
||||
"uart.cpp"
|
||||
"encoder.cpp"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
)
|
||||
|
||||
81
board_control/main/encoder.cpp
Normal file
81
board_control/main/encoder.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "encoder.h"
|
||||
extern "C"
|
||||
{
|
||||
#include <stdio.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_event.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
}
|
||||
|
||||
#include "encoder.hpp"
|
||||
|
||||
//-------------------------
|
||||
//------- variables -------
|
||||
//-------------------------
|
||||
static const char * TAG = "encoder";
|
||||
uint16_t encoderCount;
|
||||
rotary_encoder_btn_state_t encoderButtonState = {};
|
||||
QueueHandle_t encoderQueue = NULL;
|
||||
|
||||
//encoder config
|
||||
rotary_encoder_t encoderConfig = {
|
||||
.pin_a = PIN_A,
|
||||
.pin_b = PIN_B,
|
||||
.pin_btn = PIN_BUTTON,
|
||||
.code = 1,
|
||||
.store = encoderCount,
|
||||
.index = 0,
|
||||
.btn_pressed_time_us = 20000,
|
||||
.btn_state = encoderButtonState
|
||||
};
|
||||
|
||||
|
||||
|
||||
//==================================
|
||||
//========== encoder_init ==========
|
||||
//==================================
|
||||
//initialize encoder
|
||||
void encoder_init(){
|
||||
encoderQueue = xQueueCreate(QUEUE_SIZE, sizeof(rotary_encoder_event_t));
|
||||
rotary_encoder_init(encoderQueue);
|
||||
rotary_encoder_add(&encoderConfig);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//==================================
|
||||
//========== task_encoder ==========
|
||||
//==================================
|
||||
//receive and handle encoder events
|
||||
void task_encoder(void *arg) {
|
||||
rotary_encoder_event_t ev; //store event data
|
||||
while (1) {
|
||||
if (xQueueReceive(encoderQueue, &ev, portMAX_DELAY)) {
|
||||
//log enocder events
|
||||
switch (ev.type){
|
||||
case RE_ET_CHANGED:
|
||||
ESP_LOGI(TAG, "Event type: RE_ET_CHANGED, diff: %d", ev.diff);
|
||||
break;
|
||||
case RE_ET_BTN_PRESSED:
|
||||
ESP_LOGI(TAG, "Button pressed");
|
||||
break;
|
||||
case RE_ET_BTN_RELEASED:
|
||||
ESP_LOGI(TAG, "Button released");
|
||||
break;
|
||||
case RE_ET_BTN_CLICKED:
|
||||
ESP_LOGI(TAG, "Button clicked");
|
||||
break;
|
||||
case RE_ET_BTN_LONG_PRESSED:
|
||||
ESP_LOGI(TAG, "Button long-pressed");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGW(TAG, "Unknown event type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
board_control/main/encoder.hpp
Normal file
18
board_control/main/encoder.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
extern "C" {
|
||||
#include "freertos/FreeRTOS.h" // FreeRTOS related headers
|
||||
#include "freertos/task.h"
|
||||
#include "encoder.h"
|
||||
}
|
||||
|
||||
//config
|
||||
#define QUEUE_SIZE 10
|
||||
#define PIN_A GPIO_NUM_25
|
||||
#define PIN_B GPIO_NUM_26
|
||||
#define PIN_BUTTON GPIO_NUM_27
|
||||
|
||||
|
||||
//init encoder with config in encoder.cpp
|
||||
void encoder_init();
|
||||
|
||||
//task that handles encoder events
|
||||
void task_encoder(void *arg);
|
||||
@@ -18,6 +18,7 @@ extern "C"
|
||||
|
||||
|
||||
#include "uart.hpp"
|
||||
#include "encoder.hpp"
|
||||
|
||||
|
||||
//=========================
|
||||
@@ -28,6 +29,13 @@ extern "C"
|
||||
//#define UART_TEST_ONLY
|
||||
|
||||
|
||||
//=========================
|
||||
//====== encoder TEST =====
|
||||
//=========================
|
||||
//only start encoder task
|
||||
#define ENCODER_TEST_ONLY
|
||||
|
||||
|
||||
//tag for logging
|
||||
static const char * TAG = "main";
|
||||
|
||||
@@ -157,7 +165,7 @@ void setLoglevels(void){
|
||||
//=========== app_main ============
|
||||
//=================================
|
||||
extern "C" void app_main(void) {
|
||||
#ifndef UART_TEST_ONLY
|
||||
#if !defined(ENCODER_TEST_ONLY) && !defined(UART_TEST_ONLY)
|
||||
//enable 5V volate regulator
|
||||
gpio_pad_select_gpio(GPIO_NUM_17);
|
||||
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
|
||||
@@ -214,24 +222,35 @@ extern "C" void app_main(void) {
|
||||
// vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
// ESP_LOGI(TAG, "initializing http server");
|
||||
// http_init_server();
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
//-------------------------------------------
|
||||
//--- create tasks for uart communication ---
|
||||
//-------------------------------------------
|
||||
|
||||
#ifndef ENCODER_TEST_ONLY
|
||||
uart_init();
|
||||
xTaskCreate(task_uartReceive, "task_uartReceive", 4096, NULL, 10, NULL);
|
||||
xTaskCreate(task_uartSend, "task_uartSend", 4096, NULL, 10, NULL);
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------
|
||||
//----- create task that handles encoder -----
|
||||
//--------------------------------------------
|
||||
#ifndef UART_TEST_ONLY
|
||||
encoder_init();
|
||||
xTaskCreate(task_encoder, "task_encoder", 4096, NULL, 10, NULL);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
//--- main loop ---
|
||||
//does nothing except for testing things
|
||||
|
||||
//--- testing force http mode after startup ---
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
control.changeMode(controlMode_t::HTTP);
|
||||
//control.changeMode(controlMode_t::HTTP);
|
||||
while(1){
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
//---------------------------------
|
||||
|
||||
Reference in New Issue
Block a user