Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Aug 27, 2018
1 parent da365ef commit 6b79a66
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion eez_psu_sketch/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ volatile int g_acceleration;
volatile uint32_t g_switchLastTime = 0;
volatile int g_switchCounter = 0;

uint32_t g_maxDiff;

void updateMaxDiff() {
g_maxDiff = CONF_ENCODER_ACCELERATION_INCREMENT_FACTOR * MAX(g_speedUp, g_speedDown) * 1000UL / CONF_ENCODER_ACCELERATION_DECREMENT_PER_MS;
}

void abInterruptHandler() {
uint8_t pinState = (digitalRead(ENC_B) << 1) | digitalRead(ENC_A);

Expand Down Expand Up @@ -127,11 +133,17 @@ void init() {
attachInterrupt(digitalPinToInterrupt(ENC_B), abInterruptHandler, CHANGE);

attachInterrupt(digitalPinToInterrupt(ENC_SW), swInterruptHandler, CHANGE);

updateMaxDiff();
}

void read(int &counter, bool &clicked) {
void read(uint32_t tickCount, int &counter, bool &clicked) {
noInterrupts();

if (tickCount - g_rotationLastTime > g_maxDiff) {
g_rotationLastTime = tickCount - g_maxDiff;
}

counter = g_rotationCounter;
g_rotationCounter = 0;

Expand All @@ -151,6 +163,8 @@ void enableAcceleration(bool enable) {
void setMovingSpeed(uint8_t down, uint8_t up) {
g_speedDown = down;
g_speedUp = up;

updateMaxDiff();
}

#ifdef EEZ_PSU_SIMULATOR
Expand Down
2 changes: 1 addition & 1 deletion eez_psu_sketch/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static const uint8_t DEFAULT_MOVING_DOWN_SPEED = 8;
static const uint8_t DEFAULT_MOVING_UP_SPEED = 6;

void init();
void read(int &counter, bool &clicked);
void read(uint32_t tickCount, int &counter, bool &clicked);

void enableAcceleration(bool enable);
void setMovingSpeed(uint8_t down, uint8_t up);
Expand Down
2 changes: 1 addition & 1 deletion eez_psu_sketch/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ void tick(uint32_t tick_usec) {
#if OPTION_ENCODER
int counter;
bool clicked;
encoder::read(counter, clicked);
encoder::read(tick_usec, counter, clicked);
if (counter != 0 || clicked) {
idle::noteEncoderActivity();
}
Expand Down

0 comments on commit 6b79a66

Please sign in to comment.