Update OSD elements to use new font symbols to reduce clutter.
[betaflight.git] / src / main / osd / osd_screen.c
blobedf57e58498d4e6582f209e63f49b8b58db1f6ff
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
23 #include <platform.h>
24 #include "build/debug.h"
26 #include "drivers/video_textscreen.h"
27 #include "common/utils.h"
29 #include "osd/osd_element.h"
30 #include "osd/osd_screen.h"
33 textScreen_t osdTextScreen;
35 typedef struct osdCursor_s {
36 osdCoordVal_t x;
37 osdCoordVal_t y;
38 } osdCursor_t;
40 static osdCursor_t cursor = {0, 0};
42 static uint16_t osdCalculateBufferOffset(osdCoordVal_t x, osdCoordVal_t y) {
43 osdCoordVal_t yy;
44 if (y >= 0) {
45 // positive y = top aligned
46 yy = y;
47 } else {
48 // negative y = bottom aligned
49 yy = osdTextScreen.height + y;
51 // prevent bottom aligned PAL screen coordinate from appearing off-screen when NTSC is used
52 // e.g. when PAL rows = 16, and Y = -15 it's OK, but when NTSC with 13 rows is used a negative value is calculated for yy.
53 if (yy < 0) {
54 yy = 0;
57 uint16_t offset = (yy * osdTextScreen.width) + x;
58 return offset;
60 // Does not move the cursor.
61 void osdSetCharacterAtPosition(osdCoordVal_t x, osdCoordVal_t y, char c)
63 uint8_t mappedCharacter = asciiToFontMapping[(uint8_t)c];
65 uint16_t offset = osdCalculateBufferOffset(x,y);
66 textScreenBuffer[offset] = mappedCharacter;
69 // Does not move the cursor.
70 void osdSetRawCharacterAtPosition(osdCoordVal_t x, osdCoordVal_t y, char c)
72 uint16_t offset = osdCalculateBufferOffset(x,y);
73 textScreenBuffer[offset] = c;
76 void osdResetCursor(void)
78 cursor.x = 0;
79 cursor.y = 0;
82 void osdSetCursor(osdCoordVal_t x, osdCoordVal_t y)
84 cursor.x = x;
85 cursor.y = y;
88 // software cursor, handles line wrapping and row wrapping, resets to 0,0 when the end of the screen is reached
89 static void osdAdvanceCursor(void)
91 cursor.x++;
92 if (cursor.x >= osdTextScreen.width) {
93 cursor.y++;
94 cursor.x = 0;
95 if (cursor.y > osdTextScreen.height) {
96 cursor.y = 0;
101 void osdPrint(char *message)
103 uint8_t *charPtr = (uint8_t *)message;
104 bool escape = false;
105 while(*charPtr) {
106 if (!escape) {
107 if (*charPtr == 0xff) {
108 escape = true;
109 charPtr++;
110 continue;
113 if (escape) {
114 osdSetRawCharacterAtPosition(cursor.x, cursor.y, *charPtr);
115 escape = false;
116 } else {
117 osdSetCharacterAtPosition(cursor.x, cursor.y, *charPtr);
119 osdAdvanceCursor();
121 charPtr++;
125 void osdPrintAt(osdCoordVal_t x, osdCoordVal_t y, char *message)
127 osdSetCursor(x, y);
128 osdPrint(message);
131 void osdSetTextScreen(textScreen_t *textScreen)
133 osdTextScreen = *textScreen;
136 void osdClearScreen(void)
138 uint8_t mappedSpaceCharacter = asciiToFontMapping[(uint8_t)' '];
140 int offset = 0;
141 for (int y = 0; y < osdTextScreen.height; y++) {
142 for (int x = 0; x < osdTextScreen.width; x++) {
143 textScreenBuffer[offset++] = mappedSpaceCharacter;