diff --git a/src/main/io/osd.c b/src/main/io/osd.c index ac0e3ee5a5..59c3bbe59a 100755 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -88,6 +88,7 @@ #endif #define VIDEO_BUFFER_CHARS_PAL 480 +#define FULL_CIRCLE 360 const char * const osdTimerSourceNames[] = { "ON TIME ", @@ -221,14 +222,18 @@ static void osdFormatPID(char * buff, const char * label, const pid8_t * pid) tfp_sprintf(buff, "%s %3d %3d %3d", label, pid->P, pid->I, pid->D); } -static uint8_t osdGetHeadingIntoDiscreteDirections(int heading, int directions) +static uint8_t osdGetHeadingIntoDiscreteDirections(int heading, unsigned directions) { + heading += FULL_CIRCLE; // Ensure positive value + // Split input heading 0..359 into sectors 0..(directions-1), but offset // by half a sector so that sector 0 gets centered around heading 0. - heading = (heading * 2 + 360 / directions) % 720; - heading = heading / (360 * 2 / directions); + // We multiply heading by directions to not loose precision in divisions + // In this way each segment will be a FULL_CIRCLE length + int direction = (heading * directions + FULL_CIRCLE / 2) / FULL_CIRCLE; // scale with rounding + direction %= directions; // normalize - return heading; + return direction; // return segment number } static uint8_t osdGetDirectionSymbolFromHeading(int heading)