Fix reset PG behaviour and configurator interactions based on USE_OSD_SD and USE_OSD_...
[betaflight.git] / src / main / io / displayport_msp.c
blobc7901dc96c0f948c0a8bc895ae752cd2ac1aa07c
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <ctype.h>
26 #include "platform.h"
28 #ifdef USE_MSP_DISPLAYPORT
30 #include "cli/cli.h"
32 #include "common/utils.h"
34 #include "drivers/display.h"
35 #include "drivers/osd.h"
37 #include "io/displayport_msp.h"
39 #include "msp/msp.h"
40 #include "msp/msp_protocol.h"
41 #include "msp/msp_serial.h"
43 #include "osd/osd.h"
45 #include "pg/vcd.h"
47 static displayPort_t mspDisplayPort;
48 static serialPortIdentifier_e displayPortSerial;
50 static int output(displayPort_t *displayPort, uint8_t cmd, uint8_t *buf, int len)
52 UNUSED(displayPort);
54 #ifdef USE_CLI
55 // FIXME There should be no dependency on the CLI but mspSerialPush doesn't check for cli mode, and can't because it also shouldn't have a dependency on the CLI.
56 if (cliMode) {
57 return 0;
59 #endif
60 return mspSerialPush(displayPortSerial, cmd, buf, len, MSP_DIRECTION_REPLY, MSP_V1);
63 static int heartbeat(displayPort_t *displayPort)
65 uint8_t subcmd[] = { MSP_DP_HEARTBEAT };
67 // heartbeat is used to:
68 // a) ensure display is not released by MW OSD software
69 // b) prevent OSD Slave boards from displaying a 'disconnected' status.
70 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
72 return 0;
75 static int grab(displayPort_t *displayPort)
77 return heartbeat(displayPort);
80 static int release(displayPort_t *displayPort)
82 uint8_t subcmd[] = { MSP_DP_RELEASE };
84 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
87 static int clearScreen(displayPort_t *displayPort, displayClearOption_e options)
89 UNUSED(options);
91 uint8_t subcmd[] = { MSP_DP_CLEAR_SCREEN };
93 return output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
96 static bool drawScreen(displayPort_t *displayPort)
98 uint8_t subcmd[] = { MSP_DP_DRAW_SCREEN };
99 output(displayPort, MSP_DISPLAYPORT, subcmd, sizeof(subcmd));
101 return 0;
104 static int screenSize(const displayPort_t *displayPort)
106 return displayPort->rows * displayPort->cols;
109 static int writeString(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, const char *string)
111 #define MSP_OSD_MAX_STRING_LENGTH 30 // FIXME move this
112 uint8_t buf[MSP_OSD_MAX_STRING_LENGTH + 4];
114 int len = strlen(string);
115 if (len >= MSP_OSD_MAX_STRING_LENGTH) {
116 len = MSP_OSD_MAX_STRING_LENGTH;
119 buf[0] = MSP_DP_WRITE_STRING;
120 buf[1] = row;
121 buf[2] = col;
122 buf[3] = displayPortProfileMsp()->fontSelection[attr] & ~DISPLAYPORT_MSP_ATTR_BLINK & DISPLAYPORT_MSP_ATTR_MASK;
124 if (attr & DISPLAYPORT_ATTR_BLINK) {
125 buf[3] |= DISPLAYPORT_MSP_ATTR_BLINK;
128 memcpy(&buf[4], string, len);
130 return output(displayPort, MSP_DISPLAYPORT, buf, len + 4);
133 static int writeSys(displayPort_t *displayPort, uint8_t col, uint8_t row, displayPortSystemElement_e systemElement)
135 uint8_t syscmd[4];
137 syscmd[0] = MSP_DP_SYS;
138 syscmd[1] = row;
139 syscmd[2] = col;
140 syscmd[3] = systemElement;
142 return output(displayPort, MSP_DISPLAYPORT, syscmd, sizeof(syscmd));
145 static int writeChar(displayPort_t *displayPort, uint8_t col, uint8_t row, uint8_t attr, uint8_t c)
147 char buf[2];
149 buf[0] = c;
150 buf[1] = 0;
151 return writeString(displayPort, col, row, attr, buf);
154 static bool isTransferInProgress(const displayPort_t *displayPort)
156 UNUSED(displayPort);
157 return false;
160 static bool isSynced(const displayPort_t *displayPort)
162 UNUSED(displayPort);
163 return true;
166 static void redraw(displayPort_t *displayPort)
168 if (vcdProfile()->video_system == VIDEO_SYSTEM_HD) {
169 displayPort->rows = osdConfig()->canvas_rows;
170 displayPort->cols = osdConfig()->canvas_cols;
171 } else {
172 const uint8_t displayRows = (vcdProfile()->video_system == VIDEO_SYSTEM_PAL) ? VIDEO_LINES_PAL : VIDEO_LINES_NTSC;
173 displayPort->rows = displayRows + displayPortProfileMsp()->rowAdjust;
174 displayPort->cols = OSD_SD_COLS + displayPortProfileMsp()->colAdjust;
176 drawScreen(displayPort);
179 static uint32_t txBytesFree(const displayPort_t *displayPort)
181 UNUSED(displayPort);
182 return mspSerialTxBytesFree();
185 static const displayPortVTable_t mspDisplayPortVTable = {
186 .grab = grab,
187 .release = release,
188 .clearScreen = clearScreen,
189 .drawScreen = drawScreen,
190 .screenSize = screenSize,
191 .writeSys = writeSys,
192 .writeString = writeString,
193 .writeChar = writeChar,
194 .isTransferInProgress = isTransferInProgress,
195 .heartbeat = heartbeat,
196 .redraw = redraw,
197 .isSynced = isSynced,
198 .txBytesFree = txBytesFree,
199 .layerSupported = NULL,
200 .layerSelect = NULL,
201 .layerCopy = NULL,
204 displayPort_t *displayPortMspInit(void)
206 displayInit(&mspDisplayPort, &mspDisplayPortVTable, DISPLAYPORT_DEVICE_TYPE_MSP);
208 if (displayPortProfileMsp()->useDeviceBlink) {
209 mspDisplayPort.useDeviceBlink = true;
212 #ifndef USE_OSD_SD
213 if (vcdProfile()->video_system != VIDEO_SYSTEM_HD) {
214 vcdProfileMutable()->video_system = VIDEO_SYSTEM_HD;
216 #endif
217 #ifndef USE_OSD_HD
218 if (vcdProfile()->video_system == VIDEO_SYSTEM_HD) {
219 vcdProfileMutable()->video_system = VIDEO_SYSTEM_AUTO;
221 #endif
223 redraw(&mspDisplayPort);
224 return &mspDisplayPort;
227 void displayPortMspSetSerial(serialPortIdentifier_e serialPort) {
228 displayPortSerial = serialPort;
230 #endif // USE_MSP_DISPLAYPORT