Skip to content

Commit

Permalink
feat(radio): modify Sensor Edit screens to show Ratio + Percentage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiearzu authored May 29, 2024
1 parent 5d20c81 commit c19f640
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
47 changes: 42 additions & 5 deletions radio/src/gui/128x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ enum SensorFields {
SENSOR_FIELD_MAX
};

constexpr coord_t SENSOR_2ND_COLUMN = 12 * FW;
constexpr coord_t SENSOR_3RD_COLUMN = 18 * FW;
constexpr coord_t SENSOR_2ND_COLUMN = 12 * FW;
constexpr coord_t SENSOR_3RD_COLUMN = 17 * FW - 2;

void menuModelSensor(event_t event)
{
Expand Down Expand Up @@ -220,10 +220,47 @@ void menuModelSensor(event_t event)
else {
lcdDrawTextAlignedLeft(y, STR_RATIO);
if (attr) CHECK_INCDEC_MODELVAR(event, sensor->custom.ratio, 0, 30000);
if (sensor->custom.ratio == 0)
if (sensor->custom.ratio == 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
else
lcdDrawNumber(SENSOR_2ND_COLUMN, y, sensor->custom.ratio, LEFT|attr|PREC1);
} else { // Ratio + Ratio Percent
uint32_t ratio = (sensor->custom.ratio * 1000) / 255;
int ratioLen = countDigits(sensor->custom.ratio);
int ratioPercLen = countDigits(ratio);

int suffixOffset = 0;
int ratioColAdj = 0;
int ratioPercColAdj = 0;

if (ratioLen <= 3) {
ratioColAdj = 0;
} else if (ratioLen <= 4) {
ratioColAdj = (FWNUM * 1);
} else if (ratioLen >= 5) {
ratioColAdj = (FWNUM * 2);
}

if (ratioPercLen < 2) {
ratioPercColAdj = 0;
suffixOffset = (FWNUM * (ratioPercLen + 1)) + 2;
} else if (ratioPercLen <= 4 ) {
ratioPercColAdj = 0;
suffixOffset = (FWNUM * ratioPercLen) + 2;
} else if (ratioPercLen <= 5) {
ratioColAdj += (FWNUM * 1); // move first column to maintain separation
ratioPercColAdj = (FWNUM * 1);
suffixOffset = (FWNUM * (ratioPercLen - 1)) + 2;
} else if (ratioPercLen >= 6) {
ratioColAdj += (FWNUM * 2); // move first column to maintain separation
ratioPercColAdj = (FWNUM * 2);
suffixOffset = (FWNUM * (ratioPercLen - 2)) + 2;
}

lcdDrawNumber(SENSOR_2ND_COLUMN - ratioColAdj, y,
sensor->custom.ratio, LEFT | attr | PREC1);
lcdDrawNumber(SENSOR_3RD_COLUMN - ratioPercColAdj, y,
ratio, LEFT | PREC1);
lcdDrawChar(SENSOR_3RD_COLUMN + suffixOffset, y, '%', 0);
}
break;
}
}
Expand Down
9 changes: 7 additions & 2 deletions radio/src/gui/212x64/model_telemetry_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ void menuModelSensor(event_t event)
else {
lcdDrawTextAlignedLeft(y, STR_RATIO);
if (attr) sensor->custom.ratio = checkIncDec(event, sensor->custom.ratio, 0, 30000, EE_MODEL|NO_INCDEC_MARKS|INCDEC_REP10);
if (sensor->custom.ratio == 0)
if (sensor->custom.ratio == 0) {
lcdDrawChar(SENSOR_2ND_COLUMN, y, '-', attr);
else
} else { // Ratio + Ratio Percent
lcdDrawNumber(SENSOR_2ND_COLUMN, y, sensor->custom.ratio, LEFT|attr|PREC1);
uint32_t ratio = (sensor->custom.ratio * 1000) / 255;
int ratio_len = countDigits(ratio);
lcdDrawNumber(SENSOR_3RD_COLUMN, y, ratio, LEFT|PREC1);
lcdDrawChar(SENSOR_3RD_COLUMN+(FWNUM*ratio_len)+3, y, '%', 0);
}
break;
}
}
Expand Down
53 changes: 50 additions & 3 deletions radio/src/gui/colorlcd/model_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,54 @@ class SensorSourceChoice : public SourceChoice
}
};

class SensorRatioEdit : Window
{
public:
SensorRatioEdit(Window* parent, const rect_t& rect, int vmin, int vmax,
std::function<int()> _getValue,
std::function<void(int)> _setValue) :
Window(parent, rect),
m_getValue(std::move(_getValue)),
m_setValue(std::move(_setValue))
{
setFlexLayout(LV_FLEX_FLOW_ROW, 75);
lv_obj_set_flex_align(lvobj, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_SPACE_AROUND);

editRatio = new NumberEdit(
this, rect_t{}, vmin, vmax, m_getValue,
[=](int32_t newValue) {
m_setValue(newValue);
setPercent();
},
PREC1);
editRatio->setZeroText("-");

percentText = new StaticText(this, rect_t{}, "");
setPercent();
}

protected:
int32_t ratioPercent;
NumberEdit* editRatio;
StaticText* percentText;
std::function<int()> m_getValue;
std::function<void(int)> m_setValue;

private:
void setPercent()
{
int32_t value = m_getValue();
if (value == 0) {
percentText->setText("");
} else {
std::string str =
formatNumberAsString((value * 1000) / 255, PREC1, 0, "", "%");
percentText->setText(str);
}
}
};

class SensorEditWindow : public Page
{
public:
Expand Down Expand Up @@ -697,9 +745,8 @@ class SensorEditWindow : public Page

paramLines[P_RATIO] = window->newLine(grid);
new StaticText(paramLines[P_RATIO], rect_t{}, STR_RATIO);
auto edit = new NumberEdit(paramLines[P_RATIO], rect_t{}, 0, 30000,
GET_SET_DEFAULT(sensor->custom.ratio), PREC1);
edit->setZeroText("-");
new SensorRatioEdit(paramLines[P_RATIO], rect_t{}, 0, 30000,
GET_SET_DEFAULT(sensor->custom.ratio));

paramLines[P_CELLINDEX] = window->newLine(grid);
new StaticText(paramLines[P_CELLINDEX], rect_t{}, STR_CELLINDEX);
Expand Down
18 changes: 18 additions & 0 deletions radio/src/strhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,24 @@ char *strAppendDate(char *str, bool time)
#if !defined(BOOT)
#endif

/**
* @brief Count the number of digits in a string.
* Works with negative numbers, and zero is considered to have 1 digit.
* @param number Integer whose digits are to be counted.
* @return The number of digits in the integer.
*/
int countDigits(int number)
{
number = std::abs(number); // Handle negative numbers if any
if (number == 0) return 1; // Special case for 0
int count = 0;
while (number > 0) {
number /= 10;
count++;
}
return count;
}

// Manage timezones
// For backward compatibility timezone is stored as two separate values:
// timezone = hour value
Expand Down
2 changes: 2 additions & 0 deletions radio/src/strhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ std::string getGPSSensorValue(TelemetryItem &telemetryItem, LcdFlags flags);
std::string getTelemDate(TelemetryItem &telemetryItem);
std::string getTelemTime(TelemetryItem &telemetryItem);

int countDigits(int number);

// Timezone handling
extern int8_t minTimezone();
extern int8_t maxTimezone();
Expand Down

0 comments on commit c19f640

Please sign in to comment.