Outsource display, encoder cfg to config.cpp, Optimize speedsensor
- outsource configuration of display and encoder from source/header file
to config.cpp and pass it to init function or task from main()
- optimize logging in several init functions
- speedsensor:
- fix startup error: initialize ISR only once
- create instances at initialization instead of first method call
- ssd1306 display library:
- modify library to pass offsetX to init function instead of using macro
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
// NOTE: this file is included in main.cpp only.
|
||||
// outsourced all configuration related functions and structures to this file:
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "esp_log.h"
|
||||
}
|
||||
#include "motordrivers.hpp"
|
||||
#include "motorctl.hpp"
|
||||
#include "joystick.hpp"
|
||||
#include "http.hpp"
|
||||
#include "speedsensor.hpp"
|
||||
|
||||
#include "buzzer.hpp"
|
||||
#include "control.hpp"
|
||||
#include "fan.hpp"
|
||||
#include "auto.hpp"
|
||||
#include "chairAdjust.hpp"
|
||||
#include "display.hpp"
|
||||
#include "encoder.h"
|
||||
|
||||
//==================================
|
||||
//======== define loglevels ========
|
||||
@@ -181,3 +186,36 @@ speedSensor_config_t speedRight_config{
|
||||
.directionInverted = true,
|
||||
.logName = "speedRight",
|
||||
};
|
||||
|
||||
|
||||
|
||||
//-------------------------
|
||||
//-------- display --------
|
||||
//-------------------------
|
||||
display_config_t display_config {
|
||||
.gpio_scl = GPIO_NUM_22,
|
||||
.gpio_sda = GPIO_NUM_23,
|
||||
.gpio_reset = GPIO_NUM_15,
|
||||
.width = 128,
|
||||
.height = 64,
|
||||
.offsetX = 2,
|
||||
.flip = false,
|
||||
.contrast = 0xff, //max: 255
|
||||
};
|
||||
|
||||
|
||||
|
||||
//-------------------------
|
||||
//-------- encoder --------
|
||||
//-------------------------
|
||||
//configure rotary encoder (next to joystick)
|
||||
rotary_encoder_t encoder_config = {
|
||||
.pin_a = GPIO_NUM_25,
|
||||
.pin_b = GPIO_NUM_26,
|
||||
.pin_btn = GPIO_NUM_27,
|
||||
.code = 1,
|
||||
.store = 0, //encoder count
|
||||
.index = 0,
|
||||
.btn_pressed_time_us = 20000,
|
||||
.btn_state = RE_BTN_RELEASED //default state
|
||||
};
|
||||
@@ -8,15 +8,6 @@ extern "C"{
|
||||
|
||||
|
||||
|
||||
//==== display config ====
|
||||
#define I2C_INTERFACE y
|
||||
#define SCL_GPIO 22
|
||||
#define SDA_GPIO 23
|
||||
#define RESET_GPIO 15 // FIXME remove this
|
||||
// the following options are set in menuconfig: (see sdkconfig)
|
||||
// #define CONFIG_OFFSETX 2 //note: the larger display (actual 130x64) needs 2 pixel offset (prevents bugged column)
|
||||
// #define CONFIG_I2C_PORT_0 y
|
||||
|
||||
//=== content config ===
|
||||
#define STARTUP_MSG_TIMEOUT 2000
|
||||
#define ADC_BATT_VOLTAGE ADC1_CHANNEL_6
|
||||
@@ -55,22 +46,21 @@ static const char * TAG = "display";
|
||||
//==== display_init ====
|
||||
//======================
|
||||
//note CONFIG_OFFSETX is used (from menuconfig)
|
||||
void display_init(){
|
||||
void display_init(display_config_t config){
|
||||
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //max voltage
|
||||
ESP_LOGW("display", "INTERFACE is i2c");
|
||||
ESP_LOGW("display", "SDA_GPIO=%d",SDA_GPIO);
|
||||
ESP_LOGW("display", "SCL_GPIO=%d",SCL_GPIO);
|
||||
ESP_LOGW("display", "RESET_GPIO=%d",RESET_GPIO);
|
||||
i2c_master_init(&dev, SDA_GPIO, SCL_GPIO, RESET_GPIO);
|
||||
#if FLIP
|
||||
dev._flip = true;
|
||||
ESP_LOGW("display", "Flip upside down");
|
||||
#endif
|
||||
ESP_LOGI("display", "Panel is 128x64");
|
||||
ssd1306_init(&dev, 128, 64);
|
||||
ESP_LOGW(TAG, "Initializing Display...");
|
||||
ESP_LOGI(TAG, "config: sda=%d, sdl=%d, reset=%d, offset=%d, flip=%d, size: %dx%d",
|
||||
config.gpio_sda, config.gpio_scl, config.gpio_reset, config.offsetX, config.flip, config.width, config.height);
|
||||
|
||||
i2c_master_init(&dev, config.gpio_sda, config.gpio_scl, config.gpio_reset);
|
||||
if (config.flip) {
|
||||
dev._flip = true;
|
||||
ESP_LOGW(TAG, "Flip upside down");
|
||||
}
|
||||
ssd1306_init(&dev, config.width, config.height, config.offsetX);
|
||||
|
||||
ssd1306_clear_screen(&dev, false);
|
||||
ssd1306_contrast(&dev, 0xff);
|
||||
ssd1306_contrast(&dev, config.contrast);
|
||||
}
|
||||
|
||||
|
||||
@@ -264,11 +254,12 @@ void showStartupMsg(){
|
||||
|
||||
void display_task(void *pvParameters)
|
||||
{
|
||||
ESP_LOGW(TAG, "Initializing display and starting handle loop");
|
||||
//get struct with pointers to all needed global objects from task parameter
|
||||
display_task_parameters_t *objects = (display_task_parameters_t *)pvParameters;
|
||||
|
||||
// initialize display
|
||||
display_init();
|
||||
display_init(objects->displayConfig);
|
||||
// TODO check if successfully initialized
|
||||
|
||||
// show startup message
|
||||
|
||||
@@ -18,8 +18,22 @@ extern "C" {
|
||||
#include "control.hpp"
|
||||
#include "speedsensor.hpp"
|
||||
|
||||
// configuration for initializing display (passed to task as well)
|
||||
typedef struct display_config_t {
|
||||
gpio_num_t gpio_scl;
|
||||
gpio_num_t gpio_sda;
|
||||
gpio_num_t gpio_reset;
|
||||
int width;
|
||||
int height;
|
||||
int offsetX;
|
||||
bool flip;
|
||||
int contrast;
|
||||
} display_config_t;
|
||||
|
||||
|
||||
// struct with variables passed to task from main()
|
||||
typedef struct display_task_parameters_t {
|
||||
display_config_t displayConfig;
|
||||
controlledArmchair * control;
|
||||
evaluatedJoystick * joystick;
|
||||
QueueHandle_t encoderQueue;
|
||||
|
||||
@@ -17,22 +17,6 @@ extern "C"
|
||||
//------- variables -------
|
||||
//-------------------------
|
||||
static const char * TAG = "encoder";
|
||||
uint16_t encoderCount;
|
||||
rotary_encoder_btn_state_t encoderButtonState = {};
|
||||
//global event queue:
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -40,11 +24,11 @@ rotary_encoder_t encoderConfig = {
|
||||
//========== encoder_init ==========
|
||||
//==================================
|
||||
//initialize encoder //TODO pass config to this function
|
||||
QueueHandle_t encoder_init()
|
||||
QueueHandle_t encoder_init(rotary_encoder_t * encoderConfig)
|
||||
{
|
||||
QueueHandle_t encoderQueue = xQueueCreate(QUEUE_SIZE, sizeof(rotary_encoder_event_t));
|
||||
rotary_encoder_init(encoderQueue);
|
||||
rotary_encoder_add(&encoderConfig);
|
||||
rotary_encoder_add(encoderConfig);
|
||||
if (encoderQueue == NULL)
|
||||
ESP_LOGE(TAG, "Error initializing encoder or queue");
|
||||
else
|
||||
|
||||
@@ -6,12 +6,9 @@ extern "C" {
|
||||
|
||||
//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
|
||||
QueueHandle_t encoder_init(); //TODO pass config to function
|
||||
//init encoder with pointer to encoder config
|
||||
QueueHandle_t encoder_init(rotary_encoder_t * encoderConfig);
|
||||
|
||||
|
||||
//task that handles encoder events
|
||||
|
||||
@@ -69,7 +69,7 @@ cControlledRest *backRest;
|
||||
//-> makes it possible to easily use different motor drivers
|
||||
motorSetCommandFunc_t setLeftFunc = [&sabertoothDriver](motorCommand_t cmd)
|
||||
{
|
||||
sabertoothDriver->setLeft(cmd); //<= note: still using pointer to method in here (but stored in STACK)
|
||||
sabertoothDriver->setLeft(cmd); //<= note: still using pointer to method in here (but stored in STACK)
|
||||
};
|
||||
motorSetCommandFunc_t setRightFunc = [&sabertoothDriver](motorCommand_t cmd)
|
||||
{
|
||||
@@ -99,7 +99,7 @@ static const char * TAG = "main";
|
||||
//=================================
|
||||
//initialize spi flash filesystem (used for webserver)
|
||||
void init_spiffs(){
|
||||
ESP_LOGI(TAG, "init spiffs");
|
||||
ESP_LOGW(TAG, "initializing spiffs...");
|
||||
esp_vfs_spiffs_conf_t esp_vfs_spiffs_conf = {
|
||||
.base_path = "/spiffs",
|
||||
.partition_label = NULL,
|
||||
@@ -174,6 +174,7 @@ void createObjects()
|
||||
//=========== app_main ============
|
||||
//=================================
|
||||
extern "C" void app_main(void) {
|
||||
ESP_LOGW(TAG, "===== BOOT (pre main) Completed =====\n");
|
||||
|
||||
ESP_LOGW(TAG, "===== INITIALIZING COMPONENTS =====");
|
||||
//--- define log levels ---
|
||||
@@ -186,19 +187,22 @@ extern "C" void app_main(void) {
|
||||
gpio_set_level(GPIO_NUM_17, 1);
|
||||
|
||||
//--- initialize nvs-flash and netif (needed for wifi) ---
|
||||
ESP_LOGW(TAG,"initializing wifi...");
|
||||
wifi_initNvs_initNetif();
|
||||
|
||||
//--- initialize spiffs ---
|
||||
init_spiffs();
|
||||
|
||||
//--- initialize and start wifi ---
|
||||
ESP_LOGD(TAG,"starting wifi...");
|
||||
ESP_LOGW(TAG,"starting wifi...");
|
||||
//wifi_init_client(); //connect to existing wifi
|
||||
wifi_init_ap(); //start access point
|
||||
ESP_LOGD(TAG,"done starting wifi");
|
||||
|
||||
//--- initialize encoder ---
|
||||
const QueueHandle_t encoderQueue = encoder_init();
|
||||
const QueueHandle_t encoderQueue = encoder_init(&encoder_config);
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
|
||||
@@ -206,12 +210,14 @@ extern "C" void app_main(void) {
|
||||
ESP_LOGW(TAG, "===== CREATING SHARED OBJECTS =====");
|
||||
|
||||
//initialize sabertooth object in STACK (due to performance issues in heap)
|
||||
sabertoothDriver = static_cast<sabertooth2x60a*>(alloca(sizeof(sabertooth2x60a)));
|
||||
new (sabertoothDriver) sabertooth2x60a(sabertoothConfig);
|
||||
///sabertoothDriver = static_cast<sabertooth2x60a*>(alloca(sizeof(sabertooth2x60a)));
|
||||
///new (sabertoothDriver) sabertooth2x60a(sabertoothConfig);
|
||||
|
||||
//create all class instances used below in HEAP
|
||||
createObjects();
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
|
||||
//--- create tasks ---
|
||||
@@ -256,13 +262,16 @@ extern "C" void app_main(void) {
|
||||
//----- create task for display -----
|
||||
//-----------------------------------
|
||||
////task that handles the display (show stats, handle menu in 'MENU' mode)
|
||||
display_task_parameters_t display_param = {control, joystick, encoderQueue, motorLeft, motorRight, speedLeft, speedRight, buzzer};
|
||||
display_task_parameters_t display_param = {display_config, control, joystick, encoderQueue, motorLeft, motorRight, speedLeft, speedRight, buzzer};
|
||||
xTaskCreate(&display_task, "display_task", 3*2048, &display_param, 3, NULL);
|
||||
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS); //wait for all tasks to finish initializing
|
||||
printf("\n");
|
||||
|
||||
|
||||
|
||||
//--- startup finished ---
|
||||
ESP_LOGW(TAG, "===== STARTUP FINISHED =====");
|
||||
ESP_LOGW(TAG, "===== STARTUP FINISHED =====\n");
|
||||
buzzer->beep(3, 70, 50);
|
||||
|
||||
//--- testing encoder ---
|
||||
|
||||
Reference in New Issue
Block a user