Skip to content

Commit

Permalink
Merge pull request betaflight#10517 from etracer65/osd_element_types
Browse files Browse the repository at this point in the history
Add support for OSD element type variants
  • Loading branch information
mikeller authored Feb 14, 2021
2 parents bd5c813 + 45aad0d commit 28d3156
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/main/osd/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern const char * const osdTimerSourceNames[OSD_NUM_TIMER_TYPES];
#define OSD_PROFILE_BITS_POS 11
#define OSD_PROFILE_MASK (((1 << OSD_PROFILE_COUNT) - 1) << OSD_PROFILE_BITS_POS)
#define OSD_POS_MAX 0x3FF
#define OSD_POSCFG_MAX (OSD_PROFILE_MASK | 0x3FF) // For CLI values
#define OSD_POSCFG_MAX UINT16_MAX // element positions now use all 16 bits
#define OSD_PROFILE_FLAG(x) (1 << ((x) - 1 + OSD_PROFILE_BITS_POS))
#define OSD_PROFILE_1_FLAG OSD_PROFILE_FLAG(1)

Expand All @@ -73,9 +73,11 @@ extern const char * const osdTimerSourceNames[OSD_NUM_TIMER_TYPES];
// Character coordinate
#define OSD_POSITION_BITS 5 // 5 bits gives a range 0-31
#define OSD_POSITION_XY_MASK ((1 << OSD_POSITION_BITS) - 1)
#define OSD_POSITION_TYPE_MASK 0xC000 // bits 14-15
#define OSD_POS(x,y) ((x & OSD_POSITION_XY_MASK) | ((y & OSD_POSITION_XY_MASK) << OSD_POSITION_BITS))
#define OSD_X(x) (x & OSD_POSITION_XY_MASK)
#define OSD_Y(x) ((x >> OSD_POSITION_BITS) & OSD_POSITION_XY_MASK)
#define OSD_TYPE(x) ((x & OSD_POSITION_TYPE_MASK) >> 14)

// Timer configuration
// Stored as 15[alarm:8][precision:4][source:4]0
Expand Down
57 changes: 44 additions & 13 deletions src/main/osd/osd_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,22 +1061,51 @@ static void osdElementMainBatteryUsage(osdElementParms_t *element)
// Set length of indicator bar
#define MAIN_BATT_USAGE_STEPS 11 // Use an odd number so the bar can be centered.

// Calculate constrained value
const float value = constrain(batteryConfig()->batteryCapacity - getMAhDrawn(), 0, batteryConfig()->batteryCapacity);
const int usedCapacity = getMAhDrawn();
int displayBasis = usedCapacity;

// Calculate mAh used progress
const uint8_t mAhUsedProgress = (batteryConfig()->batteryCapacity) ? ceilf((value / (batteryConfig()->batteryCapacity / MAIN_BATT_USAGE_STEPS))) : 0;
switch (element->type) {
case OSD_ELEMENT_TYPE_3: // mAh remaining percentage (counts down as battery is used)
displayBasis = constrain(batteryConfig()->batteryCapacity - usedCapacity, 0, batteryConfig()->batteryCapacity);
FALLTHROUGH;

// Create empty battery indicator bar
element->buff[0] = SYM_PB_START;
for (int i = 1; i <= MAIN_BATT_USAGE_STEPS; i++) {
element->buff[i] = i <= mAhUsedProgress ? SYM_PB_FULL : SYM_PB_EMPTY;
}
element->buff[MAIN_BATT_USAGE_STEPS + 1] = SYM_PB_CLOSE;
if (mAhUsedProgress > 0 && mAhUsedProgress < MAIN_BATT_USAGE_STEPS) {
element->buff[1 + mAhUsedProgress] = SYM_PB_END;
case OSD_ELEMENT_TYPE_4: // mAh used percentage (counts up as battery is used)
{
int displayPercent = 0;
if (batteryConfig()->batteryCapacity) {
displayPercent = constrain(lrintf(100.0f * displayBasis / batteryConfig()->batteryCapacity), 0, 100);
}
tfp_sprintf(element->buff, "%c%d%%", SYM_MAH, displayPercent);
break;
}

case OSD_ELEMENT_TYPE_2: // mAh used graphical progress bar (grows as battery is used)
displayBasis = constrain(batteryConfig()->batteryCapacity - usedCapacity, 0, batteryConfig()->batteryCapacity);
FALLTHROUGH;

case OSD_ELEMENT_TYPE_1: // mAh remaining graphical progress bar (shrinks as battery is used)
default:
{
uint8_t remainingCapacityBars = 0;

if (batteryConfig()->batteryCapacity) {
const float batteryRemaining = constrain(batteryConfig()->batteryCapacity - displayBasis, 0, batteryConfig()->batteryCapacity);
remainingCapacityBars = ceilf((batteryRemaining / (batteryConfig()->batteryCapacity / MAIN_BATT_USAGE_STEPS)));
}

// Create empty battery indicator bar
element->buff[0] = SYM_PB_START;
for (int i = 1; i <= MAIN_BATT_USAGE_STEPS; i++) {
element->buff[i] = i <= remainingCapacityBars ? SYM_PB_FULL : SYM_PB_EMPTY;
}
element->buff[MAIN_BATT_USAGE_STEPS + 1] = SYM_PB_CLOSE;
if (remainingCapacityBars > 0 && remainingCapacityBars < MAIN_BATT_USAGE_STEPS) {
element->buff[1 + remainingCapacityBars] = SYM_PB_END;
}
element->buff[MAIN_BATT_USAGE_STEPS+2] = '\0';
break;
}
}
element->buff[MAIN_BATT_USAGE_STEPS+2] = '\0';
}

static void osdElementMainBatteryVoltage(osdElementParms_t *element)
Expand Down Expand Up @@ -1876,6 +1905,7 @@ static void osdDrawSingleElement(displayPort_t *osdDisplayPort, uint8_t item)
element.item = item;
element.elemPosX = elemPosX;
element.elemPosY = elemPosY;
element.type = OSD_TYPE(osdElementConfig()->item_pos[item]);
element.buff = (char *)&buff;
element.osdDisplayPort = osdDisplayPort;
element.drawElement = true;
Expand Down Expand Up @@ -1903,6 +1933,7 @@ static void osdDrawSingleElementBackground(displayPort_t *osdDisplayPort, uint8_
element.item = item;
element.elemPosX = elemPosX;
element.elemPosY = elemPosY;
element.type = OSD_TYPE(osdElementConfig()->item_pos[item]);
element.buff = (char *)&buff;
element.osdDisplayPort = osdDisplayPort;
element.drawElement = true;
Expand Down
8 changes: 8 additions & 0 deletions src/main/osd/osd_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@

#include "osd/osd.h"

typedef enum {
OSD_ELEMENT_TYPE_1 = 0,
OSD_ELEMENT_TYPE_2,
OSD_ELEMENT_TYPE_3,
OSD_ELEMENT_TYPE_4
} osdElementType_e;

typedef struct osdElementParms_s {
uint8_t item;
uint8_t elemPosX;
uint8_t elemPosY;
osdElementType_e type;
char *buff;
displayPort_t *osdDisplayPort;
bool drawElement;
Expand Down

0 comments on commit 28d3156

Please sign in to comment.