Skip to content

Commit

Permalink
keypad unit refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 23, 2021
1 parent 0ce582e commit 455fa20
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 578 deletions.
16 changes: 9 additions & 7 deletions src/bb3/psu/gui/keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,22 @@ void NumericKeypad::onEncoderClicked() {
ok();
}

float NumericKeypad::getPrecision() {
auto channel = Channel::getBySlotIndex(m_options.slotIndex, m_options.subchannelIndex);
if (channel) {
return psu::channel_dispatcher::getValuePrecision(*channel, m_startValue.getUnit(), m_startValue.getFloat());
}
return m_options.encoderPrecision;
}

void NumericKeypad::onEncoder(int counter) {
if (m_state == START) {
if (m_appContext->getActivePageId() == PAGE_ID_EDIT_MODE_KEYPAD) {
return;
}

if (m_startValue.getType() == VALUE_TYPE_FLOAT) {
float precision;
auto channel = Channel::getBySlotIndex(m_options.slotIndex, m_options.subchannelIndex);
if (channel) {
precision = psu::channel_dispatcher::getValuePrecision(*channel, m_startValue.getUnit(), m_startValue.getFloat());
} else {
precision = m_options.encoderPrecision;
}
float precision = getPrecision();
float newValue = m_startValue.getFloat() + counter * precision;
newValue = roundPrec(newValue, precision);
newValue = clamp(newValue, m_options.min, m_options.max);
Expand Down
2 changes: 2 additions & 0 deletions src/bb3/psu/gui/keypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class NumericKeypad : public eez::gui::NumericKeypad {
void onEncoder(int counter) override;
#endif

float getPrecision() override;

private:
bool getText(char *text, size_t count) override;
bool isMicroAmperAllowed() override;
Expand Down
1 change: 1 addition & 0 deletions src/eez/flow/components/call_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void executeCallActionComponent(FlowState *flowState, unsigned componentIndex) {

if (flowIndex >= (int)flowState->flowDefinition->flows.count) {
executeActionFunction(flowIndex - flowState->flowDefinition->flows.count);
propagateValueThroughSeqout(flowState, componentIndex);
return;
}

Expand Down
14 changes: 9 additions & 5 deletions src/eez/flow/flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,15 @@ void dataOperation(int16_t dataId, DataOperationEnum operation, const gui::Widge
if (component->type == WIDGET_TYPE_INPUT) {
auto inputWidget = (InputWidget *)widgetCursor.widget;
if (inputWidget->flags & INPUT_WIDGET_TYPE_NUMBER) {
Value precisionValue = getInputWidgetPrecision(widgetCursor);
float precision = precisionValue.toFloat();
float valueFloat = value.toFloat();
Unit unit = getInputWidgetUnit(widgetCursor);
setValue(flowDataId, widgetCursor, Value(roundPrec(valueFloat, precision) / getUnitBase10(unit), VALUE_TYPE_FLOAT));
if (value.isInt32()) {
setValue(flowDataId, widgetCursor, value);
} else {
Value precisionValue = getInputWidgetPrecision(widgetCursor);
float precision = precisionValue.toFloat();
float valueFloat = value.toFloat();
Unit unit = getInputWidgetUnit(widgetCursor);
setValue(flowDataId, widgetCursor, Value(roundPrec(valueFloat, precision) / getUnitFactor(unit), VALUE_TYPE_FLOAT));
}
} else {
setValue(flowDataId, widgetCursor, value);
}
Expand Down
243 changes: 4 additions & 239 deletions src/eez/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,76 +199,8 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) {

if (floatValue != 0) {
if (!fixedDecimals) {
if (unit == UNIT_VOLT) {
if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_VOLT;
floatValue *= 1E3f;
}
} else if (unit == UNIT_VOLT_PP) {
if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_VOLT_PP;
floatValue *= 1E3f;
}
} else if (unit == UNIT_AMPER) {
if (fabs(floatValue) < 0.001f && fabs(floatValue) != 0.0005f) {
unit = UNIT_MICRO_AMPER;
floatValue *= 1E6f;
} else if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_AMPER;
floatValue *= 1E3f;
}
} else if (unit == UNIT_AMPER_PP) {
if (fabs(floatValue) < 0.001f && fabs(floatValue) != 0.0005f) {
unit = UNIT_MICRO_AMPER_PP;
floatValue *= 1E6f;
} else if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_AMPER_PP;
floatValue *= 1E3f;
}
} else if (unit == UNIT_WATT) {
if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_WATT;
floatValue *= 1E3f;
}
} else if (unit == UNIT_SECOND) {
if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_SECOND;
floatValue *= 1E3f;
}
} else if (unit == UNIT_OHM) {
if (fabs(floatValue) >= 1000000) {
unit = UNIT_MOHM;
floatValue *= 1E-6F;
} else if (fabs(floatValue) >= 1000) {
unit = UNIT_KOHM;
floatValue *= 1E-3f;
}
} else if (unit == UNIT_FARAD) {
if (fabs(floatValue) < 1E-9f) {
unit = UNIT_PICO_FARAD;
floatValue *= 1E12F;
} else if (fabs(floatValue) < 1E-6f) {
unit = UNIT_NANO_FARAD;
floatValue *= 1E9F;
} else if (fabs(floatValue) < 1E-3f) {
unit = UNIT_MICRO_FARAD;
floatValue *= 1E6f;
} else if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_FARAD;
floatValue *= 1E3f;
}
} else if (unit == UNIT_HERTZ) {
if (fabs(floatValue) >= 1000000) {
unit = UNIT_MHERTZ;
floatValue *= 1E-6F;
} else if (fabs(floatValue) >= 1000) {
unit = UNIT_KHERTZ;
floatValue *= 1E-3f;
} else if (fabs(floatValue) < 1) {
unit = UNIT_MILLI_HERTZ;
floatValue *= 1E3f;
}
}
unit = findDerivedUnit(floatValue, unit);
floatValue /= getUnitFactor(unit);
}
} else {
floatValue = 0; // set to zero just in case we have negative zero
Expand Down Expand Up @@ -360,76 +292,8 @@ void DOUBLE_value_to_text(const Value &value, char *text, int count) {

if (doubleValue != 0) {
if (!fixedDecimals) {
if (unit == UNIT_VOLT) {
if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_VOLT;
doubleValue *= 1E3;
}
} else if (unit == UNIT_VOLT_PP) {
if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_VOLT_PP;
doubleValue *= 1E3;
}
} else if (unit == UNIT_AMPER) {
if (fabs(doubleValue) < 0.001 && fabs(doubleValue) != 0.0005) {
unit = UNIT_MICRO_AMPER;
doubleValue *= 1E6;
} else if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_AMPER;
doubleValue *= 1E3;
}
} else if (unit == UNIT_AMPER_PP) {
if (fabs(doubleValue) < 0.001 && fabs(doubleValue) != 0.0005) {
unit = UNIT_MICRO_AMPER_PP;
doubleValue *= 1E6;
} else if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_AMPER_PP;
doubleValue *= 1E3;
}
} else if (unit == UNIT_WATT) {
if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_WATT;
doubleValue *= 1E3;
}
} else if (unit == UNIT_SECOND) {
if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_SECOND;
doubleValue *= 1E3;
}
} else if (unit == UNIT_OHM) {
if (fabs(doubleValue) >= 1000000) {
unit = UNIT_MOHM;
doubleValue *= 1E-6;
} else if (fabs(doubleValue) >= 1000) {
unit = UNIT_KOHM;
doubleValue *= 1E-3;
}
} else if (unit == UNIT_FARAD) {
if (fabs(doubleValue) < 1E-9) {
unit = UNIT_PICO_FARAD;
doubleValue *= 1E12;
} else if (fabs(doubleValue) < 1E-6) {
unit = UNIT_NANO_FARAD;
doubleValue *= 1E9;
} else if (fabs(doubleValue) < 1E-3) {
unit = UNIT_MICRO_FARAD;
doubleValue *= 1E6;
} else if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_FARAD;
doubleValue *= 1E3;
}
} else if (unit == UNIT_HERTZ) {
if (fabs(doubleValue) >= 1000000) {
unit = UNIT_MHERTZ;
doubleValue *= 1E-6;
} else if (fabs(doubleValue) >= 1000) {
unit = UNIT_KHERTZ;
doubleValue *= 1E-3;
} else if (fabs(doubleValue) < 1) {
unit = UNIT_MILLI_HERTZ;
doubleValue *= 1E3;
}
}
unit = findDerivedUnit(fabs(doubleValue), unit);
doubleValue /= getUnitFactor(unit);
}
} else {
doubleValue = 0; // set to zero just in case we have negative zero
Expand Down Expand Up @@ -759,105 +623,6 @@ const char *Value::getString() const {
return strValue;
}

bool Value::isPico() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_FARAD) {
if (fabs(floatValue) < 1E-9f) {
return true;
}
}
}

