77 lines
1.4 KiB
C++
77 lines
1.4 KiB
C++
#ifndef ROTARY_ENCODER_DRIVER_H
|
|
#define ROTARY_ENCODER_DRIVER_H
|
|
|
|
#include <stdint.h>
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
//#include "digital_in.h"
|
|
#include "timer_usec.h"
|
|
|
|
class RotaryEncoderDriver {
|
|
public:
|
|
static constexpr uint16_t SAMPLE_PERIOD_US = 4000; // Ts = 4 ms
|
|
static constexpr float COUNTS_PER_REV = 1050.0f;
|
|
|
|
RotaryEncoderDriver();
|
|
|
|
// Returns 0 on success, or a negative Zephyr error code.
|
|
int begin();
|
|
|
|
void update();
|
|
int32_t position();
|
|
void update_delta();
|
|
float count_per_sec();
|
|
float rpm();
|
|
bool new_speed_available();
|
|
void reset_speed_measurement();
|
|
|
|
private:
|
|
static const uint8_t HIST_LEN = 4; // 4 x Ts = 16 ms gluggi fyrir rpm-utreikning
|
|
|
|
volatile int32_t count;
|
|
volatile int16_t delta;
|
|
volatile bool speed_ready;
|
|
|
|
volatile int32_t history[HIST_LEN];
|
|
volatile uint8_t hist_idx;
|
|
|
|
|
|
|
|
//Digital_in eA;
|
|
//Digital_in eB;
|
|
|
|
struct gpio_callback encoder_c1_callback;
|
|
Timer_usec timer;
|
|
|
|
static void gpio_edge_handler(
|
|
const structdevice *dev,
|
|
struct gpio_callback *callback,
|
|
uint32_t pins);
|
|
|
|
static void timer_expired(void *context);
|
|
|
|
|
|
// public:
|
|
// RotaryEncoderDriver();
|
|
|
|
// void begin();
|
|
|
|
// void update();
|
|
|
|
// int32_t position();
|
|
|
|
// void update_delta();
|
|
|
|
// float count_per_sec();
|
|
|
|
// float rpm();
|
|
|
|
// bool new_speed_available();
|
|
|
|
// void reset_speed_measurement();
|
|
|
|
|
|
|
|
};
|
|
#endif
|