Skip to content

Commit

Permalink
encoder auto mode changed
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Oct 1, 2019
1 parent 34b36da commit 87b74c0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 89 deletions.
14 changes: 1 addition & 13 deletions src/eez/apps/psu/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,7 @@ bool Channel::isMicroAmperAllowed() const {
}

float Channel::roundChannelValue(Unit unit, float value) const {
if (unit == UNIT_VOLT) {
return roundPrec(value, getVoltageResolution());
}

if (unit == UNIT_AMPER) {
return roundPrec(value, getCurrentResolution(value));
}

if (unit == UNIT_WATT) {
return roundPrec(value, getPowerResolution());
}

return value;
return roundPrec(value, getValuePrecision(unit, value));
}

void Channel::addUMonAdcValue(float value) {
Expand Down
2 changes: 2 additions & 0 deletions src/eez/apps/psu/gui/edit_mode_step.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ int getStepIndex();
int getStepValuesCount();
const eez::gui::data::Value *getStepValues();

const eez::gui::data::Value *getUnitStepValues(Unit unit);

void setStepIndex(int value);

#if OPTION_ENCODER
Expand Down
133 changes: 57 additions & 76 deletions src/eez/modules/mcu/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,58 +64,60 @@ EncoderMode g_encoderMode = ENCODER_MODE_AUTO;

////////////////////////////////////////////////////////////////////////////////

#define CONF_NUM_ACCELERATION_STEP_COUNTERS 10
#define CONF_ACCELERATION_STEP_COUNTER_DURATION_MS 50
#define CONF_MAX_ACCELERATION 99

#if defined(EEZ_PLATFORM_STM32)
#define CONF_ACCELERATION_CALC_POW_BASE 1.06f
#else
#define CONF_ACCELERATION_CALC_POW_BASE 1.05f
#endif

static uint32_t g_accelerationStepLastTick;
static int g_accumulatedAccelerationStepCounter;
static int g_accelerationStepCounters[CONF_NUM_ACCELERATION_STEP_COUNTERS];
static unsigned int g_accelerationStepCounterIndex;
static float g_acceleration;

void resetAccelerationCalculation() {
g_accelerationStepLastTick = millis();
g_accumulatedAccelerationStepCounter = 0;
for (int i = 0; i < CONF_NUM_ACCELERATION_STEP_COUNTERS; i++) {
g_accelerationStepCounters[i] = 0;
struct CalcAutoModeStepLevel {
static const int CONF_NUM_ACCELERATION_STEP_COUNTERS = 10;
static const int CONF_ACCELERATION_STEP_COUNTER_DURATION_MS = 50;
static const int CONF_COUNTER_TO_STEP_DIVISOR = 20;

uint32_t m_lastTick;
int m_accumulatedCounter;
int m_counters[CONF_NUM_ACCELERATION_STEP_COUNTERS];
unsigned int m_counterIndex;
int m_stepLevel;

void reset() {
m_lastTick = millis();
m_accumulatedCounter = 0;
for (int i = 0; i < CONF_NUM_ACCELERATION_STEP_COUNTERS; i++) {
m_counters[i] = 0;
}
m_counterIndex = 0;
m_stepLevel = 0;
}
g_accelerationStepCounterIndex = 0;
g_acceleration = 0;
}

void calculateAcceleration(int16_t diffCounter) {
g_accumulatedAccelerationStepCounter += diffCounter;
void update(int16_t diffCounter) {
m_accumulatedCounter += diffCounter;

uint32_t tick = millis();
int32_t diffTick = tick - g_accelerationStepLastTick;
if (diffTick >= CONF_ACCELERATION_STEP_COUNTER_DURATION_MS) {
g_accelerationStepCounters[g_accelerationStepCounterIndex % CONF_NUM_ACCELERATION_STEP_COUNTERS] = g_accumulatedAccelerationStepCounter;
uint32_t tick = millis();
int32_t diffTick = tick - m_lastTick;
if (diffTick >= CONF_ACCELERATION_STEP_COUNTER_DURATION_MS) {
m_counters[m_counterIndex % CONF_NUM_ACCELERATION_STEP_COUNTERS] = m_accumulatedCounter;

int stepCountersSum = 0;
for (int i = 0; i < CONF_NUM_ACCELERATION_STEP_COUNTERS; i++) {
stepCountersSum += g_accelerationStepCounters[(g_accelerationStepCounterIndex - i) % CONF_NUM_ACCELERATION_STEP_COUNTERS];
}
int stepCountersSum = 0;
for (int i = 0; i < CONF_NUM_ACCELERATION_STEP_COUNTERS; i++) {
stepCountersSum += m_counters[(m_counterIndex - i) % CONF_NUM_ACCELERATION_STEP_COUNTERS];
}

g_acceleration = powf(CONF_ACCELERATION_CALC_POW_BASE, fabsf(1.0f * stepCountersSum)) - 1.0f;
stepCountersSum = abs(stepCountersSum);

if (g_acceleration > CONF_MAX_ACCELERATION) g_acceleration = CONF_MAX_ACCELERATION;
m_stepLevel = MIN(stepCountersSum / CONF_COUNTER_TO_STEP_DIVISOR, 2);

// if (g_acceleration > 0) {
// DebugTrace("%g\n", g_acceleration);
// }
//if (stepCountersSum != 0) {
// DebugTrace("%d, %d\n", stepCountersSum, m_stepLevel);
//}

g_accelerationStepLastTick = tick;
g_accumulatedAccelerationStepCounter = 0;
g_accelerationStepCounterIndex++;
m_lastTick = tick;
m_accumulatedCounter = 0;
m_counterIndex++;
}
}
}

int stepLevel() {
return m_stepLevel;
}
};

static CalcAutoModeStepLevel g_calcAutoModeStepLevel;

////////////////////////////////////////////////////////////////////////////////

Expand All @@ -125,7 +127,7 @@ void init() {
g_lastCounter = __HAL_TIM_GET_COUNTER(&htim8);
#endif

resetAccelerationCalculation();
g_calcAutoModeStepLevel.reset();
}

int getCounter() {
Expand All @@ -141,7 +143,7 @@ int getCounter() {
#endif

// update acceleration
calculateAcceleration(diffCounter);
g_calcAutoModeStepLevel.update(diffCounter);

//
g_accumulatedCounter += diffCounter;
Expand All @@ -153,7 +155,7 @@ int getCounter() {
}

void resetEncoder() {
resetAccelerationCalculation();
g_calcAutoModeStepLevel.reset();
}

bool isButtonClicked() {
Expand Down Expand Up @@ -190,42 +192,21 @@ void switchEncoderMode() {
}

float increment(gui::data::Value value, int counter, float min, float max, int channelIndex, float precision) {
float newValue;
if (g_encoderMode == ENCODER_MODE_AUTO) {
float factor;

if (channelIndex != -1) {
factor = psu::Channel::get(channelIndex).getValuePrecision(value.getUnit(), value.getFloat());
} else {
factor = precision;
}

newValue = value.getFloat() + factor * (1 + g_acceleration) * counter;
} else {
newValue = value.getFloat() + psu::gui::edit_mode_step::getCurrentEncoderStepValue().getFloat() * counter;
}

if (channelIndex != -1) {
newValue = psu::Channel::get(channelIndex).roundChannelValue(value.getUnit(), newValue);
} else {
newValue = roundPrec(newValue, precision);
precision = psu::Channel::get(channelIndex).getValuePrecision(value.getUnit(), value.getFloat());
}

float step;

if (g_encoderMode == ENCODER_MODE_AUTO) {
float diff = fabsf(newValue - value.getFloat());
if (diff > 1) {
newValue = roundPrec(newValue, 1);
} else if (diff > 0.1) {
newValue = roundPrec(newValue, 0.1f);
} else if (diff > 0.01) {
newValue = roundPrec(newValue, 0.01f);
} else if (diff > 0.001) {
newValue = roundPrec(newValue, 0.001f);
} else if (diff > 0.0001) {
newValue = roundPrec(newValue, 0.0001f);
}
step = precision * powf(10.0f, 1.0f * g_calcAutoModeStepLevel.stepLevel());
} else {
step = psu::gui::edit_mode_step::getCurrentEncoderStepValue().getFloat();
}

float newValue = value.getFloat() + step * counter;
newValue = roundPrec(newValue, step);

return clamp(newValue, min, max);
}

Expand Down

0 comments on commit 87b74c0

Please sign in to comment.