armchair_fw/common/speedsensor.hpp
jonny_jr9 021a3660e1 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
2024-02-20 12:24:41 +01:00

53 lines
1.4 KiB
C++

#pragma once
extern "C" {
#include "esp_log.h"
#include "hal/gpio_types.h"
#include "driver/gpio.h"
#include "esp_timer.h"
}
//Encoder disk requirements:
//encoder disk has to have gaps in 3 differnt intervals (short, medium, long)
//that pattern can be repeated multiple times, see config option
typedef struct {
gpio_num_t gpioPin;
float degreePerGroup; //360 / [count of short,medium,long groups on encoder disk]
float tireCircumferenceMeter;
//positive direction is pulse order "short, medium, long"
bool directionInverted;
char* logName;
} speedSensor_config_t;
class speedSensor {
//TODO add count of revolutions/pulses if needed? (get(), reset() etc)
public:
//constructor
speedSensor(speedSensor_config_t config);
// initializes gpio pin, configures and starts interrupt
void init();
//negative values = reverse direction
//positive values = forward direction
float getKmph(); //kilometers per hour
float getMps(); //meters per second
float getRpm(); //rotations per minute
//1=forward, -1=reverse
int direction;
//variables for handling the encoder (public because ISR needs access)
speedSensor_config_t config;
int prevState = 0;
uint64_t pulseDurations[3] = {};
uint64_t lastEdgeTime = 0;
uint8_t pulseCounter = 0;
int debugCount = 0;
double currentRpm = 0;
private:
static bool isrIsInitialized; // default false due to static
};