finished new encoder
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include "counter.h"
|
||||
#include <inttypes.h>
|
||||
|
||||
uint8_t Counter::amountOfCounter = 0;
|
||||
|
||||
Counter::Counter(uint8_t pin) {
|
||||
this->pulsePin = pin;
|
||||
|
||||
this->unit = static_cast<pcnt_unit_t>(Counter::amountOfCounter);
|
||||
if (Counter::amountOfCounter < 7)
|
||||
Counter::amountOfCounter++;
|
||||
|
||||
pcnt_config_t config;
|
||||
config.unit = this->unit;
|
||||
config.channel = PCNT_CHANNEL_0;
|
||||
config.counter_h_lim = COUNTER_HIGH_LIMIT;
|
||||
config.counter_l_lim = COUNTER_LOW_LIMIT;
|
||||
config.ctrl_gpio_num = PCNT_PIN_NOT_USED;
|
||||
config.hctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
config.lctrl_mode = PCNT_CHANNEL_LEVEL_ACTION_KEEP;
|
||||
config.pulse_gpio_num = this->pulsePin;
|
||||
config.pos_mode = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
|
||||
config.neg_mode = PCNT_CHANNEL_EDGE_ACTION_HOLD;
|
||||
pcnt_unit_config(&config);
|
||||
}
|
||||
|
||||
void Counter::pause() {
|
||||
pcnt_counter_pause(this->unit);
|
||||
}
|
||||
|
||||
void Counter::resume() {
|
||||
pcnt_counter_resume(this->unit);
|
||||
}
|
||||
|
||||
void Counter::clear() {
|
||||
pcnt_counter_clear(this->unit);
|
||||
}
|
||||
|
||||
int16_t Counter::getValue() {
|
||||
int16_t res;
|
||||
pcnt_get_counter_value(this->unit, &res);
|
||||
return res;
|
||||
}
|
||||
|
||||
void Counter::setFilterValue(uint16_t value) {
|
||||
pcnt_set_filter_value(this->unit, value);
|
||||
this->filterEnable();
|
||||
}
|
||||
|
||||
void Counter::filterEnable() {
|
||||
pcnt_filter_enable(this->unit);
|
||||
}
|
||||
|
||||
void Counter::filterDisable() {
|
||||
pcnt_filter_disable(this->unit);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <driver/pcnt.h>
|
||||
|
||||
#define COUNTER_HIGH_LIMIT INT16_MAX
|
||||
#define COUNTER_LOW_LIMIT 0
|
||||
|
||||
class Counter {
|
||||
public:
|
||||
Counter(uint8_t pin);
|
||||
|
||||
void pause();
|
||||
void resume();
|
||||
void clear();
|
||||
|
||||
int16_t getValue();
|
||||
|
||||
void setFilterValue(uint16_t value);
|
||||
void filterEnable();
|
||||
void filterDisable();
|
||||
|
||||
private:
|
||||
static uint8_t amountOfCounter;
|
||||
|
||||
bool initalised = false;
|
||||
|
||||
uint8_t pulsePin;
|
||||
pcnt_unit_t unit;
|
||||
};
|
||||
Reference in New Issue
Block a user