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:
@@ -7,6 +7,9 @@
|
||||
static const char* TAG = "speedSensor";
|
||||
|
||||
|
||||
//initialize ISR only once (for multiple instances)
|
||||
bool speedSensor::isrIsInitialized = false;
|
||||
|
||||
|
||||
uint32_t min(uint32_t a, uint32_t b){
|
||||
if (a>b) return b;
|
||||
@@ -84,11 +87,8 @@ void IRAM_ATTR onEncoderChange(void* arg) {
|
||||
speedSensor::speedSensor(speedSensor_config_t config_f){
|
||||
//copy config
|
||||
config = config_f;
|
||||
//note: currently gets initialized at first method call
|
||||
//this prevents crash due to too early initialization at boot
|
||||
//TODO: create global objects later after boot
|
||||
//init gpio and ISR
|
||||
//init();
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
@@ -102,15 +102,16 @@ void speedSensor::init() {
|
||||
gpio_pad_select_gpio(config.gpioPin);
|
||||
gpio_set_direction(config.gpioPin, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(config.gpioPin, GPIO_PULLUP_ONLY);
|
||||
ESP_LOGW(TAG, "%s, configured gpio-pin %d", config.logName, (int)config.gpioPin);
|
||||
|
||||
//configure interrupt
|
||||
gpio_set_intr_type(config.gpioPin, GPIO_INTR_ANYEDGE);
|
||||
gpio_install_isr_service(0);
|
||||
if (!isrIsInitialized) {
|
||||
gpio_install_isr_service(0);
|
||||
isrIsInitialized = true;
|
||||
ESP_LOGW(TAG, "Initialized ISR service");
|
||||
}
|
||||
gpio_isr_handler_add(config.gpioPin, onEncoderChange, this);
|
||||
ESP_LOGW(TAG, "%s, configured interrupt", config.logName);
|
||||
|
||||
isInitialized = true;
|
||||
ESP_LOGW(TAG, "[%s], configured gpio-pin %d and interrupt routine", config.logName, (int)config.gpioPin);
|
||||
}
|
||||
|
||||
|
||||
@@ -121,8 +122,6 @@ void speedSensor::init() {
|
||||
//==========================
|
||||
//get rotational speed in revolutions per minute
|
||||
float speedSensor::getRpm(){
|
||||
//check if initialized
|
||||
if (!isInitialized) init();
|
||||
uint32_t timeElapsed = esp_timer_get_time() - lastEdgeTime;
|
||||
//timeout (standstill)
|
||||
//TODO variable timeout considering config.degreePerGroup
|
||||
|
||||
@@ -24,8 +24,8 @@ class speedSensor {
|
||||
public:
|
||||
//constructor
|
||||
speedSensor(speedSensor_config_t config);
|
||||
//initializes gpio pin and configures interrupt
|
||||
void init();
|
||||
// initializes gpio pin, configures and starts interrupt
|
||||
void init();
|
||||
|
||||
//negative values = reverse direction
|
||||
//positive values = forward direction
|
||||
@@ -35,8 +35,7 @@ public:
|
||||
|
||||
//1=forward, -1=reverse
|
||||
int direction;
|
||||
|
||||
//variables for handling the encoder
|
||||
//variables for handling the encoder (public because ISR needs access)
|
||||
speedSensor_config_t config;
|
||||
int prevState = 0;
|
||||
uint64_t pulseDurations[3] = {};
|
||||
@@ -44,10 +43,9 @@ public:
|
||||
uint8_t pulseCounter = 0;
|
||||
int debugCount = 0;
|
||||
double currentRpm = 0;
|
||||
bool isInitialized = false;
|
||||
|
||||
private:
|
||||
|
||||
static bool isrIsInitialized; // default false due to static
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user