/
osd_element_render.c
200 lines (156 loc) · 6.22 KB
/
osd_element_render.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <platform.h>
#include "build/debug.h"
#include "common/printf.h"
#include "common/maths.h"
#include "drivers/system.h"
#include "drivers/video_textscreen.h"
#include "drivers/video.h"
#include "osd/osd_element.h"
#include "osd/osd_element_render.h"
#include "osd/osd_screen.h"
#include "osd/fonts/font.h"
// from osd hardware implementation
void osdHardwareDisplayMotor(uint8_t x, uint8_t y, uint8_t percent);
static char elementAsciiBuffer[31];
void osdElementRender_duration(const element_t *element, elementDataProviderFn dataFn)
{
uint32_t millis = (uint32_t) dataFn();
uint32_t totalSeconds = (millis / 1000);
uint8_t minutes = totalSeconds / 60;
uint8_t seconds = totalSeconds % 60;
sprintf(elementAsciiBuffer, "%3d:%02d", minutes, seconds);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
}
void osdElementRender_mahDrawn(const element_t *element, elementDataProviderFn dataFn)
{
int32_t mAhDrawn = (int32_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%5d", mAhDrawn);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
osdSetRawCharacterAtPosition(element->x + 5, element->y, FONT_CHARACTER_MAH);
}
void osdElementRender_amperage(const element_t *element, elementDataProviderFn dataFn)
{
int32_t amperage = (int32_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%2d.%02d", amperage / 100, amperage % 100);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
osdSetRawCharacterAtPosition(element->x + 5, element->y, FONT_CHARACTER_AMP);
}
void osdElementRender_voltage(const element_t *element, elementDataProviderFn dataFn)
{
voltageAndName_t *voltageAndName= (voltageAndName_t *) dataFn();
tfp_sprintf(elementAsciiBuffer, "%s:%2d.%dV", voltageAndName->name, voltageAndName->voltage / 10, voltageAndName->voltage % 10);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
}
void osdElementRender_voltageBattery(const element_t *element, elementDataProviderFn dataFn)
{
voltageAndName_t *voltageAndName= (voltageAndName_t *) dataFn();
tfp_sprintf(elementAsciiBuffer, "%2d.%dV", voltageAndName->voltage / 10, voltageAndName->voltage % 10);
osdPrintAt(element->x +1, element->y, elementAsciiBuffer);
osdSetRawCharacterAtPosition(element->x, element->y, voltageAndName->symbol);
}
void osdElementRender_indicatorMag(const element_t *element, elementDataProviderFn dataFn)
{
bool on = (bool) dataFn();
if (!on)
return;
osdSetRawCharacterAtPosition(element->x, element->y, FONT_CHARACTER_MAG);
}
void osdElementRender_indicatorBaro(const element_t *element, elementDataProviderFn dataFn)
{
bool on = (bool) dataFn();
if (!on)
return;
osdSetRawCharacterAtPosition(element->x, element->y, FONT_CHARACTER_BARO);
}
void osdElementRender_flightMode(const element_t *element, elementDataProviderFn dataFn)
{
uint8_t modes = (int8_t) dataFn();
char *flightMode = "";
if (modes & OSD_FLIGHT_MODE_ACRO) {
flightMode = "ACRO";
} else if (modes & OSD_FLIGHT_MODE_ANGLE) {
flightMode = "ANGL";
} else if (modes & OSD_FLIGHT_MODE_HORIZON) {
flightMode = "HRZN";
}
osdPrintAt(element->x, element->y, flightMode);
}
void osdElementRender_rssi(const element_t *element, elementDataProviderFn dataFn)
{
uint16_t rssi = (uint16_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%3d", rssi / 10);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
osdSetRawCharacterAtPosition(element->x + 3, element->y, FONT_CHARACTER_RSSI);
}
void osdElementRender_callsign(const element_t *element, elementDataProviderFn dataFn)
{
uint8_t *callsign = (uint8_t *) dataFn();
osdPrintAt(element->x, element->y, (char *)callsign);
}
// 4x4 grid
struct quadMotorCoordinateOffset_s {
uint8_t x;
uint8_t y;
} quadMotorCoordinateOffsets[4] = {
// FIXME assumes standard motor order
{3, 3},
{3, 1},
{0, 3},
{0, 1}
};
void osdElementRender_motors(const element_t *element, elementDataProviderFn dataFn)
{
uint16_t *motors = (uint16_t *) dataFn();
const int maxMotors = 4; // just quad for now
for (int i = 0; i < maxMotors; i++) {
if (!motors[i]) {
continue; // skip unused/uninitialsed motors.
}
int percent = scaleRange(motors[i], 1000, 2000, 0, 100); // FIXME should use min/max command as used by the FC.
osdHardwareDisplayMotor(quadMotorCoordinateOffsets[i].x + element->x, quadMotorCoordinateOffsets[i].y + element->y, percent);
}
}
void osdElementRender_vtxChannel(const element_t *element, elementDataProviderFn dataFn)
{
uint8_t vtxChannel = (uint8_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%d", vtxChannel + 1);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
}
static const char vtxBandCodes[] = "ABCDR";
void osdElementRender_vtxBand(const element_t *element, elementDataProviderFn dataFn)
{
uint8_t vtxBand = (uint8_t) dataFn();
osdSetCharacterAtPosition(element->x, element->y, vtxBandCodes[vtxBand]);
}
void osdElementRender_vtxRfPower(const element_t *element, elementDataProviderFn dataFn)
{
uint8_t vtxRfPower = (uint8_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%d", vtxRfPower);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
}
void osdElementRender_averageSystemLoad(const element_t *element, elementDataProviderFn dataFn)
{
uint16_t averageSystemLoad = (uint16_t) dataFn();
tfp_sprintf(elementAsciiBuffer, "%3d %%", averageSystemLoad);
osdPrintAt(element->x, element->y, elementAsciiBuffer);
}