return false;
}

bool Value::isNano() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_FARAD) {
if (fabs(floatValue) < 1E-6f) {
return true;
}
}
}

return false;
}

bool Value::isMicro() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_AMPER || unit == UNIT_AMPER_PP) {
if (fabs(floatValue) < 1E-3f && fabs(floatValue) != 0.0005f) {
return true;
}
} else if (unit == UNIT_FARAD) {
if (fabs(floatValue) < 1E-3f) {
return true;
}
}
}

return false;
}

bool Value::isMilli() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_AMPER || unit == UNIT_AMPER_PP) {
if (fabs(floatValue) < 1 && !(fabs(floatValue) < 1E-3f && fabs(floatValue) != 0.0005f)) {
return true;
}
} else if (unit == UNIT_VOLT || unit == UNIT_VOLT_PP || unit == UNIT_WATT || unit == UNIT_SECOND || unit == UNIT_FARAD || unit == UNIT_HERTZ) {
if (fabs(floatValue) < 1) {
return true;
}
}
}

return false;
}

bool Value::isKilo() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_OHM || unit == UNIT_HERTZ) {
if (fabs(floatValue) >= 1E3f) {
return true;
}
}
}

return false;
}

bool Value::isMega() const {
float floatValue = getFloat();
Unit unit = getUnit();

if (floatValue != 0) {
if (unit == UNIT_OHM || unit == UNIT_HERTZ) {
if (fabs(floatValue) >= 1E6f) {
return true;
}
}
}

return false;

}

double Value::toDouble(int *err) const {
if (type == VALUE_TYPE_VALUE_PTR) {
return pValueValue->toDouble(err);
Expand Down
7 changes: 0 additions & 7 deletions src/eez/gui/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,6 @@ struct Value {

//////////

bool isPico() const;
bool isNano() const;
bool isMicro() const;
bool isMilli() const;
bool isKilo() const;
bool isMega() const;

public:
uint8_t type;
uint8_t unit;
Expand Down
Loading

0 comments on commit 455fa20

Please sign in to comment.