Move code from main to control.cpp, display pages
- move code from testing task to control.cpp - outsource functions for initializing display and encoder - delete testing code - add two display pages (current distance and counter) - run handle functions for each button
This commit is contained in:
parent
739f5ef4c2
commit
22f8aef8d2
File diff suppressed because one or more lines are too long
@ -46,7 +46,6 @@
|
|||||||
//--------------------------
|
//--------------------------
|
||||||
//----- encoder config -----
|
//----- encoder config -----
|
||||||
//--------------------------
|
//--------------------------
|
||||||
#define TAG "app"
|
|
||||||
#define ROT_ENC_A_GPIO GPIO_NUM_19
|
#define ROT_ENC_A_GPIO GPIO_NUM_19
|
||||||
#define ROT_ENC_B_GPIO GPIO_NUM_21
|
#define ROT_ENC_B_GPIO GPIO_NUM_21
|
||||||
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
|
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
|
||||||
|
160
main/control.cpp
160
main/control.cpp
@ -1,10 +1,168 @@
|
|||||||
#include "control.hpp"
|
#include "control.hpp"
|
||||||
|
|
||||||
|
//========================
|
||||||
|
//===== init display =====
|
||||||
|
//========================
|
||||||
|
max7219_t init_display(){
|
||||||
|
// Configure SPI bus
|
||||||
|
spi_bus_config_t cfg;
|
||||||
|
cfg.mosi_io_num = DISPLAY_PIN_NUM_MOSI;
|
||||||
|
cfg.miso_io_num = -1;
|
||||||
|
cfg.sclk_io_num = DISPLAY_PIN_NUM_CLK;
|
||||||
|
cfg.quadwp_io_num = -1;
|
||||||
|
cfg.quadhd_io_num = -1;
|
||||||
|
cfg.max_transfer_sz = 0;
|
||||||
|
cfg.flags = 0;
|
||||||
|
ESP_ERROR_CHECK(spi_bus_initialize(HOST, &cfg, 1));
|
||||||
|
|
||||||
|
// Configure device
|
||||||
|
max7219_t dev;
|
||||||
|
dev.cascade_size = 1;
|
||||||
|
dev.digits = 8;
|
||||||
|
dev.mirrored = true;
|
||||||
|
ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS));
|
||||||
|
ESP_ERROR_CHECK(max7219_init(&dev));
|
||||||
|
//0...15
|
||||||
|
ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12));
|
||||||
|
return dev;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//========================
|
||||||
|
//===== init encoder =====
|
||||||
|
//========================
|
||||||
|
QueueHandle_t init_encoder(rotary_encoder_info_t * info){
|
||||||
|
// 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
|
||||||
|
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));
|
||||||
|
return event_queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//============================
|
||||||
|
//===== display distance =====
|
||||||
|
//============================
|
||||||
|
//display current position in meters from current encoder count
|
||||||
|
void display_current_distance(max7219_t * dev, rotary_encoder_info_t * info){
|
||||||
|
// --- event based action ---
|
||||||
|
// Wait for incoming events on the event queue.
|
||||||
|
// rotary_encoder_event_t event;
|
||||||
|
// if (xQueueReceive(event_queue, &event, 1000 / portTICK_PERIOD_MS) == pdTRUE)
|
||||||
|
// {
|
||||||
|
// ESP_LOGI(TAG, "Event: position %d, direction %s", event.state.position,
|
||||||
|
// event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
||||||
|
// //--- calculate distalce ---
|
||||||
|
// distance_mm = event.state.position * (MEASURING_ROLL_DIAMETER * PI / 600); //TODO dont calculate constant factor every time
|
||||||
|
// //--- show current position on display ---
|
||||||
|
// //sprintf(buf, "%08d", event.state.position);
|
||||||
|
// //--- show current distance in cm on display ---
|
||||||
|
// sprintf(buf, "%06.1f cm", (float)distance_mm/10);
|
||||||
|
// //printf("float num\n");
|
||||||
|
// max7219_clear(&dev);
|
||||||
|
// max7219_draw_text_7seg(&dev, 0, buf);
|
||||||
|
// }
|
||||||
|
// else //no event for 1s
|
||||||
|
// {
|
||||||
|
// // Poll current position and direction
|
||||||
|
// rotary_encoder_state_t state;
|
||||||
|
// ESP_ERROR_CHECK(rotary_encoder_get_state(&info, &state));
|
||||||
|
// ESP_LOGI(TAG, "Poll: position %d, direction %s", state.position,
|
||||||
|
// state.direction ? (state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Poll current position and direction
|
||||||
|
rotary_encoder_state_t state;
|
||||||
|
rotary_encoder_get_state(info, &state);
|
||||||
|
//--- calculate distalce ---
|
||||||
|
float distance_mm = state.position * (MEASURING_ROLL_DIAMETER * PI / 600); //TODO dont calculate constant factor every time
|
||||||
|
|
||||||
|
//--- show current position on display ---
|
||||||
|
char buf[10]; // 8 digits + decimal point + \0
|
||||||
|
sprintf(buf, "%06.1f cm", (float)distance_mm/10);
|
||||||
|
//printf("float num\n");
|
||||||
|
max7219_clear(dev);
|
||||||
|
max7219_draw_text_7seg(dev, 0, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//====================
|
||||||
|
//==== variables =====
|
||||||
|
//====================
|
||||||
|
static const char *TAG = "control";
|
||||||
|
char display_buf[10]; // 8 digits + decimal point + \0
|
||||||
|
max7219_t display; //display device
|
||||||
|
QueueHandle_t encoder_queue = NULL; //encoder event queue
|
||||||
|
rotary_encoder_info_t encoder; //encoder device/info
|
||||||
|
|
||||||
|
uint8_t count = 0; //count for testing
|
||||||
|
uint32_t timestamp_pageSwitched = 0;
|
||||||
|
bool page = false; //store page number currently displayed
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//========================
|
||||||
|
//===== control task =====
|
||||||
|
//========================
|
||||||
void task_control(void *pvParameter)
|
void task_control(void *pvParameter)
|
||||||
{
|
{
|
||||||
|
//initialize display
|
||||||
|
display = init_display();
|
||||||
|
//initialize encoder
|
||||||
|
encoder_queue = init_encoder(&encoder);
|
||||||
|
|
||||||
|
//display startup message
|
||||||
|
max7219_clear(&display);
|
||||||
|
ESP_LOGI(TAG, "showing startup message...");
|
||||||
|
max7219_draw_text_7seg(&display, 0, "ABCDEFGH");
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(500));
|
||||||
|
|
||||||
|
|
||||||
|
//===== loop =====
|
||||||
while(1){
|
while(1){
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
//run handle functions for all switches
|
||||||
|
SW_START.handle();
|
||||||
|
SW_RESET.handle();
|
||||||
|
SW_SET.handle();
|
||||||
|
SW_PRESET1.handle();
|
||||||
|
SW_PRESET2.handle();
|
||||||
|
SW_PRESET3.handle();
|
||||||
|
|
||||||
|
//switch between two display pages
|
||||||
|
if (esp_log_timestamp() - timestamp_pageSwitched > 1000){
|
||||||
|
timestamp_pageSwitched = esp_log_timestamp();
|
||||||
|
page = !page;
|
||||||
|
}
|
||||||
|
max7219_clear(&display);
|
||||||
|
if (page){
|
||||||
|
//display current position
|
||||||
|
display_current_distance(&display, &encoder);
|
||||||
|
} else {
|
||||||
|
//display counter
|
||||||
|
sprintf(display_buf, "cnt: %02d", count);
|
||||||
|
max7219_draw_text_7seg(&display, 0, display_buf);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//test button
|
||||||
|
if(SW_START.risingEdge){
|
||||||
|
buzzer.beep(1, 100, 100);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
@ -8,7 +9,14 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
|
||||||
|
#include <max7219.h>
|
||||||
|
#include "rotary_encoder.h"
|
||||||
|
}
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
#include "gpio_evaluateSwitch.hpp"
|
||||||
|
#include "buzzer.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void task_control(void *pvParameter);
|
void task_control(void *pvParameter);
|
||||||
|
168
main/main.cpp
168
main/main.cpp
@ -7,25 +7,22 @@ extern "C"
|
|||||||
#include "freertos/queue.h"
|
#include "freertos/queue.h"
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
|
||||||
#include <max7219.h>
|
|
||||||
#include "rotary_encoder.h"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "gpio_evaluateSwitch.hpp"
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "control.hpp"
|
#include "control.hpp"
|
||||||
#include "buzzer.hpp"
|
#include "buzzer.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
//=================================
|
||||||
|
//=========== functions ===========
|
||||||
|
//=================================
|
||||||
//function to configure gpio pin as output
|
//function to configure gpio pin as output
|
||||||
void gpio_configure_output(gpio_num_t gpio_pin){
|
void gpio_configure_output(gpio_num_t gpio_pin){
|
||||||
gpio_pad_select_gpio(gpio_pin);
|
gpio_pad_select_gpio(gpio_pin);
|
||||||
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
|
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void init_gpios(){
|
void init_gpios(){
|
||||||
//initialize all outputs
|
//initialize all outputs
|
||||||
//4x stepper mosfets
|
//4x stepper mosfets
|
||||||
@ -45,150 +42,6 @@ void init_gpios(){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//task for testing the encoder and display
|
|
||||||
void task_testing(void *pvParameter)
|
|
||||||
{
|
|
||||||
|
|
||||||
//========================
|
|
||||||
//===== init encoder =====
|
|
||||||
//========================
|
|
||||||
// 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;
|
|
||||||
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));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//========================
|
|
||||||
//===== init display =====
|
|
||||||
//========================
|
|
||||||
// Configure SPI bus
|
|
||||||
spi_bus_config_t cfg;
|
|
||||||
cfg.mosi_io_num = DISPLAY_PIN_NUM_MOSI;
|
|
||||||
cfg.miso_io_num = -1;
|
|
||||||
cfg.sclk_io_num = DISPLAY_PIN_NUM_CLK;
|
|
||||||
cfg.quadwp_io_num = -1;
|
|
||||||
cfg.quadhd_io_num = -1;
|
|
||||||
cfg.max_transfer_sz = 0;
|
|
||||||
cfg.flags = 0;
|
|
||||||
ESP_ERROR_CHECK(spi_bus_initialize(HOST, &cfg, 1));
|
|
||||||
|
|
||||||
// Configure device
|
|
||||||
max7219_t dev;
|
|
||||||
dev.cascade_size = 1;
|
|
||||||
dev.digits = 8;
|
|
||||||
dev.mirrored = true;
|
|
||||||
ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS));
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(max7219_init(&dev));
|
|
||||||
|
|
||||||
//0...15
|
|
||||||
ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
char buf[10]; // 8 digits + decimal point + \0
|
|
||||||
int32_t distance_mm = 0;
|
|
||||||
|
|
||||||
//display startup message
|
|
||||||
max7219_clear(&dev);
|
|
||||||
max7219_draw_text_7seg(&dev, 0, "ABCDEFGH");
|
|
||||||
printf("showing startup message...");
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
||||||
|
|
||||||
|
|
||||||
//===== loop =====
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(10)); //limit processing of events
|
|
||||||
|
|
||||||
|
|
||||||
// Wait for incoming events on the event queue.
|
|
||||||
rotary_encoder_event_t event;
|
|
||||||
if (xQueueReceive(event_queue, &event, 1000 / portTICK_PERIOD_MS) == pdTRUE)
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Event: position %d, direction %s", event.state.position,
|
|
||||||
event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
|
||||||
|
|
||||||
|
|
||||||
//--- calculate distalce ---
|
|
||||||
distance_mm = event.state.position * (MEASURING_ROLL_DIAMETER * PI / 600); //TODO dont calculate constant factor every time
|
|
||||||
//--- show current position on display ---
|
|
||||||
//sprintf(buf, "%08d", event.state.position);
|
|
||||||
//--- show current distance in cm on display ---
|
|
||||||
sprintf(buf, "%06.1f cm", (float)distance_mm/10);
|
|
||||||
//printf("float num\n");
|
|
||||||
max7219_clear(&dev);
|
|
||||||
max7219_draw_text_7seg(&dev, 0, buf);
|
|
||||||
}
|
|
||||||
else //no event for 1s
|
|
||||||
{
|
|
||||||
// Poll current position and direction
|
|
||||||
rotary_encoder_state_t state;
|
|
||||||
ESP_ERROR_CHECK(rotary_encoder_get_state(&info, &state));
|
|
||||||
ESP_LOGI(TAG, "Poll: position %d, direction %s", state.position,
|
|
||||||
state.direction ? (state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET");
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//display test
|
|
||||||
//float count = 0;
|
|
||||||
|
|
||||||
// printf("Display cycle\n");
|
|
||||||
//
|
|
||||||
// max7219_clear(&dev);
|
|
||||||
// max7219_draw_text_7seg(&dev, 0, "12345678");
|
|
||||||
// printf("num");
|
|
||||||
// vTaskDelay(pdMS_TO_TICKS(DISPLAY_DELAY));
|
|
||||||
//
|
|
||||||
// max7219_clear(&dev);
|
|
||||||
// max7219_draw_text_7seg(&dev, 0, "ABCDEFGH");
|
|
||||||
// printf("text");
|
|
||||||
// vTaskDelay(pdMS_TO_TICKS(DISPLAY_DELAY));
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// if (count > 20){
|
|
||||||
// count = 0;
|
|
||||||
// } else {
|
|
||||||
// count += 0.005;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// sprintf(buf, "%06.3f m", count);
|
|
||||||
// //printf("float num\n");
|
|
||||||
// max7219_clear(&dev);
|
|
||||||
// max7219_draw_text_7seg(&dev, 0, buf);
|
|
||||||
// vTaskDelay(pdMS_TO_TICKS(10));
|
|
||||||
//
|
|
||||||
// max7219_clear(&dev);
|
|
||||||
// sprintf(buf, "%08x", 12345678);
|
|
||||||
// printf("num\n");
|
|
||||||
// max7219_draw_text_7seg(&dev, 0, buf);
|
|
||||||
// vTaskDelay(pdMS_TO_TICKS(DISPLAY_DELAY));
|
|
||||||
|
|
||||||
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
//======================================
|
//======================================
|
||||||
//============ buzzer task =============
|
//============ buzzer task =============
|
||||||
//======================================
|
//======================================
|
||||||
@ -205,21 +58,24 @@ void task_buzzer( void * pvParameters ){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//======================================
|
||||||
|
//=========== main function ============
|
||||||
|
//======================================
|
||||||
extern "C" void app_main()
|
extern "C" void app_main()
|
||||||
{
|
{
|
||||||
|
|
||||||
//init outputs
|
//init outputs
|
||||||
init_gpios();
|
init_gpios();
|
||||||
|
|
||||||
//enable 5V volate regulator
|
//enable 5V volate regulator
|
||||||
gpio_set_level(GPIO_NUM_17, 1);
|
gpio_set_level(GPIO_NUM_17, 1);
|
||||||
|
|
||||||
|
//define loglevel
|
||||||
|
esp_log_level_set("*", ESP_LOG_INFO);
|
||||||
|
esp_log_level_set("control", ESP_LOG_INFO);
|
||||||
|
|
||||||
//--- create task testing ---
|
//create task for controlling the machine
|
||||||
xTaskCreate(task_testing, "task_testing", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
|
|
||||||
//--- create task control ---
|
|
||||||
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
|
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
|
||||||
//--- create task for buzzer ---
|
//create task for handling the buzzer
|
||||||
xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
|
xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
|
||||||
|
|
||||||
//beep at startup
|
//beep at startup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user